Update setup script
continuous-integration/drone/push Build is passing Details

pull/2/head
Davide Polonio 2021-01-30 14:49:10 +00:00
parent 9322c638ac
commit e925016fb1
1 changed files with 34 additions and 46 deletions

View File

@ -1,26 +1,22 @@
#! /bin/bash #! /bin/bash
# Wrapper for various setup scripts # version v0.2.4 stable
# included in docker-mailserver # executed manually (via Make)
# task wrapper for various setup scripts
SCRIPT='SETUP' SCRIPT='setup.sh'
set -euEo pipefail set -euEo pipefail
trap '__log_err ${FUNCNAME[0]:-"?"} ${_:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR trap '__log_err ${FUNCNAME[0]:-"?"} ${BASH_COMMAND:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR
trap '_unset_vars || :' EXIT trap '_unset_vars || :' EXIT
function __log_err function __log_err
{ {
local FUNC_NAME LINE EXIT_CODE
FUNC_NAME="${1} / ${2}"
LINE="${3}"
EXIT_CODE="${4}"
printf "\n \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \ printf "\n \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
" script = ${SCRIPT,,}.sh" \ " script = ${SCRIPT:-${0}}" \
" function = ${FUNC_NAME}" \ " function = ${1} / ${2}" \
" line = ${LINE}" \ " line = ${3}" \
" exit code = ${EXIT_CODE}" " exit code = ${4}" >&2
} }
function _unset_vars function _unset_vars
@ -34,7 +30,7 @@ function _get_current_directory
{ {
if dirname "$(readlink -f "${0}")" &>/dev/null if dirname "$(readlink -f "${0}")" &>/dev/null
then then
CDIR="$(cd "$(dirname "$(readlink -f "${0}")")" && pwd)" CDIR="$(dirname "$(readlink -f "${0}")")"
elif realpath -e -L "${0}" &>/dev/null elif realpath -e -L "${0}" &>/dev/null
then then
CDIR="$(realpath -e -L "${0}")" CDIR="$(realpath -e -L "${0}")"
@ -48,7 +44,7 @@ _get_current_directory
CRI= CRI=
INFO= INFO=
IMAGE_NAME= IMAGE_NAME=
CONTAINER_NAME='mail' CONTAINER_NAME=
DEFAULT_CONFIG_PATH="${CDIR}/config" DEFAULT_CONFIG_PATH="${CDIR}/config"
USE_CONTAINER=false USE_CONTAINER=false
WISHED_CONFIG_PATH= WISHED_CONFIG_PATH=
@ -62,7 +58,7 @@ function _check_root
if [[ ${EUID} -ne 0 ]] if [[ ${EUID} -ne 0 ]]
then then
echo "Curently docker-mailserver doesn't support podman's rootless mode, please run this script as root user." echo "Curently docker-mailserver doesn't support podman's rootless mode, please run this script as root user."
return 1 exit 1
fi fi
} }
@ -101,15 +97,15 @@ function _inspect
function _usage function _usage
{ {
echo "${SCRIPT,,}.sh Bootstrapping Script echo "${SCRIPT:-${0}} Bootstrapping Script
Usage: ${0} [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args] Usage: ${0} [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]
OPTIONS: OPTIONS:
-i IMAGE_NAME The name of the docker-mailserver image, by default -i IMAGE_NAME The name of the docker-mailserver image
'tvial/docker-mailserver:latest' for docker, and The default value is
'docker.io/tvial/docker-mailserver:latest' for podman. 'docker.io/mailserver/docker-mailserver:latest'
-c CONTAINER_NAME The name of the running container. -c CONTAINER_NAME The name of the running container.
@ -146,7 +142,7 @@ SUBCOMMANDS:
config: config:
${0} config dkim <keysize> (default: 2048) ${0} config dkim <keysize> (default: 4096) <domain> (optional - for LDAP systems)
${0} config ssl <fqdn> ${0} config ssl <fqdn>
relay: relay:
@ -170,12 +166,8 @@ SUBCOMMANDS:
function _docker_image_exists function _docker_image_exists
{ {
if ${CRI} history -q "${1}" >/dev/null 2>&1 ${CRI} history -q "${1}" &>/dev/null
then return ${?}
return 0
else
return 1
fi
} }
function _docker_image function _docker_image
@ -204,17 +196,17 @@ function _docker_container
then then
${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" "${@}" ${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" "${@}"
else else
echo "The docker-mailserver is not running!" echo "The mailserver is not running!"
exit 5 exit 1
fi fi
} }
function _main function _main
{ {
if [[ -n $(command -v docker) ]] if command -v docker &>/dev/null
then then
CRI=docker CRI=docker
elif [[ -n $(command -v podman) ]] elif command -v podman &>/dev/null
then then
CRI=podman CRI=podman
_check_root _check_root
@ -223,29 +215,25 @@ function _main
exit 10 exit 10
fi fi
INFO=$(${CRI} ps \ INFO=$(${CRI} ps --no-trunc --format "{{.Image}};{{.Names}}" --filter \
--no-trunc \ label=org.opencontainers.image.title="docker-mailserver" | tail -1)
--format "{{.Image}};{{.Names}}" \
--filter label=org.label-schema.name="docker-mailserver" | \
tail -1)
IMAGE_NAME=${INFO%;*} IMAGE_NAME=${INFO%;*}
CONTAINER_NAME=${INFO#*;} CONTAINER_NAME=${INFO#*;}
if [[ -z ${IMAGE_NAME} ]] if [[ -z ${IMAGE_NAME} ]]
then then
if [[ ${CRI} == "docker" ]] IMAGE_NAME=${NAME:-docker.io/mailserver/docker-mailserver:latest}
then
IMAGE_NAME=tvial/docker-mailserver:latest
elif [[ ${CRI} == "podman" ]]
then
IMAGE_NAME=docker.io/tvial/docker-mailserver:latest
fi
fi fi
if tty -s if test -t 0
then then
USE_TTY="-ti" USE_TTY="-ti"
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 fi
local OPTIND local OPTIND
@ -335,7 +323,7 @@ function _main
config) config)
shift ; case ${1:-} in shift ; case ${1:-} in
dkim ) _docker_image generate-dkim-config "${2:-2048}" ;; dkim ) _docker_image generate-dkim-config "${2:-4096}" "${3:-}" ;;
ssl ) _docker_image generate-ssl-certificate "${2}" ;; ssl ) _docker_image generate-ssl-certificate "${2}" ;;
* ) _usage ;; * ) _usage ;;
esac esac