diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index baaf0b8a..977e6e05 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -183,7 +183,7 @@ jobs: sleep 5 done docker inspect -f '{{json .State.Health}}' $c || true - docker exec $c sv status /etc/service/nginx /etc/service/webvirtcloud /etc/service/novnc || true + docker exec $c supervisorctl status || true docker logs $c || true exit 1 done diff --git a/Dockerfile b/Dockerfile index d8698970..08d89026 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,39 @@ -FROM phusion/baseimage:resolute-1.0.23 AS build +# The panel in one container: supervisord runs nginx, gunicorn, the job +# worker, the two console daemons and cron (conf/docker). +FROM ubuntu:26.04 AS base -# Compilers and headers for libvirt-python and python-ldap; they stay in this stage. -# hadolint ignore=DL3008 -RUN apt-get update -qqy \ - && DEBIAN_FRONTEND=noninteractive apt-get -qyy install \ - --no-install-recommends \ - python3-venv \ - python3-dev \ - libvirt-dev \ - zlib1g-dev \ - pkg-config \ - gcc \ - libldap2-dev \ - libssl-dev \ - libsasl2-dev \ - && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +RUN apt-get update -qq \ + && DEBIAN_FRONTEND=noninteractive apt-get install -qqy --no-install-recommends \ + ca-certificates \ + cron \ + libldap2 \ + libsasl2-modules \ + libvirt0 \ + nginx \ + openssh-client \ + python3 \ + supervisor \ + tzdata \ + && rm -rf /var/lib/apt/lists/* WORKDIR /srv/webvirtcloud + +# Compilers and headers for libvirt-python and python-ldap stay in this stage. +FROM base AS build + +RUN apt-get update -qq \ + && DEBIAN_FRONTEND=noninteractive apt-get install -qqy --no-install-recommends \ + gcc \ + libldap2-dev \ + libsasl2-dev \ + libssl-dev \ + libvirt-dev \ + pkg-config \ + python3-dev \ + python3-venv \ + zlib1g-dev \ + && rm -rf /var/lib/apt/lists/* + COPY conf/requirements.txt conf/requirements-ldap.txt conf/ # pip's cache stays on the build host for the next build; pip itself is not # shipped: a dependency change is a new image @@ -25,79 +42,30 @@ RUN --mount=type=cache,target=/root/.cache/pip \ && venv/bin/pip install -r conf/requirements.txt -r conf/requirements-ldap.txt \ && venv/bin/pip uninstall -y pip -FROM phusion/baseimage:resolute-1.0.23 +FROM base -EXPOSE 80 - -# Use baseimage-docker's init system. my_init force-stops services after -# KILL_PROCESS_TIMEOUT seconds (default 30); the worker drains jobs for 60 s. -ENV KILL_PROCESS_TIMEOUT=80 -CMD ["/sbin/my_init"] - -RUN echo 'APT::Get::Clean=always;' >> /etc/apt/apt.conf.d/99AutomaticClean - -# Shared libraries of the compiled wheels, and nginx; no compilers or headers. -# hadolint ignore=DL3008 -RUN apt-get update -qqy \ - && DEBIAN_FRONTEND=noninteractive apt-get -qyy install \ - --no-install-recommends \ - python3 \ - nginx \ - libvirt0 \ - libsasl2-modules \ - && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* - -WORKDIR /srv/webvirtcloud COPY --from=build /srv/webvirtcloud/venv venv -COPY . /srv/webvirtcloud +COPY . . # The code stays root's: the panel writes only to its data and backup # directories (made its own at start). A settings file and a key exist only # for collectstatic. -RUN cp webvirtcloud/settings.py.template webvirtcloud/settings.py && \ - SECRET_KEY="build-dummy-key-only-for-collectstatic" WEBVIRTCLOUD_LOG_FILE=/tmp/build.log \ - venv/bin/python3 manage.py collectstatic --noinput && \ - rm -f webvirtcloud/settings.py /tmp/build.log - -# Setup Nginx -RUN printf "\n%s" "daemon off;" >> /etc/nginx/nginx.conf && \ - rm -f /etc/nginx/sites-enabled/default && \ - chown -R www-data:www-data /var/lib/nginx +RUN cp webvirtcloud/settings.py.template webvirtcloud/settings.py \ + && SECRET_KEY=build-only WEBVIRTCLOUD_LOG_FILE=/tmp/build.log venv/bin/python3 manage.py collectstatic --noinput \ + && rm webvirtcloud/settings.py /tmp/build.log +COPY conf/docker/nginx.conf /etc/nginx/nginx.conf COPY conf/nginx/webvirtcloud.conf /etc/nginx/conf.d/ +COPY conf/docker/supervisord.conf /etc/supervisor/supervisord.conf +COPY --chmod=644 conf/docker/backup.cron /etc/cron.d/webvirtcloud-backup +COPY --chmod=755 conf/docker/start conf/docker/healthcheck /usr/local/bin/ -# Register startup init script and services to runit -RUN mkdir -p /etc/my_init.d \ - /etc/service/nginx \ - /etc/service/nginx-log-forwarder \ - /etc/service/webvirtcloud \ - /etc/service/webvirtcloud-worker \ - /etc/service/novnc \ - /etc/service/socketiod -COPY conf/runit/10_webvirtcloud_init.sh /etc/my_init.d/10_webvirtcloud_init.sh -COPY conf/runit/nginx /etc/service/nginx/run -COPY conf/runit/nginx-log-forwarder /etc/service/nginx-log-forwarder/run -COPY conf/runit/novncd.sh /etc/service/novnc/run -COPY conf/runit/socketiod.sh /etc/service/socketiod/run -COPY conf/runit/webvirtcloud.sh /etc/service/webvirtcloud/run -COPY conf/runit/webvirtcloud-worker.sh /etc/service/webvirtcloud-worker/run -COPY --chmod=644 conf/runit/backup.cron /etc/cron.d/webvirtcloud-backup -COPY --chmod=755 conf/runit/healthcheck /usr/local/bin/webvirtcloud-healthcheck -RUN chmod +x /etc/my_init.d/10_webvirtcloud_init.sh \ - /etc/service/nginx/run \ - /etc/service/nginx-log-forwarder/run \ - /etc/service/novnc/run \ - /etc/service/socketiod/run \ - /etc/service/webvirtcloud/run \ - /etc/service/webvirtcloud-worker/run - -# The panel's page and every service (conf/runit/healthcheck). X-Forwarded-Proto: -# https keeps the probe from being redirected when the HTTPS profile is on -# (nginx passes the header on; see conf/nginx). -HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ - CMD /usr/local/bin/webvirtcloud-healthcheck - -# Declare mountable data directory for persistent SQLite, SSH keys and TLS certificates, and their backups +EXPOSE 80 +# the database, secret key and logs; SSH keys and TLS certificates for the +# computes; the daily backups VOLUME ["/srv/webvirtcloud/data", "/var/www/.ssh", "/var/www/.pki", "/srv/webvirtcloud/backups"] -WORKDIR /srv/webvirtcloud +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 CMD ["healthcheck"] + +ENTRYPOINT ["start"] +CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"] diff --git a/README.md b/README.md index 85eb731a..253a3314 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ cd /srv/webvirtcloud # 3. Settings, allowed hosts and secret key cp webvirtcloud/settings.py.template webvirtcloud/settings.py sed -i 's|^ALLOWED_HOSTS = .*|ALLOWED_HOSTS = ["wvc.example.com", "192.0.2.10", "localhost", "127.0.0.1", "[::1]"]|' webvirtcloud/settings.py -mkdir -p data && python3 conf/runit/secret_generator.py > data/secret_key && chmod 600 data/secret_key +mkdir -p data && python3 conf/secret_generator.py > data/secret_key && chmod 600 data/secret_key # 4. Virtualenv and dependencies python3 -m venv --system-site-packages venv @@ -155,7 +155,7 @@ cd /srv/webvirtcloud # 3. Settings, allowed hosts and secret key cp webvirtcloud/settings.py.template webvirtcloud/settings.py sed -i 's|^ALLOWED_HOSTS = .*|ALLOWED_HOSTS = ["wvc.example.com", "192.0.2.10", "localhost", "127.0.0.1", "[::1]"]|' webvirtcloud/settings.py -mkdir -p data && python3 conf/runit/secret_generator.py > data/secret_key && chmod 600 data/secret_key +mkdir -p data && python3 conf/secret_generator.py > data/secret_key && chmod 600 data/secret_key # 4. Virtualenv and dependencies python3 -m venv --system-site-packages venv @@ -200,7 +200,7 @@ cd /srv/webvirtcloud # 3. Settings, allowed hosts and secret key cp webvirtcloud/settings.py.template webvirtcloud/settings.py sed -i 's|^ALLOWED_HOSTS = .*|ALLOWED_HOSTS = ["wvc.example.com", "192.0.2.10", "localhost", "127.0.0.1", "[::1]"]|' webvirtcloud/settings.py -mkdir -p data && python3.11 conf/runit/secret_generator.py > data/secret_key && chmod 600 data/secret_key +mkdir -p data && python3.11 conf/secret_generator.py > data/secret_key && chmod 600 data/secret_key # 4. Virtualenv and dependencies python3.11 -m venv --system-site-packages venv @@ -234,7 +234,7 @@ sudo cat /srv/webvirtcloud/data/admin_password # installer or manual docker compose exec webvirtcloud cat data/admin_password # Docker ``` -Sign in at `http://`; you must set a new password at the first login, and the file is then deleted. To choose the name or password yourself, set `ADMIN_USERNAME` and/or `ADMIN_PASSWORD` in the environment before that first `migrate`; a password you set is not written to the file and need not be changed. A lost password is reset with `python3 manage.py changepassword admin` (Docker: `docker compose exec webvirtcloud /sbin/setuser www-data venv/bin/python3 manage.py changepassword admin`). +Sign in at `http://`; you must set a new password at the first login, and the file is then deleted. To choose the name or password yourself, set `ADMIN_USERNAME` and/or `ADMIN_PASSWORD` in the environment before that first `migrate`; a password you set is not written to the file and need not be changed. A lost password is reset with `python3 manage.py changepassword admin` (Docker: `docker compose exec -u www-data webvirtcloud venv/bin/python3 manage.py changepassword admin`). ## Adding a Compute Node @@ -503,7 +503,7 @@ The database holds compute passwords and keys: the directories are readable only Take one now: ```bash -docker compose exec webvirtcloud /sbin/setuser www-data venv/bin/python3 manage.py backup # Docker +docker compose exec -u www-data webvirtcloud venv/bin/python3 manage.py backup # Docker systemctl start webvirt-backup && journalctl -u webvirt-backup -n 3 # otherwise ``` @@ -879,7 +879,7 @@ Run every step as your normal user, not with `sudo`: if `manage.py` ever runs as ```bash cp webvirtcloud/settings.py.template webvirtcloud/settings.py - mkdir -p data && python conf/runit/secret_generator.py > data/secret_key && chmod 600 data/secret_key + mkdir -p data && python conf/secret_generator.py > data/secret_key && chmod 600 data/secret_key ``` 4. Migrate and run the dev server (`settings-dev` enables `DEBUG` and the Django Debug Toolbar); the admin password is in `data/admin_password`, or set `ADMIN_PASSWORD` beforehand: @@ -948,7 +948,7 @@ python manage.py test HOME=$PWD/.vagrant/tls TEST_LIBVIRT_TYPE=3 TEST_LIBVIRT_SSH_HOST= python manage.py test # compute1 again, over TLS # a socket compute is the panel's own libvirt: the committed HEAD runs inside compute1 git archive HEAD | vagrant ssh compute1 -c "sudo sh -c 'rm -rf /root/wvc && mkdir /root/wvc && tar -x -C /root/wvc'" -vagrant ssh compute1 -c "sudo -i sh -ec 'dnf -y install python3-devel openldap-devel cyrus-sasl-devel gcc; cd /root/wvc; python3 -m venv --system-site-packages venv; venv/bin/pip install -r dev/requirements.txt; cp webvirtcloud/settings.py.template webvirtcloud/settings.py; mkdir data; venv/bin/python conf/runit/secret_generator.py > data/secret_key; TEST_LIBVIRT_HOST=localhost TEST_LIBVIRT_TYPE=4 TEST_LIBVIRT_LOGIN=wvc TEST_LIBVIRT_PASSWORD=wvc-test TEST_LIBVIRT_LVM_DEVICE=/dev/vdb TEST_LIBVIRT_ISCSI_TARGET=iqn.2026-10.test.wvc:lun TEST_LIBVIRT_NFS_EXPORT=/srv/wvc-nfs venv/bin/python manage.py test'" +vagrant ssh compute1 -c "sudo -i sh -ec 'dnf -y install python3-devel openldap-devel cyrus-sasl-devel gcc; cd /root/wvc; python3 -m venv --system-site-packages venv; venv/bin/pip install -r dev/requirements.txt; cp webvirtcloud/settings.py.template webvirtcloud/settings.py; mkdir data; venv/bin/python conf/secret_generator.py > data/secret_key; TEST_LIBVIRT_HOST=localhost TEST_LIBVIRT_TYPE=4 TEST_LIBVIRT_LOGIN=wvc TEST_LIBVIRT_PASSWORD=wvc-test TEST_LIBVIRT_LVM_DEVICE=/dev/vdb TEST_LIBVIRT_ISCSI_TARGET=iqn.2026-10.test.wvc:lun TEST_LIBVIRT_NFS_EXPORT=/srv/wvc-nfs venv/bin/python manage.py test'" vagrant destroy -f ``` diff --git a/conf/docker/backup.cron b/conf/docker/backup.cron new file mode 100644 index 00000000..9b907042 --- /dev/null +++ b/conf/docker/backup.cron @@ -0,0 +1,2 @@ +# The daily backup into /srv/webvirtcloud/backups (the webvirtcloud-backups volume); its output goes to docker logs +15 3 * * * root (. /run/webvirtcloud.env && cd /srv/webvirtcloud && runuser -u www-data -- venv/bin/python3 manage.py backup) > /proc/1/fd/1 2>&1 diff --git a/conf/docker/healthcheck b/conf/docker/healthcheck new file mode 100644 index 00000000..b0f5df4b --- /dev/null +++ b/conf/docker/healthcheck @@ -0,0 +1,10 @@ +#!/bin/sh +# Docker's health check: every process has run for 5 s at least (one that +# keeps failing never has) and the panel answers through nginx. +# X-Forwarded-Proto: https keeps the HTTPS profile from redirecting it. +# "webvirtcloud:worker RUNNING pid 27, uptime 0:00:10" (or "1 day, 0:00:10") +supervisorctl status | awk ' + $2 != "RUNNING" { print; bad = 1; next } + $0 !~ / day/ { split($NF, t, ":"); if (t[1] * 3600 + t[2] * 60 + t[3] < 5) { print; bad = 1 } } + END { exit bad }' || exit 1 +exec /srv/webvirtcloud/venv/bin/python3 -c "import urllib.request as u; u.urlopen(u.Request('http://127.0.0.1/accounts/login/', headers={'X-Forwarded-Proto': 'https'}), timeout=5)" diff --git a/conf/docker/nginx.conf b/conf/docker/nginx.conf new file mode 100644 index 00000000..1a2c7671 --- /dev/null +++ b/conf/docker/nginx.conf @@ -0,0 +1,18 @@ +# nginx of the container: the panel's site (conf/nginx/webvirtcloud.conf), +# errors to docker logs; gunicorn logs the requests. +user www-data; +worker_processes 1; +pid /run/nginx.pid; +error_log /dev/stderr warn; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + sendfile on; + access_log off; + include /etc/nginx/conf.d/*.conf; +} diff --git a/conf/docker/start b/conf/docker/start new file mode 100644 index 00000000..e68ebab5 --- /dev/null +++ b/conf/docker/start @@ -0,0 +1,26 @@ +#!/bin/sh +# The container's start: settings, secret key and database in place, the +# data the panel's own, migrations run; then the command (supervisord). +set -eu +cd /srv/webvirtcloud + +[ -f webvirtcloud/settings.py ] || cp webvirtcloud/settings.py.template webvirtcloud/settings.py +if [ -z "${SECRET_KEY:-}" ] && [ ! -s data/secret_key ]; then + (umask 077 && venv/bin/python3 -c 'import secrets; print(secrets.token_urlsafe(50))' > data/secret_key) +fi +# the database lives in the data volume +touch data/db.sqlite3 +ln -sfn /srv/webvirtcloud/data/db.sqlite3 db.sqlite3 + +# The database holds compute passwords and password hashes: the data, keys, +# settings and backups are readable by the panel's user only. +chown -R www-data:www-data data /var/www/.ssh /var/www/.pki +chown www-data:www-data backups webvirtcloud/settings.py +chmod 700 data backups /var/www/.ssh /var/www/.pki +chmod 600 data/db.sqlite3 webvirtcloud/settings.py + +# cron starts its jobs with an empty environment: the backup reads this one +(umask 077 && export -p > /run/webvirtcloud.env) + +runuser -u www-data -- venv/bin/python3 manage.py migrate --noinput +exec "$@" diff --git a/conf/docker/supervisord.conf b/conf/docker/supervisord.conf new file mode 100644 index 00000000..eb23090d --- /dev/null +++ b/conf/docker/supervisord.conf @@ -0,0 +1,92 @@ +; The container's processes; each one's output goes to docker logs. +[supervisord] +nodaemon=true +user=root +logfile=/dev/null +logfile_maxbytes=0 +pidfile=/run/supervisord.pid + +[unix_http_server] +file=/run/supervisor.sock +chmod=0700 + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface + +[supervisorctl] +serverurl=unix:///run/supervisor.sock + +; Every process starts again whenever it ends (startsecs=0: supervisord never +; gives up on one); the health check wants each up for 5 s. +; One group: a stop signals every process at once, so the worker has its +; whole drain time within docker's stop_grace_period (95 s). +[group:webvirtcloud] +programs=nginx,gunicorn,worker,novncd,socketiod,cron + +[program:nginx] +command=/usr/sbin/nginx -g "daemon off;" +stopsignal=QUIT +autorestart=true +startsecs=0 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +[program:gunicorn] +command=/srv/webvirtcloud/venv/bin/gunicorn webvirtcloud.wsgi:application -c gunicorn.conf.py +directory=/srv/webvirtcloud +user=www-data +environment=HOME="/var/www" +; gunicorn ends its requests for up to 30 s (graceful_timeout) +stopwaitsecs=35 +autorestart=true +startsecs=0 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +; It drains running jobs for 60 s after TERM and exits by itself. +[program:worker] +command=/srv/webvirtcloud/venv/bin/python3 manage.py run_worker +directory=/srv/webvirtcloud +user=www-data +environment=HOME="/var/www",WEBVIRTCLOUD_LOG_FILE="/srv/webvirtcloud/data/webvirtcloud-worker.log" +stopwaitsecs=80 +autorestart=true +startsecs=0 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +[program:novncd] +command=/srv/webvirtcloud/venv/bin/python3 console/novncd +directory=/srv/webvirtcloud +user=www-data +environment=HOME="/var/www" +; a console is a child process of its own +stopasgroup=true +autorestart=true +startsecs=0 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +; The serial console; it refuses connections while SERIAL_CONSOLE_ENABLED is off. +[program:socketiod] +command=/srv/webvirtcloud/venv/bin/python3 console/socketiod +directory=/srv/webvirtcloud +user=www-data +environment=HOME="/var/www" +autorestart=true +startsecs=0 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +[program:cron] +command=/usr/sbin/cron -f +autorestart=true +startsecs=0 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true diff --git a/conf/runit/10_webvirtcloud_init.sh b/conf/runit/10_webvirtcloud_init.sh deleted file mode 100755 index 7ffb39b1..00000000 --- a/conf/runit/10_webvirtcloud_init.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -set -e - -APP_DIR="/srv/webvirtcloud" -DATA_DIR="$APP_DIR/data" -mkdir -p "$DATA_DIR" "/var/www/.ssh" "/var/www/.pki" "$APP_DIR/backups" - -# If settings.py doesn't exist, generate from template -if [ ! -f "$APP_DIR/webvirtcloud/settings.py" ]; then - echo "* Generating webvirtcloud/settings.py from template..." - cp "$APP_DIR/webvirtcloud/settings.py.template" "$APP_DIR/webvirtcloud/settings.py" -fi - -# Ensure SECRET_KEY is persisted across container restarts if not supplied via environment -if [ -z "$SECRET_KEY" ]; then - if [ ! -s "$DATA_DIR/secret_key" ]; then - echo "* Generating fresh random SECRET_KEY..." - # created private: not readable by others before a chmod - (umask 077 && "$APP_DIR/venv/bin/python3" -c 'import secrets; print(secrets.token_urlsafe(50))' > "$DATA_DIR/secret_key") - fi -fi - -# Persist SQLite database in DATA_DIR -if [ ! -f "$DATA_DIR/db.sqlite3" ] && [ -f "$APP_DIR/db.sqlite3" ] && [ ! -L "$APP_DIR/db.sqlite3" ]; then - mv "$APP_DIR/db.sqlite3" "$DATA_DIR/db.sqlite3" -fi -touch "$DATA_DIR/db.sqlite3" -ln -sf "$DATA_DIR/db.sqlite3" "$APP_DIR/db.sqlite3" - -# Run database migrations -echo "* Running database migrations..." -"$APP_DIR/venv/bin/python3" "$APP_DIR/manage.py" migrate --noinput - -# Set proper permissions on runtime and data directories -chown -R www-data:www-data "$DATA_DIR" "/var/www/.ssh" "/var/www/.pki" "$APP_DIR/backups" -chown www-data:www-data "$APP_DIR/webvirtcloud/settings.py" 2>/dev/null || true -chmod 700 "/var/www/.ssh" "/var/www/.pki" 2>/dev/null || true -# The database holds compute passwords and password hashes; keep it, the -# secret key and settings private to the app user (as the installer does) -chmod 700 "$DATA_DIR" "$APP_DIR/backups" -chmod 600 "$DATA_DIR/db.sqlite3" "$APP_DIR/webvirtcloud/settings.py" -for f in "$DATA_DIR/db.sqlite3-wal" "$DATA_DIR/db.sqlite3-shm"; do - if [ -e "$f" ]; then chown www-data:www-data "$f"; chmod 600 "$f"; fi -done diff --git a/conf/runit/backup.cron b/conf/runit/backup.cron deleted file mode 100644 index b7896f94..00000000 --- a/conf/runit/backup.cron +++ /dev/null @@ -1,2 +0,0 @@ -# Daily panel backup into /srv/webvirtcloud/backups (the webvirtcloud-backups volume); the output goes to docker logs -15 3 * * * root . /etc/container_environment.sh && cd /srv/webvirtcloud && /sbin/setuser www-data venv/bin/python3 manage.py backup > /proc/1/fd/1 2>&1 diff --git a/conf/runit/healthcheck b/conf/runit/healthcheck deleted file mode 100755 index 0d9ef274..00000000 --- a/conf/runit/healthcheck +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# Docker's health check: the panel answers through nginx, and every service -# has run for a few seconds. runit starts a failing service again each -# second, so one that keeps failing (the worker, novncd, socketiod) is never -# up that long. -/srv/webvirtcloud/venv/bin/python3 -c "import urllib.request as u; u.urlopen(u.Request('http://127.0.0.1/accounts/login/', headers={'X-Forwarded-Proto': 'https'}), timeout=5)" || exit 1 -for service in nginx webvirtcloud webvirtcloud-worker novnc socketiod; do - # "run: /etc/service/novnc: (pid 60) 45556s" - uptime=$(sv status "/etc/service/$service" | sed -n 's/^run: [^)]*) \([0-9]*\)s.*/\1/p') - if [ -z "$uptime" ] || [ "$uptime" -lt 5 ]; then - echo "$service is not running steadily: $(sv status "/etc/service/$service")" - exit 1 - fi -done diff --git a/conf/runit/nginx b/conf/runit/nginx deleted file mode 100755 index a7cd558b..00000000 --- a/conf/runit/nginx +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e -if [[ ! -e /var/log/nginx/error.log ]]; then - # The Nginx log forwarder might be sleeping and waiting - # until the error log becomes available. We restart it in - # 1 second so that it picks up the new log file quickly. - (sleep 1 && sv restart /etc/service/nginx-log-forwarder) -fi -exec /usr/sbin/nginx diff --git a/conf/runit/nginx-log-forwarder b/conf/runit/nginx-log-forwarder deleted file mode 100755 index 07d63c65..00000000 --- a/conf/runit/nginx-log-forwarder +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -# Forwards the Nginx error.log to the Docker logs. -set -e -if [[ -e /var/log/nginx/error.log ]]; then - exec tail -F /var/log/nginx/error.log -else - exec sleep 10 -fi diff --git a/conf/runit/novncd.sh b/conf/runit/novncd.sh deleted file mode 100755 index 7e0c62f3..00000000 --- a/conf/runit/novncd.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -cd /srv/webvirtcloud || exit 1 -# its log goes to the container's output (docker logs) -exec /sbin/setuser www-data /srv/webvirtcloud/venv/bin/python3 /srv/webvirtcloud/console/novncd 2>&1 diff --git a/conf/runit/socketiod.sh b/conf/runit/socketiod.sh deleted file mode 100755 index 9c203d0c..00000000 --- a/conf/runit/socketiod.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -# The serial console daemon; it refuses connections while SERIAL_CONSOLE_ENABLED is off. - -cd /srv/webvirtcloud || exit 1 -# its log goes to the container's output (docker logs) -exec /sbin/setuser www-data /srv/webvirtcloud/venv/bin/python3 /srv/webvirtcloud/console/socketiod 2>&1 diff --git a/conf/runit/webvirtcloud-worker.sh b/conf/runit/webvirtcloud-worker.sh deleted file mode 100755 index 0126133d..00000000 --- a/conf/runit/webvirtcloud-worker.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -# The job worker (one per installation). runit stops it with TERM; it drains -# running jobs for 60 s and exits by itself. -cd /srv/webvirtcloud || exit -export WEBVIRTCLOUD_LOG_FILE=/srv/webvirtcloud/data/webvirtcloud-worker.log -# its output goes to the container's output (docker logs) -exec /sbin/setuser www-data /srv/webvirtcloud/venv/bin/python3 manage.py run_worker 2>&1 diff --git a/conf/runit/webvirtcloud.sh b/conf/runit/webvirtcloud.sh deleted file mode 100755 index 7ec782d2..00000000 --- a/conf/runit/webvirtcloud.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -# `/sbin/setuser www-data` runs the given command as the user `www-data`. -cd /srv/webvirtcloud || exit -# gunicorn's own and access log go to the container's output (docker logs) -exec /sbin/setuser www-data /srv/webvirtcloud/venv/bin/gunicorn webvirtcloud.wsgi:application -c /srv/webvirtcloud/gunicorn.conf.py 2>&1 diff --git a/conf/runit/secret_generator.py b/conf/secret_generator.py similarity index 100% rename from conf/runit/secret_generator.py rename to conf/secret_generator.py diff --git a/doc/architecture.md b/doc/architecture.md index 2240f613..ee209484 100644 --- a/doc/architecture.md +++ b/doc/architecture.md @@ -13,7 +13,7 @@ WebVirtCloud is a web panel for libvirt hosts. libvirt is the source of truth: t | Frontend | Django templates, Bootstrap 5, Bootstrap Icons, vendored jQuery, Chart.js, noVNC, xterm.js | | Security | `django-otp` (TOTP), `django-axes` (lockout), optional `django-auth-ldap`; CSP `script-src 'self'` | | Serving | nginx in front of gunicorn (gthread workers) and WhiteNoise | -| Processes | systemd units on hosts, runit in the Docker image | +| Processes | systemd units on hosts, supervisord in the Docker image | ## Processes @@ -31,12 +31,12 @@ Django, worker, novncd, socketiod ── libvirt (TCP, SSH, TLS or local socket) panel ── gstfsd on each compute, at the compute's hostname, port 16510 (HMAC-signed requests; loopback only when the panel runs on the compute) ``` -| Unit (systemd) | runit service (Docker) | Runs | +| Unit (systemd) | supervisord program (Docker) | Runs | |---|---|---| -| `webvirt-cloud` | `webvirtcloud` | gunicorn | -| `webvirt-novncd` | `novnc` | `console/novncd` | +| `webvirt-cloud` | `gunicorn` | gunicorn | +| `webvirt-novncd` | `novncd` | `console/novncd` | | `webvirt-socketiod` | `socketiod` | `console/socketiod` | -| `webvirt-worker` | `webvirtcloud-worker` | `manage.py run_worker` | +| `webvirt-worker` | `worker` | `manage.py run_worker` | | `webvirt-backup.timer` | `cron` (`/etc/cron.d/webvirtcloud-backup`) | `manage.py backup` | | `webvirt-gstfsd` (on computes) | | `conf/daemon/gstfsd` | diff --git a/docker-compose.yml b/docker-compose.yml index 0988d919..b2792c9f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: image: webvirtcloud:latest container_name: webvirtcloud restart: unless-stopped - # above my_init's 80 s, which is above the worker's 60 s drain + # above supervisord's 80 s for the worker, which drains its jobs for 60 s stop_grace_period: 95s # every request is a line of gunicorn's access log: kept to 5 files of 10 MB, as the panel's own log logging: diff --git a/webvirtcloud/tests/test_worker_install.py b/webvirtcloud/tests/test_worker_install.py index 8685bf15..bd3cd22d 100644 --- a/webvirtcloud/tests/test_worker_install.py +++ b/webvirtcloud/tests/test_worker_install.py @@ -12,14 +12,13 @@ BASE = Path(settings.BASE_DIR) class WorkerInstallTestCase(SimpleTestCase): def test_docker(self): - dockerfile = (BASE / "Dockerfile").read_text() - self.assertIn("conf/runit/webvirtcloud-worker.sh", dockerfile) - self.assertIn("/etc/service/webvirtcloud-worker/run", dockerfile) - script = (BASE / "conf/runit/webvirtcloud-worker.sh").read_text() - for text in ("/sbin/setuser www-data", "manage.py run_worker", - "WEBVIRTCLOUD_LOG_FILE=/srv/webvirtcloud/data/webvirtcloud-worker.log"): - self.assertIn(text, script) - self.assertIn("ENV KILL_PROCESS_TIMEOUT=80", dockerfile) + conf = (BASE / "conf/docker/supervisord.conf").read_text() + worker = conf[conf.index("[program:worker]"):].split("\n\n")[0] + for text in ("user=www-data", "manage.py run_worker", + 'WEBVIRTCLOUD_LOG_FILE="/srv/webvirtcloud/data/webvirtcloud-worker.log"', "stopwaitsecs=80"): + self.assertIn(text, worker) + # a stop signals every program at once: the worker's drain is not behind the others' stops + self.assertRegex(conf, r"programs=.*\bworker\b") compose = (BASE / "docker-compose.yml").read_text() self.assertRegex(compose, r"stop_grace_period:\s*9\ds") # the worker's host identity lives in the data volume, not in a fixed hostname