Files
mtusi2026lab5-fork/scripts/stop-one.sh
T
2026-11-07 16:10:32 +03:00

31 lines
1.0 KiB
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
case "$TARGET_INSTANCE_ID" in
*[!a-zA-Z0-9_.:-]*|'') exit 1 ;;
esac
target=""
for container in $(docker compose --env-file .env ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
configured=$(docker inspect --format '{{range .Config.Env}}{{if eq (index (split . "=") 0) "INSTANCE_ID"}}{{println .}}{{end}}{{end}}' "$container" \
| sed -n 's/^INSTANCE_ID=//p' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ "$TARGET_INSTANCE_ID" = "$hostname" ] || [ "$TARGET_INSTANCE_ID" = "$configured" ]; then
if [ -n "$target" ]; then
echo 'Instance id matches multiple API containers' >&2
exit 1
fi
target="$container"
fi
done
if [ -z "$target" ]; then
echo 'Target API instance not found in this Compose project' >&2
exit 1
fi
echo "Stopping API container $target" >&2
docker stop --timeout 8 "$target" >/dev/null