dst/dst.py

77 lines
4.3 KiB
Python
Raw Permalink Normal View History

2020-03-14 10:25:27 +00:00
import docker
import json
import time
import os
2020-03-14 16:38:13 +00:00
from itertools import groupby
2020-03-14 10:25:27 +00:00
2020-03-14 14:48:59 +00:00
title = os.environ.get("DST_TITLE", "Docker Status")
caption = os.environ.get("DST_CAPTION", 'Status page using <a href="https://git.wadza.fr/me/dst">dst</a>')
path = os.environ.get("DST_PATH", "/data")
2020-03-14 10:25:27 +00:00
interval = int(os.environ.get("DST_INTERVAL", "30"))
filter_label = os.environ.get("DST_FILTER_LABEL", False)
filters = {}
if filter_label:
filters["label"] = filter_label
name_label = os.environ.get("DST_NAME_LABEL", False)
2020-03-14 16:38:13 +00:00
group_label = os.environ.get("DST_GROUP_LABEL", False)
2020-03-14 10:25:27 +00:00
url_label = os.environ.get("DST_URL_LABEL", False)
2020-03-14 16:38:13 +00:00
css = 'body{padding:0;max-width:50em;margin:auto;background:#f2f2f2;box-shadow:0 0.5rem 1rem rgba(0, 0, 0, 0.2);font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"}.content{background:#fcfcfc;padding:1rem;text-align:center}footer,header{text-align:center}article{margin:1rem}h2{margin-bottom:0}section{margin-bottom:.5rem;border:1px #eee solid;border-radius:4px;background:white;display:flex;padding:0.5rem 1rem}section *{margin:0}h3{flex-grow:1;text-align:left}h3 a{text-decoration:none}section p{align-self:center}.health,.status{text-transform:capitalize;min-width:5em}.health::after,.status::after{font-size:1.5rem;vertical-align:middle}.health-none,.status-created,.status-paused{color:cornflowerblue}.health-healthy,.status-running{color:#7ED321}.health-healthy::after,.status-running::after{content:"\\2714"}.health-starting,.status-removing,.status-restarting{color:#ffbf00}.health-starting::after,.status-restarting::after{content:"\\21bb"}.status-removing::after{content:"\\21af"}.health-unhealthy,.status-dead,.status-exited{color:orangered}.health-unhealthy::after,.status-dead::after,.status-exited::after{content:"\\2718"}a{color:black}'
2020-03-14 14:48:59 +00:00
header = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{0}</title><link rel="stylesheet" href="style.css"></head><body><div class="content"><header><h1>{0}</h1><h5>{1}</h5></header>'
2020-03-14 16:38:13 +00:00
article = '<article><h2>{}</h2>{}</article>'
section = '<section><h3><a href="{1}">{0}</a></h3><p class="status status-{2}" title="{3}">{2}</p><p class="health health-{4}" title="{5}">{4}</p></section>'
2020-03-14 14:48:59 +00:00
footer = '<footer><a href="https://git.wadza.fr/me/dst">Docker Status</a></footer></div></body></html>'
2020-03-14 10:25:27 +00:00
client = docker.from_env()
def formatContainer(container):
state = container.attrs.get("State", {})
health = state.get("Health", {})
return {
"name": container.labels.get(name_label, container.name),
2020-03-14 16:38:13 +00:00
"group": container.labels.get(group_label, ""),
2020-03-14 14:48:59 +00:00
"url": container.labels.get(url_label, ""),
2020-03-14 10:25:27 +00:00
"status": container.status,
"start": state.get("StartedAt"),
"stop": state.get("FinishedAt"),
"health": {
"status": health.get("Status", "none"),
"fails": health.get("FailingStreak", 0)
}
}
2020-03-14 16:38:13 +00:00
def byGroup(l):
return l.get("group")
2020-03-14 10:25:27 +00:00
def loop():
while True:
try:
report = list(
map(formatContainer,
client.containers.list(all=True, filters=filters)))
2020-03-14 14:48:59 +00:00
with open(path + "/style.css", 'w') as outfile:
outfile.write(css)
with open(path + "/status.json", 'w') as outfile:
2020-03-14 10:25:27 +00:00
json.dump(report, outfile)
2020-03-14 14:48:59 +00:00
with open(path + "/index.html", 'w') as outfile:
outfile.write(header.format(title, caption))
2020-03-14 16:38:13 +00:00
for (g, ls) in groupby(sorted(report, key=byGroup), key=byGroup):
content = map(lambda l: section.format(
2020-03-14 14:48:59 +00:00
l.get("name"), l.get("url"), l.get("status"),
l.get("stop") if l.get("status") in ["exited", "dead"] else l.get("start"),
2020-03-14 16:38:13 +00:00
l.get("health").get("status"), l.get("health").get("fails")), ls)
outfile.write(article.format(g, ''.join(content)))
2020-03-14 14:48:59 +00:00
outfile.write(footer)
2020-03-14 10:25:27 +00:00
except docker.errors.NotFound:
print('Container bad move. Restarting.')
time.sleep(interval)
try:
loop()
except KeyboardInterrupt:
print('\n\nKeyboard exception received. Exiting.')
exit()