123 lines
4.4 KiB
Bash
123 lines
4.4 KiB
Bash
#!/bin/bash
|
|
|
|
ICINGAWEB2_FILE_APACHE_DEFAULT=/etc/apache2/sites-available/000-default.conf
|
|
ICINGAWEB2_FILE_APACHE_PORTS=/etc/apache2/ports.conf
|
|
ICINGAWEB2_PORT=8080
|
|
ICINGAWEB2_WEBROOT=/data/usr/icingaweb2
|
|
ICINGAWEB2_WEBROOT_INIT=/data-init/usr/icingaweb2
|
|
|
|
## the webroot under /data is code from the image, config and state live in
|
|
## /data/etc/icingaweb2 and the database. when the image ships another icingaweb2
|
|
## version, the webroot has to follow, otherwise old assets, templates and modules
|
|
## run against the new php library (e.g. missing css/icinga/csp-config-editor.less).
|
|
## modules which are not part of the image stay untouched
|
|
icingaweb2_update_webroot() {
|
|
local version_old version_new module_path module
|
|
|
|
## nothing to compare without the webroot template from the image
|
|
[ -d "$ICINGAWEB2_WEBROOT_INIT" ] || return 0
|
|
|
|
if [ ! -d "$ICINGAWEB2_WEBROOT" ]; then
|
|
echo create icingaweb2 webroot from image
|
|
mkdir -p "$ICINGAWEB2_WEBROOT"
|
|
cp -a "$ICINGAWEB2_WEBROOT_INIT"/. "$ICINGAWEB2_WEBROOT"/
|
|
chown -R 33:33 "$ICINGAWEB2_WEBROOT"
|
|
return 0
|
|
fi
|
|
|
|
version_old="$(cat "$ICINGAWEB2_WEBROOT/VERSION" 2>/dev/null)"
|
|
version_new="$(cat "$ICINGAWEB2_WEBROOT_INIT/VERSION" 2>/dev/null)"
|
|
|
|
## skip when the image carries no version to compare against
|
|
[ -n "$version_new" ] || return 0
|
|
|
|
## skip when the webroot already runs the version from the image
|
|
[ "$version_old" != "$version_new" ] || return 0
|
|
|
|
echo "replace icingaweb2 webroot: ${version_old:-unknown} -> $version_new"
|
|
|
|
## keep one backup of the replaced webroot for comparisons
|
|
rm -rf "${ICINGAWEB2_WEBROOT}.bak"
|
|
if ! cp -ax "$ICINGAWEB2_WEBROOT" "${ICINGAWEB2_WEBROOT}.bak"; then
|
|
echo "backup of $ICINGAWEB2_WEBROOT failed, keeping the current webroot" >&2
|
|
return 1
|
|
fi
|
|
|
|
## replace the modules shipped by the image, self installed ones are kept
|
|
for module_path in "$ICINGAWEB2_WEBROOT_INIT"/modules/*/; do
|
|
[ -d "$module_path" ] || continue
|
|
module="$(basename "$module_path")"
|
|
echo "replace module $module"
|
|
rm -rf "$ICINGAWEB2_WEBROOT/modules/$module"
|
|
done
|
|
|
|
## VERSION is the marker of this replacement, so it is replaced too
|
|
rm -rf "$ICINGAWEB2_WEBROOT"/application \
|
|
"$ICINGAWEB2_WEBROOT"/public \
|
|
"$ICINGAWEB2_WEBROOT"/schema \
|
|
"$ICINGAWEB2_WEBROOT"/doc \
|
|
"$ICINGAWEB2_WEBROOT"/VERSION
|
|
cp -a "$ICINGAWEB2_WEBROOT_INIT"/. "$ICINGAWEB2_WEBROOT"/
|
|
|
|
chown -R 33:33 "$ICINGAWEB2_WEBROOT"
|
|
|
|
for module_path in "$ICINGAWEB2_WEBROOT"/modules/*/; do
|
|
[ -d "$module_path" ] || continue
|
|
module="$(basename "$module_path")"
|
|
[ -d "$ICINGAWEB2_WEBROOT_INIT/modules/$module" ] \
|
|
|| echo "keep module $module (not part of the image)"
|
|
done
|
|
}
|
|
|
|
## init icinga config files if need
|
|
if [ -z "$(ls -A /data)" ]; then
|
|
# enable python script for inital deployments
|
|
ICINGAWEB2_CONF_ONCE=false
|
|
export INIT_DB=true
|
|
|
|
## move init files
|
|
echo init icingaweb2 config
|
|
cp -ax /data-init/* /data
|
|
rm -rf /data-init
|
|
|
|
chown -R 33:33 /data
|
|
|
|
## check if a var exist they is must have for deployment
|
|
## because without this check, moduls init everytime and this blocks /setup page
|
|
if [ ! -z "$ICINGAWEB2_DEFAULT_ADMIN_USER" ]; then
|
|
## enable modules
|
|
icingacli module enable icingadb
|
|
mkdir -p /etc/icingaweb2/modules/icingadb/
|
|
|
|
icingacli module enable grafana
|
|
mkdir -p /etc/icingaweb2/modules/grafana/
|
|
else
|
|
## print setup token because no auto deployment will run
|
|
icingacli setup token create;
|
|
fi
|
|
fi
|
|
|
|
## follow the icingaweb2 version of the image, also when /data is already populated
|
|
if ! icingaweb2_update_webroot; then
|
|
echo "warning: icingaweb2 webroot does not match the image version" >&2
|
|
fi
|
|
|
|
## apache setup
|
|
if [ ! -z "$ICINGAWEB2_PORT" ]; then
|
|
echo set icingaweb2 apache ports
|
|
sed -i "s|^Listen .*|Listen $ICINGAWEB2_PORT|" $ICINGAWEB2_FILE_APACHE_PORTS
|
|
sed -i "s|VirtualHost \*:.*>|VirtualHost \*:$ICINGAWEB2_PORT>|g" $ICINGAWEB2_FILE_APACHE_DEFAULT
|
|
fi
|
|
|
|
## edit icingaweb2 config only on first setup or is enabled
|
|
if { [ -z "${ICINGAWEB2_CONF_ONCE+x}" ] || [ "$ICINGAWEB2_CONF_ONCE" = "false" ]; } \
|
|
&& [ -n "${ICINGAWEB2_DEFAULT_ADMIN_USER+x}" ]; then
|
|
/usr/bin/python3 /root/conf_icingaweb2.py
|
|
fi
|
|
|
|
## init apache
|
|
. /etc/apache2/envvars
|
|
|
|
# icinga2 daemon -C -X ?
|
|
# run CMD
|
|
exec "$@" |