From d6aecd66be739a788e0daeec4b26f40c9cf8bd81 Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Sat, 7 Sep 2019 13:57:34 +0200 Subject: [PATCH] added Dockerfile --- Dockerfile | 9 +++++++++ README.md | 16 ++++++++++++++++ receiver.py | 18 +++++++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..219a611 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.7 +WORKDIR /app +COPY receiver.py . +COPY requirements.txt . +RUN python -m pip install -r requirements.txt +CMD ["python", "/app/receiver.py"] + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..254ef2a --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Mail Receiver for Healthcheck + +This is a simple docker container which acts as SMTP backend and forwards pings from mail to healthchecks. + +I made this up, because the docker container I use for healthchecks does not include smtp, and I did not want do maintain this container on my own. + +## Usage: + +Basically run the docker container with the following environment variables set: + +- `PING_URL`: The base url for the ping to healthchecks in the format `https://yourinstance.com/ping/` please do not remove the trailing slash. +- `PING_ID`: The ID to track the health of this script/container itself. +- `PING_TIMEOUT`: The time between pings for the script should be done. +- `MAIL_DOMAIN`: The domain name to accept messages for. + + diff --git a/receiver.py b/receiver.py index 784b768..19481b5 100644 --- a/receiver.py +++ b/receiver.py @@ -3,12 +3,19 @@ import re import requests from aiosmtpd.controller import Controller import os +import logging + +logging.basicConfig(level=logging.INFO) + +_logger = logging.getLogger(__name__) class CustomHandler: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.domain = os.getenv('MAIL_DOMAIN', 'localhost') self.ping_url = os.getenv('PING_URL', 'https://healthcheck.io/ping/') + _logger.debug('Mail domain: %s', self.domain) + _logger.debug('Mail ping url: %s', self.ping_url) async def handle_DATA(self, server, session, envelope): peer = session.peer @@ -16,23 +23,28 @@ class CustomHandler: rcpt_tos = envelope.rcpt_tos data = envelope.content # type: bytes for rcpt in rcpt_tos: - print(rcpt) + _logger.debug('Cecking to address: %s', rcpt) match = re.match(f'([a-zA-Z-_0-9]+)@{self.domain}', rcpt) if match is not None: + _logger.debug('match found: %s', rcpt) found = match break else: + _logger.debug('no match found') return '500 Could not process your message' headers= { 'User-Agent': 'EmailPing from ' + mail_from } - requests.get(f'{self.ping_url}{match.group(1)}', headers=headers) + url = f'{self.ping_url}{match.group(1)}' + _logger.info('Calling Ping url: %s for address: %s', url, mail_from) + requests.get(url, headers=headers) return '250 OK' async def health_check(): ping_url = os.getenv('PING_URL', 'https://healthcheck.io/ping/') ping_id = os.getenv('PING_ID', '101fcaa8-32c5-4281-936f-330412b7afa4') ping_timeout = os.getenv('PING_TIMEOUT', '60') + _logger.info('Reporting own health to: %s', f'{ping_url}{ping_id}') while True: - requests.get(f'{self.ping_url}{ping_id}') + requests.get(f'{ping_url}{ping_id}') await asyncio.sleep(ping_timeout) if __name__ == '__main__':