Files
mtusi2026lab7/scripts/stop-one.sh
T

20 lines
920 B
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?Required API hostname from X-Instance-Id}"
for id in $(scripts/compose.sh ps -q api); do
hostname=$(docker exec "$id" python -c "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live').headers['X-Instance-Id'])")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
docker stop --time 3 "$id" >/dev/null
python3 - "$id" "$TARGET_INSTANCE_ID" <<'PYLOG'
import json,sys,time,subprocess
from pathlib import Path
with Path('evidence/failover-actions.jsonl').open('a') as f:
f.write(json.dumps({'container_id':sys.argv[1],'container_name':subprocess.check_output(['docker','inspect','--format','{{.Name}}',sys.argv[1]],text=True).strip(),'instance_alias':sys.argv[2],'stopped_at_unix':time.time()})+'\n')
PYLOG
exit 0
fi
done
printf '%s\n' 'Target is not a running API replica' >&2
exit 1