Skip to content
Snippets Groups Projects
Commit f057ed3c authored by David Trattnig's avatar David Trattnig
Browse files

refact: remove mail service

parent 00946568
No related branches found
No related tags found
1 merge request!21Refact: remove mail service
...@@ -16,24 +16,6 @@ fallback_show_name="${AURA_ENGINE_FALLBACK_SHOW_NAME}" ...@@ -16,24 +16,6 @@ fallback_show_name="${AURA_ENGINE_FALLBACK_SHOW_NAME}"
fallback_show_id="${AURA_ENGINE_FALLBACK_SHOW_ID}" fallback_show_id="${AURA_ENGINE_FALLBACK_SHOW_ID}"
[monitoring] [monitoring]
# Mail server credentials for sending email notifications (Admin and Programme Coordination)
mail_server="mail.aura.radio"
mail_server_port="587"
mail_user="userid_mailbox"
mail_pass="---SECRET--PASSWORD---"
# Set to "true" if you want to notify programme-coordinators about about fallback situations, otherwise "false"
mail_coordinator_enabled="false"
# If you want to address multiple programme-coordinators separate their emails by space
coordinator_mail="programme-coordinator@aura.radio"
# Set to "true" if you want to notify admins about incidents, otherwise "false"
mail_admin_enabled="false"
# If you want to address multiple administrators separate their emails by space
admin_mail="admin@aura.radio"
# The FROM email address used when sending
from_mail="monitoring@aura.radio"
# A subject prefix allows applying filter rules in your mail client
mailsubject_prefix="[AURA Engine]" # default: [AURA Engine]
# Seconds how often the vitality of Engine Core should be checked (default=1) # Seconds how often the vitality of Engine Core should be checked (default=1)
heartbeat_frequency="${AURA_ENGINE_HEARTBEAT_FREQUENCY}" heartbeat_frequency="${AURA_ENGINE_HEARTBEAT_FREQUENCY}"
# Host where heartbeat is sent to (disabled if empty string) # Host where heartbeat is sent to (disabled if empty string)
......
...@@ -16,24 +16,6 @@ fallback_show_name="Random Music" ...@@ -16,24 +16,6 @@ fallback_show_name="Random Music"
fallback_show_id="-1" fallback_show_id="-1"
[monitoring] [monitoring]
# Mail server credentials for sending email notifications (Admin and Programme Coordination)
mail_server="mail.your-radio.org"
mail_server_port="587"
mail_user="aura@subsquare.at"
mail_pass="---SECRET--PASSWORD---"
# Set to "true" if you want to notify programme-coordinators about about fallback situations, otherwise "false"
mail_coordinator_enabled="false"
# If you want to address multiple programme-coordinators separate their emails by space
coordinator_mail="programme-coordinator@your-radio.org"
# Set to "true" if you want to notify admins about incidents, otherwise "false"
mail_admin_enabled="false"
# If you want to address multiple administrators separate their emails by space
admin_mail="david@subsquare.at"
# The FROM email address used when sending
from_mail="monitoring@aura.engine"
# A subject prefix allows applying filter rules in your mail client
mailsubject_prefix="[AURA Engine]" # default: [AURA Engine]
# Seconds how often the vitality of Engine Core should be checked (default=1) # Seconds how often the vitality of Engine Core should be checked (default=1)
heartbeat_frequency=1 heartbeat_frequency=1
# Host where heartbeat is sent to (disabled if empty string) # Host where heartbeat is sent to (disabled if empty string)
......
...@@ -27,7 +27,6 @@ from threading import Thread ...@@ -27,7 +27,6 @@ from threading import Thread
from aura_engine.base.config import AuraConfig from aura_engine.base.config import AuraConfig
from aura_engine.plugins.clock import ClockInfoHandler from aura_engine.plugins.clock import ClockInfoHandler
from aura_engine.plugins.mailer import AuraMailer
from aura_engine.plugins.monitor import AuraMonitor from aura_engine.plugins.monitor import AuraMonitor
...@@ -89,12 +88,6 @@ class EngineEventDispatcher: ...@@ -89,12 +88,6 @@ class EngineEventDispatcher:
self.config = AuraConfig.config() self.config = AuraConfig.config()
self.engine = engine self.engine = engine
binding = self.attach(AuraMailer)
binding.subscribe("on_critical")
binding.subscribe("on_sick")
binding.subscribe("on_resurrect")
binding.subscribe("on_fallback_active")
binding = self.attach(AuraMonitor) binding = self.attach(AuraMonitor)
binding.subscribe("on_boot") binding.subscribe("on_boot")
binding.subscribe("on_sick") binding.subscribe("on_sick")
......
#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-2020 - The Aura Engine Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Send Email notifications.
"""
import logging
import smtplib
from email.message import EmailMessage
from aura_engine.base.config import AuraConfig
from aura_engine.base.utils import SimpleUtil as SU
class MailingException(Exception):
"""
Thrown when some mail cannot be sent.
"""
class AuraMailer:
"""
Event handler to send emails to AURA administrators and programme coordinators.
"""
logger = None
engine = None
mail = None
previous_timeslot = None
def __init__(self, engine):
self.logger = logging.getLogger("engine")
self.engine = engine
self.mail = MailService()
#
# METHODS
#
def on_fallback_active(self, timeslot, fallback_type):
"""
Call when a fallback is activated for the given timeslot.
"""
show = "EMPTY TIMESLOT"
show_id = ""
timeframe = ""
if timeslot:
show = timeslot.show_name
show_id = "The ID of the show is: " + str(timeslot.show_id)
timeframe = SU.fmt_time(timeslot.start_unix) + " - " + SU.fmt_time(timeslot.end_unix)
subject = f"Fallback for show '{show}' activated"
message = "Dear programme coordinator, \n\n"
message += (
f"AURA would like to notify you, that a '{fallback_type}' fallback for show '{show}'"
f" ({timeframe}) just got activated. "
)
message += f"{show_id}\n\nStay tuned!"
self.logger.debug(message)
self.mail.notify_coordinator(subject, message)
def on_sick(self, data):
"""
Call when the engine is in some unhealthy state.
"""
subject = "ERROR - Engine turned into some INVALID STATE!"
message = "There's an issue with your AURA Engine '%s':\n\n%s" % (
data.get("engine_id"),
data.get("status"),
)
self.mail.notify_admin(subject, message)
def on_resurrect(self, data):
"""
Call when the engine turned healthy again after being sick.
"""
subject = "OK - Engine became healthy again"
message = "Good news, things seem fine again with your AURA Engine '%s':\n\n%s" % (
data.get("engine_id"),
data.get("status"),
)
self.mail.notify_admin(subject, message)
def on_critical(self, data):
"""
Call when some critical event occurs.
"""
(subject, message, data) = data
self.mail.notify_admin(subject, message + "\n\n" + str(data))
class MailService:
"""
Service to send emails to Aura administrators.
"""
config = None
logger = None
admin_mails = None
admin_mails_enabled = None
coordinator_mails = None
coordinator_mails_enabled = None
def __init__(self):
self.config = AuraConfig.config()
self.logger = logging.getLogger("engine")
self.admin_mails = self.config.get("admin_mail")
self.admin_mails_enabled = self.config.get("mail_admin_enabled")
self.coordinator_mails = self.config.get("coordinator_mail")
self.coordinator_mails_enabled = self.config.get("mail_coordinator_enabled")
#
# METHODS
#
def notify_admin(self, subject, body):
"""
Send an email to the administrator(s) as defined in the configuration.
Args:
subject (String): The email subject
body (String): The email body text
"""
if self.admin_mails_enabled == "false":
# msg = "No admin mail sent, because doing so is disabled in engine.ini!"
# self.logger.warning(SU.red(msg))
return False
admin_mails = self.admin_mails.split()
for mail_to in admin_mails:
self.send(mail_to, subject, body)
def notify_coordinator(self, subject, body):
"""
Send an email to the programme coordinator(s) as defined in the configuration.
Args:
subject (String): The email subject
body (String): The email body text
"""
if self.coordinator_mails_enabled == "false":
# msg = "No programme coordinator mail sent, because it is disabled in engine.ini!"
# self.logger.warning(SU.yellow(msg))
return False
coordinator_mails = self.coordinator_mails.split()
for mail_to in coordinator_mails:
self.send(mail_to, subject, body)
def send(self, mail_to, subject, body):
"""
Send an email to the given address.
Args:
subject (String): The email's subject
body (String): The email's body text
"""
mail_server = self.config.get("mail_server")
mail_port = self.config.get("mail_server_port")
mail_user = self.config.get("mail_user")
mail_pass = self.config.get("mail_pass")
from_mail = self.config.get("from_mail")
# Check settings
if mail_server == "":
raise MailingException("Mail Server not set")
if mail_port == "":
raise MailingException("Mailserver Port not set")
if mail_user == "":
raise MailingException("Mail user not set")
if mail_pass == "":
raise MailingException("No Password for mailing set")
if from_mail == "":
raise MailingException("From Mail not set")
# Compile the message and ...
msg = EmailMessage()
msg.set_content(body)
mailsubject_prefix = self.config.get("mailsubject_prefix")
if mailsubject_prefix == "":
msg["Subject"] = subject
else:
msg["Subject"] = mailsubject_prefix + " " + subject
msg["From"] = from_mail
msg["To"] = mail_to
# ... send the mail
try:
server = smtplib.SMTP(mail_server, int(mail_port))
server.starttls()
server.login(mail_user, mail_pass)
server.send_message(msg)
server.quit()
except Exception as e:
raise MailingException(str(e))
...@@ -72,7 +72,6 @@ class AuraMonitor: ...@@ -72,7 +72,6 @@ class AuraMonitor:
logger = None logger = None
engine = None engine = None
mailer = None
status = None status = None
already_invalid = None already_invalid = None
engine_id = None engine_id = None
...@@ -371,7 +370,7 @@ class AuraMonitor: ...@@ -371,7 +370,7 @@ class AuraMonitor:
""" """
Retrieve a String identifier consisting of IP and Hostname. Retrieve a String identifier consisting of IP and Hostname.
This is used to differentiate the engine in mails and status broadcasts. This is used to identify engine in status broadcasts.
""" """
host = platform.node() host = platform.node()
return "%s (%s)" % (self.get_ip(), host) return "%s (%s)" % (self.get_ip(), host)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment