infrastructure/visualize.py

101 lines
2.6 KiB
Python
Executable file

#!/usr/bin/env python
import json
import sys
import jinja2
icon_overrides = {
"acme_dns": "lets-encrypt",
"extra_services": None,
"faas": None,
"forgejo_runner": "forgejo",
"healthcheck": "healthchecks",
"mailcowdockerized": "mailcow",
"reitanlage_oranienburg": "grav",
"tandoor": "tandoor-recipes",
"teamspeak_fallback": None,
"tinytinyrss": "tiny-tiny-rss",
"wiki_js": "wiki-js",
"woodpecker": None,
}
icon_format = {
"linkwarden": "webp",
"telegraf": "webp",
"tiny-tiny-rss": "webp",
}
def get_icon(svc):
svc = icon_overrides.get(svc, svc) or 'docker'
fmt = icon_format.get(svc, 'svg')
return f'https://cdn.jsdelivr.net/gh/selfhst/icons/{fmt}/{svc}.{fmt}'
def service_key(svc, data):
return f"'serguzim.net'.{data['host']}.{svc}"
def host_key(host):
return f"'serguzim.net'.{host}"
def service_key_find(svc_name, services):
for svc, data in services.items():
if svc == svc_name:
return service_key(svc, data)
return None
def parse_hosts(hosts):
result = []
for host, data in hosts.items():
result.append({
'key': host_key(host),
'name': host,
})
return result
def parse_services(services):
result = []
postgresql_key = service_key_find("postgresql", services)
authentik_key = service_key_find("authentik", services)
for svc, data in services.items():
svc_key = service_key(svc, data)
domains = []
for dns in data.get("dns") or []:
domain = ""
if dns.get("target") != "@":
domain += f"{dns["target"]}."
domain += dns['domain']
domains.append(f"- {domain}")
data['key'] = svc_key
data['label'] = "\\n".join([svc] + domains)
data['icon'] = get_icon(svc)
result.append(data)
return result
if __name__ == '__main__':
hosts = []
services = []
data = json.loads(sys.stdin.read())
db_key = service_key_find("postgresql", data["services"])
auth_key = service_key_find("authentik", data["services"])
monitoring_key = service_key_find("gatus", data["services"])
jinja_loader = jinja2.FileSystemLoader(searchpath="./templates")
jinja_env = jinja2.Environment(loader=jinja_loader)
template = jinja_env.get_template("infrastructure.d2.j2")
text = template.render(
grid_svcs=[db_key, auth_key],
svcs=parse_services(data["services"]),
hosts=parse_hosts(data["hosts"]),
db_key=db_key,
auth_key=auth_key,
monitoring_key=monitoring_key,
)
print(text)