From a1726dc45add7525f51617cd399a3fe22b5d474b Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Wed, 6 Apr 2022 16:48:41 +0200 Subject: [PATCH] scripts: added `TZ` environment variable to set timezone (#2530) --- docs/content/config/environment.md | 4 ++++ mailserver.env | 6 ++++++ target/scripts/start-mailserver.sh | 3 +++ target/scripts/startup/setup-stack.sh | 22 ++++++++++++++++++++ test/mail_time.bats | 29 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 test/mail_time.bats diff --git a/docs/content/config/environment.md b/docs/content/config/environment.md index 2a645150..600c334c 100644 --- a/docs/content/config/environment.md +++ b/docs/content/config/environment.md @@ -52,6 +52,10 @@ Set different options for mynetworks option (can be overwrite in postfix-main.cf Note: you probably want to [set `POSTFIX_INET_PROTOCOLS=ipv4`](#postfix_inet_protocols) to make it work fine with Docker. +##### TZ + +Set the timezone. If this variable is unset, the container runtime will try to detect the time using `/etc/localtime`, which you can alternatively mount into the container. The value of this variable must follow the pattern `AREA/ZONE`, i.e. of you want to use Germany's time zone, use `Europe/Berlin`. You can lookup all available timezones [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List). + ##### ENABLE_AMAVIS Amavis content filter (used for ClamAV & SpamAssassin) diff --git a/mailserver.env b/mailserver.env index 158fb9a0..dc577928 100644 --- a/mailserver.env +++ b/mailserver.env @@ -62,6 +62,12 @@ UPDATE_CHECK_INTERVAL=1d # connected-networks => Add all connected docker networks (ipv4 only) PERMIT_DOCKER=none +# Set the timezone. If this variable is unset, the container runtime will try to detect the time using +# `/etc/localtime`, which you can alternatively mount into the container. The value of this variable +# must follow the pattern `AREA/ZONE`, i.e. of you want to use Germany's time zone, use `Europe/Berlin`. +# You can lookup all available timezones here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List +TZ= + # In case you network interface differs from 'eth0', e.g. when you are using HostNetworking in Kubernetes, # you can set NETWORK_INTERFACE to whatever interface you want. This interface will then be used. # - **empty** => eth0 diff --git a/target/scripts/start-mailserver.sh b/target/scripts/start-mailserver.sh index b4512a9c..cb5d2be5 100755 --- a/target/scripts/start-mailserver.sh +++ b/target/scripts/start-mailserver.sh @@ -107,6 +107,7 @@ VARS[SPOOF_PROTECTION]="${SPOOF_PROTECTION:=0}" VARS[SRS_SENDER_CLASSES]="${SRS_SENDER_CLASSES:=envelope_sender}" VARS[SSL_TYPE]="${SSL_TYPE:=}" VARS[TLS_LEVEL]="${TLS_LEVEL:=modern}" +VARS[TZ]="${TZ:=}" VARS[UPDATE_CHECK_INTERVAL]="${UPDATE_CHECK_INTERVAL:=1d}" VARS[VIRUSMAILS_DELETE_DELAY]="${VIRUSMAILS_DELETE_DELAY:=7}" @@ -131,6 +132,8 @@ function register_functions _register_setup_function '_setup_default_vars' _register_setup_function '_setup_file_permissions' + [[ -n ${TZ} ]] && _register_setup_function '_setup_timezone' + if [[ ${SMTP_ONLY} -ne 1 ]] then _register_setup_function '_setup_dovecot' diff --git a/target/scripts/startup/setup-stack.sh b/target/scripts/startup/setup-stack.sh index f4eaf0be..b54c410e 100644 --- a/target/scripts/startup/setup-stack.sh +++ b/target/scripts/startup/setup-stack.sh @@ -1263,3 +1263,25 @@ EOF supervisorctl reread supervisorctl update } + +function _setup_timezone +{ + _log 'debug' "Setting timezone to '${TZ}'" + + local ZONEINFO_FILE="/usr/share/zoneinfo/${TZ}" + + if [[ ! -e ${ZONEINFO_FILE} ]] + then + _log 'warn' "Cannot find timezone '${TZ}'" + return 1 + fi + + if ln -fs "${ZONEINFO_FILE}" /etc/localtime \ + && dpkg-reconfigure -f noninteractive tzdata &>/dev/null + then + _log 'trace' "Set time zone to '${TZ}'" + else + _log 'warn' "Setting timezone to '${TZ}' failed" + return 1 + fi +} diff --git a/test/mail_time.bats b/test/mail_time.bats new file mode 100644 index 00000000..4fb5e9bc --- /dev/null +++ b/test/mail_time.bats @@ -0,0 +1,29 @@ +load 'test_helper/common' + +setup_file() { + local PRIVATE_CONFIG + PRIVATE_CONFIG="$(duplicate_config_for_container .)" + + docker run -d --name mail_time \ + -v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ + -v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \ + -e TZ='Asia/Jakarta' \ + -e LOG_LEVEL=debug \ + -h mail.my-domain.com -t "${NAME}" + + wait_for_smtp_port_in_container mail_time +} + +teardown_file() { + docker rm -f mail_time +} + +@test "checking time: setting the time with TZ works correctly" { + run docker exec mail_time cat /etc/timezone + assert_success + assert_output 'Asia/Jakarta' + + run docker exec mail_time date '+%Z' + assert_success + assert_output 'WIB' +}