1
1
Fork 0
mirror of https://github.com/docker-mailserver/docker-mailserver synced 2024-04-27 05:15:06 +02:00

make setup.sh completely non-interactive (#2201)

Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
This commit is contained in:
Georg Lauterbach 2021-09-21 08:51:59 +02:00 committed by GitHub
parent 8ffbeb1c0f
commit 3b8059f2da
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 106 deletions

206
setup.sh
View File

@ -4,7 +4,20 @@
# executed manually / via Make # executed manually / via Make
# task wrapper for various setup scripts # task wrapper for various setup scripts
DIR="$(pwd)"
CONFIG_PATH=
CONTAINER_NAME=
CRI=
DEFAULT_CONFIG_PATH=
DESIRED_CONFIG_PATH=
DMS_CONFIG='/tmp/docker-mailserver'
IMAGE_NAME=
INFO=
PODMAN_ROOTLESS=false
SCRIPT='setup.sh' SCRIPT='setup.sh'
USE_SELINUX=
USE_TTY=
VOLUME=
set -euEo pipefail set -euEo pipefail
trap '__err "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" "${LINENO:-?}" "${?:-?}"' ERR trap '__err "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" "${LINENO:-?}" "${?:-?}"' ERR
@ -13,12 +26,13 @@ function __err
{ {
[[ ${4} -gt 1 ]] && exit 1 [[ ${4} -gt 1 ]] && exit 1
local ERR_MSG='--- \e[31m\e[1mUNCHECKED ERROR\e[0m' local ERR_MSG='\n--- \e[31m\e[1mUNCHECKED ERROR\e[0m'
ERR_MSG+="\n - script = ${SCRIPT:-${0}}" ERR_MSG+="\n - script = ${SCRIPT:-${0}}"
ERR_MSG+="\n - function = ${1} / ${2}" ERR_MSG+="\n - function = ${1}"
ERR_MSG+="\n - command = ${2}"
ERR_MSG+="\n - line = ${3}" ERR_MSG+="\n - line = ${3}"
ERR_MSG+="\n - exit code = ${4}" ERR_MSG+="\n - exit code = ${4}"
ERR_MSG+='\n' ERR_MSG+='\n\nThis should not have happened. Please file a bug report.\n'
echo -e "${ERR_MSG}" echo -e "${ERR_MSG}"
} }
@ -41,7 +55,8 @@ function _show_local_usage
Provides the name of the running container. Provides the name of the running container.
-p PATH -p PATH
Provides the config folder path to the temporary container (does not work if docker-mailserver container already exists). Provides the local path of the config folder to the temporary container instance.
Does not work if an existing docker-mailserver container is already running.
${LBLUE}SELinux${RESET} ${LBLUE}SELinux${RESET}
-z -z
@ -52,7 +67,16 @@ function _show_local_usage
Allows container access to the bind mount content that is private and Allows container access to the bind mount content that is private and
unshared with other containers on a SELinux-enabled host. unshared with other containers on a SELinux-enabled host.
${ORANGE}EXIT STATUS${RESET} ${LBLUE}Podman${RESET}
-R
Accept running in Podman rootless mode. Ignored when using Docker / Docker Compose.
"
[[ ${1:-} == 'no-exit' ]] && return 0
# shellcheck disable=SC2059
printf "${ORANGE}EXIT STATUS${RESET}
Exit status is 0 if the command was successful. If there was an unexpected error, an error Exit status is 0 if the command was successful. If there was an unexpected error, an error
message is shown describing the error. In case of an error, the script will exit with exit message is shown describing the error. In case of an error, the script will exit with exit
status 1. status 1.
@ -62,13 +86,14 @@ ${ORANGE}EXIT STATUS${RESET}
function _get_absolute_script_directory function _get_absolute_script_directory
{ {
if [[ "$(uname)" == "Darwin" ]] if [[ "$(uname)" == 'Darwin' ]]
then then
readlink() { readlink() {
# requires coreutils # requires coreutils
greadlink "${@:+$@}" greadlink "${@:+$@}"
} }
fi fi
if dirname "$(readlink -f "${0}")" &>/dev/null if dirname "$(readlink -f "${0}")" &>/dev/null
then then
DIR="$(dirname "$(readlink -f "${0}")")" DIR="$(dirname "$(readlink -f "${0}")")"
@ -77,100 +102,107 @@ function _get_absolute_script_directory
DIR="$(realpath -e -L "${0}")" DIR="$(realpath -e -L "${0}")"
DIR="${DIR%/setup.sh}" DIR="${DIR%/setup.sh}"
fi fi
DEFAULT_CONFIG_PATH="${DIR}/config"
} }
DIR="$(pwd)" function _handle_config_path
_get_absolute_script_directory
CRI=
CONFIG_PATH=
CONTAINER_NAME=
DEFAULT_CONFIG_PATH="${DIR}/config"
IMAGE_NAME=
INFO=
USE_TTY=
USE_SELINUX=
VOLUME=
WISHED_CONFIG_PATH=
function _check_root
{ {
if [[ ${EUID} -ne 0 ]] if [[ -z ${DESIRED_CONFIG_PATH} ]]
then then
echo "Curently, DMS doesn't support podman's rootless mode. # no desired config path
Please run this script as root user." if [[ -n ${CONTAINER_NAME} ]]
then
VOLUME=$(${CRI} inspect "${CONTAINER_NAME}" \
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
grep "${DMS_CONFIG}$" 2>/dev/null || :)
fi
exit 1 if [[ -n ${VOLUME} ]]
then
CONFIG_PATH=$(echo "${VOLUME}" | awk '{print $1}')
fi
if [[ -z ${CONFIG_PATH} ]]
then
CONFIG_PATH=${DEFAULT_CONFIG_PATH}
fi
else
CONFIG_PATH=${DESIRED_CONFIG_PATH}
fi fi
} }
function _update_config_path function _run_in_new_container
{
if [[ -n ${CONTAINER_NAME} ]]
then
VOLUME=$(${CRI} inspect "${CONTAINER_NAME}" \
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
grep "/tmp/docker-mailserver$" 2>/dev/null || :)
fi
if [[ -n ${VOLUME} ]]
then
CONFIG_PATH=$(echo "${VOLUME}" | awk '{print $1}')
fi
}
function _docker_image_exists
{
${CRI} history -q "${1}" &>/dev/null
return ${?}
}
function _docker_image
{ {
# start temporary container with specified image # start temporary container with specified image
if ! _docker_image_exists "${IMAGE_NAME}" if ! ${CRI} history -q "${IMAGE_NAME}" &>/dev/null
then then
echo "Image '${IMAGE_NAME}' not found. Pulling ..." echo "Image '${IMAGE_NAME}' not found. Pulling ..."
${CRI} pull "${IMAGE_NAME}" ${CRI} pull "${IMAGE_NAME}"
fi fi
${CRI} run --rm "${USE_TTY}" \ ${CRI} run --rm "${USE_TTY}" \
-v "${CONFIG_PATH}:/tmp/docker-mailserver${USE_SELINUX}" \ -v "${CONFIG_PATH}:${DMS_CONFIG}${USE_SELINUX}" \
"${IMAGE_NAME}" "${@:+$@}" "${IMAGE_NAME}" "${@:+$@}"
} }
function _docker_container
{
if [[ -n ${CONTAINER_NAME} ]]
then
${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" "${@:+$@}"
else
# if no container is running, run a temporary one:
# https://github.com/docker-mailserver/docker-mailserver/pull/1874#issuecomment-809781531
_docker_image "${@:+$@}"
fi
}
function _main function _main
{ {
_get_absolute_script_directory
local OPTIND
while getopts ":c:i:p:zZR" OPT
do
case ${OPT} in
( i ) IMAGE_NAME="${OPTARG}" ;;
( z | Z ) USE_SELINUX=":${OPT}" ;;
( c ) CONTAINER_NAME="${OPTARG}" ;;
( R ) PODMAN_ROOTLESS=true ;;
( p )
case "${OPTARG}" in
( /* ) DESIRED_CONFIG_PATH="${OPTARG}" ;;
( * ) DESIRED_CONFIG_PATH="${DIR}/${OPTARG}" ;;
esac
if [[ ! -d ${DESIRED_CONFIG_PATH} ]]
then
echo "Specified directory '${DESIRED_CONFIG_PATH}' doesn't exist" >&2
exit 1
fi
;;
( * )
echo "Invalid option: '-${OPTARG}'" >&2
echo -e "Use './setup.sh help' to get a complete overview.\n" >&2
_show_local_usage 'no-exit'
exit 1
;;
esac
done
shift $(( OPTIND - 1 ))
if command -v docker &>/dev/null if command -v docker &>/dev/null
then then
CRI=docker CRI=docker
elif command -v podman &>/dev/null elif command -v podman &>/dev/null
then then
CRI=podman CRI=podman
_check_root if ! ${PODMAN_ROOTLESS} && [[ ${EUID} -ne 0 ]]
then
read -r -p "You are running Podman in rootless mode. Continue? [Y/n] "
[[ -n ${REPLY} ]] && [[ ${REPLY} =~ (n|N) ]] && exit 0
fi
else else
echo "No supported Container Runtime Interface detected." echo 'No supported Container Runtime Interface detected.'
exit 1 exit 1
fi fi
INFO=$(${CRI} ps --no-trunc --format "{{.Image}};{{.Names}}" --filter \ INFO=$(${CRI} ps --no-trunc --format "{{.Image}};{{.Names}}" --filter \
label=org.opencontainers.image.title="docker-mailserver" | tail -1) label=org.opencontainers.image.title="docker-mailserver" | tail -1)
IMAGE_NAME=${INFO%;*}
CONTAINER_NAME=${INFO#*;} CONTAINER_NAME=${INFO#*;}
[[ -z ${IMAGE_NAME} ]] && IMAGE_NAME=${INFO%;*}
if [[ -z ${IMAGE_NAME} ]] if [[ -z ${IMAGE_NAME} ]]
then then
IMAGE_NAME=${NAME:-docker.io/mailserver/docker-mailserver:latest} IMAGE_NAME=${NAME:-docker.io/mailserver/docker-mailserver:latest}
@ -186,51 +218,15 @@ function _main
USE_TTY="-t" USE_TTY="-t"
fi fi
local OPTIND _handle_config_path
while getopts ":c:i:p:zZ" OPT
do
case ${OPT} in
( i ) IMAGE_NAME="${OPTARG}" ;;
( z | Z ) USE_SELINUX=":${OPTARG}" ;;
( c ) CONTAINER_NAME="${OPTARG}" ;;
( p ) if [[ -n ${CONTAINER_NAME} ]]
case "${OPTARG}" in
( /* ) WISHED_CONFIG_PATH="${OPTARG}" ;;
( * ) WISHED_CONFIG_PATH="${DIR}/${OPTARG}" ;;
esac
if [[ ! -d ${WISHED_CONFIG_PATH} ]]
then
echo "Specified directory '${WISHED_CONFIG_PATH}' doesn't exist" >&2
exit 1
fi
;;
( * )
echo "Invalid option: -${OPT}" >&2
exit 1
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ -z ${WISHED_CONFIG_PATH} ]]
then then
# no wished config path ${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" setup "${@:+$@}"
_update_config_path
if [[ -z ${CONFIG_PATH} ]]
then
CONFIG_PATH=${DEFAULT_CONFIG_PATH}
fi
else else
CONFIG_PATH=${WISHED_CONFIG_PATH} _run_in_new_container setup "${@:+$@}"
fi fi
_docker_container setup "${@:+$@}"
[[ ${1} == 'help' ]] && _show_local_usage [[ ${1} == 'help' ]] && _show_local_usage
return 0 return 0

View File

@ -7,7 +7,6 @@
"Exclude": [ "Exclude": [
"^test/", "^test/",
"\\.git.*", "\\.git.*",
"\\.bats$",
"\\.cf$", "\\.cf$",
"\\.conf$", "\\.conf$",
"\\.init$", "\\.init$",