Browse Source

Updated prompt flow of update

uefigs139 3 months ago
parent
commit
3b02d3eedd
2 changed files with 69 additions and 25 deletions
  1. 3 1
      install/README.md
  2. 66 24
      install/update.sh

+ 3 - 1
install/README.md

@@ -168,8 +168,10 @@ sudo ./update.sh
 The updater performs:
 The updater performs:
 - Root permission check (fails fast before any work)
 - Root permission check (fails fast before any work)
 - Optional built-in backup API call (`/api/v1/settings/backup`) before update
 - Optional built-in backup API call (`/api/v1/settings/backup`) before update
+- Keeps only the newest 5 local backup ZIP files
 - Local-change warning + confirmation before `git reset --hard`
 - Local-change warning + confirmation before `git reset --hard`
-- Service stop/start with rollback attempt if update fails
+- If remote has no new commits, updater exits early without stopping the service
+- Service stop/start with code rollback + service restart attempt if update fails
 
 
 Useful environment overrides:
 Useful environment overrides:
 ```bash
 ```bash

+ 66 - 24
install/update.sh

@@ -10,9 +10,12 @@ BACKUP_DIR="${BACKUP_DIR:-$INSTALL_DIR/backups}"
 BAMBUDDY_API_URL="${BAMBUDDY_API_URL:-http://127.0.0.1:8000/api/v1}"
 BAMBUDDY_API_URL="${BAMBUDDY_API_URL:-http://127.0.0.1:8000/api/v1}"
 BAMBUDDY_API_KEY="${BAMBUDDY_API_KEY:-}"
 BAMBUDDY_API_KEY="${BAMBUDDY_API_KEY:-}"
 BACKUP_MODE="${BACKUP_MODE:-auto}" # auto|require|skip
 BACKUP_MODE="${BACKUP_MODE:-auto}" # auto|require|skip
+BACKUP_KEEP_COUNT=5
 FORCE="${FORCE:-0}"
 FORCE="${FORCE:-0}"
 
 
 SERVICE_STOPPED=0
 SERVICE_STOPPED=0
