🎨 — 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):
"""Mail configuration"""
mailfrom: EmailStr
host: Optional[str] = None
port: Optional[PositiveInt] = None
ssl: Optional[StrictBool] = None
starttls: Optional[StrictBool] = None
host: str = '127.0.0.1'
port: PositiveInt = 25
ssl: StrictBool = False
starttls: StrictBool = False
auth: Optional[MailAuth] = None
addresses: List[EmailStr]

View file

@ -4,16 +4,17 @@ import smtplib
from urllib.parse import urlparse
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 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"""
msg = f"task={task.id}, status={result.status}, {severity=}"
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')
msg = f"""\
@ -24,57 +25,32 @@ Check: {task.check}
Status: {severity}
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:
"""Notify by mail
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']:
def notify_by_mail(msg: str, config: Mail) -> None:
if config.ssl:
logger.debug('Mail notification: SSL')
context = ssl.create_default_context()
smtp = smtplib.SMTP_SSL(host=config['host'],
port=config['port'],
smtp = smtplib.SMTP_SSL(host=config.host,
port=config.port,
context=context)
else:
smtp = smtplib.SMTP(host=config['host'], # type: ignore
port=config['port'])
if config['starttls']:
smtp = smtplib.SMTP(host=config.host, # type: ignore
port=config.port)
if config.starttls:
logger.debug('Mail notification: STARTTLS')
context = ssl.create_default_context()
smtp.starttls(context=context)
if 'auth' in config:
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
if config.auth is not None:
logger.debug('Mail notification: authentification')
smtp.login(config['auth']['login'],
config['auth']['password'])
smtp.login(config.auth.login,
config.auth.password)
for address in config['addresses']:
for address in config.addresses:
logger.debug('Sending mail to %s', address)
logger.debug(msg)
smtp.sendmail(config['from'], address, msg)
smtp.sendmail(config.mailfrom, address, msg)
return None