This repository has been archived on 2022-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
docker_systems/archive/docker-mailserver/setup.sh

241 lines
5.9 KiB
Bash
Raw Normal View History

2021-05-17 11:20:02 +02:00
#! /bin/bash
2021-11-22 14:53:17 +01:00
# version v1.0.0
2021-05-17 11:20:02 +02:00
# executed manually / via Make
# task wrapper for various setup scripts
2021-11-22 14:29:20 +01:00
CONFIG_PATH=
2021-05-17 11:20:02 +02:00
CONTAINER_NAME=
2021-11-22 14:53:17 +01:00
CRI=
DEFAULT_CONFIG_PATH=
DESIRED_CONFIG_PATH=
2022-10-17 11:40:23 +02:00
DIR=$(pwd)
2021-11-22 14:53:17 +01:00
DMS_CONFIG='/tmp/docker-mailserver'
2021-11-22 14:29:20 +01:00
IMAGE_NAME=
2021-12-23 18:24:12 +01:00
DEFAULT_IMAGE_NAME='docker.io/mailserver/docker-mailserver:latest'
2021-11-22 14:29:20 +01:00
INFO=
2021-11-22 14:53:17 +01:00
PODMAN_ROOTLESS=false
2021-11-22 14:29:20 +01:00
USE_SELINUX=
2021-11-22 14:53:17 +01:00
USE_TTY=
2021-11-22 14:29:20 +01:00
VOLUME=
2021-05-17 11:20:02 +02:00
2022-10-17 11:40:23 +02:00
RED=$(echo -ne '\e[31m\e[1m')
WHITE=$(echo -ne '\e[37m')
ORANGE=$(echo -ne '\e[38;5;214m')
LBLUE=$(echo -ne '\e[94m')
RESET=$(echo -ne '\e[0m')
2021-05-17 11:20:02 +02:00
2021-11-22 14:53:17 +01:00
set -euEo pipefail
2022-05-05 20:21:09 +02:00
shopt -s inherit_errexit 2>/dev/null || true
2021-11-22 14:53:17 +01:00
trap '__err "${BASH_SOURCE}" "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" "${LINENO:-?}" "${?:-?}"' ERR
2021-05-17 11:20:02 +02:00
2021-11-22 14:53:17 +01:00
function __err
2021-05-17 11:20:02 +02:00
{
2021-11-22 14:53:17 +01:00
[[ ${5} -gt 1 ]] && exit 1
2021-05-17 11:20:02 +02:00
2021-11-22 14:53:17 +01:00
local ERR_MSG="\n--- ${RED}UNCHECKED ERROR${RESET}"
ERR_MSG+="\n - script = ${1}"
ERR_MSG+="\n - function = ${2}"
ERR_MSG+="\n - command = ${3}"
ERR_MSG+="\n - line = ${4}"
ERR_MSG+="\n - exit code = ${5}"
ERR_MSG+='\n\nThis should not have happened. Please file a bug report.\n'
echo -e "${ERR_MSG}"
2021-05-17 11:20:02 +02:00
}
2021-11-22 14:53:17 +01:00
function _show_local_usage
2021-05-17 11:20:02 +02:00
{
# shellcheck disable=SC2059
2022-10-17 11:40:23 +02:00
printf '%s' "${ORANGE}OPTIONS${RESET}
2021-11-22 14:29:20 +01:00
${LBLUE}Config path, container or image adjustments${RESET}
2021-05-17 11:20:02 +02:00
-i IMAGE_NAME
2021-11-22 14:53:17 +01:00
Provides the name of the 'docker-mailserver' image. The default value is
2021-12-23 18:24:12 +01:00
'${WHITE}${DEFAULT_IMAGE_NAME}${RESET}'
2021-05-17 11:20:02 +02:00
-c CONTAINER_NAME
Provides the name of the running container.
-p PATH
2021-11-22 14:53:17 +01:00
Provides the local path of the config folder to the temporary container instance.
Does not work if an existing a 'docker-mailserver' container is already running.
2021-05-17 11:20:02 +02:00
2021-11-22 14:29:20 +01:00
${LBLUE}SELinux${RESET}
2021-05-17 11:20:02 +02:00
-z
Allows container access to the bind mount content that is shared among
multiple containers on a SELinux-enabled host.
-Z
Allows container access to the bind mount content that is private and
unshared with other containers on a SELinux-enabled host.
2021-11-22 14:53:17 +01:00
${LBLUE}Podman${RESET}
-R
Accept running in Podman rootless mode. Ignored when using Docker / Docker Compose.
"
[[ ${1:-} == 'no-exit' ]] && return 0
# shellcheck disable=SC2059
2022-10-17 11:40:23 +02:00
printf '%s' "${ORANGE}EXIT STATUS${RESET}
2021-05-17 11:20:02 +02:00
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
status 1.
"
}
2021-11-22 14:53:17 +01:00
function _get_absolute_script_directory
2021-05-17 11:20:02 +02:00
{
2021-11-22 14:53:17 +01:00
if dirname "$(readlink -f "${0}")" &>/dev/null
then
2022-10-17 11:40:23 +02:00
DIR=$(dirname "$(readlink -f "${0}")")
2021-11-22 14:53:17 +01:00
elif realpath -e -L "${0}" &>/dev/null
then
2022-10-17 11:40:23 +02:00
DIR=$(realpath -e -L "${0}")
2021-11-22 14:53:17 +01:00
DIR="${DIR%/setup.sh}"
fi
2021-12-23 18:24:12 +01:00
}
2021-11-22 14:53:17 +01:00
2021-12-23 18:24:12 +01:00
function _set_default_config_path
{
if [[ -d "${DIR}/config" ]]
then
# legacy path (pre v10.2.0)
DEFAULT_CONFIG_PATH="${DIR}/config"
else
DEFAULT_CONFIG_PATH="${DIR}/docker-data/dms/config"
fi
2021-05-17 11:20:02 +02:00
}
2021-11-22 14:53:17 +01:00
function _handle_config_path
2021-05-17 11:20:02 +02:00
{
2021-11-22 14:53:17 +01:00
if [[ -z ${DESIRED_CONFIG_PATH} ]]
2021-05-17 11:20:02 +02:00
then
2021-11-22 14:53:17 +01:00
# no desired config path
if [[ -n ${CONTAINER_NAME} ]]
2021-05-17 11:20:02 +02:00
then
2021-11-22 14:53:17 +01:00
VOLUME=$(${CRI} inspect "${CONTAINER_NAME}" \
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
grep "${DMS_CONFIG}$" 2>/dev/null || :)
2021-05-17 11:20:02 +02:00
fi
2021-11-22 14:53:17 +01:00
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}
2021-05-17 11:20:02 +02:00
fi
}
2021-11-22 14:53:17 +01:00
function _run_in_new_container
2021-05-17 11:20:02 +02:00
{
2021-11-22 14:53:17 +01:00
# start temporary container with specified image
if ! ${CRI} history -q "${IMAGE_NAME}" &>/dev/null
2021-05-17 11:20:02 +02:00
then
2021-11-22 14:53:17 +01:00
echo "Image '${IMAGE_NAME}' not found. Pulling ..."
${CRI} pull "${IMAGE_NAME}"
2021-05-17 11:20:02 +02:00
fi
2021-11-22 14:53:17 +01:00
${CRI} run --rm "${USE_TTY}" \
-v "${CONFIG_PATH}:${DMS_CONFIG}${USE_SELINUX}" \
2022-05-05 20:21:09 +02:00
"${IMAGE_NAME}" "${@}"
2021-05-17 11:20:02 +02:00
}
function _main
{
2021-11-22 14:53:17 +01:00
_get_absolute_script_directory
2021-12-23 18:24:12 +01:00
_set_default_config_path
2021-11-22 14:53:17 +01:00
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 ))
2021-05-17 11:20:02 +02:00
if command -v docker &>/dev/null
then
CRI=docker
elif command -v podman &>/dev/null
then
CRI=podman
2021-11-22 14:53:17 +01:00
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
2021-05-17 11:20:02 +02:00
else
2021-11-22 14:53:17 +01:00
echo 'No supported Container Runtime Interface detected.'
exit 1
2021-05-17 11:20:02 +02:00
fi
INFO=$(${CRI} ps --no-trunc --format "{{.Image}};{{.Names}}" --filter \
label=org.opencontainers.image.title="docker-mailserver" | tail -1)
2022-10-17 11:40:23 +02:00
[[ -z ${CONTAINER_NAME} ]] && CONTAINER_NAME=${INFO#*;}
2021-11-22 14:53:17 +01:00
[[ -z ${IMAGE_NAME} ]] && IMAGE_NAME=${INFO%;*}
2021-05-17 11:20:02 +02:00
if [[ -z ${IMAGE_NAME} ]]
then
2021-12-23 18:24:12 +01:00
IMAGE_NAME=${NAME:-${DEFAULT_IMAGE_NAME}}
2021-05-17 11:20:02 +02:00
fi
if test -t 0
then
2021-11-22 14:53:17 +01:00
USE_TTY="-it"
2021-05-17 11:20:02 +02:00
else
# GitHub Actions will fail (or really anything else
# lacking an interactive tty) if we don't set a
# value here; "-t" alone works for these cases.
USE_TTY="-t"
fi
2021-11-22 14:53:17 +01:00
_handle_config_path
2021-05-17 11:20:02 +02:00
2021-11-22 14:53:17 +01:00
if [[ -n ${CONTAINER_NAME} ]]
2021-05-17 11:20:02 +02:00
then
2022-05-05 20:21:09 +02:00
${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" setup "${@}"
2021-05-17 11:20:02 +02:00
else
2022-05-05 20:21:09 +02:00
_run_in_new_container setup "${@}"
2021-05-17 11:20:02 +02:00
fi
2022-10-17 11:40:23 +02:00
[[ ${1:-} == 'help' ]] && _show_local_usage
2021-05-17 11:20:02 +02:00
2021-11-22 14:53:17 +01:00
return 0
2021-05-17 11:20:02 +02:00
}
2022-05-05 20:21:09 +02:00
[[ -z ${1:-} ]] && set 'help'
_main "${@}"