+CODE_UPDATED=0
+old_commit=""
 
 
 log() {
 log() {
   printf '[bambuddy-update] %s\n' "$*"
   printf '[bambuddy-update] %s\n' "$*"
@@ -31,18 +34,44 @@ require_cmd() {
   command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
   command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
 }
 }
 
 
+cleanup_old_backups() {
+  local -a backup_files
+  local max_count="$1"
+
+  [ "$max_count" -gt 0 ] || return 0
+
+  mapfile -t backup_files < <(ls -1t "$BACKUP_DIR"/bambuddy-backup-*.zip 2>/dev/null || true)
+  if [ "${#backup_files[@]}" -le "$max_count" ]; then
+    return 0
+  fi
+
+  for old_file in "${backup_files[@]:$max_count}"; do
+    rm -f "$old_file"
+  done
+
+  log "Pruned old backups, kept newest $max_count file(s)"
+}
+
 on_error() {
 on_error() {
   local exit_code="$1"
   local exit_code="$1"
+
   if [ "$SERVICE_STOPPED" -eq 1 ]; then
   if [ "$SERVICE_STOPPED" -eq 1 ]; then
+    if [ "$CODE_UPDATED" -eq 1 ] && [ -n "$old_commit" ]; then
+      warn "Update failed after code change, attempting rollback to $old_commit"
+      git reset --hard "$old_commit" || warn "Rollback reset failed"
+    fi
+
     warn "Update failed, attempting to restart service: $SERVICE_NAME"
     warn "Update failed, attempting to restart service: $SERVICE_NAME"
     systemctl start "$SERVICE_NAME" || true
     systemctl start "$SERVICE_NAME" || true
   fi
   fi
+
   exit "$exit_code"
   exit "$exit_code"
 }
 }
 trap 'on_error $?' ERR
 trap 'on_error $?' ERR
 
 
 create_backup() {
 create_backup() {
   local ts backup_file
   local ts backup_file
+  local -a auth_args=()
 
 
   if [ "$BACKUP_MODE" = "skip" ]; then
   if [ "$BACKUP_MODE" = "skip" ]; then
     log "Skipping backup (BACKUP_MODE=skip)"
     log "Skipping backup (BACKUP_MODE=skip)"
@@ -61,24 +90,17 @@ create_backup() {
   ts="$(date +%Y%m%d-%H%M%S)"
   ts="$(date +%Y%m%d-%H%M%S)"
   backup_file="$BACKUP_DIR/bambuddy-backup-$ts.zip"
   backup_file="$BACKUP_DIR/bambuddy-backup-$ts.zip"
 
 
+  [ -n "$BAMBUDDY_API_KEY" ] && auth_args=(-H "X-API-Key: $BAMBUDDY_API_KEY")
+
   log "Creating built-in backup via API: $backup_file"
   log "Creating built-in backup via API: $backup_file"
-  if [ -n "$BAMBUDDY_API_KEY" ]; then
-    if curl --silent --show-error --fail --location \
-      --connect-timeout 5 --max-time 900 \
-      -H "X-API-Key: $BAMBUDDY_API_KEY" \
-      "$BAMBUDDY_API_URL/settings/backup" \
-      --output "$backup_file"; then
-      log "Backup created successfully"
-      return 0
-    fi
-  else
-    if curl --silent --show-error --fail --location \
-      --connect-timeout 5 --max-time 900 \
-      "$BAMBUDDY_API_URL/settings/backup" \
-      --output "$backup_file"; then
-      log "Backup created successfully"
-      return 0
-    fi
+  if curl --silent --show-error --fail --location \
+    --connect-timeout 5 --max-time 900 \
+    "${auth_args[@]}" \
+    "$BAMBUDDY_API_URL/settings/backup" \
+    --output "$backup_file"; then
+    log "Backup created successfully"
+    cleanup_old_backups "$BACKUP_KEEP_COUNT"
+    return 0
   fi
   fi
 
 
   rm -f "$backup_file"
   rm -f "$backup_file"
@@ -113,11 +135,33 @@ if [ -z "$load_state" ] || [ "$load_state" = "not-found" ]; then
   die "Service not found: ${SERVICE_NAME}.service"
   die "Service not found: ${SERVICE_NAME}.service"
 fi
 fi
 
 
+old_commit="$(git rev-parse --short HEAD || true)"
+
+log "Fetching latest code from origin/$BRANCH"
+git fetch --prune origin
+
+remote_commit="$(git rev-parse --short "origin/$BRANCH" || true)"
+log "Current commit: ${old_commit:-unknown}"
+log "Remote commit: ${remote_commit:-unknown}"
+
+if git diff --quiet HEAD "origin/$BRANCH"; then
+  log "You are already running the latest version of Bambuddy."
+  read -r -p "Do you want to run the update process anyway? [y/N]: " run_anyway
+  case "${run_anyway:-}" in
+    y|Y|yes|YES) ;;
+    *) exit 0 ;;
+  esac
+else
+  read -r -p "An update for Bambuddy is available. Install now? [y/N]: " install_now
+  case "${install_now:-}" in
+    y|Y|yes|YES) ;;
+    *) exit 0 ;;
+  esac
+fi
+
 if [ -n "$(git status --porcelain)" ]; then
 if [ -n "$(git status --porcelain)" ]; then
-  warn "Local changes detected in $INSTALL_DIR."
-  warn "This update uses: git reset --hard origin/$BRANCH"
   if [ "$FORCE" != "1" ]; then
   if [ "$FORCE" != "1" ]; then
-    read -r -p "Discard local changes and continue? [y/N]: " answer
+    read -r -p "Local edits were detected in your installation. Updating now will overwrite those edits. Continue? [y/N]: " answer
     case "${answer:-}" in
     case "${answer:-}" in
       y|Y|yes|YES) ;;
       y|Y|yes|YES) ;;
       *) die "Update cancelled by user." ;;
       *) die "Update cancelled by user." ;;
@@ -129,15 +173,13 @@ fi
 
 
 create_backup
 create_backup
 
 
-old_commit="$(git rev-parse --short HEAD || true)"
-
 log "Stopping service: $SERVICE_NAME"
 log "Stopping service: $SERVICE_NAME"
 systemctl stop "$SERVICE_NAME"
 systemctl stop "$SERVICE_NAME"
 SERVICE_STOPPED=1
 SERVICE_STOPPED=1
 
 
-log "Updating code from origin/$BRANCH"
-git fetch --prune origin
+log "Updating code to origin/$BRANCH"
 git reset --hard "origin/$BRANCH"
 git reset --hard "origin/$BRANCH"
+CODE_UPDATED=1
 
 
 if [ -x "$VENV_PIP" ] && [ -f requirements.txt ]; then
 if [ -x "$VENV_PIP" ] && [ -f requirements.txt ]; then
   log "Updating Python dependencies"
   log "Updating Python dependencies"