mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
The database now lives at <media>/config/wrolpi.db so a library (files, configs, AND database) travels with the drive between WROLPi systems. Core: - wrolpi/db.py: lazy SQLite engine factory (WAL, busy_timeout=30s, foreign_keys=ON); get_db_curs yields a plain sqlite3.Cursor; native sqlite3 paramstyle everywhere (no compatibility shims); global sqlite3 adapters keep raw-SQL datetime formats identical to the ORM's. - wrolpi/fts.py: FTS5 external-content search replacing tsvector/GIN. Injection-proof websearch->FTS5 translator, bm25(10,4,2,1) reproducing A/B/C/D weighting, fast(abc)/deep column filtering, snippet headlines. Stemming contract pinned by dedicated tests (watch/watches/watched/ watching are interchangeable). - wrolpi/schema_ddl.py: SQLite triggers shared verbatim by the alembic baseline and test DBs; fixes the channel video_count DELETE/UPDATE bugs and a latent total_size bug (it used the video id as channel id). - wrolpi/db_bootstrap.py: startup auto-create/migrate; refuses databases written by a newer WROLPi (db_version_mismatch); SQLite 3.40 floor; network-filesystem refusal; never creates/migrates/trusts a database beneath an unmounted production media directory (shadow-DB guard). - Alembic: 85 Postgres-era migrations squashed into one baseline; revision ids are dates (2026_07_10_1350) so filenames self-sort and alembic_version says when a schema was created; render_as_batch=True. - All raw Postgres SQL rewritten (ANY->json_each IN, array_agg @> -> HAVING COUNT(DISTINCT), unnest->executemany/UPDATE..FROM, generate_series histograms->strftime GROUP BY, DISTINCT ON-> row_number(), writable CTEs->two statements, jsonb_*->json_*, explicit NULLS FIRST/LAST, LIMIT-before-OFFSET). Simple statements converted to SQLAlchemy instead (flag upsert, download retry/summary/tag checks, channel-by-tag estimate, Directory delete). - Config imports count skipped entries; background dumps are blocked after a lossy import so partial imports can never overwrite YAML configs (they are the sole source of truth for rebuilds now). - Search tolerates explicit JSON nulls for limit/offset (Postgres treated OFFSET NULL as 0; SQLite errors). - file_group.id/doc_section.id render as INTEGER PRIMARY KEY on SQLite (BIGINT is not a rowid alias; FTS5 content_rowid requires it). Ops teardown: docker-compose db service, CI postgres sidecars, initialize_api_db.sh, controller postgresql service, psycopg2-binary all removed. upgrade.sh stops/disables postgresql (data is NOT deleted; `systemctl enable --now postgresql` restores it); systemd units no longer Want postgresql.service. Existing-user upgrade: first boot creates+migrates the DB, configs import (tags recreate their FileGroups from disk), a refresh rebuilds the library index. QA'd on the 10.0.0.8 RPi: rebuild matched the Postgres baseline exactly; survived an uncontrolled hard reboot with clean integrity checks. Accepted behavior changes: FTS5 has no stopwords; bm25 length normalization shifts some rank orders; fast-path d_headline is un-highlighted; all-negative queries match nothing. Deferred: Debian/trixie package lists (incl. sqlite3 CLI), Cypress re-baseline, VACUUM INTO snapshots, exFAT/NTFS warning, auto-kicked rebuild refresh with progress UX. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
212 lines
8.3 KiB
Bash
Executable file
212 lines
8.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# This script will attempt to repair a WROLPi installation. It will not use internet.
|
|
|
|
cd /opt/wrolpi || (echo "Cannot repair. /opt/wrolpi does not exist" && exit 1)
|
|
|
|
# Re-execute this script if it wasn't called with sudo.
|
|
if [ $EUID != 0 ]; then
|
|
sudo "$0" "$@"
|
|
exit $?
|
|
fi
|
|
|
|
rpi=false
|
|
if (grep 'Raspberry Pi' /proc/cpuinfo >/dev/null); then
|
|
rpi=true
|
|
fi
|
|
|
|
set -e
|
|
set -x
|
|
|
|
# Stop services if they are running.
|
|
systemctl stop wrolpi-controller.service || :
|
|
systemctl stop wrolpi-api.service || :
|
|
systemctl stop wrolpi-app.service || :
|
|
systemctl stop wrolpi-kiwix.service || :
|
|
systemctl stop caddy || :
|
|
|
|
# Clear Python bytecode cache from virtual environments to fix potential corruption.
|
|
echo "Clearing Python bytecode cache..."
|
|
find /opt/wrolpi/venv -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || :
|
|
find /opt/wrolpi/venv -name "*.pyc" -delete 2>/dev/null || :
|
|
find /opt/wrolpi/controller/venv -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || :
|
|
find /opt/wrolpi/controller/venv -name "*.pyc" -delete 2>/dev/null || :
|
|
find /opt/wrolpi-help/venv -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || :
|
|
find /opt/wrolpi-help/venv -name "*.pyc" -delete 2>/dev/null || :
|
|
|
|
# Create the WROLPi user
|
|
grep wrolpi: /etc/passwd || useradd -md /home/wrolpi wrolpi -s "$(command -v bash)"
|
|
[ -f /home/wrolpi/.pgpass ] || cat >/home/wrolpi/.pgpass <<'EOF'
|
|
127.0.0.1:5432:wrolpi:wrolpi:wrolpi
|
|
EOF
|
|
chmod 0600 /home/wrolpi/.pgpass
|
|
|
|
# Reset any inadvertent changes to the WROLPi repo. Restore ownership in case this repair script fails.
|
|
git config --global --add safe.directory /opt/wrolpi
|
|
git reset HEAD --hard
|
|
chown -R wrolpi:wrolpi /opt/wrolpi
|
|
|
|
# Scripts are run by systemd services (e.g. wrolpi-cert-renew.service) which fail with
|
|
# "Permission denied" if the executable bit was lost.
|
|
chmod +x /opt/wrolpi/*.sh /opt/wrolpi/scripts/*.sh
|
|
|
|
# Copy configs to system.
|
|
mkdir -p /etc/caddy
|
|
cp /opt/wrolpi/etc/raspberrypios/Caddyfile /etc/caddy/Caddyfile
|
|
mkdir -p /var/www
|
|
cp /opt/wrolpi/etc/raspberrypios/50x.html /var/www/50x.html
|
|
|
|
# Generate certificate for HTTPS.
|
|
/opt/wrolpi/scripts/generate_certificates.sh
|
|
|
|
# Install map fonts from blob (needed for MapLibre map labels).
|
|
MAP_FONTS_BLOB=/opt/wrolpi-blobs/map-fonts.tar.gz
|
|
if [ -f "${MAP_FONTS_BLOB}" ]; then
|
|
mkdir -p /opt/wrolpi/modules/map/static/fonts
|
|
tar -xzf "${MAP_FONTS_BLOB}" -C /opt/wrolpi/modules/map/static/
|
|
fi
|
|
|
|
# Start Caddy quickly so user can access Controller from React UI
|
|
systemctl start caddy || :
|
|
|
|
# WROLPi needs a few privileged commands.
|
|
cp /opt/wrolpi/etc/raspberrypios/90-wrolpi /etc/sudoers.d/90-wrolpi
|
|
chmod 0440 /etc/sudoers.d/90-wrolpi
|
|
# Verify this new file is valid.
|
|
visudo -c -f /etc/sudoers.d/90-wrolpi
|
|
|
|
# Update the Portable bootstrap on boxes installed from the Live USB; it is
|
|
# otherwise frozen at image-build time.
|
|
LIVE_INCLUDES=/opt/wrolpi/debian-live-config/config/includes.chroot
|
|
if [ -f /usr/local/sbin/wrolpi-bootstrap.sh ]; then
|
|
cp "${LIVE_INCLUDES}/usr/local/sbin/wrolpi-bootstrap.sh" /usr/local/sbin/wrolpi-bootstrap.sh
|
|
chmod 0755 /usr/local/sbin/wrolpi-bootstrap.sh
|
|
if [ -f /etc/systemd/system/wrolpi-bootstrap.service ]; then
|
|
cp "${LIVE_INCLUDES}/etc/systemd/system/wrolpi-bootstrap.service" /etc/systemd/system/wrolpi-bootstrap.service
|
|
fi
|
|
fi
|
|
|
|
# Install the systemd services
|
|
cp /opt/wrolpi/etc/raspberrypios/wrolpi*.service /etc/systemd/system/
|
|
cp /opt/wrolpi/etc/raspberrypios/wrolpi-*.timer /etc/systemd/system/
|
|
cp /opt/wrolpi/etc/raspberrypios/wrolpi.target /etc/systemd/system/
|
|
systemctl enable wrolpi-controller.service
|
|
systemctl enable wrolpi-api.service
|
|
systemctl enable wrolpi-app.service
|
|
systemctl enable wrolpi-kiwix.service
|
|
systemctl enable wrolpi-help.service
|
|
systemctl enable wrolpi-mounts.service
|
|
systemctl enable wrolpi-cert-renew.timer
|
|
|
|
# Repair Controller (offline-safe - no pip install)
|
|
repair_controller() {
|
|
echo "Checking WROLPi Controller..."
|
|
|
|
# Check venv exists
|
|
if [ ! -f /opt/wrolpi/controller/venv/bin/python ]; then
|
|
echo "ERROR: Controller venv missing!"
|
|
echo "Run install.sh or upgrade.sh (requires internet) to reinstall."
|
|
return 1
|
|
fi
|
|
|
|
# Check venv is functional
|
|
if ! /opt/wrolpi/controller/venv/bin/python --version > /dev/null 2>&1; then
|
|
echo "ERROR: Controller venv is corrupted!"
|
|
echo "Run install.sh or upgrade.sh (requires internet) to reinstall."
|
|
return 1
|
|
fi
|
|
|
|
# Check systemd service is installed
|
|
if [ ! -f /etc/systemd/system/wrolpi-controller.service ]; then
|
|
echo "Controller systemd service not installed, copying..."
|
|
cp /opt/wrolpi/etc/raspberrypios/wrolpi-controller.service /etc/systemd/system/
|
|
systemctl daemon-reload
|
|
fi
|
|
|
|
# Ensure service is enabled
|
|
if ! systemctl is-enabled wrolpi-controller > /dev/null 2>&1; then
|
|
echo "Enabling Controller service..."
|
|
systemctl enable wrolpi-controller
|
|
fi
|
|
|
|
echo "Controller repair completed"
|
|
}
|
|
|
|
# Run Controller repair if venv exists (skip on fresh install)
|
|
if [ -d /opt/wrolpi/controller/venv ]; then
|
|
repair_controller || echo "Controller repair failed, continuing..."
|
|
fi
|
|
|
|
# Start Controller so user can monitor repair in UI.
|
|
systemctl start wrolpi-controller
|
|
|
|
/usr/bin/systemctl daemon-reload
|
|
|
|
# WROLPi needs a few privileged commands.
|
|
cp /opt/wrolpi/etc/raspberrypios/90-wrolpi /etc/sudoers.d/90-wrolpi
|
|
chmod 0440 /etc/sudoers.d/90-wrolpi
|
|
# Verify this new file is valid.
|
|
visudo -c -f /etc/sudoers.d/90-wrolpi
|
|
|
|
# WROLPi uses SQLite inside the media directory; the API creates/migrates the database at
|
|
# startup. Never fatal: the media drive may not be mounted yet.
|
|
(cd /opt/wrolpi && /opt/wrolpi/venv/bin/python3 /opt/wrolpi/main.py db upgrade) || \
|
|
echo "WARNING: db upgrade failed (media directory unavailable?); the API will retry at startup."
|
|
|
|
# Create the media directory. This should be mounted by the maintainer.
|
|
[ -d /media/wrolpi ] || mkdir /media/wrolpi
|
|
# Create config directory if external drive is mounted, and is empty.
|
|
if grep -qs /media/wrolpi /proc/mounts && [ -z "$(ls -A /media/wrolpi)" ] && [ ! -d /media/wrolpi/config ]; then
|
|
mkdir /media/wrolpi/config
|
|
fi
|
|
|
|
# Build the frontend app.
|
|
(cd /opt/wrolpi/app && npm run build)
|
|
|
|
# Change owner of the media directory, ignore any errors because the drive may be fat/exfat/etc.
|
|
chown wrolpi:wrolpi /media/wrolpi 2>/dev/null || echo "Ignoring failure to change media directory permissions."
|
|
# Ensure the config directory is owned by wrolpi so the API can write config files.
|
|
[ -d /media/wrolpi/config ] && chown -R wrolpi:wrolpi /media/wrolpi/config 2>/dev/null || :
|
|
# On Portable (live-boot) systems the Postgres cluster lives under config/postgresql,
|
|
# bind-mounted over /var/lib/postgresql — so the config chown above just swept it. Restore
|
|
# ownership only when that bind is active; on installed systems Postgres lives on the root
|
|
# filesystem and any config/postgresql directory is inert (dead weight or a user's backup).
|
|
if mountpoint -q /var/lib/postgresql 2>/dev/null && id postgres >/dev/null 2>&1; then
|
|
chown -R postgres:postgres /var/lib/postgresql /etc/postgresql 2>/dev/null || :
|
|
fi
|
|
|
|
# Remove immutable flag from blob files before chown (may not exist or may not be set).
|
|
chattr -i /opt/wrolpi-blobs/* 2>/dev/null || :
|
|
|
|
chown -R wrolpi:wrolpi /home/wrolpi /opt/wrolpi*
|
|
|
|
# Protect blob files from accidental modification or deletion.
|
|
# These files are critical for initializing WROLPi.
|
|
if [ -d /opt/wrolpi-blobs ] && ls /opt/wrolpi-blobs/* >/dev/null 2>&1; then
|
|
chown -R root:root /opt/wrolpi-blobs
|
|
chmod 444 /opt/wrolpi-blobs/*
|
|
chattr +i /opt/wrolpi-blobs/*
|
|
fi
|
|
|
|
# Copy MOTD once the repair has been successful.
|
|
cp /opt/wrolpi/etc/raspberrypios/motd/30-wrolpi.motd /etc/update-motd.d/30-wrolpi
|
|
chmod +x /etc/update-motd.d/*
|
|
|
|
# Copy desktop shortcuts to existing users (new users get them from /etc/skel).
|
|
if [[ ${rpi} == true ]]; then
|
|
for user_home in /home/wrolpi /home/pi; do
|
|
if [ -d "${user_home}/Desktop" ]; then
|
|
cp /opt/wrolpi/etc/raspberrypios/*.desktop "${user_home}/Desktop/"
|
|
chown -R "$(basename ${user_home})":"$(basename ${user_home})" "${user_home}/Desktop/"
|
|
fi
|
|
done
|
|
# Update skeleton for future users.
|
|
mkdir -p /etc/skel/Desktop
|
|
cp /opt/wrolpi/etc/raspberrypios/*.desktop /etc/skel/Desktop/
|
|
fi
|
|
|
|
systemctl restart wrolpi-help
|
|
systemctl start wrolpi.target
|
|
|
|
set +x
|
|
|
|
echo "Repair has completed. You may run /opt/wrolpi/help.sh to check system status."
|