update.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh
  2. # ==========================================================================
  3. # WeBrake updater — pulls the latest app.py and static/index.html straight
  4. # from the repository and restarts the service.
  5. #
  6. # /opt/webrake/update.sh
  7. #
  8. # Tries branches master and main, and both static/index.html and a root
  9. # index.html, validating every payload before anything is swapped in.
  10. # token.json is never touched: secrets stay local, exactly as installed.
  11. # ==========================================================================
  12. set -eu
  13. REPO_BASE="${WEBRAKE_REPO_BASE:-https://gogs.av2x.dev/av2x/WeBrake/raw}"
  14. BRANCHES="${WEBRAKE_BRANCH:-master main}"
  15. APP_DIR="/opt/webrake"
  16. BACKUP_DIR="$APP_DIR/.backup/$(date +%Y%m%d-%H%M%S)"
  17. say() { printf '\033[1;33m[WeBrake]\033[0m %s\n' "$*"; }
  18. die() { printf '\033[1;31m[WeBrake]\033[0m %s\n' "$*" >&2; exit 1; }
  19. [ "$(id -u)" = "0" ] || die "Run as root inside the Alpine container."
  20. [ -d "$APP_DIR" ] || die "WeBrake is not installed at $APP_DIR — run install.sh first."
  21. TMP="$(mktemp -d)"
  22. trap 'rm -rf "$TMP"' EXIT
  23. # fetch <dest> <validator-grep> <path> [alt-path…]
  24. fetch() {
  25. dest="$1"; check="$2"; shift 2
  26. for br in $BRANCHES; do
  27. for p in "$@"; do
  28. if wget -q -O "$dest" "$REPO_BASE/$br/$p" && [ -s "$dest" ] \
  29. && head -c 4096 "$dest" | grep -q "$check"; then
  30. say " fetched $p (branch: $br)"
  31. return 0
  32. fi
  33. done
  34. done
  35. return 1
  36. }
  37. say "Fetching latest files from ${REPO_BASE} (branches tried: ${BRANCHES})…"
  38. fetch "$TMP/app.py" "^#!/usr/bin/env python3" "app.py" \
  39. || die "No valid app.py found in the repo — nothing changed."
  40. fetch "$TMP/index.html" "^<!DOCTYPE html>" "static/index.html" "index.html" \
  41. || die "No valid index.html found (looked at static/index.html and index.html) — nothing changed."
  42. fetch "$TMP/update.sh" "^#!/bin/sh" "update.sh" \
  43. || say "Could not refresh update.sh (keeping current copy)."
  44. say "Backing up current files to $BACKUP_DIR…"
  45. mkdir -p "$BACKUP_DIR" "$APP_DIR/static"
  46. cp -a "$APP_DIR/app.py" "$APP_DIR/static/index.html" "$BACKUP_DIR/" 2>/dev/null || true
  47. install -m 0755 "$TMP/app.py" "$APP_DIR/app.py"
  48. install -m 0644 "$TMP/index.html" "$APP_DIR/static/index.html"
  49. [ -s "$TMP/update.sh" ] && install -m 0755 "$TMP/update.sh" "$APP_DIR/update.sh" || true
  50. say "token.json untouched — local secrets preserved."
  51. say "Restarting service…"
  52. rc-service webrake restart >/dev/null 2>&1 || rc-service webrake start
  53. say "WeBrake updated."