1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

feat: an installation's own settings in data/local_settings.py; settings.py is part of the code

Every update rebuilt settings.py from the template, and the operator put
back their own values by reading a diff: a forgotten DATABASES started
the panel on a new, empty sqlite database. Docker users who needed an
option only settings.py has (LDAP, OTP_ENABLED, LOGIN_TRUSTED_PROXIES)
mounted their own copy and kept it up to date by hand.

webvirtcloud/settings.py is now the tracked file (no template, no copy
in the installer, the image, the container's start or CI), and it runs
data/local_settings.py last: the installation's own values, which an
update does not touch, kept in Docker's data volume, in the panel's
backups and readable by the panel's user only. The installer writes the
time zone, the noVNC port (now checked to be a number), ALLOWED_HOSTS
and CSRF_TRUSTED_ORIGINS there instead of editing settings.py with sed.
LDAP is set up there too; settings.py keeps only python-ldap's
timeouts. The README's update steps move an older version's own
settings.py into the backup before git pull and have its values moved
into the local file once.
This commit is contained in:
catborise 2026-11-20 17:22:49 +03:00
parent 0fb0b61a1a
commit 29a1cbcace
15 changed files with 221 additions and 180 deletions

View file

@ -1,10 +1,9 @@
#!/bin/sh
# The container's start: settings, secret key and database in place, the
# The container's start: 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
@ -12,12 +11,14 @@ fi
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.
# The database holds compute passwords and password hashes: the data (the
# database, the key, local_settings.py), keys 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
chown www-data:www-data backups
chmod 700 data backups /var/www/.ssh /var/www/.pki
chmod 600 data/db.sqlite3 webvirtcloud/settings.py
chmod 600 data/db.sqlite3
[ ! -f data/local_settings.py ] || chmod 600 data/local_settings.py
# cron starts its jobs with an empty environment: the backup reads this one
(umask 077 && export -p > /run/webvirtcloud.env)

View file

@ -19,27 +19,33 @@ wvc_ensure_secret_key() {
[ -s "$key_file" ]
}
# Copy settings.py from the template only when it does not already exist, so a
# re-run never overwrites local edits. 0 + "created" on first copy; 1 + "kept"
# when it already exists.
wvc_ensure_settings() {
local template="$1" target="$2"
if [ -f "$target" ]; then
# Write <data_dir>/local_settings.py (mode 600) from stdin only when it does not
# exist: the installation's own settings, which a re-run and updates keep.
# 0 + "created" when written; 1 + "kept" when it already exists; 2 when it could
# not be written (no file is left).
wvc_ensure_local_settings() {
local data_dir="$1" file
file="$data_dir/local_settings.py"
if [ -f "$file" ]; then
echo "kept"
return 1
fi
cp "$template" "$target"
if ! ( umask 077; cat > "$file.part" ) || ! mv "$file.part" "$file"; then
rm -f "$file.part"
return 2
fi
echo "created"
return 0
}
# Restrict modes so the secret, settings, and db are not world-readable.
# Restrict modes so the secret, the local settings and the db are not world-readable.
wvc_harden_modes() {
local settings="$1" db="$2" data_dir="$3"
[ -e "$settings" ] && chmod 600 "$settings"
local db="$1" data_dir="$2" file
[ -e "$db" ] && chmod 600 "$db"
[ -d "$data_dir" ] && chmod 700 "$data_dir"
[ -e "$data_dir/secret_key" ] && chmod 600 "$data_dir/secret_key"
for file in secret_key local_settings.py; do
[ -e "$data_dir/$file" ] && chmod 600 "$data_dir/$file"
done
return 0
}