|
| 1 | +#!/bin/bash |
| 2 | +# Manage ephemeral DigitalOcean droplets for CI. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# bin/ci/droplet.sh create <name> <image> <ssh_pub_key_file> |
| 6 | +# bin/ci/droplet.sh destroy <droplet_id> [ssh_key_id] |
| 7 | +# bin/ci/droplet.sh wait-ssh <ip> <ssh_private_key_file> |
| 8 | +# bin/ci/droplet.sh run <ip> <ssh_private_key_file> <script> |
| 9 | +# |
| 10 | +# Requires: DO_API_TOKEN env var |
| 11 | +# |
| 12 | +# create: Registers SSH key with DO, creates droplet, polls until active. |
| 13 | +# Outputs: DROPLET_ID=xxx DROPLET_IP=xxx SSH_KEY_ID=xxx |
| 14 | +# destroy: Deletes droplet and (optionally) SSH key from DO. |
| 15 | +# wait-ssh: Polls until SSH is reachable (up to 120s). |
| 16 | +# run: Executes a script on the droplet via SSH. |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +DO_API="https://api.digitalocean.com/v2" |
| 21 | +REGION="${DO_REGION:-tor1}" |
| 22 | +SIZE="${DO_SIZE:-s-2vcpu-4gb}" |
| 23 | + |
| 24 | +die() { echo "❌ $1" >&2; exit 1; } |
| 25 | + |
| 26 | +require_token() { |
| 27 | + if [ -z "${DO_API_TOKEN:-}" ]; then |
| 28 | + die "DO_API_TOKEN not set" |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +do_api() { |
| 33 | + local method="$1" endpoint="$2" |
| 34 | + shift 2 |
| 35 | + curl -s -X "$method" \ |
| 36 | + -H "Authorization: Bearer $DO_API_TOKEN" \ |
| 37 | + -H "Content-Type: application/json" \ |
| 38 | + "$DO_API/$endpoint" "$@" |
| 39 | +} |
| 40 | + |
| 41 | +# ── create <name> <image> <ssh_pub_key_file> ───────────────────────────────── |
| 42 | +cmd_create() { |
| 43 | + require_token |
| 44 | + local name="${1:?Usage: droplet.sh create <name> <image> <ssh_pub_key_file>}" |
| 45 | + local image="${2:?}" |
| 46 | + local pub_key_file="${3:?}" |
| 47 | + |
| 48 | + [ -f "$pub_key_file" ] || die "SSH public key not found: $pub_key_file" |
| 49 | + |
| 50 | + local pub_key |
| 51 | + pub_key=$(cat "$pub_key_file") |
| 52 | + |
| 53 | + # Register ephemeral SSH key |
| 54 | + local key_name |
| 55 | + key_name="ci-${name}-$(date +%s)" |
| 56 | + local key_result |
| 57 | + key_result=$(do_api POST "account/keys" -d "{\"name\":\"$key_name\",\"public_key\":\"$pub_key\"}") |
| 58 | + |
| 59 | + local ssh_key_id |
| 60 | + ssh_key_id=$(echo "$key_result" | python3 -c "import json,sys; print(json.load(sys.stdin)['ssh_key']['id'])" 2>/dev/null) \ |
| 61 | + || die "Failed to register SSH key: $key_result" |
| 62 | + |
| 63 | + echo " SSH key registered: $ssh_key_id ($key_name)" >&2 |
| 64 | + |
| 65 | + # Create droplet |
| 66 | + local create_result |
| 67 | + create_result=$(do_api POST "droplets" -d "{ |
| 68 | + \"name\": \"$name\", |
| 69 | + \"region\": \"$REGION\", |
| 70 | + \"size\": \"$SIZE\", |
| 71 | + \"image\": \"$image\", |
| 72 | + \"ssh_keys\": [$ssh_key_id], |
| 73 | + \"backups\": false, |
| 74 | + \"monitoring\": false |
| 75 | + }") |
| 76 | + |
| 77 | + local droplet_id |
| 78 | + droplet_id=$(echo "$create_result" | python3 -c "import json,sys; print(json.load(sys.stdin)['droplet']['id'])" 2>/dev/null) \ |
| 79 | + || die "Failed to create droplet: $create_result" |
| 80 | + |
| 81 | + echo " Droplet created: $droplet_id (polling for IP...)" >&2 |
| 82 | + |
| 83 | + # Poll until active with public IP |
| 84 | + local ip="none" |
| 85 | + for i in $(seq 1 60); do |
| 86 | + local data |
| 87 | + data=$(do_api GET "droplets/$droplet_id") |
| 88 | + local status |
| 89 | + status=$(echo "$data" | python3 -c "import json,sys; print(json.load(sys.stdin)['droplet']['status'])") |
| 90 | + ip=$(echo "$data" | python3 -c " |
| 91 | +import json,sys |
| 92 | +d=json.load(sys.stdin)['droplet'] |
| 93 | +v4=[n for n in d['networks']['v4'] if n['type']=='public'] |
| 94 | +print(v4[0]['ip_address'] if v4 else 'none') |
| 95 | +" 2>/dev/null || echo "none") |
| 96 | + |
| 97 | + if [ "$status" = "active" ] && [ "$ip" != "none" ]; then |
| 98 | + echo " Droplet active: $ip" >&2 |
| 99 | + break |
| 100 | + fi |
| 101 | + sleep 3 |
| 102 | + done |
| 103 | + |
| 104 | + if [ "$ip" = "none" ]; then |
| 105 | + die "Droplet $droplet_id never became active" |
| 106 | + fi |
| 107 | + |
| 108 | + # Output for GitHub Actions $GITHUB_OUTPUT or eval |
| 109 | + echo "DROPLET_ID=$droplet_id" |
| 110 | + echo "DROPLET_IP=$ip" |
| 111 | + echo "SSH_KEY_ID=$ssh_key_id" |
| 112 | +} |
| 113 | + |
| 114 | +# ── destroy <droplet_id> [ssh_key_id] ──────────────────────────────────────── |
| 115 | +cmd_destroy() { |
| 116 | + require_token |
| 117 | + local droplet_id="${1:-}" |
| 118 | + local ssh_key_id="${2:-}" |
| 119 | + |
| 120 | + if [ -n "$droplet_id" ]; then |
| 121 | + local http_code |
| 122 | + http_code=$(do_api DELETE "droplets/$droplet_id" -o /dev/null -w "%{http_code}") |
| 123 | + if [ "$http_code" = "204" ]; then |
| 124 | + echo " Droplet $droplet_id destroyed" >&2 |
| 125 | + else |
| 126 | + echo " ⚠️ Droplet destroy returned $http_code (may already be gone)" >&2 |
| 127 | + fi |
| 128 | + fi |
| 129 | + |
| 130 | + if [ -n "$ssh_key_id" ]; then |
| 131 | + local http_code |
| 132 | + http_code=$(do_api DELETE "account/keys/$ssh_key_id" -o /dev/null -w "%{http_code}") |
| 133 | + if [ "$http_code" = "204" ]; then |
| 134 | + echo " SSH key $ssh_key_id deleted" >&2 |
| 135 | + else |
| 136 | + echo " ⚠️ SSH key delete returned $http_code (may already be gone)" >&2 |
| 137 | + fi |
| 138 | + fi |
| 139 | +} |
| 140 | + |
| 141 | +# ── wait-ssh <ip> <ssh_private_key_file> ────────────────────────────────────── |
| 142 | +cmd_wait_ssh() { |
| 143 | + local ip="${1:?Usage: droplet.sh wait-ssh <ip> <ssh_private_key_file>}" |
| 144 | + local key_file="${2:?}" |
| 145 | + |
| 146 | + echo " Waiting for SSH on $ip..." >&2 |
| 147 | + for i in $(seq 1 40); do |
| 148 | + if ssh -o StrictHostKeyChecking=no -o ConnectTimeout=3 -o BatchMode=yes \ |
| 149 | + -i "$key_file" "root@$ip" true 2>/dev/null; then |
| 150 | + echo " SSH ready ($((i * 3))s)" >&2 |
| 151 | + return 0 |
| 152 | + fi |
| 153 | + sleep 3 |
| 154 | + done |
| 155 | + die "SSH not reachable on $ip after 120s" |
| 156 | +} |
| 157 | + |
| 158 | +# ── run <ip> <ssh_private_key_file> <script> ────────────────────────────────── |
| 159 | +cmd_run() { |
| 160 | + local ip="${1:?Usage: droplet.sh run <ip> <ssh_private_key_file> <script>}" |
| 161 | + local key_file="${2:?}" |
| 162 | + local script="${3:?}" |
| 163 | + |
| 164 | + ssh -o StrictHostKeyChecking=no -o BatchMode=yes \ |
| 165 | + -i "$key_file" "root@$ip" bash -s < "$script" |
| 166 | +} |
| 167 | + |
| 168 | +# ── Dispatch ────────────────────────────────────────────────────────────────── |
| 169 | +case "${1:-}" in |
| 170 | + create) shift; cmd_create "$@" ;; |
| 171 | + destroy) shift; cmd_destroy "$@" ;; |
| 172 | + wait-ssh) shift; cmd_wait_ssh "$@" ;; |
| 173 | + run) shift; cmd_run "$@" ;; |
| 174 | + *) die "Usage: droplet.sh {create|destroy|wait-ssh|run} ..." ;; |
| 175 | +esac |
0 commit comments