loopingz / aws-smtp-relay

Local SMTP server that convert SMTP message to AWS SES API Call to allow you to use role instance
GNU Lesser General Public License v3.0
53 stars 21 forks source link

Code offer: init scripts #23

Open ThrawnCA opened 5 years ago

ThrawnCA commented 5 years ago

Expected behavior

Starting and stopping the SMTP relay on machine start/stop.

Actual behavior

Steps to reproduce the behavior

/usr/local/sbin/start-aws-smtp-relay.sh:

#!/bin/sh
PIDFILE=/var/run/aws-smtp-relay.pid
if [ -e $PIDFILE ]; then
  echo "Found $PIDFILE - relay already running?"
  ps -p `head -1 $PIDFILE` > /dev/null && exit 1 || echo "Relay process not found; starting..."
fi

java -jar /usr/share/aws-smtp-relay/aws-smtp-relay-1.0.0-jar-with-dependencies.jar -r us-east-1 &
echo $! > $PIDFILE

/usr/local/sbin/stop-aws-smtp-relay.sh:

#!/bin/sh
PIDFILE=/var/run/aws-smtp-relay.pid
if [ -e $PIDFILE ]; then
  head -1 $PIDFILE | xargs kill
  rm $PIDFILE
fi

/etc/init.d/aws-smtp-relay:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          aws-smtp-relay
# Required-Start:    $remote_fs $network $named
# Required-Stop:     $remote_fs
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Relay SMTP traffic to AWS SES
# Description:       Mail relay to convert SMTP traffic to Amazon Simple Email Service API calls.
### END INIT INFO

PIDFILE=/var/run/aws-smtp-relay.pid
case $1 in
  start)
    /bin/sh /usr/local/sbin/start-aws-smtp-relay.sh
  ;;
  stop)
    /bin/sh /usr/local/sbin/stop-aws-smtp-relay.sh
  ;;
  status)
    if [ -e $PIDFILE ]; then
      PID=`head -1 $PIDFILE`
    fi
    if [ "$PID" == "" ]; then
      echo "AWS SMTP relay is not running"
    else
      echo "AWS SMTP relay is running with PID $PID"
    fi
  ;;
  restart)
    /bin/sh /usr/local/sbin/stop-aws-smtp-relay.sh
    /bin/sh /usr/local/sbin/start-aws-smtp-relay.sh
  ;;
esac
exit 0

This could also be adapted to replace the default mail sender, of course, by setting the port to 25 and disabling the other sender.

ThrawnCA commented 5 years ago

initd is getting old, I know, but it's still used by Amazon Linux 1, which appears to be the version you get if you start OpsWorks instances without specifying a custom AMI.

loopingz commented 5 years ago

You can also look at the Pull Request #22

ThrawnCA commented 5 years ago

Nice to know, thanks. The servers we're working on don't use systemd though.

(I did search for existing issues before posting, just didn't check pull requests.)

I'm surprised that more people aren't taking this approach. Google can't seem to find anyone except you guys. Everyone just generates smtp credentials. But rotating those for CIS compliance is a proper pain when you have lots of applications.