From 20f7b538e52ce47eefbe0dbb8e44fb5298df7820 Mon Sep 17 00:00:00 2001 From: tcely Date: Mon, 29 May 2023 07:12:23 -0400 Subject: [PATCH] Docker entrypoint fixes (#753) * docker entrypoint: DRY store log backup Don't Repeat Yourself Set a variable for the full path to the source file. Create the backup path by appending an extension from `date` output. Also fixed quoting and switched to an `if` block. Clean up block level variables. * docker entrypoint: use ISO 8601 format The previous format discards information about the local time zone. * docker entrypoint: always use UTC Now the format always ends in +00:00, and we can ignore that part again. * docker entrypoint: provide the date format again * docker entrypoint: remove time zone from the date format * docker entrypoint: use an unambiguous date format Present a leading zero before the month: YYYY-0MM-DD Both YYYY-MM-DD and YYYY-DD-MM are used by people and can be confusing in the beginning of the month. * docker entrypoint: use appropriate quoting Without avoiding field splitting a password containing a space changes the number of arguments being set. * docker entrypoint: use explicit braces for custom variables Make the intentions clear and don't assume the user knows the special cases for when variables won't be extended. Example: word=animal words=mistake echo "$words vs $word vs ${word}s" * docker entrypoint-xftp-server: braces and quoting * docker entrypoint-xftp-server: backup block * docker entrypoints: explain date format in a comment * switched from long to short option to date for POSIX * docker entrypoint-smp-server: explain date format * docker entrypoint-xftp-server: explain date format * docker entrypoints: further explain format I fixed the case to match the date format letters. Also, use words to explain since I don't want everyone to need to read about date formats to understand. * docker entrypoints: only quote letters I was either going to quote the dashes too or stop quoting the colons. Having less quotes was more readable. * Revert "docker entrypoint: use an unambiguous date format" This reverts commit ba2a93bad98d5dbd9a27b737bcfab36bae7bd0f7. * docker entrypoints: remove %F * docker entrypoint-smp-server: remove %F Used part of the explicit ISO 8601 format, provided by the coreutils date invocation guide. * docker entrypoint-xftp-server: remove %F Used part of the explicit ISO 8601 format, provided by the coreutils date invocation guide. --- scripts/docker/entrypoint-smp-server | 37 ++++++++++++++++--------- scripts/docker/entrypoint-xftp-server | 39 ++++++++++++++++++--------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/scripts/docker/entrypoint-smp-server b/scripts/docker/entrypoint-smp-server index 8958874f8..5817b7b56 100755 --- a/scripts/docker/entrypoint-smp-server +++ b/scripts/docker/entrypoint-smp-server @@ -1,25 +1,25 @@ #!/usr/bin/env sh -confd="/etc/opt/simplex" -logd="/var/opt/simplex/" +confd='/etc/opt/simplex' +logd='/var/opt/simplex/' # Check if server has been initialized -if [ ! -f "$confd/smp-server.ini" ]; then +if [ ! -f "${confd}/smp-server.ini" ]; then # If not, determine ip or domain - case "$ADDR" in - '') printf "Please specify \$ADDR environment variable.\n"; exit 1 ;; + case "${ADDR}" in + '') printf 'Please specify $ADDR environment variable.\n'; exit 1 ;; *[a-zA-Z]*) - case "$ADDR" in - *:*) set -- --ip "$ADDR" ;; - *) set -- -n "$ADDR" ;; + case "${ADDR}" in + *:*) set -- --ip "${ADDR}" ;; + *) set -- -n "${ADDR}" ;; esac ;; - *) set -- --ip "$ADDR" ;; + *) set -- --ip "${ADDR}" ;; esac # Optionally, set password - case "$PASS" in + case "${PASS}" in '') set -- "$@" --no-password ;; - *) set -- "$@" --password "$PASS" ;; + *) set -- "$@" --password "${PASS}" ;; esac # And init certificates and configs @@ -27,7 +27,20 @@ if [ ! -f "$confd/smp-server.ini" ]; then fi # Backup store log just in case -[ -f "$logd/smp-server-store.log" ] && cp "$logd"/smp-server-store.log "$logd"/smp-server-store.log."$(date +'%FT%T')" +# +# Uses the UTC (universal) time zone and this +# format: YYYY-mm-dd'T'HH:MM:SS +# year, month, day, letter T, hour, minute, second +# +# This is the ISO 8601 format without the time zone at the end. +# +_file="${logd}/smp-server-store.log" +if [ -f "${_file}" ]; then + _backup_extension="$(date -u '+%Y-%m-%dT%H:%M:%S')" + cp -v -p "${_file}" "${_file}.${_backup_extension:-date-failed}" + unset -v _backup_extension +fi +unset -v _file # Finally, run smp-sever. Notice that "exec" here is important: # smp-server replaces our helper script, so that it can catch INT signal diff --git a/scripts/docker/entrypoint-xftp-server b/scripts/docker/entrypoint-xftp-server index c23262670..55757401e 100755 --- a/scripts/docker/entrypoint-xftp-server +++ b/scripts/docker/entrypoint-xftp-server @@ -1,25 +1,25 @@ #!/usr/bin/env sh -confd="/etc/opt/simplex-xftp" -logd="/var/opt/simplex-xftp" +confd='/etc/opt/simplex-xftp' +logd='/var/opt/simplex-xftp' # Check if server has been initialized -if [ ! -f "$confd/file-server.ini" ]; then +if [ ! -f "${confd}/file-server.ini" ]; then # If not, determine ip or domain - case "$ADDR" in - '') printf "Please specify \$ADDR environment variable.\n"; exit 1 ;; + case "${ADDR}" in + '') printf 'Please specify $ADDR environment variable.\n'; exit 1 ;; *[a-zA-Z]*) - case "$ADDR" in - *:*) set -- --ip "$ADDR" ;; - *) set -- -n "$ADDR" ;; + case "${ADDR}" in + *:*) set -- --ip "${ADDR}" ;; + *) set -- -n "${ADDR}" ;; esac ;; - *) set -- --ip "$ADDR" ;; + *) set -- --ip "${ADDR}" ;; esac # Set quota - case "$QUOTA" in - '') printf "Please specify \$QUOTA environment variable.\n"; exit 1 ;; - *) set -- "$@" --quota "$QUOTA" ;; + case "${QUOTA}" in + '') printf 'Please specify $QUOTA environment variable.\n'; exit 1 ;; + *) set -- "$@" --quota "${QUOTA}" ;; esac # Init the certificates and configs @@ -27,7 +27,20 @@ if [ ! -f "$confd/file-server.ini" ]; then fi # Backup store log just in case -[ -f "$logd/file-server-store.log" ] && cp "$logd"/file-server-store.log "$logd"/file-server-store.log."$(date +'%FT%T')" +# +# Uses the UTC (universal) time zone and this +# format: YYYY-mm-dd'T'HH:MM:SS +# year, month, day, letter T, hour, minute, second +# +# This is the ISO 8601 format without the time zone at the end. +# +_file="${logd}/file-server-store.log" +if [ -f "${_file}" ]; then + _backup_extension="$(date -u '+%Y-%m-%dT%H:%M:%S')" + cp -v -p "${_file}" "${_file}.${_backup_extension:-date-failed}" + unset -v _backup_extension +fi +unset -v _file # Finally, run xftp-sever. Notice that "exec" here is important: # smp-server replaces our helper script, so that it can catch INT signal