Create checksums in start script, avoid race condition

This commit is contained in:
Erik Wramner 2019-08-01 09:58:22 +02:00
parent 21c89b3364
commit 7f3e5a22e1
2 changed files with 40 additions and 12 deletions

View file

@ -2,15 +2,8 @@
# create date for log output
log_date=$(date +"%Y-%m-%d %H:%M:%S ")
# Prevent a start too early
sleep 5
echo "${log_date} Start check-for-changes script."
# create checksum file outside mounted directory
# the checksum file should be reused on subsequent runs,
# but only by this container, not by others
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum
# change directory
cd /tmp/docker-mailserver
@ -20,6 +13,13 @@ if [ ! -f postfix-accounts.cf ]; then
exit
fi
# Verify checksum file exists; must be prepared by start-mailserver.sh
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum
if [ ! -f $CHKSUM_FILE ]; then
echo "${log_date} ${CHKSUM_FILE} is missing! Start script failed? Exit!"
exit
fi
# Determine postmaster address, duplicated from start-mailserver.sh
# This script previously didn't work when POSTMASTER_ADDRESS was empty
if [[ -n "${OVERRIDE_HOSTNAME}" ]]; then
@ -30,15 +30,15 @@ fi
PM_ADDRESS="${POSTMASTER_ADDRESS:=postmaster@${DOMAINNAME}}"
echo "${log_date} Using postmaster address ${PM_ADDRESS}"
# create an array of files to monitor (perhaps simple *.cf would be ok here)
# Create an array of files to monitor, must be the same as in start-mailserver.sh
declare -a cf_files=()
for file in postfix-accounts.cf postfix-virtual.cf postfix-aliases.cf; do
[ -f "$file" ] && cf_files+=("$file")
done
# Update / generate after start
echo "${log_date} Makeing new checksum file."
sha512sum ${cf_files[@]/#/--tag } >$CHKSUM_FILE
# Wait to make sure server is up before we start
# Plus the files have just been generated, no hurry to process changes
sleep 20
# Run forever
while true; do
@ -54,6 +54,8 @@ if [[ $chksum == *"FAIL"* ]]; then
# Bug alert! This overwrites the alias set by start-mailserver.sh
# Take care that changes in one script are propagated to the other
# Also note that changes are performed in place and are not atomic
# We should fix that and write to temporary files, stop, swap and start
#regen postix aliases.
echo "root: ${PM_ADDRESS}" > /etc/aliases

View file

@ -49,6 +49,7 @@ DEFAULT_VARS["VIRUSMAILS_DELETE_DELAY"]="${VIRUSMAILS_DELETE_DELAY:="7"}"
##########################################################################
HOSTNAME="$(hostname -f)"
DOMAINNAME="$(hostname -d)"
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum
##########################################################################
# << GLOBAL VARS
##########################################################################
@ -88,6 +89,7 @@ function register_functions() {
################### >> setup funcs
_register_setup_function "_setup_default_vars"
_register_setup_function "_setup_chksum_file"
if [ "$ENABLE_ELK_FORWARDER" = 1 ]; then
_register_setup_function "_setup_elk_forwarder"
@ -439,6 +441,30 @@ function _setup_default_vars() {
done
}
function _setup_chksum_file() {
notify 'task' "Setting up configuration checksum file"
if [ -d /tmp/docker-mailserver ]; then
pushd /tmp/docker-mailserver
declare -a cf_files=()
for file in postfix-accounts.cf postfix-virtual.cf postfix-aliases.cf; do
[ -f "$file" ] && cf_files+=("$file")
done
notify 'inf' "Creating $CHKSUM_FILE"
sha512sum ${cf_files[@]/#/--tag } >$CHKSUM_FILE
popd
else
# We could just skip the file, but perhaps config can be added later?
# If so it must be processed by the check for changes script
notify 'inf' "Creating empty $CHKSUM_FILE (no config)"
touch $CHKSUM_FILE
fi
}
function _setup_mailname() {
notify 'task' 'Setting up Mailname'