diff --git a/README.md b/README.md index 41d7c50..21a0e29 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,18 @@ DST_PATH | Files output | `/data` DST_INTERVAL | Update interval is seconds | `30` DST_FILTER_LABEL | Filter visible containers with label | `False` DST_NAME_LABEL | Override container name with label | `False` +DST_GROUP_LABEL | Group containers by label | `False` DST_URL_LABEL | Provide url with label | `False` DST_TITLE | Page title | `Docker Status` DST_CAPTION | Page caption ## TODO -[x] Json periodic static -[ ] Static Front -[ ] Groups -[ ] Events -[ ] Gotify +- [x] Json periodic static +- [x] Static Front +- [x] Groups +- [ ] Events +- [ ] Gotify ## Licence Distributed under the GPL3 license. \ No newline at end of file diff --git a/dst.py b/dst.py index f5778b7..31dbc24 100644 --- a/dst.py +++ b/dst.py @@ -2,6 +2,7 @@ import docker import json import time import os +from itertools import groupby title = os.environ.get("DST_TITLE", "Docker Status") caption = os.environ.get("DST_CAPTION", 'Status page using dst') @@ -12,11 +13,13 @@ filters = {} if filter_label: filters["label"] = filter_label name_label = os.environ.get("DST_NAME_LABEL", False) +group_label = os.environ.get("DST_GROUP_LABEL", False) url_label = os.environ.get("DST_URL_LABEL", False) -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}section{margin:1rem;border:1px #eee solid;border-radius:4px;background:white;display:flex;padding:0.5rem 1rem}section *{margin:0}h2{flex-grow:1;text-align:left}h2 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}' +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}' header = '{0}

{0}

{1}
' -section = '

{0}

{2}

{4}

' +article = '

{}

{}
' +section = '

{0}

{2}

{4}

' footer = '
' client = docker.from_env() @@ -26,6 +29,7 @@ def formatContainer(container): health = state.get("Health", {}) return { "name": container.labels.get(name_label, container.name), + "group": container.labels.get(group_label, ""), "url": container.labels.get(url_label, ""), "status": container.status, "start": state.get("StartedAt"), @@ -36,6 +40,9 @@ def formatContainer(container): } } +def byGroup(l): + return l.get("group") + def loop(): while True: try: @@ -51,12 +58,12 @@ def loop(): with open(path + "/index.html", 'w') as outfile: outfile.write(header.format(title, caption)) - for l in report: - outfile.write( - section.format( + for (g, ls) in groupby(sorted(report, key=byGroup), key=byGroup): + content = map(lambda l: section.format( l.get("name"), l.get("url"), l.get("status"), l.get("stop") if l.get("status") in ["exited", "dead"] else l.get("start"), - l.get("health").get("status"), l.get("health").get("fails"))) + l.get("health").get("status"), l.get("health").get("fails")), ls) + outfile.write(article.format(g, ''.join(content))) outfile.write(footer) except docker.errors.NotFound: