Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: get rid of subshell + exec in helper-functions.sh #2401

Merged
merged 3 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions target/scripts/check-for-changes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ do
# get chksum and check it, no need to lock config yet
_monitored_files_checksums >"${CHKSUM_FILE}.new"
cmp --silent -- "${CHKSUM_FILE}" "${CHKSUM_FILE}.new"

# cmp return codes
# 0 – files are identical
# 1 – files differ
Expand All @@ -60,13 +61,7 @@ do
create_lock # Shared config safety lock
CHANGED=$(grep -Fxvf "${CHKSUM_FILE}" "${CHKSUM_FILE}.new" | sed 's/^[^ ]\+ //')

# Bug alert! This overwrites the alias set by start-mailserver.sh
# Take care that changes in one script are propagated to the other

# ! NEEDS FIX -----------------------------------------
# TODO FIX --------------------------------------------
# ! NEEDS EXTENSIONS ----------------------------------
# TODO Perform updates below conditionally too --------
# TODO Perform updates below conditionally too
# 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

Expand Down Expand Up @@ -137,7 +132,7 @@ do
# mark changes as applied
mv "${CHKSUM_FILE}.new" "${CHKSUM_FILE}"

sleep 1
sleep 2
done

exit 0
30 changes: 15 additions & 15 deletions target/scripts/helper-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,33 +225,33 @@ export -f _notify
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum

# Compute checksums of monitored files,
# returned output is lines of hashed content + filepath pairs.
# returned output on `stdout`: hash + filepath tuple on each line
function _monitored_files_checksums
{
# If a wildcard path pattern (or an empty ENV) would yield an invalid path
# or no results, `shopt -s nullglob` prevents it from being added.
shopt -s nullglob
declare -a CERT_FILES

# React to any cert changes within the following letsencrypt locations:
local CERT_FILES=(
CERT_FILES=(
/etc/letsencrypt/live/"${SSL_DOMAIN}"/*.pem
/etc/letsencrypt/live/"${HOSTNAME}"/*.pem
/etc/letsencrypt/live/"${DOMAINNAME}"/*.pem
)

# CERT_FILES should expand to separate paths, not a single string;
# otherwise fails to generate checksums for these file paths.
#shellcheck disable=SC2068
(
cd /tmp/docker-mailserver || exit 1
exec sha512sum 2>/dev/null -- \
polarathene marked this conversation as resolved.
Show resolved Hide resolved
postfix-accounts.cf \
postfix-virtual.cf \
postfix-aliases.cf \
dovecot-quotas.cf \
/etc/letsencrypt/acme.json \
${CERT_FILES[@]}
)
if [[ ! -d /tmp/docker-mailserver ]]
then
return 1
fi

sha512sum 2>/dev/null -- \
/tmp/docker-mailserver/postfix-accounts.cf \
/tmp/docker-mailserver/postfix-virtual.cf \
/tmp/docker-mailserver/postfix-aliases.cf \
/tmp/docker-mailserver/dovecot-quotas.cf \
/etc/letsencrypt/acme.json \
"${CERT_FILES[@]}"
georglauterbach marked this conversation as resolved.
Show resolved Hide resolved
}
export -f _monitored_files_checksums

Expand Down
2 changes: 1 addition & 1 deletion test/mail_changedetector.bats
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function teardown_file() {

@test "checking changedetector: can detect changes & between two containers using same config" {
echo "" >> "$(private_config_path mail_changedetector_one)/postfix-accounts.cf"
sleep 15
sleep 25
run docker exec mail_changedetector_one /bin/bash -c "supervisorctl tail -3000 changedetector"
assert_output --partial "postfix: stopped"
assert_output --partial "postfix: started"
Expand Down