Files
ytstream/deploy/decommission.sh
T
Claude c7a80d200e Hand cron over from youtube-automate, and script the rest
Cron now calls `ytstream run` hourly and ytstream's update-ytdlp.sh weekly.
Both inherit youtube-automate's healthchecks UUIDs and keep its schedules
unchanged: a check may be configured with a cron expression rather than a
simple period, so moving to the :23/04:50 slots the fragment proposed could
have alerted on a job that ran fine. Inheriting also means the placeholder
UUIDs never needed filling in.

The old service turned out to track only 2 channels, and one of them --
The Pyramid Podcast -- was sitting unresolved in ytstream's approval queue.
Decommissioning without checking would have silently dropped half of what
the old service existed to follow. Approved and backfilled.

decommission.sh does the two steps needing root (nginx repoint, disable the
unit) and refuses until an admin password is set, because the UI fails
closed and the hostname would otherwise serve a login nobody can pass.
Deleting the 2.0 GB of old downloads and touching the Jellyfin libraries
are left out on purpose.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-13 10:53:48 +01:00

92 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
# Retire youtube-automate and hand tube.jihakuz.xyz to ytstream.
#
# sudo /opt/ytstream/deploy/decommission.sh
#
# This is plan.md §12 steps 4 and 5 — the two that need root. Everything it does
# is reversible: the nginx change is backed up next to the original, and the
# service is disabled rather than removed.
#
# Deliberately NOT done here, because each one destroys something:
# * deleting /disks/Plex/YouTube (§12 step 6)
# * removing or renaming the Jellyfin libraries (§12 step 3)
# * removing /opt/youtube-automate, its repo, subs.db or specs.md (§12 step 7)
set -euo pipefail
CONF=/etc/nginx/sites-available/jihakuz.xyz
OLD_PORT=8085 # youtube-automate
NEW_PORT=8086 # ytstream admin
DB=/var/lib/ytstream/ytstream.db
if [[ $EUID -ne 0 ]]; then
echo "This script needs root. Run: sudo $0" >&2
exit 1
fi
# ---------------------------------------------------------------- preflight
# Repointing a public hostname at a service that cannot be logged into wastes an
# evening working out why. The admin UI fails closed with no password set: every
# login attempt is rejected, including your brother's.
if ! sqlite3 "$DB" \
"SELECT 1 FROM setting WHERE key='admin_password_hash' AND value <> '';" \
2>/dev/null | grep -q 1; then
echo "!! No admin password is set, so nobody can log in to the UI this would" >&2
echo " expose. Run this first, then re-run me:" >&2
echo " sudo -u susan /var/lib/ytstream/venv/bin/ytstream set-password" >&2
exit 1
fi
# And do not hand the hostname to a port nothing is listening on.
code=$(curl -s -o /dev/null -w '%{http_code}' -m 10 "http://127.0.0.1:${NEW_PORT}/" || true)
if [[ "$code" != "200" && "$code" != "303" && "$code" != "302" ]]; then
echo "!! ytstream-admin is not answering on ${NEW_PORT} (got '${code}')." >&2
echo " systemctl status ytstream-admin" >&2
exit 1
fi
echo "==> ytstream-admin answers on ${NEW_PORT} (HTTP ${code}) and has a password set"
# ------------------------------------------------------------------- nginx
if grep -q "127.0.0.1:${NEW_PORT}" "$CONF"; then
echo "==> nginx already points tube.jihakuz.xyz at ${NEW_PORT}; skipping"
elif ! grep -q "127.0.0.1:${OLD_PORT}" "$CONF"; then
echo "!! Found neither ${OLD_PORT} nor ${NEW_PORT} in $CONF. Not guessing." >&2
exit 1
else
BACKUP="${CONF}.bak-$(date +%Y%m%d%H%M%S)"
cp -a "$CONF" "$BACKUP"
echo "==> Backed up $CONF to $BACKUP"
# One line. The proxy_set_header block the youtube-automate fix added is
# already there and still correct — X-Forwarded-For in particular, because
# the login throttle keys on it and without it one attacker locks out all.
sed -i "s|proxy_pass http://127.0.0.1:${OLD_PORT};|proxy_pass http://127.0.0.1:${NEW_PORT};|" "$CONF"
echo "==> Repointed tube.jihakuz.xyz at 127.0.0.1:${NEW_PORT}"
if ! nginx -t; then
echo "!! nginx config test failed — restoring the backup" >&2
cp -a "$BACKUP" "$CONF"
nginx -t
exit 1
fi
systemctl reload nginx
echo "==> nginx reloaded"
fi
# ---------------------------------------------------------------- the service
if systemctl is-enabled --quiet youtube-automate.service 2>/dev/null \
|| systemctl is-active --quiet youtube-automate.service 2>/dev/null; then
systemctl disable --now youtube-automate.service
echo "==> Stopped and disabled youtube-automate.service"
echo " Unit left in place at /etc/systemd/system/ — 'systemctl enable --now"
echo " youtube-automate' brings it back if this turns out to be premature."
else
echo "==> youtube-automate.service already stopped and disabled"
fi
echo
echo "Done. tube.jihakuz.xyz now serves ytstream's admin UI."
echo "Still holding, on purpose:"
echo " * /disks/Plex/YouTube ($(du -sh /disks/Plex/YouTube 2>/dev/null | cut -f1)) — the old downloads"
echo " * both Jellyfin libraries — remove/rename by hand when you are ready"
echo " * /opt/youtube-automate, its repo, subs.db and specs.md — keep these"