wrolpi/scripts/reset_virtual_environments.sh
Roland 950a53017f
upgrade: rebuild venvs after a Python major-version change
A Debian major upgrade (e.g. 12 -> 13) replaces /usr/bin/python3
(3.11 -> 3.13).  Existing venvs still resolve `python3` to the system
interpreter, but their packages live in lib/python3.11/site-packages,
which the new interpreter never searches -- every import fails and
`pip install --upgrade` cannot repair them, taking down the API,
Controller, yt-dlp, and Help.

upgrade.sh now compares each venv's recorded Python version against the
current one and does a full rebuild via reset_virtual_environments.sh
when they differ.  Detecting a mismatch (rather than literally "3.11")
also self-heals future bumps and is a no-op once versions match or on a
fresh install with no pyvenv.cfg yet.

reset_virtual_environments.sh gains a -y/--yes flag to skip its
confirmation prompt; the interactive yes_or_no read loops forever on EOF
under the tty-less wrolpi-upgrade.service.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-20 09:08:30 -06:00

66 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Deletes and recreates all WROLPi Python virtual environments.
# This script requires internet access for pip install.
#
# Usage: reset_virtual_environments.sh [-y|--yes]
# -y, --yes Skip the confirmation prompt. Used by automated upgrades
# (wrolpi-upgrade.service), which run without a tty -- the
# interactive prompt would otherwise loop forever on EOF.
ASSUME_YES=false
for arg in "$@"; do
case "${arg}" in
-y | --yes) ASSUME_YES=true ;;
esac
done
# Re-execute this script if it wasn't called with sudo.
if [ $EUID != 0 ]; then
sudo "$0" "$@"
exit $?
fi
source /opt/wrolpi/wrolpi/scripts/lib.sh
if [ "${ASSUME_YES}" != true ]; then
yes_or_no "This script requires internet access. Do you want to continue?" || exit 0
fi
set -e
set -x
echo "Stopping WROLPi services..."
systemctl stop wrolpi-api.service || :
systemctl stop wrolpi-controller.service || :
systemctl stop wrolpi-help.service || :
# Reset main API venv
echo "Resetting /opt/wrolpi/venv..."
rm -rf /opt/wrolpi/venv
python3 -m venv /opt/wrolpi/venv
/opt/wrolpi/venv/bin/pip3 install --upgrade pip
/opt/wrolpi/venv/bin/pip3 install -r /opt/wrolpi/requirements.txt
# Reset Controller venv
echo "Resetting /opt/wrolpi/controller/venv..."
rm -rf /opt/wrolpi/controller/venv
python3 -m venv /opt/wrolpi/controller/venv
/opt/wrolpi/controller/venv/bin/pip install --upgrade pip
/opt/wrolpi/controller/venv/bin/pip install -r /opt/wrolpi/controller/requirements.txt
# Reset Help venv
echo "Resetting /opt/wrolpi-help/venv..."
rm -rf /opt/wrolpi-help/venv
python3 -m venv /opt/wrolpi-help/venv
/opt/wrolpi-help/venv/bin/pip3 install --upgrade pip
/opt/wrolpi-help/venv/bin/pip3 install -r /opt/wrolpi-help/requirements.txt
# Fix ownership
chown -R wrolpi:wrolpi /opt/wrolpi /opt/wrolpi-help
echo "Restarting WROLPi services..."
systemctl start wrolpi-controller.service || :
systemctl start wrolpi-api.service || :
systemctl start wrolpi-help.service || :
echo "Virtual environments have been reset."