#!/usr/bin/env bash # update.sh — pull the latest from the configured branch, refresh deps, restart. # # Optional env (override defaults set at install time): # INSTALL_DIR default /opt/busbar-designer # SVC_USER default busbar # BRANCH default main (or whatever the local checkout tracks) set -euo pipefail INSTALL_DIR="${INSTALL_DIR:-/opt/busbar-designer}" SVC_USER="${SVC_USER:-busbar}" BRANCH="${BRANCH:-}" GREEN='\033[0;32m'; RED='\033[0;31m'; NC='\033[0m' log() { echo -e "${GREEN}▸${NC} $*"; } die() { echo -e "${RED}✗ $*${NC}" >&2; exit 1; } [[ $EUID -eq 0 ]] || die "Run as root." [[ -d "$INSTALL_DIR/.git" ]] || die "$INSTALL_DIR is not a git checkout." cd "$INSTALL_DIR" # Use the branch the working copy tracks if not overridden. if [[ -z "$BRANCH" ]]; then BRANCH=$(sudo -u "$SVC_USER" git -C "$INSTALL_DIR" rev-parse --abbrev-ref HEAD) fi log "Pulling origin/$BRANCH..." sudo -u "$SVC_USER" git -C "$INSTALL_DIR" fetch --depth 1 origin "$BRANCH" sudo -u "$SVC_USER" git -C "$INSTALL_DIR" reset --hard "origin/$BRANCH" log "Refreshing Python deps..." sudo -u "$SVC_USER" "$INSTALL_DIR/.venv/bin/pip" install --quiet --upgrade \ -r "$INSTALL_DIR/requirements.txt" gunicorn log "Restarting busbar-designer..." systemctl restart busbar-designer sleep 2 if systemctl is-active --quiet busbar-designer; then log "✓ Updated and restarted." else die "Service failed after restart. journalctl -u busbar-designer -n 50" fi