wrolpi/scripts/upgrade.sh

83 lines
2.7 KiB
Bash
Executable file

#! /usr/bin/env bash
# Installs any new dependencies of the App and API. Uses internet.
set -e
set -x
# Ensure help service is always restarted, but preserve exit code
cleanup() {
local exit_code=$?
systemctl restart wrolpi-help.service || :
exit $exit_code
}
trap cleanup EXIT
# Install any App dependencies.
cd /opt/wrolpi/app || exit 1
npm install || npm install || npm install || npm install # try install multiple times :(
# Install any new Python requirements.
/opt/wrolpi/venv/bin/pip3 install --upgrade -r /opt/wrolpi/requirements.txt
# Upgrade the WROLPi database.
(cd /opt/wrolpi && /opt/wrolpi/venv/bin/python3 /opt/wrolpi/main.py db upgrade)
# Upgrade Controller
upgrade_controller() {
echo "Upgrading WROLPi Controller..."
# Stop Controller during upgrade
systemctl stop wrolpi-controller || true
# Create Controller venv if it doesn't exist
if [ ! -d /opt/wrolpi/controller/venv ]; then
echo "Creating Controller virtual environment..."
python3 -m venv /opt/wrolpi/controller/venv
fi
# Update dependencies
echo "Updating Controller dependencies..."
/opt/wrolpi/controller/venv/bin/pip install --upgrade pip
/opt/wrolpi/controller/venv/bin/pip install --upgrade -r /opt/wrolpi/controller/requirements.txt
# Reload systemd in case service file changed
systemctl daemon-reload
# Start Controller
systemctl start wrolpi-controller
# Wait for health check (up to 30 seconds)
echo "Waiting for Controller health check..."
for i in {1..30}; do
if curl -s http://localhost:8087/api/health > /dev/null 2>&1; then
echo "Controller upgraded successfully"
return 0
fi
sleep 1
done
echo "WARNING: Controller health check failed after upgrade"
echo "Check logs: journalctl -u wrolpi-controller -n 50"
return 1
}
upgrade_controller || echo "Controller upgrade failed, continuing..."
# Upgrade WROLPi Help.
/opt/wrolpi/scripts/install_help_service.sh || echo "Help install failed."
# Get map db blob, if it is missing.
MAP_DB_BLOB=/opt/wrolpi-blobs/map-db-gis.dump
if [[ ! -f ${MAP_DB_BLOB} || ! -s ${MAP_DB_BLOB} ]]; then
echo "Downloading new map blob (1.2 GB)..."
wget https://wrolpi.nyc3.cdn.digitaloceanspaces.com/map-db-gis.dump -O ${MAP_DB_BLOB}
fi
if [[ -f /opt/wrolpi-blobs/gis-map.dump.gz && -s ${MAP_DB_BLOB} ]]; then
# Remove old blob now that we have the new one.
rm /opt/wrolpi-blobs/gis-map.dump.gz
fi
# Migrate map DB if necessary. Do this before repair because it will reset map if map db is empty.
/opt/wrolpi/wrolpi/scripts/migrate_map_db.sh || echo "Map DB migration failed."
# Install any configs, restart services.
/opt/wrolpi/repair.sh