scripts: added TZ environment variable to set timezone (#2530)

This commit is contained in:
Georg Lauterbach 2022-04-06 16:48:41 +02:00 committed by GitHub
parent b1594a8b1c
commit a1726dc45a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 0 deletions

View file

@ -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. 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 ##### ENABLE_AMAVIS
Amavis content filter (used for ClamAV & SpamAssassin) Amavis content filter (used for ClamAV & SpamAssassin)

View file

@ -62,6 +62,12 @@ UPDATE_CHECK_INTERVAL=1d
# connected-networks => Add all connected docker networks (ipv4 only) # connected-networks => Add all connected docker networks (ipv4 only)
PERMIT_DOCKER=none 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, # 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. # you can set NETWORK_INTERFACE to whatever interface you want. This interface will then be used.
# - **empty** => eth0 # - **empty** => eth0

View file

@ -107,6 +107,7 @@ VARS[SPOOF_PROTECTION]="${SPOOF_PROTECTION:=0}"
VARS[SRS_SENDER_CLASSES]="${SRS_SENDER_CLASSES:=envelope_sender}" VARS[SRS_SENDER_CLASSES]="${SRS_SENDER_CLASSES:=envelope_sender}"
VARS[SSL_TYPE]="${SSL_TYPE:=}" VARS[SSL_TYPE]="${SSL_TYPE:=}"
VARS[TLS_LEVEL]="${TLS_LEVEL:=modern}" VARS[TLS_LEVEL]="${TLS_LEVEL:=modern}"
VARS[TZ]="${TZ:=}"
VARS[UPDATE_CHECK_INTERVAL]="${UPDATE_CHECK_INTERVAL:=1d}" VARS[UPDATE_CHECK_INTERVAL]="${UPDATE_CHECK_INTERVAL:=1d}"
VARS[VIRUSMAILS_DELETE_DELAY]="${VIRUSMAILS_DELETE_DELAY:=7}" 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_default_vars'
_register_setup_function '_setup_file_permissions' _register_setup_function '_setup_file_permissions'
[[ -n ${TZ} ]] && _register_setup_function '_setup_timezone'
if [[ ${SMTP_ONLY} -ne 1 ]] if [[ ${SMTP_ONLY} -ne 1 ]]
then then
_register_setup_function '_setup_dovecot' _register_setup_function '_setup_dovecot'

View file

@ -1263,3 +1263,25 @@ EOF
supervisorctl reread supervisorctl reread
supervisorctl update 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
}

29
test/mail_time.bats Normal file
View file

@ -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'
}