#!/usr/bin/env bash # Provision the terraform test droplet end-to-end. # # Usage: # 1. Create terraform/.testenv with your DO token: # DO_TOKEN=dop_v1_... # 2. Run: ./terraform/setup.sh # # On first run this allocates a DigitalOcean reserved IP and saves it # to .testenv as RESERVED_IP. On subsequent runs it reuses that IP. # The reserved IP survives droplet destroy/recreate — set your DNS once. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" TESTENV="$SCRIPT_DIR/.testenv" # --- Source .testenv --- if [[ ! -f "$TESTENV" ]]; then echo "ERROR: $TESTENV not found." echo "" echo "Create it with your DigitalOcean API token:" echo " echo 'DO_TOKEN=dop_v1_...' > $TESTENV" exit 1 fi # shellcheck source=/dev/null source "$TESTENV" if [[ -z "${DO_TOKEN:-}" ]]; then echo "ERROR: DO_TOKEN is not set in $TESTENV" exit 1 fi # --- Ensure a reserved IP exists --- if [[ -z "${RESERVED_IP:-}" ]]; then echo "No RESERVED_IP in .testenv, allocating one..." RESERVED_IP="$(curl -s -X POST \ -H "Authorization: Bearer $DO_TOKEN" \ -H "Content-Type: application/json" \ -d '{"region":"ams3"}' \ https://api.digitalocean.com/v2/reserved_ips \ | grep -o '"ip":"[^"]*"' | head -1 | cut -d'"' -f4)" if [[ -z "$RESERVED_IP" ]]; then echo "ERROR: Failed to allocate reserved IP" exit 1 fi echo "RESERVED_IP=$RESERVED_IP" >> "$TESTENV" echo "Allocated and saved reserved IP: $RESERVED_IP" else echo "Using existing reserved IP: $RESERVED_IP" fi # --- Generate terraform.tfvars --- TFVARS="$SCRIPT_DIR/terraform.tfvars" cat > "$TFVARS" <