🎨 — Improve mail alerting code

This commit is contained in:
Luc Didry 2023-11-28 12:20:49 +01:00
parent de1f0cc22a
commit 2ffea278dd
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C
2 changed files with 20 additions and 44 deletions

View file

@ -102,10 +102,10 @@ class MailAuth(BaseModel):
class Mail(BaseModel): class Mail(BaseModel):
"""Mail configuration""" """Mail configuration"""
mailfrom: EmailStr mailfrom: EmailStr
host: Optional[str] = None host: str = '127.0.0.1'
port: Optional[PositiveInt] = None port: PositiveInt = 25
ssl: Optional[StrictBool] = None ssl: StrictBool = False
starttls: Optional[StrictBool] = None starttls: StrictBool = False
auth: Optional[MailAuth] = None auth: Optional[MailAuth] = None
addresses: List[EmailStr] addresses: List[EmailStr]

View file

@ -4,16 +4,17 @@ import smtplib
from urllib.parse import urlparse from urllib.parse import urlparse
from argos.logging import logger from argos.logging import logger
from argos.schemas.config import Config, Mail
# XXX Implement mail alerts https://framagit.org/framasoft/framaspace/argos/-/issues/15 # XXX Implement mail alerts https://framagit.org/framasoft/framaspace/argos/-/issues/15
# XXX Implement gotify alerts https://framagit.org/framasoft/framaspace/argos/-/issues/16 # XXX Implement gotify alerts https://framagit.org/framasoft/framaspace/argos/-/issues/16
def handle_alert(config, result, task, severity): def handle_alert(config: Config, result, task, severity):
"""Dispatch alert through configured alert channels""" """Dispatch alert through configured alert channels"""
msg = f"task={task.id}, status={result.status}, {severity=}" msg = f"task={task.id}, status={result.status}, {severity=}"
logger.error("Alerting stub: %s", msg) logger.error("Alerting stub: %s", msg)
if 'mail' in config and 'addresses' in config['mail']: if config.general.mail is not None:
logger.debug('Notify of failure by mail') logger.debug('Notify of failure by mail')
msg = f"""\ msg = f"""\
@ -24,57 +25,32 @@ Check: {task.check}
Status: {severity} Status: {severity}
Time: {result.submitted_at} Time: {result.submitted_at}
""" """
notify_by_mail(msg=msg, config=config['mail']) notify_by_mail(msg, config.general.mail)
def notify_by_mail(msg: str, config: dict) -> None: def notify_by_mail(msg: str, config: Mail) -> None:
"""Notify by mail if config.ssl:
Keyword argument:
msg -- string, the mail to send (including subject)
config -- dict, configuration of mail system
"""
if 'mailfrom' not in config:
logger.error('No "mailfrom" address in mail config. '
'Not sending mail.')
return None
if 'host' not in config:
config['host'] = '127.0.0.1'
if 'port' not in config:
config['port'] = 25
if 'ssl' not in config:
config['ssl'] = False
if 'starttls' not in config:
config['starttls'] = False
if config['ssl']:
logger.debug('Mail notification: SSL') logger.debug('Mail notification: SSL')
context = ssl.create_default_context() context = ssl.create_default_context()
smtp = smtplib.SMTP_SSL(host=config['host'], smtp = smtplib.SMTP_SSL(host=config.host,
port=config['port'], port=config.port,
context=context) context=context)
else: else:
smtp = smtplib.SMTP(host=config['host'], # type: ignore smtp = smtplib.SMTP(host=config.host, # type: ignore
port=config['port']) port=config.port)
if config['starttls']: if config.starttls:
logger.debug('Mail notification: STARTTLS') logger.debug('Mail notification: STARTTLS')
context = ssl.create_default_context() context = ssl.create_default_context()
smtp.starttls(context=context) smtp.starttls(context=context)
if 'auth' in config: if config.auth is not None:
if 'login' not in config['auth'] \
or 'password' not in config['auth']:
logger.warning('Mail credentials are incomplete. '
'No mail authentication can be done. '
'Not sending mail.')
return None
logger.debug('Mail notification: authentification') logger.debug('Mail notification: authentification')
smtp.login(config['auth']['login'], smtp.login(config.auth.login,
config['auth']['password']) config.auth.password)
for address in config['addresses']: for address in config.addresses:
logger.debug('Sending mail to %s', address) logger.debug('Sending mail to %s', address)
logger.debug(msg) logger.debug(msg)
smtp.sendmail(config['from'], address, msg) smtp.sendmail(config.mailfrom, address, msg)
return None return None