2020-11-06 13:04:23 +00:00
|
|
|
#! /bin/bash
|
2016-08-28 19:10:25 +00:00
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
# version v1.0.0
|
2021-02-19 09:37:28 +00:00
|
|
|
# executed manually / via Make
|
2021-01-16 09:16:05 +00:00
|
|
|
# task wrapper for various setup scripts
|
2016-09-01 10:10:23 +00:00
|
|
|
|
2020-12-22 17:09:03 +00:00
|
|
|
SCRIPT='setup.sh'
|
2020-10-04 17:34:15 +00:00
|
|
|
|
2020-09-04 06:42:12 +00:00
|
|
|
set -euEo pipefail
|
2021-09-11 23:29:02 +00:00
|
|
|
trap '__err "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" "${LINENO:-?}" "${?:-?}"' ERR
|
2019-11-22 14:42:25 +00:00
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
function __err
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2021-09-11 23:29:02 +00:00
|
|
|
[[ ${4} -gt 1 ]] && exit 1
|
|
|
|
|
|
|
|
local ERR_MSG='--- \e[31m\e[1mUNCHECKED ERROR\e[0m'
|
|
|
|
ERR_MSG+="\n - script = ${SCRIPT:-${0}}"
|
|
|
|
ERR_MSG+="\n - function = ${1} / ${2}"
|
|
|
|
ERR_MSG+="\n - line = ${3}"
|
|
|
|
ERR_MSG+="\n - exit code = ${4}"
|
|
|
|
ERR_MSG+='\n'
|
|
|
|
|
|
|
|
echo -e "${ERR_MSG}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function _show_local_usage
|
|
|
|
{
|
|
|
|
local WHITE="\e[37m"
|
|
|
|
local ORANGE="\e[38;5;214m"
|
|
|
|
local LBLUE="\e[94m"
|
|
|
|
local RESET="\e[0m"
|
|
|
|
|
|
|
|
# shellcheck disable=SC2059
|
|
|
|
printf "${ORANGE}OPTIONS${RESET}
|
|
|
|
${LBLUE}Config path, container or image adjustments${RESET}
|
|
|
|
-i IMAGE_NAME
|
|
|
|
Provides the name of the docker-mailserver image. The default value is
|
|
|
|
${WHITE}docker.io/mailserver/docker-mailserver:latest${RESET}
|
|
|
|
|
|
|
|
-c CONTAINER_NAME
|
|
|
|
Provides the name of the running container.
|
|
|
|
|
|
|
|
-p PATH
|
|
|
|
Provides the config folder path to the temporary container (does not work if docker-mailserver container already exists).
|
|
|
|
|
|
|
|
${LBLUE}SELinux${RESET}
|
|
|
|
-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.
|
|
|
|
|
|
|
|
${ORANGE}EXIT STATUS${RESET}
|
|
|
|
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.
|
|
|
|
|
|
|
|
"
|
2019-11-22 14:42:25 +00:00
|
|
|
}
|
2019-11-22 15:10:42 +00:00
|
|
|
|
2021-04-07 09:11:54 +00:00
|
|
|
function _get_absolute_script_directory
|
2020-09-06 10:27:40 +00:00
|
|
|
{
|
2021-06-07 12:58:34 +00:00
|
|
|
if [[ "$(uname)" == "Darwin" ]]
|
|
|
|
then
|
|
|
|
readlink() {
|
2021-09-11 23:29:02 +00:00
|
|
|
# requires coreutils
|
|
|
|
greadlink "${@:+$@}"
|
2021-06-07 12:58:34 +00:00
|
|
|
}
|
|
|
|
fi
|
2020-09-06 10:27:40 +00:00
|
|
|
if dirname "$(readlink -f "${0}")" &>/dev/null
|
|
|
|
then
|
2021-04-07 09:11:54 +00:00
|
|
|
DIR="$(dirname "$(readlink -f "${0}")")"
|
2020-09-06 10:27:40 +00:00
|
|
|
elif realpath -e -L "${0}" &>/dev/null
|
|
|
|
then
|
2021-04-07 09:11:54 +00:00
|
|
|
DIR="$(realpath -e -L "${0}")"
|
|
|
|
DIR="${DIR%/setup.sh}"
|
2020-09-06 10:27:40 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-04-07 09:11:54 +00:00
|
|
|
DIR="$(pwd)"
|
|
|
|
_get_absolute_script_directory
|
2020-09-08 17:49:19 +00:00
|
|
|
|
2020-09-04 06:42:12 +00:00
|
|
|
CRI=
|
2021-04-07 09:11:54 +00:00
|
|
|
CONFIG_PATH=
|
2021-01-27 17:42:31 +00:00
|
|
|
CONTAINER_NAME=
|
2021-04-07 09:11:54 +00:00
|
|
|
DEFAULT_CONFIG_PATH="${DIR}/config"
|
|
|
|
IMAGE_NAME=
|
|
|
|
INFO=
|
2020-09-04 06:42:12 +00:00
|
|
|
USE_TTY=
|
2021-04-07 09:11:54 +00:00
|
|
|
USE_SELINUX=
|
|
|
|
VOLUME=
|
|
|
|
WISHED_CONFIG_PATH=
|
2020-09-04 06:42:12 +00:00
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
function _check_root
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ ${EUID} -ne 0 ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2021-09-11 23:29:02 +00:00
|
|
|
echo "Curently, DMS doesn't support podman's rootless mode.
|
|
|
|
Please run this script as root user."
|
|
|
|
|
2021-01-16 09:16:05 +00:00
|
|
|
exit 1
|
2020-09-04 06:42:12 +00:00
|
|
|
fi
|
|
|
|
}
|
2018-04-28 08:27:42 +00:00
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
function _update_config_path
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ -n ${CONTAINER_NAME} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2020-09-09 20:36:00 +00:00
|
|
|
VOLUME=$(${CRI} inspect "${CONTAINER_NAME}" \
|
2019-12-22 08:37:58 +00:00
|
|
|
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
|
2021-09-11 23:29:02 +00:00
|
|
|
grep "/tmp/docker-mailserver$" 2>/dev/null || :)
|
2019-12-22 08:37:58 +00:00
|
|
|
fi
|
2018-04-28 08:27:42 +00:00
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ -n ${VOLUME} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2020-09-06 10:27:40 +00:00
|
|
|
CONFIG_PATH=$(echo "${VOLUME}" | awk '{print $1}')
|
2018-04-28 08:27:42 +00:00
|
|
|
fi
|
|
|
|
}
|
2016-08-28 19:10:25 +00:00
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
function _docker_image_exists
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2021-01-16 09:16:05 +00:00
|
|
|
${CRI} history -q "${1}" &>/dev/null
|
|
|
|
return ${?}
|
2016-09-01 10:10:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
function _docker_image
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2021-09-06 10:13:12 +00:00
|
|
|
# start temporary container with specified image
|
|
|
|
if ! _docker_image_exists "${IMAGE_NAME}"
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2021-09-06 10:13:12 +00:00
|
|
|
echo "Image '${IMAGE_NAME}' not found. Pulling ..."
|
|
|
|
${CRI} pull "${IMAGE_NAME}"
|
2019-07-28 12:33:52 +00:00
|
|
|
fi
|
2021-09-06 10:13:12 +00:00
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
${CRI} run --rm "${USE_TTY}" \
|
2021-09-06 10:13:12 +00:00
|
|
|
-v "${CONFIG_PATH}:/tmp/docker-mailserver${USE_SELINUX}" \
|
2021-09-11 23:29:02 +00:00
|
|
|
"${IMAGE_NAME}" "${@:+$@}"
|
2016-08-28 19:10:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
function _docker_container
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ -n ${CONTAINER_NAME} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2021-06-07 12:58:34 +00:00
|
|
|
${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" "${@:+$@}"
|
2016-09-01 10:10:23 +00:00
|
|
|
else
|
2021-09-11 23:29:02 +00:00
|
|
|
# if no container is running, run a temporary one:
|
|
|
|
# https://github.com/docker-mailserver/docker-mailserver/pull/1874#issuecomment-809781531
|
2021-09-06 10:13:12 +00:00
|
|
|
_docker_image "${@:+$@}"
|
2016-09-01 10:10:23 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
function _main
|
2020-09-04 06:42:12 +00:00
|
|
|
{
|
2021-01-16 09:16:05 +00:00
|
|
|
if command -v docker &>/dev/null
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
|
|
|
CRI=docker
|
2021-01-16 09:16:05 +00:00
|
|
|
elif command -v podman &>/dev/null
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
|
|
|
CRI=podman
|
|
|
|
_check_root
|
|
|
|
else
|
|
|
|
echo "No supported Container Runtime Interface detected."
|
2021-09-11 23:29:02 +00:00
|
|
|
exit 1
|
2018-04-28 08:27:42 +00:00
|
|
|
fi
|
|
|
|
|
2021-01-16 09:16:05 +00:00
|
|
|
INFO=$(${CRI} ps --no-trunc --format "{{.Image}};{{.Names}}" --filter \
|
|
|
|
label=org.opencontainers.image.title="docker-mailserver" | tail -1)
|
2020-09-04 06:42:12 +00:00
|
|
|
|
|
|
|
IMAGE_NAME=${INFO%;*}
|
|
|
|
CONTAINER_NAME=${INFO#*;}
|
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ -z ${IMAGE_NAME} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2021-01-18 19:51:56 +00:00
|
|
|
IMAGE_NAME=${NAME:-docker.io/mailserver/docker-mailserver:latest}
|
2020-09-04 06:42:12 +00:00
|
|
|
fi
|
2016-09-01 10:10:23 +00:00
|
|
|
|
2021-01-16 09:16:05 +00:00
|
|
|
if test -t 0
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
|
|
|
USE_TTY="-ti"
|
2021-01-16 09:16:05 +00: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"
|
2020-09-04 06:42:12 +00:00
|
|
|
fi
|
2016-08-28 19:10:25 +00:00
|
|
|
|
2020-09-04 06:42:12 +00:00
|
|
|
local OPTIND
|
2021-09-11 23:29:02 +00:00
|
|
|
while getopts ":c:i:p:zZ" OPT
|
2020-09-04 06:42:12 +00:00
|
|
|
do
|
2020-09-06 10:27:40 +00:00
|
|
|
case ${OPT} in
|
2021-09-11 23:29:02 +00:00
|
|
|
( i ) IMAGE_NAME="${OPTARG}" ;;
|
|
|
|
( z | Z ) USE_SELINUX=":${OPTARG}" ;;
|
|
|
|
( c ) CONTAINER_NAME="${OPTARG}" ;;
|
2020-11-06 13:04:23 +00:00
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
( p )
|
2020-09-06 10:27:40 +00:00
|
|
|
case "${OPTARG}" in
|
2021-09-11 23:29:02 +00:00
|
|
|
( /* ) WISHED_CONFIG_PATH="${OPTARG}" ;;
|
|
|
|
( * ) WISHED_CONFIG_PATH="${DIR}/${OPTARG}" ;;
|
2020-09-04 06:42:12 +00:00
|
|
|
esac
|
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ ! -d ${WISHED_CONFIG_PATH} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2021-09-11 23:29:02 +00:00
|
|
|
echo "Specified directory '${WISHED_CONFIG_PATH}' doesn't exist" >&2
|
|
|
|
exit 1
|
2020-09-04 06:42:12 +00:00
|
|
|
fi
|
2016-08-28 19:10:25 +00:00
|
|
|
;;
|
2020-11-06 13:04:23 +00:00
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
( * )
|
2021-03-24 19:42:00 +00:00
|
|
|
echo "Invalid option: -${OPT}" >&2
|
2021-09-11 23:29:02 +00:00
|
|
|
exit 1
|
2020-11-06 13:04:23 +00:00
|
|
|
;;
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
esac
|
2020-09-04 06:42:12 +00:00
|
|
|
done
|
2020-11-06 13:04:23 +00:00
|
|
|
|
|
|
|
shift $(( OPTIND - 1 ))
|
2016-08-28 19:10:25 +00:00
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ -z ${WISHED_CONFIG_PATH} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
|
|
|
# no wished config path
|
|
|
|
_update_config_path
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
if [[ -z ${CONFIG_PATH} ]]
|
2020-09-04 06:42:12 +00:00
|
|
|
then
|
2020-09-06 10:27:40 +00:00
|
|
|
CONFIG_PATH=${DEFAULT_CONFIG_PATH}
|
2020-09-04 06:42:12 +00:00
|
|
|
fi
|
|
|
|
else
|
2020-09-06 10:27:40 +00:00
|
|
|
CONFIG_PATH=${WISHED_CONFIG_PATH}
|
2020-09-04 06:42:12 +00:00
|
|
|
fi
|
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
_docker_container setup "${@:+$@}"
|
|
|
|
[[ ${1} == 'help' ]] && _show_local_usage
|
2020-09-04 06:42:12 +00:00
|
|
|
|
2021-09-11 23:29:02 +00:00
|
|
|
return 0
|
2020-09-04 06:42:12 +00:00
|
|
|
}
|
2016-08-28 19:10:25 +00:00
|
|
|
|
2021-06-07 12:58:34 +00:00
|
|
|
_main "${@:+$@}"
|