update_macos.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. INSTALL_DIR="${INSTALL_DIR:-/opt/bambuddy}"
  4. SERVICE_NAME="${SERVICE_NAME:-com.bambuddy.app}"
  5. PLIST_PATH="${PLIST_PATH:-$HOME/Library/LaunchAgents/com.bambuddy.app.plist}"
  6. BRANCH="${BRANCH:-}"
  7. VENV_PIP="${VENV_PIP:-$INSTALL_DIR/venv/bin/pip}"
  8. FRONTEND_DIR="${FRONTEND_DIR:-$INSTALL_DIR/frontend}"
  9. BACKUP_DIR="${BACKUP_DIR:-$INSTALL_DIR/backups}"
  10. BAMBUDDY_API_URL="${BAMBUDDY_API_URL:-http://127.0.0.1:8000/api/v1}"
  11. BAMBUDDY_API_KEY="${BAMBUDDY_API_KEY:-}"
  12. BACKUP_MODE="${BACKUP_MODE:-auto}" # auto|require|skip
  13. BACKUP_KEEP_COUNT=5
  14. FORCE="${FORCE:-0}"
  15. SERVICE_STOPPED=0
  16. CODE_UPDATED=0
  17. old_commit=""
  18. log() {
  19. printf '[bambuddy-update] %s\n' "$*"
  20. }
  21. warn() {
  22. printf '[bambuddy-update] WARNING: %s\n' "$*" >&2
  23. }
  24. die() {
  25. printf '[bambuddy-update] ERROR: %s\n' "$*" >&2
  26. exit 1
  27. }
  28. require_cmd() {
  29. command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
  30. }
  31. cleanup_old_backups() {
  32. local -a backup_files
  33. local max_count="$1"
  34. [ "$max_count" -gt 0 ] || return 0
  35. mapfile -t backup_files < <(ls -1t "$BACKUP_DIR"/bambuddy-backup-*.zip 2>/dev/null || true)
  36. if [ "${#backup_files[@]}" -le "$max_count" ]; then
  37. return 0
  38. fi
  39. for old_file in "${backup_files[@]:$max_count}"; do
  40. rm -f "$old_file"
  41. done
  42. log "Pruned old backups, kept newest $max_count file(s)"
  43. }
  44. is_service_active() {
  45. launchctl list | grep -q "$SERVICE_NAME"
  46. }
  47. # Restore the --loop asyncio pin on a plist written before it existed (#3001).
  48. #
  49. # The macOS twin of the systemd repair in update.sh, and there for the same
  50. # reason: install.sh has pinned the loop since 2026-07-05 (#1896), this script
  51. # has never rewritten the plist, and nothing else does -- so an install created
  52. # before that date still launches on uvloop today. uvloop reaches macOS as
  53. # well, since uvicorn[standard] only excludes it on Windows. That costs every
  54. # RTSP camera (#3001) and risks silently truncated Virtual Printer FTP uploads
  55. # (#1896), neither of which is visible from outside the machine.
  56. #
  57. # PlistBuddy is used rather than sed because the plist is XML and
  58. # ProgramArguments is an array; appending the two strings is safe because
  59. # uvicorn accepts its options in any order after the app path.
  60. repair_loop_flag() {
  61. local plistbuddy="/usr/libexec/PlistBuddy" backup
  62. [ -f "$PLIST_PATH" ] || return 0
  63. if grep -q -- '--loop' "$PLIST_PATH"; then
  64. return 0
  65. fi
  66. if [ ! -x "$plistbuddy" ]; then
  67. warn "PlistBuddy not found; add '--loop' and 'asyncio' to ProgramArguments in $PLIST_PATH by hand. See #1896."
  68. return 0
  69. fi
  70. # A plist that does not invoke uvicorn directly is someone else's
  71. # arrangement and is described rather than edited.
  72. if ! grep -q 'uvicorn' "$PLIST_PATH"; then
  73. warn "$PLIST_PATH does not start uvicorn directly; add '--loop asyncio' to it by hand. See #1896."
  74. return 0
  75. fi
  76. backup="$PLIST_PATH.bak-$(date +%Y%m%d-%H%M%S)"
  77. cp -p "$PLIST_PATH" "$backup" || {
  78. warn "Could not back up $PLIST_PATH; leaving it alone."
  79. return 0
  80. }
  81. if ! "$plistbuddy" -c 'Add :ProgramArguments: string --loop' \
  82. -c 'Add :ProgramArguments: string asyncio' "$PLIST_PATH" >/dev/null 2>&1; then
  83. warn "Failed to edit $PLIST_PATH; restoring from $backup."
  84. cp -p "$backup" "$PLIST_PATH" || true
  85. return 0
  86. fi
  87. log "Added the missing '--loop asyncio' flag to $PLIST_PATH (was written before #1896; backup at $backup)"
  88. log "Without it Bambuddy runs on uvloop, which breaks RTSP cameras (#3001) and can truncate Virtual Printer FTP uploads (#1896)."
  89. }
  90. on_error() {
  91. local exit_code="$1"
  92. if [ "$SERVICE_STOPPED" -eq 1 ]; then
  93. if [ "$CODE_UPDATED" -eq 1 ] && [ -n "$old_commit" ]; then
  94. warn "Update failed after code change, attempting rollback to $old_commit"
  95. git reset --hard "$old_commit" || warn "Rollback reset failed"
  96. fi
  97. warn "Update failed, attempting to restart service: $SERVICE_NAME"
  98. launchctl load "$PLIST_PATH" || true
  99. fi
  100. exit "$exit_code"
  101. }
  102. trap 'on_error $?' ERR
  103. create_backup() {
  104. local ts backup_file
  105. local -a auth_args=()
  106. if [ "$BACKUP_MODE" = "skip" ]; then
  107. log "Skipping backup (BACKUP_MODE=skip)"
  108. return 0
  109. fi
  110. if ! is_service_active; then
  111. if [ "$BACKUP_MODE" = "require" ]; then
  112. die "Service is not running; cannot call built-in backup API."
  113. fi
  114. warn "Service is not running; skipping built-in backup API call."
  115. return 0
  116. fi
  117. mkdir -p "$BACKUP_DIR"
  118. ts="$(date +%Y%m%d-%H%M%S)"
  119. backup_file="$BACKUP_DIR/bambuddy-backup-$ts.zip"
  120. [ -n "$BAMBUDDY_API_KEY" ] && auth_args=(-H "X-API-Key: $BAMBUDDY_API_KEY")
  121. log "Creating built-in backup via API: $backup_file"
  122. if curl --silent --show-error --fail --location \
  123. --connect-timeout 5 --max-time 900 \
  124. ${auth_args:+${auth_args[@]}} \
  125. "$BAMBUDDY_API_URL/settings/backup" \
  126. --output "$backup_file"; then
  127. log "Backup created successfully"
  128. cleanup_old_backups "$BACKUP_KEEP_COUNT"
  129. return 0
  130. fi
  131. rm -f "$backup_file"
  132. if [ "$BACKUP_MODE" = "require" ]; then
  133. die "Built-in backup API call failed (BACKUP_MODE=require)."
  134. fi
  135. warn "Built-in backup API call failed. Continuing because BACKUP_MODE=auto."
  136. }
  137. # NOTE: kept root check as-is (you can remove if desired)
  138. #[ "${EUID:-$(id -u)}" -eq 0 ] || die "Run as root (or with sudo)."
  139. case "$BACKUP_MODE" in
  140. auto|require|skip) ;;
  141. *) die "Invalid BACKUP_MODE '$BACKUP_MODE' (expected: auto, require, skip)." ;;
  142. esac
  143. require_cmd git
  144. require_cmd launchctl
  145. require_cmd curl
  146. [ -d "$INSTALL_DIR" ] || die "Install directory not found: $INSTALL_DIR"
  147. [ -f "$PLIST_PATH" ] || die "Service plist not found: $PLIST_PATH"
  148. cd "$INSTALL_DIR"
  149. [ -d .git ] || die "No git repository found in: $INSTALL_DIR"
  150. if [ -z "$BRANCH" ]; then
  151. BRANCH="$(git rev-parse --abbrev-ref HEAD)"
  152. [ "$BRANCH" = "HEAD" ] && BRANCH="main"
  153. fi
  154. # replaced systemctl show check
  155. if ! launchctl list | grep -q "$SERVICE_NAME" && [ ! -f "$PLIST_PATH" ]; then
  156. die "Service not found: $SERVICE_NAME"
  157. fi
  158. old_commit="$(git rev-parse --short HEAD || true)"
  159. log "Fetching latest code from origin/$BRANCH"
  160. git fetch --prune origin
  161. remote_commit="$(git rev-parse --short "origin/$BRANCH" || true)"
  162. log "Current commit: ${old_commit:-unknown}"
  163. log "Remote commit: ${remote_commit:-unknown}"
  164. if git diff --quiet HEAD "origin/$BRANCH"; then
  165. log "You are already running the latest version of Bambuddy."
  166. read -r -p "Do you want to run the update process anyway? [y/N]: " run_anyway
  167. case "${run_anyway:-}" in
  168. y|Y|yes|YES) ;;
  169. *) exit 0 ;;
  170. esac
  171. else
  172. read -r -p "An update for Bambuddy is available. Install now? [y/N]: " install_now
  173. case "${install_now:-}" in
  174. y|Y|yes|YES) ;;
  175. *) exit 0 ;;
  176. esac
  177. fi
  178. if [ -n "$(git status --porcelain)" ]; then
  179. if [ "$FORCE" != "1" ]; then
  180. read -r -p "Local edits were detected in your installation. Updating now will overwrite those edits. Continue? [y/N]: " answer
  181. case "${answer:-}" in
  182. y|Y|yes|YES) ;;
  183. *) die "Update cancelled by user." ;;
  184. esac
  185. else
  186. warn "Proceeding without prompt because FORCE=1."
  187. fi
  188. fi
  189. create_backup
  190. log "Stopping service: $SERVICE_NAME"
  191. launchctl unload "$PLIST_PATH"
  192. SERVICE_STOPPED=1
  193. log "Updating code to origin/$BRANCH"
  194. git reset --hard "origin/$BRANCH"
  195. CODE_UPDATED=1
  196. if [ -x "$VENV_PIP" ] && [ -f requirements.txt ]; then
  197. log "Updating Python dependencies"
  198. "$VENV_PIP" install -r requirements.txt
  199. else
  200. warn "Skipping Python dependency update (venv pip or requirements.txt missing)."
  201. fi
  202. if [ -f "$FRONTEND_DIR/package.json" ]; then
  203. if command -v npm >/dev/null 2>&1; then
  204. log "Building frontend"
  205. (
  206. cd "$FRONTEND_DIR"
  207. npm ci
  208. npm run build
  209. )
  210. else
  211. warn "Skipping frontend build (npm not installed)."
  212. fi
  213. else
  214. warn "Skipping frontend build (frontend/package.json not found)."
  215. fi
  216. repair_loop_flag
  217. log "Starting service: $SERVICE_NAME"
  218. launchctl load "$PLIST_PATH"
  219. SERVICE_STOPPED=0
  220. launchctl list | grep "$SERVICE_NAME" || true
  221. new_commit="$(git rev-parse --short HEAD || true)"
  222. log "Update complete: ${old_commit:-unknown} -> ${new_commit:-unknown}"