switch-release.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. [[ $# == 5 ]] || { echo "usage: switch-release.sh publisher.pem manifest.json signature.txt release.zip health-seconds" >&2; exit 2; }
  4. root=/opt/aidop-edge
  5. data=/var/lib/aidop-edge
  6. config=/etc/aidop-edge/edge.json
  7. unit=aidop-edge.service
  8. health="$5"
  9. [[ "$health" =~ ^[1-9][0-9]?$ && "$health" -ge 4 ]]
  10. exec 9>/run/lock/aidop-edge-upgrade.lock
  11. flock -n 9
  12. prior="$(realpath "$root/current")"
  13. [[ "$prior" == "$root/"* || "$prior" == "$data/releases/"* ]]
  14. candidate="$("$prior/Admin.NET.DataPlatform.Edge" --stage-release "$root" "$1" "$2" "$3" "$4")"
  15. [[ "$candidate" == "$root/releases/"* && -x "$candidate/Admin.NET.DataPlatform.Edge" ]]
  16. chown -R root:aidop-edge "$candidate"
  17. chmod -R u=rwX,g=rX,o= "$candidate"
  18. switch_link() {
  19. local target="$1"
  20. ln -s "$target" "$root/current.pending"
  21. mv -Tf "$root/current.pending" "$root/current"
  22. }
  23. rollback() {
  24. systemctl stop "$unit"
  25. switch_link "$prior"
  26. systemctl start "$unit"
  27. logger -t aidop-edge-upgrade "rolled back to $(basename "$prior")"
  28. }
  29. systemctl stop "$unit"
  30. if ! switch_link "$candidate" || ! systemctl start "$unit"; then rollback; exit 1; fi
  31. sleep "$health"
  32. if ! systemctl is-active --quiet "$unit" || [[ ! -f "$data/health" ]] ||
  33. [[ "$(stat -c %Y "$data/health")" -le "$(stat -c %Y "$root/current")" ]]; then
  34. rollback
  35. exit 1
  36. fi
  37. logger -t aidop-edge-upgrade "activated $(basename "$candidate")"