#!/bin/sh set -eu # Stop ONE API replica matching TARGET_INSTANCE_ID (= X-Instance-Id / hostname). # Leave it stopped; do not touch DB/Redis/LB/volumes. : "${TARGET_INSTANCE_ID:?test provides target API instance alias}" cd "$(dirname "$0")/.." found="" # Compose project containers for service api ids=$(docker compose --env-file .env ps -q api 2>/dev/null || true) for id in $ids; do [ -n "$id" ] || continue host=$(docker inspect -f '{{.Config.Hostname}}' "$id") if [ "$host" = "$TARGET_INSTANCE_ID" ]; then found=$id break fi iid=$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$id" | sed -n 's/^INSTANCE_ID=//p' | head -n1) if [ -n "$iid" ] && [ "$iid" = "$TARGET_INSTANCE_ID" ]; then found=$id break fi done if [ -z "$found" ]; then # Fallback: any running container with matching hostname (scaled names differ). for id in $(docker ps -q --filter "ancestor=mtusi-messenger:lab3"); do host=$(docker inspect -f '{{.Config.Hostname}}' "$id") if [ "$host" = "$TARGET_INSTANCE_ID" ]; then found=$id break fi done fi if [ -z "$found" ]; then printf '%s\n' "stop-one: replica not found for TARGET_INSTANCE_ID" >&2 exit 1 fi # Graceful stop; API marks not-ready on SIGTERM before exit (uvicorn grace ~8s). docker stop -t 10 "$found" >/dev/null exit 0