summaryrefslogtreecommitdiff
path: root/scripts/start-debug.sh
blob: 698ae68d1b443f85a65d44e137eaee965cd47f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash

set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
repo_dir="$(cd -- "$script_dir/.." && pwd)"
env_file="${AIGW_DEBUG_ENV_FILE:-$repo_dir/.env.debug}"

log() {
  printf '[aigw-debug] %s\n' "$*"
}

fail() {
  printf '[aigw-debug] error: %s\n' "$*" >&2
  exit 1
}

command -v docker >/dev/null 2>&1 || fail "docker is not installed"
docker info >/dev/null 2>&1 || fail "cannot access Docker; check that the daemon is running and your user belongs to the docker group"

if [[ ! -f "$env_file" ]]; then
  command -v openssl >/dev/null 2>&1 || fail "openssl is required to generate local credentials"
  credential_key="$(openssl rand -base64 32 | tr -d '\n')"
  admin_token="aigw-admin-$(openssl rand -hex 24)"
  umask 077
  {
    printf 'AIGW_DATABASE_URL=postgres://aigw:aigw@127.0.0.1:5432/aigw?sslmode=disable\n'
    printf 'AIGW_REDIS_URL=redis://127.0.0.1:6379/0\n'
    printf 'AIGW_CREDENTIAL_KEY=%s\n' "$credential_key"
    printf 'AIGW_ADMIN_TOKEN=%s\n' "$admin_token"
    printf 'AIGW_STRIPE_API_KEY=rk_test_replace_me\n'
    printf 'AIGW_STRIPE_WEBHOOK_SECRET=whsec_replace_me\n'
  } >"$env_file"
  chmod 600 "$env_file"
  log "created $env_file"
fi

admin_token="$(sed -n 's/^AIGW_ADMIN_TOKEN=//p' "$env_file" | head -n 1)"
[[ -n "$admin_token" ]] || fail "AIGW_ADMIN_TOKEN is missing from $env_file"

cd "$repo_dir"
if [[ "${AIGW_DEBUG_SKIP_BUILD:-0}" == "1" ]]; then
  log "skipping image build (AIGW_DEBUG_SKIP_BUILD=1)"
else
  build_network="${AIGW_DOCKER_BUILD_NETWORK:-host}"
  log "building gateway image (network: $build_network)"
  docker build --network="$build_network" -t aigw-debug:local .
fi

log "starting PostgreSQL, Redis, and gateway"
if ! docker compose --env-file "$env_file" up -d --no-build --wait --wait-timeout 120; then
  docker compose --env-file "$env_file" ps >&2 || true
  gateway_logs="$(docker compose --env-file "$env_file" logs --no-color --tail=120 aigw 2>&1 || true)"
  printf '%s\n' "$gateway_logs" >&2
  if [[ "$gateway_logs" == *"decrypt credential"* ]]; then
    printf '[aigw-debug] the AIGW_CREDENTIAL_KEY in %s does not match credentials already stored in the PostgreSQL volume\n' "$env_file" >&2
    printf '[aigw-debug] restore the previous key, or explicitly remove the debug volumes if the stored control-plane data is disposable\n' >&2
  fi
  fail "services did not become healthy"
fi

log "services are ready"
printf '\nAdmin UI:    http://127.0.0.1:8080/admin/\n'
printf 'Health:      http://127.0.0.1:8080/readyz\n'
printf 'Admin token: %s\n' "$admin_token"
printf 'Environment: %s\n' "$env_file"
printf '\nLogs:        docker compose --env-file %q logs -f aigw\n' "$env_file"
printf 'Stop:        ./scripts/stop-debug.sh\n'

if grep -q '^AIGW_STRIPE_API_KEY=rk_test_replace_me$' "$env_file" 2>/dev/null; then
  printf '\nStripe uses placeholders. Replace the two Stripe values in %s before testing Checkout.\n' "$env_file"
fi