From be3df7018cc5386a9e704bb8e876ca3802613664 Mon Sep 17 00:00:00 2001 From: tcely Date: Tue, 30 May 2023 08:11:11 -0400 Subject: [PATCH 1/5] Create functions.sh The entrypoint files had significant common operations. This is a first step towards only maintaining one copy of the code. --- scripts/docker/functions.sh | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 scripts/docker/functions.sh diff --git a/scripts/docker/functions.sh b/scripts/docker/functions.sh new file mode 100644 index 000000000..941638ea5 --- /dev/null +++ b/scripts/docker/functions.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env sh + +print_to_stderr() { + # Print each argument as a separate line + printf -- '%s\n' "$@" 1>&2 +} + +set_args_from_env_addr() { + # Determine IP or domain name + case "${ADDR}" in + '') print_to_stderr 'Please specify $ADDR environment variable.'; return 1 ;; + *[a-zA-Z]*) + case "${ADDR}" in + *:*) set -- --ip "${ADDR}" ;; + *) set -- -n "${ADDR}" ;; + esac + ;; + *) set -- --ip "${ADDR}" ;; + esac + return 0 +} + +append_args_from_env_pass() { + # Optionally, set password + case "${PASS}" in + '') set -- "$@" --no-password ;; + *) set -- "$@" --password "${PASS}" ;; + esac + return 0 +} + +append_args_from_env_quota() { + # Set quota + case "${QUOTA}" in + '') print_to_stderr 'Please specify $QUOTA environment variable.'; return 1 ;; + *) set -- "$@" --quota "${QUOTA}" ;; + esac + return 0 +} + +# 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. +backup_file_iso8601() { + for _file in "$@"; do + 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 + done + unset -v _file +} From 7f034ff2323636d104481f74cfb6de3cc3858c3d Mon Sep 17 00:00:00 2001 From: tcely Date: Tue, 30 May 2023 08:56:56 -0400 Subject: [PATCH 2/5] Dockerfile: use install instead of mv `install` has strip support built-in and ensures permissions. https://www.gnu.org/software/coreutils/manual/coreutils.html#install-invocation --- Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1a2aabf88..dffe2be66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,9 +38,9 @@ WORKDIR /final # Strip the binary from debug symbols to reduce size RUN bin=$(find /project/dist-newstyle -name "$APP" -type f -executable) && \ - mv "$bin" ./ && \ - strip ./"$APP" &&\ - mv /project/scripts/docker/entrypoint-"$APP" ./entrypoint + install --strip -t . "$bin" && \ + install -m 'a=r' -t . /project/scripts/docker/*.sh && \ + install -T /project/scripts/docker/entrypoint-"$APP" ./entrypoint ### Final stage FROM ubuntu:${TAG} From fb9da915da23a0628910eb994152164813085138 Mon Sep 17 00:00:00 2001 From: tcely Date: Tue, 30 May 2023 10:27:52 -0400 Subject: [PATCH 3/5] docker functions.sh: set affects only the function args To have the desired effect we need to call set with the determined argument instead. --- scripts/docker/functions.sh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts/docker/functions.sh b/scripts/docker/functions.sh index 941638ea5..ee7e2d930 100644 --- a/scripts/docker/functions.sh +++ b/scripts/docker/functions.sh @@ -5,35 +5,39 @@ print_to_stderr() { printf -- '%s\n' "$@" 1>&2 } -set_args_from_env_addr() { +print_arg() { + printf -- '%s' "$1" +} + +print_arg_from_env_addr() { # Determine IP or domain name case "${ADDR}" in '') print_to_stderr 'Please specify $ADDR environment variable.'; return 1 ;; *[a-zA-Z]*) case "${ADDR}" in - *:*) set -- --ip "${ADDR}" ;; - *) set -- -n "${ADDR}" ;; + *:*) print_arg --ip ;; + *) print_arg -n ;; esac ;; - *) set -- --ip "${ADDR}" ;; + *) print_arg --ip ;; esac return 0 } -append_args_from_env_pass() { +print_arg_from_env_pass() { # Optionally, set password case "${PASS}" in - '') set -- "$@" --no-password ;; - *) set -- "$@" --password "${PASS}" ;; + '') print_arg --no-password ; return 1 ;; + *) print_arg --password ;; esac return 0 } -append_args_from_env_quota() { +print_arg_from_env_quota() { # Set quota case "${QUOTA}" in '') print_to_stderr 'Please specify $QUOTA environment variable.'; return 1 ;; - *) set -- "$@" --quota "${QUOTA}" ;; + *) print_arg --quota ;; esac return 0 } From cc1437f93967c020a6f67183359edffaa2ced406 Mon Sep 17 00:00:00 2001 From: tcely Date: Tue, 30 May 2023 12:18:41 -0400 Subject: [PATCH 4/5] docker entrypoints: use functions.sh * docker entrypoint-smp-server: use functions.sh * docker entrypoint-xftp-server: use functions.sh --- scripts/docker/entrypoint-smp-server | 32 ++++++++----------------- scripts/docker/entrypoint-xftp-server | 34 +++++++++------------------ 2 files changed, 21 insertions(+), 45 deletions(-) diff --git a/scripts/docker/entrypoint-smp-server b/scripts/docker/entrypoint-smp-server index 5817b7b56..c63f5d703 100755 --- a/scripts/docker/entrypoint-smp-server +++ b/scripts/docker/entrypoint-smp-server @@ -2,19 +2,18 @@ confd='/etc/opt/simplex' logd='/var/opt/simplex/' +# Avoid a search of PATH +_shd="$(dirname "$0")" +: "${_shd:=/usr/local/bin}" +. "${_shd}/functions.sh" +unset -v _shd + # Check if server has been initialized 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 ;; - *[a-zA-Z]*) - case "${ADDR}" in - *:*) set -- --ip "${ADDR}" ;; - *) set -- -n "${ADDR}" ;; - esac - ;; - *) set -- --ip "${ADDR}" ;; - esac + _arg1="$(print_arg_from_env_addr)" || exit 1 + set -- "${_arg1}" "${ADDR}" + unset -v _arg1 # Optionally, set password case "${PASS}" in @@ -28,19 +27,8 @@ fi # Backup store log just in case # -# 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 +backup_file_iso8601 "${logd}/smp-server-store.log" # 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 55757401e..024b5cd9e 100755 --- a/scripts/docker/entrypoint-xftp-server +++ b/scripts/docker/entrypoint-xftp-server @@ -2,23 +2,22 @@ confd='/etc/opt/simplex-xftp' logd='/var/opt/simplex-xftp' +# Avoid a search of PATH +_shd="$(dirname "$0")" +: "${_shd:=/usr/local/bin}" +. "${_shd}/functions.sh" +unset -v _shd + # Check if server has been initialized 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 ;; - *[a-zA-Z]*) - case "${ADDR}" in - *:*) set -- --ip "${ADDR}" ;; - *) set -- -n "${ADDR}" ;; - esac - ;; - *) set -- --ip "${ADDR}" ;; - esac + _arg1="$(print_arg_from_env_addr)" || exit 1 + set -- "${_arg1}" "${ADDR}" + unset -v _arg1 # Set quota case "${QUOTA}" in - '') printf 'Please specify $QUOTA environment variable.\n'; exit 1 ;; + '') print_to_stderr 'Please specify $QUOTA environment variable.'; exit 1 ;; *) set -- "$@" --quota "${QUOTA}" ;; esac @@ -28,19 +27,8 @@ fi # Backup store log just in case # -# 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 +backup_file_iso8601 "${logd}/file-server-store.log" # Finally, run xftp-sever. Notice that "exec" here is important: # smp-server replaces our helper script, so that it can catch INT signal From d7994d4d6c6dfa81192b78ab372dd270c6029c4a Mon Sep 17 00:00:00 2001 From: tcely Date: Tue, 30 May 2023 12:23:36 -0400 Subject: [PATCH 5/5] docker functions.sh: remove pass and quota functions Using these didn't improve the situation. They were only used once and made understanding what was happening harder. --- scripts/docker/functions.sh | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/scripts/docker/functions.sh b/scripts/docker/functions.sh index ee7e2d930..dcac5af98 100644 --- a/scripts/docker/functions.sh +++ b/scripts/docker/functions.sh @@ -24,24 +24,6 @@ print_arg_from_env_addr() { return 0 } -print_arg_from_env_pass() { - # Optionally, set password - case "${PASS}" in - '') print_arg --no-password ; return 1 ;; - *) print_arg --password ;; - esac - return 0 -} - -print_arg_from_env_quota() { - # Set quota - case "${QUOTA}" in - '') print_to_stderr 'Please specify $QUOTA environment variable.'; return 1 ;; - *) print_arg --quota ;; - esac - return 0 -} - # Uses the UTC (universal) time zone and this # format: YYYY-mm-dd'T'HH:MM:SS # year, month, day, letter T, hour, minute, second