mirror of
https://framagit.org/framasoft/framaspace/argos.git
synced 2025-04-28 18:02:41 +02:00
🎨 — Improve mail alerting code
This commit is contained in:
parent
de1f0cc22a
commit
2ffea278dd
2 changed files with 20 additions and 44 deletions
|
@ -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]
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue