#!/bin/sh # Alternate the two Hermes containers; no VPN-container operations. set -eu case "${1:-}" in test) target=Hermes-Webapp-Test; other=Hermes-Agent-Desktop ;; production) target=Hermes-Agent-Desktop; other=Hermes-Webapp-Test ;; *) echo 'Usage: switch.sh test|production' >&2; exit 2 ;; esac docker inspect "$target" >/dev/null if [ "$(docker inspect -f '{{.State.Running}}' "$other")" = true ]; then docker exec --user 99:100 -i "$other" /opt/hermes/.venv/bin/python - <<'PY' import datetime, json, os from pathlib import Path root = Path('/opt/data') paths = [root/'gateway_state.json'] + list((root/'profiles').glob('*/gateway_state.json')) assert paths[0].is_file(), 'No gateway status; refusing a blind stop' for p in paths: d = json.loads(p.read_text()) assert int(d.get('active_agents', 0)) == 0, 'Hermes is busy; wait before switching' if p == paths[0]: os.kill(int(d['pid']), 0) age = datetime.datetime.now(datetime.timezone.utc) - datetime.datetime.fromisoformat(d['updated_at']) assert age.total_seconds() < 120, 'Gateway heartbeat stale; inspect before switching' print('No active agent turns in the persisted gateway status.') PY docker stop --time 120 "$other" fi docker start "$target" echo "Running: $target. Other instance remains stopped. Data folders are independent."