#!/usr/bin/env bash set -euo pipefail [[ $# == 5 ]] || { echo "usage: switch-release.sh publisher.pem manifest.json signature.txt release.zip health-seconds" >&2; exit 2; } root=/opt/aidop-edge data=/var/lib/aidop-edge config=/etc/aidop-edge/edge.json unit=aidop-edge.service health="$5" [[ "$health" =~ ^[1-9][0-9]?$ && "$health" -ge 4 ]] exec 9>/run/lock/aidop-edge-upgrade.lock flock -n 9 prior="$(realpath "$root/current")" [[ "$prior" == "$root/"* || "$prior" == "$data/releases/"* ]] candidate="$("$prior/Admin.NET.DataPlatform.Edge" --stage-release "$root" "$1" "$2" "$3" "$4")" [[ "$candidate" == "$root/releases/"* && -x "$candidate/Admin.NET.DataPlatform.Edge" ]] chown -R root:aidop-edge "$candidate" chmod -R u=rwX,g=rX,o= "$candidate" switch_link() { local target="$1" ln -s "$target" "$root/current.pending" mv -Tf "$root/current.pending" "$root/current" } rollback() { systemctl stop "$unit" switch_link "$prior" systemctl start "$unit" logger -t aidop-edge-upgrade "rolled back to $(basename "$prior")" } systemctl stop "$unit" if ! switch_link "$candidate" || ! systemctl start "$unit"; then rollback; exit 1; fi sleep "$health" if ! systemctl is-active --quiet "$unit" || [[ ! -f "$data/health" ]] || [[ "$(stat -c %Y "$data/health")" -le "$(stat -c %Y "$root/current")" ]]; then rollback exit 1 fi logger -t aidop-edge-upgrade "activated $(basename "$candidate")"