docker-kopano/src/kopano/entry.d/10-kopano-common

113 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
#
# 10-kopano-common
#
# Kopano now installs without any cfg files, so we just write custom values
# into their target cfg file.
#
# Variables defined in Dockerfile
# DOCKER_CONF_DIR1 DOCKER_CONF_DIR2 DOCKER_ACME_SSL_DIR DOCKER_APPL_SSL_DIR
#
#
# Configuration
#
DOCKER_CONF_DIR1=${DOCKER_CONF_DIR1-/etc/kopano}
DOCKER_CONF_DIR2=${DOCKER_CONF_DIR2-/usr/share/z-push}
DOCKER_MAN5_DIR=${DOCKER_MAN5_DIR-/usr/share/man/man5/}
DOCKER_APPL_SSL_CERT=${DOCKER_APPL_SSL_CERT-$DOCKER_APPL_SSL_DIR/cert.pem}
DOCKER_APPL_SSL_KEY=${DOCKER_APPL_SSL_KEY-$DOCKER_APPL_SSL_DIR/priv_key.pem}
sqlstate_cfg_file=$DOCKER_CONF_DIR2/backend/sqlstatemachine/config.php
zpush_cfg_file=$DOCKER_CONF_DIR2/config.php
webapp_cfg_file=$DOCKER_CONF_DIR1/webapp/config.php
#
# Apply environment variables to configuration files.
# Uuse all valid keys (variables) for a service to see if there is a envvar with
# identical name, if so apply its value to the config file.
# With kopano-core use man page files to lookup valid keys.
# With kopano-webapp and z-push use installed config file to find valid keys.
#
kopano_apply_envvars_core() {
for service in dagent gateway ical ldap search server spamd spooler; do
kopano_apply_envvars_cfg $service
done
}
kopano_apply_envvars_webapp() {
kopano_apply_envvars_php $webapp_cfg_file
}
kopano_apply_envvars_zpush() {
kopano_apply_envvars_php $sqlstate_cfg_file
kopano_apply_envvars_php $zpush_cfg_file
}
kopano_apply_envvars_cfg() {
local cfg_file=$DOCKER_CONF_DIR1/$1.cfg
local man_file=$DOCKER_MAN5_DIR/kopano-$1.cfg.5.gz
if [ -f $man_file ]; then
local env_vars="$(kopano_get_envvars_man $man_file)"
if [ -e $cfg_file ]; then
mv -f $cfg_file $cfg_file.orig
fi
for env_var in $env_vars; do
if [ -n "${!env_var+x}" ]; then
dc_log 5 "Setting ${env_var,,} = ${!env_var} in $cfg_file"
echo ${env_var,,} = ${!env_var} >> $cfg_file
fi
done
else
dc_log 4 "Could not find $man_file"
fi
}
kopano_apply_envvars_php() {
local cfg_file=$1
if [ -e $cfg_file ]; then
local env_vars="$(kopano_get_envvars_php $cfg_file)"
cp -f $cfg_file $cfg_file.orig
for env_var in $env_vars; do
if [ -n "${!env_var}" ]; then
dc_log 5 "Setting ${env_var} = ${!env_var} in $cfg_file"
sed -ri "s/(\s*define).+${env_var}.+/\1\(\x27${env_var}\x27, \x27${!env_var}\x27\);/g" $cfg_file
fi
done
fi
}
kopano_get_envvars_man() { zcat $1 | sed -r "/^\.SS/!d;{s/^\.SS (.*)/\U\1/g;s/,//g}" | sort -u ;}
kopano_get_envvars_php() { sed -nr "/define\(/s/.*define\(['\"](.*)['\"], .*/\1/p" $1 | sort -u ;}
#
# Update SSL_CERTIFICATE_FILE and SSL_PRIVATE_KEY_FILE.
#
kopano_export_tls_cert() {
if ([ -f "$DOCKER_APPL_SSL_CERT" ] && [ -f "$DOCKER_APPL_SSL_KEY" ]); then
export SSL_CERTIFICATE_FILE=${SSL_CERTIFICATE_FILE-$DOCKER_APPL_SSL_CERT}
export SSL_PRIVATE_KEY_FILE=${SSL_PRIVATE_KEY_FILE-$DOCKER_APPL_SSL_KEY}
fi
if ([ -f "$SSL_CERTIFICATE_FILE" ] && [ -f "$SSL_PRIVATE_KEY_FILE" ]); then
dc_cond_chown $DOCKER_APPL_RUNAS $SSL_CERTIFICATE_FILE $SSL_PRIVATE_KEY_FILE
fi
}
#
# Generate self signed certificate if any of IMAPS_LISTEN, POP3S_LISTEN or
# ICALS_LISTEN is no-empty but no certificates are given.
#
kopano_generate_tls_cert() {
for prot in IMAPS_LISTEN POP3S_LISTEN ICALS_LISTEN; do
if [ -n "${!prot}" ]; then
local secure="$prot=${!prot}"
break
fi
done
if ([ -z "$SSL_CERTIFICATE_FILE" ] && [ -n "$secure" ] && dc_is_installed openssl); then
dc_log 4 "$secure, but no certs given, so generating self-signed cert for host $HOSTNAME"
mkdir -p $DOCKER_APPL_SSL_DIR
dc_tls_setup_selfsigned_cert $DOCKER_APPL_SSL_CERT $DOCKER_APPL_SSL_KEY
fi
}