Skip to content

Instantly share code, notes, and snippets.

@ganwell
Last active March 10, 2020 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganwell/0085fbfba516f950e5be9c6ab3b2cc13 to your computer and use it in GitHub Desktop.
Save ganwell/0085fbfba516f950e5be9c6ab3b2cc13 to your computer and use it in GitHub Desktop.
Alpine Builder Error to Email
import json
import smtplib
import subprocess
import sys
import time
from email.message import EmailMessage
import toml
TO = "i@k.l"
FROM = "x@y.z"
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