Files
stolzor 5cba142003
terraform / check (push) Waiting to run
Resources and access
2026-10-11 21:04:27 +03:00

37 lines
1.9 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#MISE description="Запустить пример Go-сервиса: mise run example:up [сервис] [static|dynamic]"
set -euo pipefail
config=$(echo 'jsonencode(yamldecode(file("config.yaml")))' | terraform console | jq -r .)
service=${1:-$(jq -r '[.services // {} | to_entries[] | select((.value.buckets // {} | length) > 0 and (.value.databases // [] | length) > 0) | .key][0] // empty' <<<"$config")}
[[ -n "$service" ]] || { echo "в config.yaml нет сервиса с бакетом и базой" >&2; exit 1; }
creds=${2:-static}
database=$(jq -r --arg s "$service" '.services[$s].databases[0] // empty' <<<"$config")
[[ -n "$database" ]] || { echo "у сервиса $service нет базы, пример рассчитан на сервис с базой и бакетом" >&2; exit 1; }
compose=(docker compose -f examples/go-service/compose.yml)
url=http://127.0.0.1:${EXAMPLE_PORT:-8090}/
role_id=$(terraform -chdir=vault output -json approle_role_ids | jq -er --arg s "$service" '.[$s]') ||
{ echo "нет роли AppRole для сервиса $service" >&2; exit 1; }
secret_id=$(curl -sS -X POST -H "X-Vault-Token: $VAULT_TOKEN" -d '{"ttl": "5m", "num_uses": 1}' \
"$VAULT_ADDR/v1/auth/approle/role/$service/secret-id" | jq -er .data.secret_id)
export SERVICE_NAME=$service DATABASE_NAME=$database VAULT_ROLE_ID=$role_id VAULT_SECRET_ID=$secret_id POSTGRES_CREDS=$creds
INFRA_NETWORK=$(docker compose config --format json | jq -er .networks.infra.name)
export INFRA_NETWORK
"${compose[@]}" up -d --build --force-recreate --quiet-pull 2>&1 | tail -3
for _ in $(seq 1 30); do
if response=$(curl -fsS "$url" 2>/dev/null); then
"${compose[@]}" logs --no-log-prefix example
printf '\nGET %s\n' "$url"
jq . <<<"$response"
exit 0
fi
sleep 1
done
"${compose[@]}" logs --no-log-prefix example
echo "сервис не ответил на $url" >&2
exit 1