Files
selfheal-lab-gitops/vm/legacy-api/apply.sh
T
seon 1128adc1be fix: pass the env file to compose ps as well
두 번째 compose 호출에 --env-file이 없어 "DB_DSN variable is not set" 경고가 났다(동작 무관).
2026-09-24 00:44:22 +09:00

23 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# legacy-api VM (GCP MIG instance). Idempotent: run at first boot by the startup script and again by `make vm-sync`.
# 1) DB password from Secret Manager via the VM service account (metadata token; no key files, design 6.11)
# 2) docker compose up with the pinned image tag
# Alloy (metrics/logs shipping) is added in WP-15.
set -euo pipefail
cd "$(dirname "$0")"
PROJECT="$(curl -sf -H 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/project/project-id)"
command -v jq >/dev/null || { apt-get update -qq && apt-get install -y -qq jq >/dev/null; }
TOKEN="$(curl -sf -H 'Metadata-Flavor: Google' 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token' | jq -r .access_token)"
DB_PASSWORD="$(curl -sf -H "Authorization: Bearer ${TOKEN}" \
"https://secretmanager.googleapis.com/v1/projects/${PROJECT}/secrets/selfheal-db-password/versions/latest:access" \
| jq -r .payload.data | base64 -d || true)"
unset TOKEN
ENV_FILE=/opt/selfheal/legacy-api.env
umask 077
{
printf 'DB_DSN=postgres://app:%s@10.20.0.20:5432/app?sslmode=require\n' "${DB_PASSWORD}"
} > "$ENV_FILE"
unset DB_PASSWORD
docker compose --env-file "$ENV_FILE" -f compose.yaml up -d --pull always --remove-orphans
docker compose --env-file "$ENV_FILE" -f compose.yaml ps