# SSH Key Management {%- if ssh_key %} echo "" echo "=== Managing SSH Keys ===" SSH_KEY_NAME="{{ ssh_key.name }}" SSH_PUBLIC_KEY_PATH="{{ ssh_key.public_key_path }}" # Expand tilde in path SSH_PUBLIC_KEY_PATH="${SSH_PUBLIC_KEY_PATH/#\~/$HOME}" if [ ! -f "$SSH_PUBLIC_KEY_PATH" ]; then echo "ERROR: SSH public key not found at $SSH_PUBLIC_KEY_PATH" exit 1 fi echo "✓ SSH public key found: $SSH_PUBLIC_KEY_PATH" # Read SSH public key content (needed for both new creation and fingerprint calculation) SSH_PUBLIC_KEY=$(cat "$SSH_PUBLIC_KEY_PATH") # Check if SSH key already exists in Hetzner Cloud echo "Checking if SSH key '$SSH_KEY_NAME' exists in Hetzner..." # SSH key already exists SSH_KEY_ID=$(hcloud ssh-key describe "$SSH_KEY_NAME" -o yaml 2>/dev/null | grep ^id: | cut -f2 -d":" | sed "s/ //g") if [ -n "$SSH_KEY_ID" ] ; then echo "✓ SSH key $SSH_KEY_NAME already exists with ID: $SSH_KEY_ID" else # Create new SSH key using hcloud CLI echo "Creating SSH key '$SSH_KEY_NAME'..." hcloud ssh-key create \ --name "$SSH_KEY_NAME" \ --public-key-from-file "$SSH_PUBLIC_KEY_PATH" \ --label "cluster={{ cluster_name }}" \ --label "provider=hetzner" \ --label "managed_by=provisioning" \ > /dev/null SSH_KEY_ID=$(hcloud ssh-key describe "$SSH_KEY_NAME" -o yaml 2>/dev/null | grep ^id: | cut -f2 -d":" | sed "s/ //g") if [ -z "$SSH_KEY_ID" ]; then echo "ERROR: Failed to create SSH key" exit 1 fi echo "✓ SSH key '$SSH_KEY_NAME' created with ID: $SSH_KEY_ID" fi # Export SSH_KEY_ID for next provisioning steps echo "SSH_KEY_ID=$SSH_KEY_ID" >> "$STATE_DIR/.env" echo "SSH_KEY_NAME=$SSH_KEY_NAME" >> "$STATE_DIR/.env" echo "" echo "=== SSH Key Management Complete ===" echo "SSH_KEY_ID: $SSH_KEY_ID" echo "SSH_KEY_NAME: $SSH_KEY_NAME" echo "Environment exported to: $STATE_DIR/.env" {%- else %} echo "WARNING: No SSH key configuration provided" {%- endif %}