1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/conf/install-lib.sh
catborise 29a1cbcace 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.
2026-11-20 17:50:43 +03:00

81 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Idempotent installer primitives. Sourcing this file has NO side effects;
# it only defines functions, so it can be unit-tested (webvirtcloud/tests/sh/install_lib.test.sh).
# Create <data_dir>/secret_key (mode 600) only if missing or empty; keep an
# existing key. A blank key would make Django fail to boot, so an empty file is
# treated as missing, and the function fails when it could not write one.
wvc_ensure_secret_key() {
local data_dir="$1" python="${2:-python3}" key_file
key_file="$data_dir/secret_key"
mkdir -p "$data_dir"
chmod 700 "$data_dir"
if [ ! -s "$key_file" ]; then
# umask 077 so the key is never group/world readable, even briefly
( umask 077; "$python" -c 'import secrets; print(secrets.token_urlsafe(50))' > "$key_file" )
fi
chmod 600 "$key_file"
# an empty key means the python did not run: the panel would not start
[ -s "$key_file" ]
}
# 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
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, the local settings and the db are not world-readable.
wvc_harden_modes() {
local db="$1" data_dir="$2" file
[ -e "$db" ] && chmod 600 "$db"
[ -d "$data_dir" ] && chmod 700 "$data_dir"
for file in secret_key local_settings.py; do
[ -e "$data_dir/$file" ] && chmod 600 "$data_dir/$file"
done
return 0
}
# Install the nginx main config only on first run (keeping a one-time backup of
# whatever was there), and always refresh the app's conf.d snippet. A re-run
# therefore never clobbers an operator's edited main nginx.conf.
wvc_install_nginx() {
local main_template="$1" snippet="$2" etc="$3" marker
marker="$etc/.wvc-nginx-installed"
mkdir -p "$etc/conf.d"
# Use a dedicated marker (always created on first install) rather than the
# backup file, which is absent when the host had no nginx.conf to begin with.
if [ ! -f "$marker" ]; then
[ -f "$etc/nginx.conf" ] && cp "$etc/nginx.conf" "$etc/nginx.conf.wvc-orig"
cp "$main_template" "$etc/nginx.conf"
: > "$marker"
fi
cp "$snippet" "$etc/conf.d/webvirtcloud.conf"
return 0
}
# Python list literal for ALLOWED_HOSTS: the FQDN, the loopbacks and the given
# IPs. IPv6 addresses are bracketed, as Django compares them.
wvc_allowed_hosts() {
local host out="" seen=" "
for host in "$1" localhost 127.0.0.1 ::1 "${@:2}"; do
case "$host" in *:*) host="[$host]" ;; esac
case "$seen" in *" $host "*) continue ;; esac
seen+="$host "
out+="${out:+, }'$host'"
done
echo "[$out]"
}