1
0
Fork 0
scripts/sysadmin/letsencrypt_mail.py

65 lines
1.3 KiB
Python
Executable file

#!/usr/bin/python3
# SPDX-FileCopyrightText: 2021 Luca Beltrame <lbeltrame@kde.org>
#
# SPDX-License-Identifier: BSD-3-Clause
from enum import Enum
import fileinput
import drymail # Vendored
SUBJECT = "Certbot certificate renewal - Status: {0}"
HEADER = {"X-Notification-Type": "letsencrypt"}
class State(Enum):
renewed = 1
no_action = 2
error = 3
unknown = 4
def __str__(self):
return f'{self.name.capitalize().replace("_", " ")}'
def main():
state = State.unknown
text = list()
for line in fileinput.input():
line = line.strip()
text.append(line)
if "all renewals succeeded" in line:
state = State.renewed
elif "not due for renewal" in line:
state = State.no_action
elif "unexpected error occurred" in line:
state = State.error
if state is State.no_action:
return
text = "\n".join(text)
subject = SUBJECT.format(str(state))
message = drymail.Message(
sender=("Certbot renewal bot", "notify@dennogumi.org"),
receivers=["root"],
subject=subject,
headers=HEADER,
text=text
)
client = drymail.SMTPMailer(
host='localhost')
client.send(message)
if __name__ == "__main__":
main()