#!/usr/bin/env bash set -euo pipefail root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" runtime="$root/.runtime" binary="$runtime/bin/cli-proxy-api" pid_file="$runtime/cpa.pid" log_file="$runtime/logs/cpa-console.log" if [[ -f "$pid_file" ]]; then old_pid="$(cat "$pid_file" || true)" if [[ -n "$old_pid" ]] && kill -0 "$old_pid" 2>/dev/null; then echo "CPA test host is already running (PID $old_pid)." exit 0 fi fi if [[ ! -x "$binary" ]]; then echo "CPA test host binary not found: $binary" >&2 exit 1 fi mkdir -p "$runtime/auths" "$runtime/plugins" "$runtime/logs" : >"$log_file" cd "$runtime" nohup "$binary" --config "$runtime/config.yaml" \ >"$log_file" 2>&1 "$pid_file" echo "Started CPA test host (PID $pid)." for _ in $(seq 1 30); do if curl -fsS http://127.0.0.1:8317/healthz >/dev/null 2>&1; then echo 'CPA test host is ready at http://127.0.0.1:8317' exit 0 fi if ! kill -0 "$pid" 2>/dev/null; then echo 'CPA test host exited during startup.' >&2 tail -n 100 "$log_file" >&2 exit 1 fi sleep 1 done echo 'Timed out waiting for CPA test host.' >&2 tail -n 100 "$log_file" >&2 exit 1