#!/bin/bash # Install hcloud CLI for Hetzner Cloud provider # Usage: ./install.sh [version] set -e VERSION="${1:-1.44.0}" INSTALL_DIR="${2:-/usr/local/bin}" # Detect OS and architecture OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case $ARCH in x86_64) ARCH="amd64" ;; aarch64|arm64) ARCH="arm64" ;; *) echo "ERROR: Unsupported architecture: $ARCH" exit 1 ;; esac # Determine if using sudo - only use if /usr/local/bin is not writable SUDO="" if [ "$EUID" -ne 0 ]; then # Check if we can write to the install directory without sudo if [ ! -w "$INSTALL_DIR" ]; then SUDO="sudo" fi fi # Download URL URL="https://github.com/hetznercloud/cli/releases/download/v${VERSION}/hcloud-${OS}-${ARCH}.tar.gz" echo "=== Installing hcloud CLI v${VERSION} ===" echo "OS: $OS" echo "Architecture: $ARCH" echo "Download URL: $URL" echo "" # Create temporary directory TEMP_DIR=$(mktemp -d) trap "rm -rf $TEMP_DIR" EXIT echo "Downloading hcloud CLI..." if ! curl -L --progress-bar "$URL" -o "$TEMP_DIR/hcloud.tar.gz"; then echo "ERROR: Failed to download hcloud CLI" exit 1 fi echo "Extracting..." tar -xz -C "$TEMP_DIR" -f "$TEMP_DIR/hcloud.tar.gz" # Find the hcloud binary (could be in root or a subdirectory) HCLOUD_BIN=$(find "$TEMP_DIR" -type f -name "hcloud" | head -1) if [ -z "$HCLOUD_BIN" ]; then echo "ERROR: hcloud binary not found in archive" exit 1 fi echo "Installing to $INSTALL_DIR..." # Try to install without sudo first if [ -z "$SUDO" ]; then mv "$HCLOUD_BIN" "$INSTALL_DIR/hcloud" && chmod +x "$INSTALL_DIR/hcloud" else # Try with sudo but with -n flag (non-interactive) to fail gracefully if $SUDO -n mv "$HCLOUD_BIN" "$INSTALL_DIR/hcloud" 2>/dev/null && $SUDO -n chmod +x "$INSTALL_DIR/hcloud" 2>/dev/null; then : # Success else # Fallback: try user-local directory USER_BIN="${HOME}/.local/bin" mkdir -p "$USER_BIN" cp "$HCLOUD_BIN" "$USER_BIN/hcloud" chmod +x "$USER_BIN/hcloud" echo "" echo "⚠ Installed to $USER_BIN (not in PATH by default)" echo "Add to PATH with: export PATH=$USER_BIN:\$PATH" fi fi echo "" echo "✓ hcloud CLI installed successfully" echo "" # Verify installation echo "Verifying installation..." HCLOUD_LOCATION="" if command -v hcloud &> /dev/null; then HCLOUD_LOCATION=$(command -v hcloud) echo "✓ hcloud found in PATH: $HCLOUD_LOCATION" echo "" hcloud version elif [ -f "$USER_BIN/hcloud" ]; then echo "✓ hcloud installed in user directory: $USER_BIN/hcloud" echo " Run 'export PATH=$USER_BIN:\$PATH' to use in current shell" echo " Or add to ~/.bashrc, ~/.zshrc, or equivalent for permanent setup" elif [ -f "$INSTALL_DIR/hcloud" ]; then echo "✓ hcloud installed to $INSTALL_DIR" echo " Add $INSTALL_DIR to your PATH if not already present" echo " Or run: export PATH=$INSTALL_DIR:\$PATH" else echo "WARNING: hcloud installation could not be verified" exit 1 fi echo "" echo "Next steps:" echo "1. Set your Hetzner Cloud API token:" echo " export HCLOUD_TOKEN='your_token_here'" echo "2. Test the installation:" echo " hcloud location list"