Added Send mail functionality & Dockerized application #1
No reviewers
Labels
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: Sysdeploy/influx-smtp-gateway#1
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "cis-kuldeep/influx-smtp-gateway:master"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Added Send mail functionality & Dockerized application
@ -0,0 +9,4 @@
ports:
- 8081:8081
env_file:
- ./config.ini
env_file
is for shell variables, it won't work in this case. Instead we need a simpleentrypoint.sh
which will for example format theconfig.ini
@ -0,0 +12,4 @@
- ./config.ini
volumes:
mailsend_server:
we don't need a volume, the app will already be inside the dockerfile
@ -0,0 +3,4 @@
services:
web:
build: .
command: python main.py
perhaps this should be inside the
Dockerfile
In general it's in the direction I wanted.
@ -0,0 +17,4 @@
import cherrypy
PATH = os.path.dirname(os.path.abspath(__file__))
this should go under
__main__
@ -0,0 +24,4 @@
TO_MAIL = CONFIG["app"].get("to_mail")
FROM_MAIL = CONFIG["app"].get("from_mail")
FROM_MAIL_PASSWORD = CONFIG["app"].get("from_mail_password")
print("TO_MAIL: ", TO_MAIL)
debug we don't need
@ -0,0 +29,4 @@
print("FROM_MAIL_PASSWORD: ", FROM_MAIL_PASSWORD)
class CloudInitRequest:
this whole class we don't need
@ -0,0 +89,4 @@
self.hostinfo = (self.remoteip, )
class CloudInitApp:
different name
@ -0,0 +95,4 @@
"""
@staticmethod
def _content_type(data):
remove
@ -0,0 +146,4 @@
ENGINE = cherrypy.engine
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG = {
remove
CONFIG
@ -4,1 +3,4 @@
SMTP gateway accessible from InfluxDB for sending alerts.
# create .env file with following parameters
remove as it's matched with docker service
@ -5,0 +5,4 @@
# create .env file with following parameters
server_host = 0.0.0.0
server_port = 8081
add smtp server name
@ -0,0 +4,4 @@
web:
build: .
ports:
- 8081:8081
If we only use this internally inside a docker service, we should use
expose
, notport
.@ -0,0 +7,4 @@
ports:
- 8081:8081
env_file:
- ./config.ini
wrong file
@ -0,0 +1,77 @@
#!/usr/bin/env python3
"""
Serve cloud init files
change docstring
@ -0,0 +13,4 @@
import cherrypy
class CloudInitApp:
class name
@ -0,0 +15,4 @@
class CloudInitApp:
"""
Serve cloud init files
docstring
@ -0,0 +18,4 @@
import cherrypy
PATH = os.path.dirname(os.path.abspath(__file__))
CONFIG = configparser.ConfigParser()
remove
CONFIG
and only use environment variablesif some variable missing, display a helpful error message and quit with non-zero error code
@ -0,0 +20,4 @@
PATH = os.path.dirname(os.path.abspath(__file__))
CONFIG = configparser.ConfigParser()
CONFIG.read(os.path.join(PATH, "config.ini"))
override from env variables
@ -0,0 +26,4 @@
req_body = json.loads(rawbody)
subject = req_body['subject']
body = req_body['body']
client = smtplib.SMTP('smtp.gmail.com')
port 587
@ -0,0 +45,4 @@
@cherrypy.expose
def send_mail(self):
"""
v1 api endpoint user-data
docstring
@ -0,0 +59,4 @@
TO_MAIL = os.environ["to_mail"]
FROM_MAIL = os.environ["from_mail"]
FROM_MAIL_PASSWORD = os.environ["from_mail_password"]
except: # noqa:E722
except KeyError
@ -0,0 +15,4 @@
# copy project
COPY . .
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
run as non-root
@ -0,0 +1,9 @@
version: '3.8'
services:
web:
smtp-gateway:
@ -0,0 +15,4 @@
# copy project
COPY . .
ENTRYPOINT ["/usr/src/app/entrypoint.sh", "--user"]
this doesn't do anything. The
Dockerfile
should create a non-privileged user and then just beforeENTRYPOINT
have a correspondingUSER
instruction.@ -0,0 +54,4 @@
if __name__ == "__main__":
try:
SERVER_HOST = os.environ["server_host"]
SERVER_PORT = os.environ["server_port"]
SERVER_PORT
should default to587
and be an integer.@ -0,0 +14,4 @@
RUN pip install -r requirements.txt
# add user
RUN adduser --disabled-password --gecos '' newuser
let's call it something else
@ -0,0 +38,4 @@
client.sendmail(msg['From'], msg['To'], msg.as_string())
client.quit()
return "mail sent successfully"
except Exception as e:
ideally an exception per line rather than together, then you can also use more specific exceptions.
@ -0,0 +39,4 @@
client.quit()
return "mail sent successfully"
except Exception as e:
return "some error: {}".format(e)
sleep
, instead look for a way to do this asynchronously@ -0,0 +64,4 @@
client.starttls()
client.ehlo()
client.login(msg["From"], FROM_MAIL_PASSWORD)
except Exception as e:
except smtplib.SMTPException
@ -0,0 +70,4 @@
return {"status": 500, "message": "some error in from mail "
"login: {}".format(e)}
try:
merge with previous
try
/except
@ -0,0 +72,4 @@
try:
client.sendmail(msg['From'], msg['To'], msg.as_string())
client.quit()
quit()
should be infinally:
@ -0,0 +106,4 @@
SMTP_SERVER_HOST = "smtp.gmail.com"
TO_MAIL = "test111@mailinator.com"
FROM_MAIL = "cis.dev393@gmail.com"
FROM_MAIL_PASSWORD = "akeel@123#"
live data shouldn't be here, squash to get rid of it
4cca0e8aa9
toe7fe463b82
@ -0,0 +14,4 @@
import cherrypy
import logging
logging.basicConfig(filename='app.log', filemode='w',
should log to stdout or stderr (research which one is more appropriate)
@ -0,0 +49,4 @@
try:
client = smtplib.SMTP(host=SMTP_SERVER_HOST, port=SMTP_SERVER_PORT)
except Exception as e:
except (SMTPConnectionError, TimeoutError) as e:
Recipient should be possible to specify inside the JSON