update.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. # ===========================================================================
  3. # Dis2Hook updater — pulls the latest app.py and index.html straight from the
  4. # repo and restarts the service. Never touches token.json or config.json.
  5. #
  6. # /opt/dis2hook/update.sh update app.py + index.html
  7. # /opt/dis2hook/update.sh --all also refresh update.sh and the initd
  8. #
  9. # D2H_BRANCH=master ./update.sh pull from a different branch
  10. # ===========================================================================
  11. set -eu
  12. REPO="${D2H_REPO:-https://gogs.av2x.dev/av2x/Dis2Hook}"
  13. APP_DIR="$(cd "$(dirname "$0")" && pwd)"
  14. SERVICE="dis2hook"
  15. BRANCH="${D2H_BRANCH:-$(cat "$APP_DIR/.branch" 2>/dev/null || echo main)}"
  16. say() { printf '\033[1;33m[dis2hook]\033[0m %s\n' "$*"; }
  17. fail() { printf '\033[1;31m[dis2hook] ERROR:\033[0m %s\n' "$*" >&2; exit 1; }
  18. fetch() { wget -q -O "$2" "$REPO/raw/$BRANCH/$1" && [ -s "$2" ]; }
  19. TMP="$(mktemp -d)"
  20. trap 'rm -rf "$TMP"' EXIT
  21. say "Pulling latest files from $REPO ($BRANCH)…"
  22. fetch app.py "$TMP/app.py" || fail "Could not download app.py."
  23. fetch index.html "$TMP/index.html" || fail "Could not download index.html."
  24. # Sanity checks so a broken download never replaces a working install.
  25. grep -q DIS2HOOK_VERSION "$TMP/app.py" || fail "Downloaded app.py failed validation. Nothing changed."
  26. grep -qi '<html' "$TMP/index.html" || fail "Downloaded index.html failed validation. Nothing changed."
  27. python3 -m py_compile "$TMP/app.py" 2>/dev/null || fail "Downloaded app.py does not compile. Nothing changed."
  28. OLD_VER="$(grep -m1 'DIS2HOOK_VERSION =' "$APP_DIR/app.py" 2>/dev/null | cut -d'"' -f2 || echo '?')"
  29. NEW_VER="$(grep -m1 'DIS2HOOK_VERSION =' "$TMP/app.py" | cut -d'"' -f2)"
  30. cp -f "$APP_DIR/app.py" "$APP_DIR/app.py.bak" 2>/dev/null || true
  31. cp -f "$APP_DIR/index.html" "$APP_DIR/index.html.bak" 2>/dev/null || true
  32. install -m 644 "$TMP/app.py" "$APP_DIR/app.py"
  33. install -m 644 "$TMP/index.html" "$APP_DIR/index.html"
  34. say "app.py + index.html updated ($OLD_VER → $NEW_VER). Previous versions kept as *.bak."
  35. if [ "${1:-}" = "--all" ]; then
  36. fetch update.sh "$TMP/update.sh" && install -m 755 "$TMP/update.sh" "$APP_DIR/update.sh" \
  37. && say "update.sh refreshed." || say "Skipped update.sh (not downloadable)."
  38. fetch dis2hook.initd "$TMP/dis2hook.initd" && install -m 755 "$TMP/dis2hook.initd" "/etc/init.d/$SERVICE" \
  39. && say "Service script refreshed." || say "Skipped service script (not downloadable)."
  40. fi
  41. say "token.json and config.json were left untouched."
  42. if rc-service "$SERVICE" restart >/dev/null 2>&1; then
  43. say "Service restarted — Dis2Hook v$NEW_VER is live."
  44. else
  45. say "Could not restart via OpenRC. Start manually: rc-service $SERVICE start"
  46. fi