From 29d4bc2e7aecc08d04d577e60665891508b90396 Mon Sep 17 00:00:00 2001 From: catborise Date: Fri, 20 Nov 2026 17:57:29 +0300 Subject: [PATCH] feat: update.sh updates an installer or manual installation in one command Updating took four steps of commands copied from the README, with a diff to read for settings.py and the nginx config. update.sh does them: run from the repository (git show origin/master:update.sh | bash), it is always the newest one, also on an installation that has none yet. Nothing changes before its checks pass: root, an installation, with PostgreSQL --database-backed-up, and an older version's own settings.py moved into data/local_settings.py first (it stops and shows how that settings.py differs from its version's template). Then it stops the panel, backs it up into /root/webvirtcloud-backup-, pulls master, installs the requirements (LDAP's too when installed), migrates, collects the static files, gives the files to the panel's user, installs the units and starts the panel. It does not replace the nginx config, which holds the installation's own lines: it prints what the new version changes in it. The whole script is one function, so the pull cannot change what runs. dev/test-installer.sh IMAGE FROM installs FROM and updates it to HEAD; the Installer workflow runs it from v4.3.0 on Debian 13 and AlmaLinux 10. The README's update section is the command and what it does. --- .github/workflows/installer.yml | 24 ++++- README.md | 97 +++-------------- dev/test-installer.sh | 58 ++++++++-- update.sh | 182 ++++++++++++++++++++++++++++++++ 4 files changed, 272 insertions(+), 89 deletions(-) create mode 100755 update.sh diff --git a/.github/workflows/installer.yml b/.github/workflows/installer.yml index 627f9dce..a538ec52 100644 --- a/.github/workflows/installer.yml +++ b/.github/workflows/installer.yml @@ -1,6 +1,7 @@ name: Installer -# install.sh on each supported distribution (dev/test-installer.sh): on each +# install.sh on each supported distribution, and update.sh from the last +# release on two (dev/test-installer.sh): on each # push to master (the panel it installs changed), on a pull request that # changes the installer or what it installs, and weekly, as the # distributions' packages change under it @@ -12,6 +13,7 @@ on: - install.sh - webvirtcloud.sh - conf/** + - update.sh - dev/test-installer.sh - .github/workflows/installer.yml schedule: @@ -45,3 +47,23 @@ jobs: - name: Install and check the panel run: dev/test-installer.sh "${{ matrix.image }}" + + # update.sh from the last release, as an operator runs it + update: + name: ${{ matrix.image }} from v4.3.0 + runs-on: ubuntu-latest + timeout-minutes: 40 + strategy: + fail-fast: false + matrix: + image: + - debian:13 + - almalinux:10 + steps: + - name: Checkout Code + uses: actions/checkout@v4 + with: + fetch-depth: 0 # the release's commit + + - name: Install v4.3.0, update to this commit and check the panel + run: dev/test-installer.sh "${{ matrix.image }}" v4.3.0 diff --git a/README.md b/README.md index 825b82a8..d9ef59a3 100644 --- a/README.md +++ b/README.md @@ -654,7 +654,7 @@ The TLS client certificates (`/var/www/.pki`) go into the backup and back into t ### Moving from supervisor to systemd -Versions up to v3.0.0 ran the panel under supervisor. Once, before the update steps below, stop it there, keep its config and remove it: +Versions up to v3.0.0 ran the panel under supervisor. Once, before the update below, stop it there, keep its config and remove it: ```bash supervisorctl stop webvirtcloud novncd socketiod webvirt-worker @@ -663,88 +663,25 @@ mv /etc/supervisord.d/webvirtcloud.ini /root/webvirtcloud-supervisor.conf supervisorctl update ``` -Move the `environment=` values of that config into `/etc/default/webvirtcloud` (root, mode 600), one `NAME='value'` per line; leave out the worker's `WEBVIRTCLOUD_LOG_FILE`. Step 4 installs and starts the systemd units. Remove supervisor once nothing else uses it; a compute moves gstfsd as in [Adding a Compute Node](#adding-a-compute-node), step 3. +Move the `environment=` values of that config into `/etc/default/webvirtcloud` (root, mode 600), one `NAME='value'` per line; leave out the worker's `WEBVIRTCLOUD_LOG_FILE`. The update installs and starts the systemd units. Remove supervisor once nothing else uses it; a compute moves gstfsd as in [Adding a Compute Node](#adding-a-compute-node), step 3. ### Installer and manual installation -`git pull` never changes `data/` (the database, the secret key, [`data/local_settings.py`](#local-settings)) or `/etc/nginx/conf.d/webvirtcloud.conf`; step 4 brings the nginx config up to date. Run the steps from a root shell; they use `$b`, so in a new shell set it again to the backup directory. +From a root shell: -1. Stop the panel, back up and pull: +```bash +(set -o pipefail && + git -C /srv/webvirtcloud -c safe.directory=/srv/webvirtcloud fetch -q origin && + git -C /srv/webvirtcloud -c safe.directory=/srv/webvirtcloud show origin/master:update.sh | bash) +``` - ```bash - systemctl stop webvirt-cloud webvirt-novncd webvirt-socketiod webvirt-worker - # once: root may use the repo, which belongs to the service user - git config --global --add safe.directory /srv/webvirtcloud - b=/root/webvirtcloud-backup-$(date +%Y%m%d-%H%M%S) - cd /srv/webvirtcloud && - ! systemctl is-active --quiet webvirt-cloud webvirt-novncd webvirt-socketiod webvirt-worker && - mkdir -m 700 "$b" && - venv/bin/python3 -c "import sqlite3,sys; sqlite3.connect('db.sqlite3').backup(sqlite3.connect(sys.argv[1]))" "$b/db.sqlite3.part" && - mv "$b/db.sqlite3.part" "$b/db.sqlite3" && - cp -p /etc/nginx/conf.d/webvirtcloud.conf "$b"/ && - { [ ! -f data/local_settings.py ] || cp -p data/local_settings.py "$b"/; } && - { git ls-files --error-unmatch webvirtcloud/settings.py >/dev/null 2>&1 || [ ! -f webvirtcloud/settings.py ] || mv webvirtcloud/settings.py "$b"/; } && - { [ ! -f /etc/default/webvirtcloud ] || cp -p /etc/default/webvirtcloud "$b"/; } && - git rev-parse HEAD > "$b/commit" && - git ls-files -z --others --exclude-standard -- '*/migrations/*.py' | tar --null -T - --remove-files -cf "$b/local-migrations.tar" && - git checkout -- $(git ls-files -m -- static dev/scss install.sh webvirtcloud.sh) && - git checkout master && git pull - ``` - - Older installers collected static files into the tracked `static/` and deleted the tracked `install.sh` and `webvirtcloud.sh`; the second-to-last line puts those files back, so that `git pull` can update them. Older versions also had a `settings.py` of their own, not in the repository: step 1 moves it into the backup, as `git pull` now brings `settings.py`. - -2. Once, coming from a version with its own `settings.py` (step 1 moved it to `$b`): put your own values into [`data/local_settings.py`](#local-settings); the `diff` lists them: - - ```bash - diff "$b/settings.py" webvirtcloud/settings.py - ``` - - Typical ones are `TIME_ZONE`, `WS_*`, `ALLOWED_HOSTS`, `CSRF_TRUSTED_ORIGINS`, LDAP and, with PostgreSQL, your `DATABASES`: the default is sqlite, so without it `migrate` would make a new, empty sqlite database and the panel would start on that. Two need care: - - a literal `SECRET_KEY = "..."` (older installers wrote it there): put the value into `data/secret_key` instead (`mkdir -p data` first if there is no `data/`). With no key, `migrate` in step 3 stops with "The SECRET_KEY setting must not be empty" and every page fails; with another key, every user is signed out; - - `SHOW_PROFILE_EDIT_PASSWORD`: keep it for this one `migrate` if you have it (migration `accounts.0008` reads it), then delete it. - -3. Load `/etc/default/webvirtcloud` if you use it (its database is the one to migrate), install, migrate, and fix ownership and modes (`nginx:nginx` instead of `www-data:www-data` on RHEL / openSUSE). With LDAP, also run `venv/bin/pip3 install -U -r conf/requirements-ldap.txt` (headers: [LDAP](#ldap)): - - ```bash - { [ ! -f /etc/default/webvirtcloud ] || { set -a; . /etc/default/webvirtcloud; set +a; }; } && - venv/bin/pip3 install -U -r conf/requirements.txt && - { ! tar -tf "$b/local-migrations.tar" | grep -qx accounts/migrations/0006_alter_userattributes_id_alter_userinstance_id_and_more.py || - venv/bin/python3 manage.py migrate accounts 0005 --fake; } && - venv/bin/python3 manage.py migrate && - venv/bin/python3 manage.py collectstatic --noinput && - chown -R www-data:www-data /srv/webvirtcloud && - chmod -R go-rwx data db.sqlite3 - ``` - - Installers from before September 2025 generated migrations of their own; step 1 moved them into `local-migrations.tar`. One of them has the name of a tracked migration, which `migrate` would skip; the second line marks it unapplied so that the tracked one runs. - -4. Update the nginx config, then put back your own lines; the `diff` lists them: - - ```bash - cp conf/nginx/webvirtcloud.conf /etc/nginx/conf.d/webvirtcloud.conf - diff "$b/webvirtcloud.conf" /etc/nginx/conf.d/webvirtcloud.conf - ``` - - Put back your own lines (`server_name`, TLS, the noVNC port). Install the systemd units; your own service settings stay in `/etc/default/webvirtcloud` and in drop-ins. Debian / Ubuntu: - - ```bash - cp conf/systemd/webvirt-{cloud,novncd,socketiod,worker,backup}.service conf/systemd/webvirt-backup.timer /etc/systemd/system/ - ``` - - RHEL / openSUSE: - - ```bash - for u in cloud novncd socketiod worker backup; do sed 's/^\(User\|Group\)=.*/\1=nginx/' conf/systemd/webvirt-$u.service > /etc/systemd/system/webvirt-$u.service; done - cp conf/systemd/webvirt-backup.timer /etc/systemd/system/ - ``` - - Then reload nginx and start the panel: - - ```bash - nginx -t && systemctl reload nginx && systemctl daemon-reload && systemctl enable --now webvirt-cloud webvirt-novncd webvirt-socketiod webvirt-worker webvirt-backup.timer - ``` - -Older versions collected static files into `static/`; `git clean -n static/` lists the leftovers, which can be deleted. +- It runs the newest `update.sh` of the repository, also on an installation that has none yet. Another directory than `/srv/webvirtcloud`: set it in the command and `APP_PATH` for `bash`; the units get it. +- Nothing in the installation changes until its checks pass; files of the repository changed by hand stop it, listed. With PostgreSQL (the database Django is set up with), back that database up first and run it with `... | bash -s -- --database-backed-up`. +- An installation of an older version has its own `webvirtcloud/settings.py`: it stops and lists the lines that differ from that version's template: your own. Put your own values into [`data/local_settings.py`](#local-settings) and run it again. Typical ones are `TIME_ZONE`, `WS_*`, `ALLOWED_HOSTS`, `CSRF_TRUSTED_ORIGINS`, LDAP and, with PostgreSQL, your `DATABASES`: the default is sqlite, so without it `migrate` would make a new, empty sqlite database and the panel would start on that. A literal `SECRET_KEY = "..."` (older installers wrote it there) goes into `data/secret_key` instead (with no key every page fails, with another every user is signed out); keep `SHOW_PROFILE_EDIT_PASSWORD` for this one update if you have it (migration `accounts.0008` reads it), then delete it. +- It stops the panel and backs it up into `/root/webvirtcloud-backup-`: the database, the commit, `data/local_settings.py`, the nginx config, `/etc/default/webvirtcloud`, an older version's own `settings.py` and the migrations an old installer generated (`local-migrations.tar`). Then it pulls `master`, installs the requirements (the LDAP ones too if installed), migrates, collects the static files, gives the files to the panel's user (the owner of `/srv/webvirtcloud`), installs the systemd units and starts the panel. +- It does not replace `/etc/nginx/conf.d/webvirtcloud.conf`, which holds your `server_name`, TLS and noVNC port: when the new version changes that config, it prints the change to apply by hand. +- A failure stops it with the reason and the backup's directory. A failed backup starts the panel again; a later failure leaves it stopped until the cause is fixed and the command runs again, which goes on with that backup, or it is [rolled back](#restore-and-rollback). +- Older versions collected static files into `static/`; `git clean -n static/` lists the leftovers, which can be deleted. ### Restore and rollback @@ -798,7 +735,7 @@ Put back `/etc/default/webvirtcloud` if the backup has it (`cp -p "$b/webvirtclo nginx -t && systemctl reload nginx && systemctl start webvirt-cloud webvirt-novncd webvirt-socketiod webvirt-worker ``` -A version from before v3.0.0 has no worker: `systemctl disable --now webvirt-worker`. A rollback keeps the systemd units. A rollback leaves the clone on a detached HEAD at the restored commit; the update steps record that commit before they switch back to `master`. +A version from before v3.0.0 has no worker: `systemctl disable --now webvirt-worker`. A rollback keeps the systemd units. A rollback leaves the clone on a detached HEAD at the restored commit; `update.sh` records that commit before it switches back to `master`. ### Moving to a new machine @@ -849,7 +786,7 @@ What an update from an older version changes for operators and users. `manage.py - The admin's first-login password change survives losing `data/admin_password` (migration `accounts.0007`); run `migrate` as a user who can read that file. - A new VM and each clone get a random VNC password of their own (clones no longer share their source's). The console page sends it for a user with **Open a console without the VM's console password**, which a new user has unless unticked; others are asked for it. Existing VMs keep theirs. - An `admin` account that still has the password `admin` (the first user of versions up to v2.0.0) must change it at its next login, and its API tokens are refused until then (migration `accounts.0013`). -- `webvirtcloud/settings.py` is part of the code, and every update replaces it; an installation's own settings are in [`data/local_settings.py`](#local-settings). Step 2 of the update moves them there once from an older version's own `settings.py`; in Docker, remove a mounted `settings.py` from `docker-compose.override.yml`. +- `webvirtcloud/settings.py` is part of the code, and every update replaces it; an installation's own settings are in [`data/local_settings.py`](#local-settings). `update.sh` has them moved there once from an older version's own `settings.py`; in Docker, remove a mounted `settings.py` from `docker-compose.override.yml`. - The VNC console connects to `/novncd/` on the panel's own address through nginx, and the serial console to `/socket.io/`: they work on HTTPS and behind another proxy without settings. `WS_PUBLIC_PORT`, `WS_PUBLIC_HOST`, `WS_PUBLIC_PATH`, `WS_CERT` and `SOCKETIO_PUBLIC_HOST`/`_PORT`/`_PATH` are gone; remove them from `docker-compose.override.yml`, `/etc/default/webvirtcloud` or `data/local_settings.py`, and close port 6080 in the firewall. - Installer and manual installations run the panel as systemd units (`webvirt-cloud`, `webvirt-novncd`, `webvirt-socketiod`, `webvirt-worker`, `webvirt-backup.timer`) and computes run `webvirt-gstfsd`, instead of supervisor; see [Moving from supervisor to systemd](#moving-from-supervisor-to-systemd). Service settings go into `/etc/default/webvirtcloud`, service output into the journal (`journalctl -u webvirt-cloud`); the app log stays in `data/`. novncd and socketiod run sandboxed (read-only system directories, no `/home`). - gstfsd does only what the panel signs with the host's key: it does not start without `GSTFSD_KEY` in `/etc/default/webvirt-gstfsd`. On every compute, update gstfsd and its unit and make the key as in [Adding a Compute Node](#adding-a-compute-node), step 3 (a remote panel's `GSTFSD_BIND_HOST` and `GSTFSD_ALLOW_REMOTE` move into that file from `systemctl edit webvirt-gstfsd`), then enter the key on each compute's edit page. diff --git a/dev/test-installer.sh b/dev/test-installer.sh index dba9e13e..5941648c 100755 --- a/dev/test-installer.sh +++ b/dev/test-installer.sh @@ -2,13 +2,15 @@ # Installs the panel with install.sh in a systemd container of one distribution # and checks that it answers: its services run, it has a secret key, an admin # password and its local settings, and the login page loads. It installs the -# committed HEAD. +# committed HEAD; with FROM, it installs that version and updates it to HEAD +# with update.sh. # # Usage, from the repository root (needs docker): -# dev/test-installer.sh IMAGE e.g. debian:13, almalinux:10, opensuse/leap:15.6 +# dev/test-installer.sh IMAGE [FROM] e.g. debian:13, almalinux:10 v4.3.0 set -euo pipefail -image=${1:?usage: dev/test-installer.sh IMAGE} +image=${1:?usage: dev/test-installer.sh IMAGE [FROM]} +from=${2:-} case $image in ubuntu:* | debian:*) prepare="apt-get update && apt-get install -y systemd systemd-sysv" ;; almalinux:* | */openeuler:*) prepare="dnf -y install systemd findutils" ;; @@ -27,8 +29,16 @@ cleanup() { } trap cleanup EXIT -# the installer clones the repository: a clone of HEAD (root in the container reads it) +# the installer clones the repository: a clone of HEAD as master (root in the +# container reads it), and of FROM for an update git clone -q --no-hardlinks . "$repo/webvirtcloud" +git -C "$repo/webvirtcloud" checkout -q -B master +installed=$repo/webvirtcloud +if [ -n "$from" ]; then + git clone -q --no-hardlinks . "$repo/from" + git -C "$repo/from" checkout -q -B master "$from" + installed=$repo/from +fi docker build -q -t "$name" - > /dev/null < /dev/null + -v "$repo/webvirtcloud:/repo:ro" -v "$installed:/installed:ro" "$name" > /dev/null for _ in $(seq 60); do state=$(docker exec "$name" systemctl is-system-running 2>/dev/null || true) @@ -49,10 +59,42 @@ done docker exec "$name" bash -c ' # git comes with the installer; the mounted clone is not root'"'"'s printf "[safe]\n\tdirectory = *\n" >> /etc/gitconfig - mkdir -p /root/inst && cp /repo/install.sh /repo/webvirtcloud.sh /root/inst/ - cd /root/inst && printf "n\n\n" | APP_REPO_URL=/repo ./install.sh' || + mkdir -p /root/inst && cp /installed/install.sh /installed/webvirtcloud.sh /root/inst/ + cd /root/inst && printf "n\n\n" | APP_REPO_URL=/installed ./install.sh' || { docker exec "$name" tail -n 50 /var/log/webvirtcloud-install.log; exit 1; } +if [ -n "$from" ]; then + # the README's update command, from HEAD as the origin's master + docker exec "$name" bash -e -c ' + update() { + (set -o pipefail && + git -C /srv/webvirtcloud fetch -q origin && git -C /srv/webvirtcloud show origin/master:update.sh | bash) + } + git -C /srv/webvirtcloud remote set-url origin /repo + if [ -f /srv/webvirtcloud/webvirtcloud/settings.py ] && [ ! -f /srv/webvirtcloud/data/local_settings.py ]; then + # an older version'"'"'s own settings.py: it stops before changing anything ... + commit=$(git -C /srv/webvirtcloud rev-parse HEAD) + if update 2>/root/refused; then echo "update.sh did not refuse"; exit 1; fi + grep -q "has its own webvirtcloud/settings.py" /root/refused + systemctl is-active --quiet webvirt-cloud + [ "$(git -C /srv/webvirtcloud rev-parse HEAD)" = "$commit" ] + if ls -d /root/webvirtcloud-backup-* >/dev/null 2>&1; then echo "a refused update made a backup"; exit 1; fi + # ... until the operator moved its values into the local file + grep -E "^(TIME_ZONE|WS_PORT|ALLOWED_HOSTS|CSRF_TRUSTED_ORIGINS) = " /srv/webvirtcloud/webvirtcloud/settings.py \ + > /srv/webvirtcloud/data/local_settings.py + fi + # a failure after the backup (pip): run again, it goes on with that backup + old=$(git -C /srv/webvirtcloud rev-parse HEAD) + chmod -x /srv/webvirtcloud/venv/bin/pip3 + if update; then echo "update.sh did not stop at pip"; exit 1; fi + chmod +x /srv/webvirtcloud/venv/bin/pip3 + update + [ "$(ls -d /root/webvirtcloud-backup-* | wc -l)" = 1 ] + [ "$(cat /root/webvirtcloud-backup-*/commit)" = "$old" ] + [ "$(git -C /srv/webvirtcloud rev-parse HEAD)" = "$(git -C /repo rev-parse HEAD)" ] + update # once more, with nothing new: it runs again as safely' +fi + docker exec "$name" bash -e -c ' test -s /srv/webvirtcloud/data/secret_key || { echo "No secret key"; exit 1; } test -s /srv/webvirtcloud/data/admin_password || { echo "No admin password"; exit 1; } @@ -80,4 +122,4 @@ PY for unit in webvirt-cloud webvirt-novncd webvirt-socketiod webvirt-worker nginx; do systemctl is-active --quiet "$unit" || { systemctl status "$unit" --no-pager; exit 1; } done' -echo "$image: installed, the login page and its stylesheet load" +echo "$image: ${from:+$from updated to HEAD, }installed, the login page and its stylesheet load" diff --git a/update.sh b/update.sh new file mode 100755 index 00000000..7011654c --- /dev/null +++ b/update.sh @@ -0,0 +1,182 @@ +#!/bin/bash +# Updates an installer or manual installation of WebVirtCloud to the newest +# master, as root: +# +# (set -o pipefail && +# git -C /srv/webvirtcloud -c safe.directory=/srv/webvirtcloud fetch -q origin && +# git -C /srv/webvirtcloud -c safe.directory=/srv/webvirtcloud show origin/master:update.sh | bash) +# +# It runs the newest update.sh, also on an installation that has none yet. +# Nothing in the installation changes before its checks pass; then it stops the panel, backs it up +# into /root/webvirtcloud-backup-, pulls, installs, migrates and starts +# the panel again. With PostgreSQL, back that database up first and pass +# --database-backed-up (... | bash -s -- --database-backed-up). +# +# The whole script is in main(): bash reads all of it before it runs, so the +# pull cannot change what runs; nothing in it reads the script piped in. + +main() { + set -o pipefail + local app=${APP_PATH:-/srv/webvirtcloud} database_backed_up=false + [ "${1:-}" = --database-backed-up ] && database_backed_up=true + local units="webvirt-cloud webvirt-novncd webvirt-socketiod webvirt-worker" + local marker=$app/data/.update-backup # a stopped update's backup: a run again resumes with it + git() { command git -c safe.directory="$app" -C "$app" "$@"; } + fail() { echo "update.sh: $*" >&2; exit 1; } + + # checks: nothing in the installation changes before they pass + [ "$(id -u)" = 0 ] || fail "run it as root" + [ -x "$app/venv/bin/python3" ] && [ -d "$app/.git" ] || fail "no installation in $app (set APP_PATH)" + cd "$app" || fail "cannot enter $app" + # the services' user and group: the installed unit's, else the owner of the panel's directory + local user group + user=$(systemctl show -P User webvirt-cloud 2>/dev/null) + group=$(systemctl show -P Group webvirt-cloud 2>/dev/null) + [ -n "$user" ] || user=$(stat -c %U "$app") + [ -n "$group" ] || group=$user + [ "$user" != root ] || fail "$app belongs to root: chown it to the panel's user first" + # the services' environment: its database is the one to migrate + if [ -f /etc/default/webvirtcloud ]; then + set -a + # shellcheck source=/dev/null + . /etc/default/webvirtcloud + set +a + fi + git fetch -q origin || fail "git fetch failed" + local resume="" + [ ! -f "$marker" ] || resume=$(cat "$marker") + if [ -z "$resume" ]; then + # an older version's own settings.py, not in the repository: its values go + # into data/local_settings.py first, by hand (README, Local settings) + if [ -f webvirtcloud/settings.py ] && ! git ls-files --error-unmatch webvirtcloud/settings.py >/dev/null 2>&1 && + [ ! -f data/local_settings.py ]; then + echo "update.sh: this installation has its own webvirtcloud/settings.py. Put your own values" >&2 + echo "into $app/data/local_settings.py (README, Local settings), then run it again. Your" >&2 + echo "settings.py differs from its version's template in these lines (> yours):" >&2 + git show HEAD:webvirtcloud/settings.py.template | diff - webvirtcloud/settings.py >&2 + exit 1 + fi + # tracked files changed by hand would stop the pull with the panel down; the ones + # older versions changed themselves are put back below + local edited + edited=$(git status --porcelain --untracked-files=no | cut -c4- | + grep -vE '^(static/|dev/scss/|install\.sh$|webvirtcloud\.sh$)') + [ -z "$edited" ] || fail "these files of the repository were changed here; keep your changes elsewhere and undo them: $edited" + fi + # the database the panel uses, as its settings give it (the environment, settings.py and + # local settings): the one backed up and migrated; a run that goes on reads it from the backup + local loaded database + if [ -n "$resume" ]; then + database=$(cat "$resume/database") || fail "$resume/database is missing" + else + loaded=$(DJANGO_SETTINGS_MODULE=webvirtcloud.settings venv/bin/python3 -c 'from django.conf import settings +d = settings.DATABASES["default"]; print("DATABASE", d["ENGINE"].rsplit(".", 1)[-1], d["NAME"])' 2>&1) + database=$(sed -n 's/^DATABASE //p' <<< "$loaded") + [ -n "$database" ] || fail "the panel's settings do not load: $loaded" + fi + if [ "${database%% *}" != sqlite3 ] && ! $database_backed_up; then + fail "the panel uses ${database%% *}: back that database up, then run it again with --database-backed-up" + fi + # root runs git in the repository, which belongs to the panel's user (the rollback steps too) + command git config --global --get-all safe.directory 2>/dev/null | grep -qxF "$app" || + command git config --global --add safe.directory "$app" + + local b old + if [ -n "$resume" ]; then + b=$resume + echo "* Going on with the update that stopped; its backup is $b" + else + b=/root/webvirtcloud-backup-$(date +%Y%m%d-%H%M%S) + mkdir -m 700 "$b" && git rev-parse HEAD > "$b/commit" && echo "$database" > "$b/database" && echo "$b" > "$marker" || + fail "cannot make the backup directory $b" + fi + old=$(cat "$b/commit") + echo "* Stopping the panel" + # shellcheck disable=SC2086 + systemctl stop $units + # shellcheck disable=SC2086 + ! systemctl is-active --quiet $units || fail "the panel did not stop" + + if [ -z "$resume" ]; then + echo "* Backing up into $b" + if ! ( + set -e + if [ "${database%% *}" = sqlite3 ]; then + venv/bin/python3 -c "import sqlite3,sys; sqlite3.connect(sys.argv[1]).backup(sqlite3.connect(sys.argv[2]))" \ + "${database#* }" "$b/db.sqlite3.part" + mv "$b/db.sqlite3.part" "$b/db.sqlite3" + fi + for file in data/local_settings.py /etc/nginx/conf.d/webvirtcloud.conf /etc/default/webvirtcloud; do + [ ! -f "$file" ] || cp -p "$file" "$b"/ + done + ); then + rm -f "$marker" + # shellcheck disable=SC2086 + systemctl start $units + fail "the backup into $b failed; nothing was changed and the panel was started again" + fi + # what older versions left in the checkout: their own settings.py (git pull now + # brings settings.py), migrations an old installer generated (archived whole, + # then removed), static files they collected into the tracked static/, and + # install.sh and webvirtcloud.sh they deleted + if [ -f webvirtcloud/settings.py ] && ! git ls-files --error-unmatch webvirtcloud/settings.py >/dev/null 2>&1; then + mv webvirtcloud/settings.py "$b"/ || fail "cannot move the old settings.py; the panel is stopped, the backup is $b" + fi + git ls-files -z --others --exclude-standard -- '*/migrations/*.py' > "$b/local-migrations.list" && + tar --null -T "$b/local-migrations.list" -cf "$b/local-migrations.tar" && + xargs -0 -r rm -f < "$b/local-migrations.list" && rm "$b/local-migrations.list" || + fail "cannot keep the generated migrations; the panel is stopped, the backup is $b" + local changed + changed=$(git ls-files -m -- static dev/scss install.sh webvirtcloud.sh) + # shellcheck disable=SC2086 + [ -z "$changed" ] || git checkout -- $changed || + fail "cannot restore the tracked files older versions changed; the panel is stopped, the backup is $b" + fi + + echo "* Pulling" + git checkout -q master && git pull -q --ff-only || fail "git pull failed; the panel is stopped, the backup is $b" + + echo "* Installing, migrating" + venv/bin/pip3 install -q -U -r conf/requirements.txt || fail "pip failed; the panel is stopped, the backup is $b" + if venv/bin/pip3 show -q django-auth-ldap >/dev/null 2>&1; then + venv/bin/pip3 install -q -U -r conf/requirements-ldap.txt || fail "pip (LDAP) failed; the panel is stopped, the backup is $b" + fi + # an old installer's migration has the name of a tracked one, which migrate would skip + if tar -tf "$b/local-migrations.tar" | grep -qx accounts/migrations/0006_alter_userattributes_id_alter_userinstance_id_and_more.py; then + venv/bin/python3 manage.py migrate accounts 0005 --fake || fail "migrate failed; the panel is stopped, the backup is $b" + fi + venv/bin/python3 manage.py migrate || fail "migrate failed; the panel is stopped, the backup is $b" + venv/bin/python3 manage.py collectstatic --noinput -v 0 || fail "collectstatic failed; the panel is stopped, the backup is $b" + chown -R "$user:$group" "$app" && chmod -R go-rwx data && { [ ! -f db.sqlite3 ] || chmod 600 db.sqlite3; } || + fail "cannot set the files' owner; the panel is stopped, the backup is $b" + + echo "* Starting the panel" + local unit + for unit in $units webvirt-backup; do + sed -e "s/^User=.*/User=$user/" -e "s/^Group=.*/Group=$group/" -e "s#/srv/webvirtcloud#$app#g" \ + "conf/systemd/$unit.service" > "/etc/systemd/system/.$unit.service" && + mv "/etc/systemd/system/.$unit.service" "/etc/systemd/system/$unit.service" || + fail "cannot install $unit.service; the panel is stopped, the backup is $b" + done + cp conf/systemd/webvirt-backup.timer /etc/systemd/system/ || fail "cannot install webvirt-backup.timer" + # shellcheck disable=SC2086 + systemctl daemon-reload && systemctl enable --now -q $units webvirt-backup.timer || + fail "the panel did not start: journalctl -u webvirt-cloud; the backup is $b" + rm -f "$marker" + nginx -t -q && systemctl reload nginx || fail "nginx refused its config; the panel runs, nginx was not reloaded" + + # the nginx config holds this installation's own lines (server_name, TLS): it is + # not replaced; what the new version changes in it is shown to be applied by hand + git diff --quiet "$old" HEAD -- conf/nginx/webvirtcloud.conf + case $? in + 0) ;; + 1) echo "* The nginx config of this version changed; apply this to /etc/nginx/conf.d/webvirtcloud.conf," + echo " then: nginx -t && systemctl reload nginx" + git diff "$old" HEAD -- conf/nginx/webvirtcloud.conf ;; + *) echo "* Compare /etc/nginx/conf.d/webvirtcloud.conf with $app/conf/nginx/webvirtcloud.conf by hand." ;; + esac + echo "* Updated from ${old:0:8} to $(git rev-parse --short=8 HEAD); the backup is $b" +} + +main "$@" < /dev/null +exit