#!/bin/sh # YAAR — Update script # ───────────────────────────────────────────────────────────────────────────── # Pulls the two app files directly from master, redeploys, and restarts the # service. Runs INSIDE the Alpine LXC container as root. Never touches the # Proxmox host. # # Usage (inside the container): # sh /opt/yaar/update.sh # update code + restart service # sh /opt/yaar/update.sh --deps # also upgrade yt-dlp / Flask / Gunicorn # sh /opt/yaar/update.sh --reboot # reboot the whole container after update # yaar-update # convenience alias (installed by setup.sh) # # One-liner (always fetches the newest update logic too): # curl -fsSL https://gogs.av2x.dev/av2x/yaar/raw/master/update.sh | sh # # Safety: current files are backed up before overwriting. New files are fetched # to a temp dir first and only moved into place if BOTH downloads succeed, so a # failed pull can't leave a half-updated deploy. If the service fails its health # check on the new code, the previous version is restored automatically. # ───────────────────────────────────────────────────────────────────────────── set -e RAW="https://gogs.av2x.dev/av2x/yaar/raw/master" YAAR_DIR="/opt/yaar" WEB_DIR="${YAAR_DIR}/web" VENV_DIR="${YAAR_DIR}/venv" LOG_DIR="${YAAR_DIR}/logs" BACKUP_DIR="${YAAR_DIR}/backups" PORT=7474 RED='\033[0;31m'; GREEN='\033[0;32m'; CYAN='\033[0;36m'; YELLOW='\033[0;33m'; NC='\033[0m' info() { printf "${CYAN}[YAAR]${NC} %s\n" "$*"; } success() { printf "${GREEN}[OK]${NC} %s\n" "$*"; } warn() { printf "${YELLOW}[WARN]${NC} %s\n" "$*"; } die() { printf "${RED}[ERR]${NC} %s\n" "$*"; exit 1; } # ── Flags ───────────────────────────────────────────────────────────────────── DO_DEPS=0 DO_REBOOT=0 for arg in "$@"; do case "$arg" in --deps) DO_DEPS=1 ;; --reboot) DO_REBOOT=1 ;; -h|--help) grep '^#' "$0" | sed 's/^# \{0,1\}//' exit 0 ;; *) warn "Unknown flag: $arg (ignored)" ;; esac done # ── Preflight ───────────────────────────────────────────────────────────────── [ "$(id -u)" -eq 0 ] || die "Run as root: sh /opt/yaar/update.sh" command -v pveversion >/dev/null 2>&1 && \ die "Run this INSIDE the container, not on the Proxmox host." [ -d "${WEB_DIR}" ] || die "${WEB_DIR} not found — is YAAR installed? Run setup.sh first." command -v curl >/dev/null 2>&1 || { info "Installing curl..."; apk add --no-cache curl >/dev/null; } # ── 1. Fetch new files to a temp dir ────────────────────────────────────────── # Download both first; only deploy if BOTH succeed, so we never half-update. TMP_DIR="$(mktemp -d /tmp/yaar-update.XXXXXX)" trap 'rm -rf "${TMP_DIR}"' EXIT mkdir -p "${TMP_DIR}/templates" info "Fetching app.py..." curl -fsSL "${RAW}/web/app.py" -o "${TMP_DIR}/app.py" \ || die "Failed to download app.py — nothing changed." info "Fetching index.html..." curl -fsSL "${RAW}/web/templates/index.html" -o "${TMP_DIR}/templates/index.html" \ || die "Failed to download index.html — nothing changed." # Sanity: make sure we got real files, not an error page. [ -s "${TMP_DIR}/app.py" ] || die "Downloaded app.py is empty." head -c 64 "${TMP_DIR}/app.py" | grep -q "python\|import\|#" \ || warn "app.py doesn't look like Python — continuing, health check will catch it." success "Both files downloaded" # ── 2. Back up current files ────────────────────────────────────────────────── STAMP="$(date -u +%Y-%m-%d_%H-%M-%S)" BACKUP="${BACKUP_DIR}/${STAMP}" mkdir -p "${BACKUP}/templates" info "Backing up current files to ${BACKUP}..." [ -f "${WEB_DIR}/app.py" ] && cp "${WEB_DIR}/app.py" "${BACKUP}/app.py" [ -f "${WEB_DIR}/templates/index.html" ] && cp "${WEB_DIR}/templates/index.html" "${BACKUP}/templates/index.html" success "Backup saved" # Keep only the 5 most recent backups. ls -1dt "${BACKUP_DIR}"/*/ 2>/dev/null | tail -n +6 | while read -r old; do rm -rf "${old}"; done # ── 3. Deploy (atomic move into place) ──────────────────────────────────────── info "Deploying new files..." mv "${TMP_DIR}/app.py" "${WEB_DIR}/app.py" mv "${TMP_DIR}/templates/index.html" "${WEB_DIR}/templates/index.html" success "New files deployed" # Refresh this update script too, so the next run uses the newest logic. curl -fsSL "${RAW}/update.sh" -o "${YAAR_DIR}/update.sh" 2>/dev/null \ && chmod +x "${YAAR_DIR}/update.sh" || true # ── 4. Optional dependency upgrade ──────────────────────────────────────────── if [ "${DO_DEPS}" = "1" ]; then info "Upgrading Python dependencies (yt-dlp, Flask, Gunicorn)..." "${VENV_DIR}/bin/pip" install --quiet --upgrade yt-dlp flask gunicorn success "Dependencies upgraded — yt-dlp $(${VENV_DIR}/bin/yt-dlp --version)" fi # ── 5. Restart with health check + auto-rollback ────────────────────────────── info "Restarting YAAR service..." rc-service yaar restart >/dev/null 2>&1 || true sleep 2 healthy=0 rc-service yaar status 2>/dev/null | grep -q "started" && healthy=1 if command -v curl >/dev/null 2>&1; then curl -fsS -o /dev/null --max-time 5 "http://127.0.0.1:${PORT}/" 2>/dev/null && healthy=1 fi if [ "${healthy}" = "1" ]; then success "Service restarted and healthy on port ${PORT}" else warn "Service did not come up cleanly — rolling back..." [ -f "${BACKUP}/app.py" ] && cp "${BACKUP}/app.py" "${WEB_DIR}/app.py" [ -f "${BACKUP}/templates/index.html" ] && cp "${BACKUP}/templates/index.html" "${WEB_DIR}/templates/index.html" rc-service yaar restart >/dev/null 2>&1 || true sleep 2 if rc-service yaar status 2>/dev/null | grep -q "started"; then warn "Rolled back — YAAR is running on the PREVIOUS version." warn "Check ${LOG_DIR}/gunicorn.log for why the new code failed." exit 1 else die "Rollback also failed to start. Inspect ${LOG_DIR}/gunicorn.log manually." fi fi # ── 6. Optional container reboot ────────────────────────────────────────────── if [ "${DO_REBOOT}" = "1" ]; then warn "Rebooting the container in 3 seconds (Ctrl+C to cancel)..." sleep 3 reboot exit 0 fi # ── Done ────────────────────────────────────────────────────────────────────── HOST_IP=$(ip route get 1.1.1.1 2>/dev/null | awk '/src/{print $7}' | head -1 || echo "") printf "\n" printf "${GREEN}╔══════════════════════════════════════════════════════╗${NC}\n" printf "${GREEN}║ YAAR updated successfully ║${NC}\n" printf "${GREEN}╠══════════════════════════════════════════════════════╣${NC}\n" printf "${GREEN}║ Web UI: http://%-34s║${NC}\n" "${HOST_IP}:${PORT}" printf "${GREEN}║ Backup: %-42s║${NC}\n" "${BACKUP}" printf "${GREEN}╚══════════════════════════════════════════════════════╝${NC}\n" printf "\n" info "Hard-refresh your browser (Ctrl+Shift+R) to load the new UI."