#!/bin/bash # Hetzner Cloud Provisioning Orchestrator # Generated by provisioning {{ provisioning_version }} at {{ now }} # Purpose: Orchestrate creation of SSH keys → Networks → Firewalls → Volumes → Servers set -e {%- if debug and debug == "yes" %} {%- endif %} # Configuration STATE_DIR="{{ state_dir }}" CLUSTER_NAME="{{ cluster_name }}" PROVISIONING_VERSION="{{ provisioning_version }}" # Export for all subscripts export HCLOUD_TOKEN="${HCLOUD_TOKEN}" export STATE_DIR="$STATE_DIR" # Helper function for script execution with logging run_step() { local step_num=$1 local step_name=$2 local script_path=$3 echo "" echo "==================================================" echo "STEP $step_num: $step_name" echo "==================================================" echo "Executing: $script_path" echo "State directory: $STATE_DIR" echo "" if [ ! -f "$script_path" ]; then echo "ERROR: Script not found at $script_path" return 1 fi # Execute the step script if bash "$script_path"; then echo "" echo "✓ STEP $step_num COMPLETE: $step_name" return 0 else echo "" echo "✗ STEP $step_num FAILED: $step_name" echo "State directory preserved for debugging: $STATE_DIR" return 1 fi } # Cleanup function (optional - can be called manually) cleanup_state() { if [ -d "$STATE_DIR" ]; then echo "Removing state directory: $STATE_DIR" rm -rf "$STATE_DIR" fi } # Trap errors to show helpful debug info trap 'echo "ERROR: Provisioning failed at step. Check $STATE_DIR for details."' ERR # Main orchestration echo "" echo "╔════════════════════════════════════════════════════════════════════════════╗" echo "║ Hetzner Cloud Infrastructure Provisioning ║" echo "║ Cluster: $CLUSTER_NAME ║" echo "║ Version: $PROVISIONING_VERSION at {{ now }} ║" echo "╚════════════════════════════════════════════════════════════════════════════╝" # Verify prerequisites echo "" echo "=== Verifying Prerequisites ===" if [ -z "$HCLOUD_TOKEN" ]; then echo "ERROR: HCLOUD_TOKEN environment variable not set" exit 1 fi echo "✓ Prerequisites satisfied" # Create state directory if not exists if [ ! -d "$STATE_DIR" ]; then mkdir -p "$STATE_DIR" echo "✓ Created state directory: $STATE_DIR" else echo "✓ Using existing state directory: $STATE_DIR" fi # Execute provisioning steps in sequence STEPS=( "1:SSH_KEYS:$STATE_DIR/01_ssh_keys.sh:SSH Key Management" "2:NETWORKS:$STATE_DIR/02_networks.sh:Network Setup" "3:FIREWALLS:$STATE_DIR/03_firewalls.sh:Firewall Configuration" "4:VOLUMES:$STATE_DIR/04_volumes.sh:Volume Management" "5:SERVERS:$STATE_DIR/05_servers.sh:Server Provisioning" ) FAILED_STEPS=() COMPLETED_STEPS=() for step_config in "${STEPS[@]}"; do IFS=':' read -r step_num step_type script_path step_name <<< "$step_config" if run_step "$step_num" "$step_name" "$script_path"; then COMPLETED_STEPS+=("$step_type") else FAILED_STEPS+=("$step_type") echo "" echo "WARNING: Step $step_num failed. Stopping orchestration." echo "State preserved in: $STATE_DIR" echo "To resume, ensure the failed step's dependencies are satisfied." break fi done # Summary echo "" echo "╔════════════════════════════════════════════════════════════════════════════╗" echo "║ PROVISIONING SUMMARY ║" echo "╚════════════════════════════════════════════════════════════════════════════╝" echo "" echo "Cluster: $CLUSTER_NAME" echo "State directory: $STATE_DIR" echo "" echo "Completed steps (${#COMPLETED_STEPS[@]}):" for step in "${COMPLETED_STEPS[@]}"; do echo " ✓ $step" done if [ ${#FAILED_STEPS[@]} -gt 0 ]; then echo "" echo "Failed steps (${#FAILED_STEPS[@]}):" for step in "${FAILED_STEPS[@]}"; do echo " ✗ $step" done echo "" echo "Provisioning INCOMPLETE" exit 1 else echo "" echo "✓ All provisioning steps completed successfully!" echo "" echo "Next steps:" echo " 1. Verify server creation: hcloud server list" echo " 2. Review state file: cat $STATE_DIR/.provisioning-state.json" echo " 3. Export environment: source $STATE_DIR/.env" echo "" echo "To cleanup state directory (after verification):" echo " rm -rf $STATE_DIR" echo "" exit 0 fi