From 4d90c1df40eef5047dbcf927af36310431c66ae2 Mon Sep 17 00:00:00 2001 From: Arkady Emelyanov Date: Mon, 21 Aug 2017 14:54:45 +0300 Subject: [PATCH] replace nullmailer with msmtp --- Dockerfile.baseimage | 3 +- README.md | 13 +++---- build_scripts/test.image.sh | 2 +- build_scripts/testcase.baseimage.sh | 16 +++++--- system/configure.sh | 9 ++--- system/service.available/sendmail/init | 38 ++++++++++++------- .../sendmail/nullmailer-send/run | 7 ---- 7 files changed, 47 insertions(+), 41 deletions(-) delete mode 100755 system/service.available/sendmail/nullmailer-send/run diff --git a/Dockerfile.baseimage b/Dockerfile.baseimage index 1b12328..e8d4c9b 100644 --- a/Dockerfile.baseimage +++ b/Dockerfile.baseimage @@ -53,7 +53,8 @@ RUN apt-get -qq install -y \ psmisc \ nginx \ bcron \ - nullmailer \ + gettext-base \ + msmtp \ ansible \ net-tools \ curl \ diff --git a/README.md b/README.md index a90cb28..0b6fe7f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ It uses same directory layout for defining services, patched versions of `my_ini * [my_wait](sbin/my_wait) - dependency manager, a-la [wait-for-it](https://github.com/vishnubob/wait-for-it) * [runit](http://smarden.org/runit/) - process supervisor * [nginx](https://nginx.org/) - reverse proxy service, disabled by default -* [nullmailer](https://github.com/bruceg/nullmailer) - smtp relay/sendmail service, disabled by default +* [msmtp](http://msmtp.sourceforge.net/doc/msmtp.html) - smtp relay/sendmail service, disabled by default * [bcron](https://github.com/bruceg/bcron) - cron service, disabled by default ### Container control @@ -137,19 +137,18 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin * * * * * root /app/system_cleanup.sh ``` -#### Sendmail / Nullmailer +#### Sendmail / msmtp -* Repository: [official](https://packages.ubuntu.com/xenial/nullmailer) +* Repository: [official](https://packages.ubuntu.com/xenial/msmtp) * Dockerfile: `RUN /sbin/enable_service sendmail` * Configure: provide environment variables on container start: - * `NULLMAILER_REMOTES` - see section `remotes` from [man page](http://manpages.ubuntu.com/manpages/xenial/man8/nullmailer-send.8.html) - * `NULLMAILER_HOSTNAME` - hostname (default is "docker") + * `SENDMAIL_HOST` - ip address or domain name of SMTP server + * `SENDMAIL_PORT` - override smtp port, default is `25` Sample `docker run` command: ``` docker run --rm \ - -e NULLMAILER_HOSTNAME="example.com" \ - -e NULLMAILER_REMOTES="smtp.example.com smtp --port=25" \ + -e SENDMAIL_HOST="mail.example.com" \ ... ``` diff --git a/build_scripts/test.image.sh b/build_scripts/test.image.sh index 0e2398a..d8c88e7 100755 --- a/build_scripts/test.image.sh +++ b/build_scripts/test.image.sh @@ -17,7 +17,7 @@ echo "Building ${IMAGE_VERSION}" IMAGE_ID=$(docker build --quiet -t "${IMAGE_VERSION}" . -f "./Dockerfile.${IMAGE}_test") echo "==> Starting container: ${IMAGE_VERSION}..." -CONTAINER_ID=$(docker run -d -v ${PWD}/build_scripts:/build_scripts ${IMAGE_ID}) +CONTAINER_ID=$(docker run -d -e SENDMAIL_HOST="mail.example.com" -v ${PWD}/build_scripts:/build_scripts ${IMAGE_ID}) sleep 5 echo "==> Starting testcase: ${TESTCASE}..." diff --git a/build_scripts/testcase.baseimage.sh b/build_scripts/testcase.baseimage.sh index 6b3e6db..99c363d 100755 --- a/build_scripts/testcase.baseimage.sh +++ b/build_scripts/testcase.baseimage.sh @@ -8,20 +8,24 @@ if [ "$?" != 0 ]; then echo "==> FAIL: cron-start failed" exit 1 fi - service=$(/usr/bin/sv -w 0 check /etc/service/cron-update | grep -q '^ok:') if [ "$?" != 0 ]; then echo "==> FAIL: cron-update failed" exit 1 fi -# sendmail -echo "==> Checking sendmail..." -service=$(/usr/bin/sv -w 0 check /etc/service/nullmailer-send | grep -q '^ok:') -if [ "$?" != 0 ]; then - echo "==> FAIL: nullmailer-send failed" +# smtp +echo "==> Checking presence of msmtp configuration file..." +if [ ! -f "/etc/msmtprc" ]; then + echo "==> FAIL: no msmtp configuration installed" exit 1 fi +echo "==> Checking presence of sendmail wrapper..." +if [ ! -f "/usr/sbin/sendmail" ]; then + echo "==> FAIL: no sendmail wrapper installed" + exit 1 +fi + # nginx echo "==> Checking nginx..." diff --git a/system/configure.sh b/system/configure.sh index fd66773..9028b45 100755 --- a/system/configure.sh +++ b/system/configure.sh @@ -10,6 +10,9 @@ set -eo pipefail cp /build/sbin/* /sbin/ && chmod +x /sbin/* cp -r /build/system/service.available /etc/service.available +# .bashrc +cp -f /build/system/bash.rc /root/.bashrc + # # my_init environment # @see https://github.com/phusion/baseimage-docker/tree/master/image @@ -26,11 +29,5 @@ ln -s /usr/bin/pstree /usr/bin/ps ln -s /usr/bin/htop /usr/bin/top ln -s /usr/bin/vim.tiny /usr/bin/vim -# .bashrc -cp -f /build/system/bash.rc /root/.bashrc - - # some mandatory fixes for services mkdir -p /etc/nginx/virtuals -chown mail:root /usr/sbin/nullmailer-queue -chmod u+s /usr/sbin/nullmailer-queue diff --git a/system/service.available/sendmail/init b/system/service.available/sendmail/init index a032688..e93abac 100755 --- a/system/service.available/sendmail/init +++ b/system/service.available/sendmail/init @@ -2,19 +2,31 @@ set -eo pipefail # -# fetching current options +# if SENDMAIL_HOST is set, forward mail to that host. +# if SENDMAIL_PORT is set, use port from environment. # -RELAY="" -HOST="docker" -if [ ! -z "${NULLMAILER_REMOTES}" ]; then RELAY="${NULLMAILER_REMOTES}"; fi -if [ ! -z "${NULLMAILER_HOSTNAME}" ]; then HOST="${NULLMAILER_HOSTNAME}"; fi +if [ -z "${SENDMAIL_HOST}" ]; then + echo "==> Environment variable SENDMAIL_HOST is not set, skipping configuration..." + exit 0 +fi +if [ -z "${SENDMAIL_PORT}" ]; then + SENDMAIL_PORT="25" +fi -mkdir -p /etc/nullmailer -echo "${RELAY}" > "/etc/nullmailer/remotes" -echo "${HOST}" > "/etc/nullmailer/me" -chmod 0600 /etc/nullmailer/remotes /etc/nullmailer/me -chown mail:root /etc/nullmailer/remotes /etc/nullmailer/me +# +# create system-wide configuration file. +# +SENDMAIL_HOST=${SENDMAIL_HOST} \ +SENDMAIL_PORT=${SENDMAIL_PORT} \ +envsubst <<"EOF" > /etc/msmtprc +defaults +account default +auto_from on +host $SENDMAIL_HOST +port $SENDMAIL_PORT +EOF -mkdir -p /var/spool/nullmailer/queue /var/spool/nullmailer/tmp -mkfifo /var/spool/nullmailer/trigger -chown mail:root -R /var/spool/nullmailer +# +# symlink /usr/sbin/sendmail => /usr/bin/msmtp +# +ln -s /usr/bin/msmtp /usr/sbin/sendmail diff --git a/system/service.available/sendmail/nullmailer-send/run b/system/service.available/sendmail/nullmailer-send/run deleted file mode 100755 index 903c7c1..0000000 --- a/system/service.available/sendmail/nullmailer-send/run +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash -set -eo pipefail - -# start nullmailer: -# @see https://github.com/bruceg/nullmailer - -exec /sbin/setuser mail /usr/sbin/nullmailer-send >/dev/null 2>&1