#!/usr/bin/env bash set -euo pipefail umask 077 cd "$(dirname "$0")/.." action=${1:-help} mode=${2:-offline} args=(-f compose.yaml) case "$mode" in offline) args+=(-f compose.offline.yaml) ;; online) ;; *) echo 'Mode must be online or offline'; exit 1 ;; esac compose() { docker compose "${args[@]}" "$@"; } image=${HN_BUILD_IMAGE:-hainuo-relay:deploy-20261027} build() { if [[ -f .env ]]; then compose build connector return fi if [[ "$mode" == offline ]]; then test -d wheelhouse || { echo 'Missing wheelhouse; obtain the ARM64 offline bundle'; exit 1; } docker build --network none --pull=false -f Dockerfile.offline -t "$image" . else docker build -f Dockerfile -t "$image" . fi } case "$action" in build) build ;; init) test -f settings.env || { echo 'Copy .env.example to settings.env and configure it'; exit 1; } docker run --rm --user 0:0 --entrypoint python -e PYTHONPATH=/app -v "$PWD:/work" "$image" /work/scripts/initialize.py "${@:3}" ;; check) test -f .env && test -f runtime/issuer/ca.key && test -f runtime/connector/connector.key test -f runtime/trust/ca.pem && test -f runtime/tls/server.pem && test -f runtime/tls/server.key compose config --quiet docker run --rm --user 0:0 --entrypoint python -e PYTHONPATH=/app -v "$PWD:/work:ro" "$image" /work/scripts/validate.py ;; start) bash scripts/relayctl.sh check "$mode" compose up -d --no-build --pull never --wait --wait-timeout 180 compose ps echo 'Containers ready. Now verify platform_connected and perform the documented business acceptance.' ;; status) compose ps; compose exec -T connector python -m relay.cli status --url http://127.0.0.1:8000 ;; logs) compose logs --since 10m --tail 80 "${@:3}" ;; stop) compose stop ;; backup) stamp=$(date +%Y%m%d-%H%M%S) out="backups/$stamp" mkdir -p "$out" compose exec -T postgres pg_dump -U hainuo -d hainuo -Fc > "$out/metadata.dump.partial" mv "$out/metadata.dump.partial" "$out/metadata.dump" tar -czf "$out/config-and-keys.tar.gz" .env runtime cp compose*.yaml "$out/" compose images -q > "$out/image-ids.txt" (cd "$out" && sha256sum metadata.dump config-and-keys.tar.gz > SHA256SUMS) echo "Backup: $PWD/$out (contains private keys and secrets; restrict access). Not a restore verification." ;; update) bash scripts/relayctl.sh backup "$mode" stamp=$(date +%Y%m%d-%H%M%S) current=$(compose images -q connector) test -n "$current" rollback="hainuo-relay:rollback-$stamp" docker image tag "$current" "$rollback" echo "Rollback image: $rollback (record this before proceeding)" build bash scripts/relayctl.sh start "$mode" ;; rollback) previous=${3:?Provide the recorded rollback image tag} docker image inspect "$previous" >/dev/null HN_IMAGE="$previous" compose up -d --no-build --pull never --wait --wait-timeout 180 echo 'Temporary rollback active. Set HN_IMAGE in .env to this tag before subsequent commands.' ;; *) echo 'Usage: bash scripts/relayctl.sh {build|init|check|start|status|logs|stop|backup|update|rollback} {offline|online} [arguments]'; exit 1 ;; esac