77#
88# Usage: stop-gateway.sh [--state-dir <dir>]
99#
10- # Writes <state-dir>/gateway.log. Safe to run when nothing is running.
10+ # Writes <state-dir>/gateway.log. Safe to run when nothing is running. Runs
11+ # every cleanup step even when one fails, then exits nonzero if any failed.
1112set -uo pipefail
1213
1314state=${OPENSHELL_E2E_STATE:- ${RUNNER_TEMP:-/ tmp} / openshell-e2e}
@@ -17,16 +18,36 @@ state=${OPENSHELL_E2E_STATE:-${RUNNER_TEMP:-/tmp}/openshell-e2e}
1718semver=$( sed ' s/^v//' " $state /core-version" 2> /dev/null || echo latest)
1819compose () { IMAGE_TAG=$semver docker compose -p openshell-e2e -f " $state /docker-compose.yml" " $@ " ; }
1920
20- compose logs --no-color --timestamps > " $state /gateway.log" 2>&1
21- echo " Gateway log: $state /gateway.log"
22- # Sandbox containers carry the namespace label from core's gateway.toml.
23- ids=$( docker ps -aq --filter label=openshell.sandbox_namespace=openshell)
24- [ -z " $ids " ] || docker rm -f $ids > /dev/null
25- compose down --volumes --remove-orphans > /dev/null 2>&1
21+ failed=0
22+ fail () { echo " stop-gateway: $* " >&2 ; failed=1; }
23+
24+ if compose logs --no-color --timestamps > " $state /gateway.log" 2>&1 ; then
25+ echo " Gateway log: $state /gateway.log"
26+ else
27+ fail " could not save the gateway log; see $state /gateway.log"
28+ fi
29+
30+ # The Docker driver labels sandbox and supervisor containers with core's
31+ # openshell.ai/managed-by and openshell.ai/sandbox-namespace labels. Compose
32+ # doesn't know about them, so remove them before taking the project down.
33+ if ids=$( docker ps -aq \
34+ --filter label=openshell.ai/managed-by=openshell \
35+ --filter label=openshell.ai/sandbox-namespace=openshell) ; then
36+ # shellcheck disable=SC2086 # one container ID per word
37+ [ -z " $ids " ] || docker rm -f $ids > /dev/null || fail " could not remove sandbox containers"
38+ else
39+ fail " could not list sandbox containers"
40+ fi
41+ compose down --volumes --remove-orphans > /dev/null || fail " docker compose down failed"
2642
2743config_home=${XDG_CONFIG_HOME:- $HOME / .config}
28- rm -rf " $config_home /openshell/gateways/e2e"
44+ rm -rf " $config_home /openshell/gateways/e2e" || fail " could not remove the e2e gateway registration "
2945if [ " $( cat " $config_home /openshell/active_gateway" 2> /dev/null) " = e2e ]; then
30- rm -f " $config_home /openshell/active_gateway"
46+ rm -f " $config_home /openshell/active_gateway" || fail " could not remove the active-gateway pointer"
47+ fi
48+
49+ if [ " $failed " -ne 0 ]; then
50+ echo " Gateway cleanup failed; the gateway or its sandboxes may still be running." >&2
51+ exit 1
3152fi
3253echo " Gateway stopped."
0 commit comments