diff --git a/scripts/deploy.sh b/scripts/deploy.sh index abdd658..498cc02 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -9,6 +9,13 @@ # shellcheck source=/dev/null [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" +if [ -f "$REPO_ROOT/server/.env" ]; then + set -a + # shellcheck disable=SC1091 + . "$REPO_ROOT/server/.env" + set +a +fi + echo "==> Updating source..." git pull @@ -27,16 +34,32 @@ cd "$REPO_ROOT/server" uv pip install -e ".[dev]" +if [ "${GNEXUS_BOOK_STORAGE_BACKEND:-file}" = "db" ]; then + if [ -z "${GNEXUS_BOOK_DATABASE_URL:-}" ]; then + echo "GNEXUS_BOOK_DATABASE_URL is required for the db backend" >&2 + exit 2 + fi + echo "==> Applying database migrations..." + GNEXUS_BOOK_DATABASE_URL="$GNEXUS_BOOK_DATABASE_URL" .venv/bin/alembic upgrade head +fi + echo "==> Syncing dist to /var/www..." sudo cp -r "$REPO_ROOT/ui/dist/"* /var/www/gnexus-book/ sudo chown -R www-data:www-data /var/www/gnexus-book -echo "==> Restarting services..." +echo "==> Refreshing systemd units..." +cp "$REPO_ROOT"/systemd/*.service "$HOME/.config/systemd/user/" 2>/dev/null || true +cp "$REPO_ROOT"/systemd/*.timer "$HOME/.config/systemd/user/" 2>/dev/null || true systemctl --user daemon-reload + +echo "==> Restarting services..." systemctl --user restart gnexus-book-backend.service +systemctl --user restart gnexus-book-mcp.service +systemctl --user restart gnexus-book-frontend.service || true echo "==> Reloading nginx..." sudo nginx -t && sudo systemctl reload nginx echo "==> Deploy complete." systemctl --user status gnexus-book-backend.service --no-pager +systemctl --user status gnexus-book-mcp.service --no-pager diff --git a/scripts/install.sh b/scripts/install.sh index 409ca39..8292e26 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -42,18 +42,28 @@ echo "==> Installing systemd user units..." mkdir -p "$HOME/.config/systemd/user" -cp "$REPO_ROOT/systemd/gnexus-book-backend.service" "$HOME/.config/systemd/user/" -cp "$REPO_ROOT/systemd/gnexus-book-frontend.service" "$HOME/.config/systemd/user/" +cp "$REPO_ROOT"/systemd/*.service "$HOME/.config/systemd/user/" +cp "$REPO_ROOT"/systemd/*.timer "$HOME/.config/systemd/user/" + +if [ ! -f "$REPO_ROOT/server/.env" ]; then + echo "WARNING: server/.env is missing; backend, MCP and backups may fail until it is configured." >&2 +fi echo "==> Enabling lingering for boot autostart..." loginctl enable-linger "$(whoami)" systemctl --user daemon-reload -systemctl --user enable gnexus-book-backend.service gnexus-book-frontend.service +systemctl --user enable gnexus-book-backend.service gnexus-book-frontend.service \ + gnexus-book-mcp.service gnexus-book-backup.timer systemctl --user start gnexus-book-backend.service sleep 3 -systemctl --user start gnexus-book-frontend.service +systemctl --user start gnexus-book-frontend.service gnexus-book-mcp.service gnexus-book-backup.timer + +echo "==> Restore-drill timer is installed but not enabled; enable it after a manual drill:" +echo " systemctl --user enable --now gnexus-book-restore-drill.timer" echo "==> Done. Services:" systemctl --user status gnexus-book-backend.service --no-pager systemctl --user status gnexus-book-frontend.service --no-pager +systemctl --user status gnexus-book-mcp.service --no-pager +systemctl --user status gnexus-book-backup.timer --no-pager diff --git a/server/.env.example b/server/.env.example index 9bb1162..4e08fe2 100644 --- a/server/.env.example +++ b/server/.env.example @@ -23,6 +23,14 @@ # GNEXUS_BOOK_INTERNAL_ALLOWLIST=127.0.0.1,::1 # GNEXUS_BOOK_INTERNAL_RATE_LIMIT=60 +# Operations: off-host backups, health probes and Synapse alerts. +# GNEXUS_BOOK_BACKUP_DIR=/mnt/backups/gnexus-book/db +# GNEXUS_BOOK_BACKUP_KEEP=14 +# GNEXUS_BOOK_HEALTH_URL=http://127.0.0.1:8000/health +# SYNAPSE_URL=https://synapse.gnexus.space +# SYNAPSE_API_KEY= +# SYNAPSE_DEFAULT_SOURCE=gnexus-book + # gnexus-auth SSO. Leave the client id empty to run with authentication disabled. # GNEXUS_BOOK_AUTH_CLIENT_ID= # GNEXUS_BOOK_AUTH_CLIENT_SECRET= diff --git a/systemd/gnexus-book-backup.service b/systemd/gnexus-book-backup.service new file mode 100644 index 0000000..235b584 --- /dev/null +++ b/systemd/gnexus-book-backup.service @@ -0,0 +1,10 @@ +[Unit] +Description=Gnexus Book PostgreSQL backup +After=network-online.target + +[Service] +Type=oneshot +WorkingDirectory=/home/ubuntu/gnexus-book +EnvironmentFile=-/home/ubuntu/gnexus-book/server/.env +ExecStart=/home/ubuntu/gnexus-book/scripts/backup-db.sh +Nice=10 diff --git a/systemd/gnexus-book-backup.timer b/systemd/gnexus-book-backup.timer new file mode 100644 index 0000000..08b3cc5 --- /dev/null +++ b/systemd/gnexus-book-backup.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Daily Gnexus Book PostgreSQL backup + +[Timer] +OnCalendar=daily +Persistent=true +RandomizedDelaySec=15m + +[Install] +WantedBy=timers.target diff --git a/systemd/gnexus-book-restore-drill.service b/systemd/gnexus-book-restore-drill.service new file mode 100644 index 0000000..e100a8b --- /dev/null +++ b/systemd/gnexus-book-restore-drill.service @@ -0,0 +1,10 @@ +[Unit] +Description=Gnexus Book restore drill +After=network-online.target + +[Service] +Type=oneshot +WorkingDirectory=/home/ubuntu/gnexus-book +EnvironmentFile=-/home/ubuntu/gnexus-book/server/.env +ExecStart=/home/ubuntu/gnexus-book/scripts/restore-drill.sh +Nice=10 diff --git a/systemd/gnexus-book-restore-drill.timer b/systemd/gnexus-book-restore-drill.timer new file mode 100644 index 0000000..8213c4a --- /dev/null +++ b/systemd/gnexus-book-restore-drill.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Monthly Gnexus Book restore drill + +[Timer] +OnCalendar=monthly +Persistent=true +RandomizedDelaySec=1h + +[Install] +WantedBy=timers.target