Last active
March 10, 2020 12:14
-
-
Save rhizoome/0085fbfba516f950e5be9c6ab3b2cc13 to your computer and use it in GitHub Desktop.
Alpine Builder Error to Email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import smtplib | |
import subprocess | |
import sys | |
import time | |
from email.message import EmailMessage | |
import toml | |
TO = "[email protected]" | |
FROM = "[email protected]" | |
SERVER = "localhost" | |
PKGS = { | |
"x", | |
"y", | |
} | |
def send_mail(subject, message, to=TO, server=SERVER, from_=FROM): | |
msg = EmailMessage() | |
msg["Subject"] = subject | |
msg["From"] = from_ | |
msg["To"] = to | |
msg.set_content(message) | |
server = smtplib.SMTP(server) | |
server.send_message(msg) | |
server.quit() | |
def eprint(*args, **kwargs): | |
print(*args, file=sys.stderr, **kwargs) | |
def read(): | |
proc = subprocess.Popen( | |
["mosquitto_sub", "-h", "msg.alpinelinux.org", "-t", "build/+/errors"], | |
stdout=subprocess.PIPE, | |
) | |
stdout = proc.stdout | |
eprint(f"started mosquitto_sub: {proc.pid}") | |
try: | |
while True: | |
line = stdout.readline() | |
eprint(f"got message: {line}") | |
try: | |
msg = json.loads(line) | |
if msg["pkgname"] in PKGS: | |
send_mail("alpine build error", toml.dumps(msg)) | |
eprint("sent mail") | |
except json.JSONDecodeError: | |
eprint("Could not parse") | |
finally: | |
if proc.poll() is None: | |
eprint("terminate mosquitto_sub") | |
proc.terminate() | |
time.sleep(2) | |
if proc.poll() is None: | |
eprint("kill mosquitto_sub") | |
proc.kill() | |
if __name__ == "__main__": | |
read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment