diff --git a/Dockerfile b/Dockerfile index c7b8946..acca4f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,9 @@ -FROM alpine +FROM rawmind/alpine-monit:0.5.20-2 +MAINTAINER Fabio Rauber RUN apk add --no-cache bash postfix postfix-pcre rsyslog COPY conf /etc/postfix COPY rsyslog.conf /etc/rsyslog.conf -COPY start.sh /start.sh - -CMD ["/start.sh"] +COPY monit-service.conf /opt/monit/etc/conf.d diff --git a/conf/postfix-service.sh b/conf/postfix-service.sh new file mode 100755 index 0000000..5d58a24 --- /dev/null +++ b/conf/postfix-service.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +function log { + echo `date` $ME - $@ +} + +function serviceConf { + # Substitute configuration + for VARIABLE in `env | cut -f1 -d=`; do + sed -i "s={{ $VARIABLE }}=${!VARIABLE}=g" /etc/postfix/*.cf + done + + # Override Postfix configuration + if [ -f /overrides/postfix.cf ]; then + while read line; do + postconf -e "$line" + done < /overrides/postfix.cf + echo "Loaded '/overrides/postfix.cf'" + else + echo "No extra postfix settings loaded because optional '/overrides/postfix.cf' not provided." + fi + + # Include table-map files + if ls -A /overrides/*.map 1> /dev/null 2>&1; then + cp /overrides/*.map /etc/postfix/ + postmap /etc/postfix/*.map + rm /etc/postfix/*.map + chown root:root /etc/postfix/*.db + chmod 0600 /etc/postfix/*.db + echo "Loaded 'map files'" + else + echo "No extra map files loaded because optional '/overrides/*.map' not provided." + fi +} + +function serviceStart { + serviceConf + # Actually run Postfix + log "[ Starting Postfix... ]" + rm -f /var/run/rsyslogd.pid + nohup /usr/lib/postfix/master & + echo $! > /var/run/postfix.pid + nohup rsyslogd -n & +} + +function serviceStop { + log "[ Stopping Postfix... ]" + kill `cat /var/run/postfix.pid` +} + +function serviceRestart { + log "[ Restarting Postfix... ]" + serviceStop + serviceStart + /opt/monit/bin/monit reload +} + +export DOMAIN=${DOMAIN:-"localhost"} +export MESSAGE_SIZE_LIMIT=${MESSAGE_SIZE_LIMIT:-"50000000"} +export RELAYNETS=${RELAYNETS:-""} +export RELAYHOST=${RELAYHOST:-""} + +case "$1" in + "start") + serviceStart &>> /proc/1/fd/1 + ;; + "stop") + serviceStop &>> /proc/1/fd/1 + ;; + "restart") + serviceRestart &>> /proc/1/fd/1 + ;; + *) echo "Usage: $0 restart|start|stop" + ;; +esac diff --git a/monit-service.conf b/monit-service.conf new file mode 100644 index 0000000..e23f057 --- /dev/null +++ b/monit-service.conf @@ -0,0 +1,4 @@ +check process master with pidfile /var/run/postfix.pid + start program = "/etc/postfix/postfix-service.sh start" + stop program = "/etc/postfix/postfix-service.sh stop" + if failed port 25 type tcp for 5 cycles then exec "/opt/monit/bin/monit quit" diff --git a/start.sh b/start.sh deleted file mode 100755 index c093143..0000000 --- a/start.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -# Substitute configuration -for VARIABLE in `env | cut -f1 -d=`; do - sed -i "s={{ $VARIABLE }}=${!VARIABLE}=g" /etc/postfix/*.cf -done - -# Override Postfix configuration -if [ -f /overrides/postfix.cf ]; then - while read line; do - postconf -e "$line" - done < /overrides/postfix.cf - echo "Loaded '/overrides/postfix.cf'" -else - echo "No extra postfix settings loaded because optional '/overrides/postfix.cf' not provided." -fi - -# Include table-map files -if ls -A /overrides/*.map 1> /dev/null 2>&1; then - cp /overrides/*.map /etc/postfix/ - postmap /etc/postfix/*.map - rm /etc/postfix/*.map - chown root:root /etc/postfix/*.db - chmod 0600 /etc/postfix/*.db - echo "Loaded 'map files'" -else - echo "No extra map files loaded because optional '/overrides/*.map' not provided." -fi - -# Actually run Postfix -rm -f /var/run/rsyslogd.pid -/usr/lib/postfix/master & -rsyslogd -n