| 1 | #!/usr/bin/env bash |
| 2 | # EXPERIMENTAL — tears down the CodeWhale DigitalOcean smoke lab and lists |
| 3 | # anything billable that remains (droplets, volumes, snapshots, reserved |
| 4 | # IPs). Safe to re-run; prints what it finds before deleting. |
| 5 | set -euo pipefail |
| 6 | |
| 7 | DROPLET_NAME="${DROPLET_NAME:-codewhale-smoke}" |
| 8 | SSH_KEY_NAME="${SSH_KEY_NAME:-${DROPLET_NAME}-key}" |
| 9 | FIREWALL_NAME="${FIREWALL_NAME:-${DROPLET_NAME}-ssh-only}" |
| 10 | |
| 11 | command -v doctl >/dev/null || { echo "doctl is required" >&2; exit 1; } |
| 12 | doctl account get >/dev/null || { echo "doctl is not authenticated" >&2; exit 1; } |
| 13 | |
| 14 | echo "== Current droplets ==" |
| 15 | doctl compute droplet list --format ID,Name,Status,Region,SizeSlug |
| 16 | |
| 17 | DROPLET_ID=$(doctl compute droplet list --format ID,Name --no-header | awk -v n="$DROPLET_NAME" '$2 == n {print $1; exit}') |
| 18 | if [[ -n "$DROPLET_ID" ]]; then |
| 19 | read -r -p "Destroy droplet '${DROPLET_NAME}' (id ${DROPLET_ID})? Type 'yes': " CONFIRM |
| 20 | [[ "$CONFIRM" == "yes" ]] || { echo "Aborted."; exit 1; } |
| 21 | doctl compute droplet delete "$DROPLET_ID" --force |
| 22 | echo "destroyed droplet ${DROPLET_NAME}" |
| 23 | else |
| 24 | echo "droplet ${DROPLET_NAME} not found (already destroyed?)" |
| 25 | fi |
| 26 | |
| 27 | FW_ID=$(doctl compute firewall list --format ID,Name --no-header | awk -v n="$FIREWALL_NAME" '$2 == n {print $1; exit}') |
| 28 | if [[ -n "$FW_ID" ]]; then |
| 29 | doctl compute firewall delete "$FW_ID" --force |
| 30 | echo "deleted firewall ${FIREWALL_NAME}" |
| 31 | fi |
| 32 | |
| 33 | KEY_ID=$(doctl compute ssh-key list --format ID,Name --no-header | awk -v n="$SSH_KEY_NAME" '$2 == n {print $1; exit}') |
| 34 | if [[ -n "$KEY_ID" ]]; then |
| 35 | doctl compute ssh-key delete "$KEY_ID" --force |
| 36 | echo "deleted ssh key ${SSH_KEY_NAME}" |
| 37 | fi |
| 38 | |
| 39 | echo "== Leftover billable resources check ==" |
| 40 | echo "-- droplets:" |
| 41 | doctl compute droplet list --format ID,Name,Status |
| 42 | echo "-- volumes:" |
| 43 | doctl compute volume list --format ID,Name,Size |
| 44 | echo "-- snapshots:" |
| 45 | doctl compute snapshot list --format ID,Name,ResourceType |
| 46 | echo "-- reserved IPs (billed when unassigned):" |
| 47 | doctl compute reserved-ip list --format IP,DropletID |
| 48 | echo |
| 49 | echo "If all lists above are empty, DigitalOcean billing for this lab is fully stopped." |
| 50 |