infrastructure/visualize.py

102 lines
2.6 KiB
Python
Raw Normal View History

2024-10-14 15:49:44 +00:00
#!/usr/bin/env python
import json
import sys
2024-10-14 20:11:20 +00:00
import jinja2
2024-10-14 15:49:44 +00:00
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,
}
2024-10-14 18:53:13 +00:00
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}'
2024-10-14 15:49:44 +00:00
def service_key(svc, data):
return f"'serguzim.net'.{data['host']}.{svc}"
2024-10-14 20:11:20 +00:00
def host_key(host):
return f"'serguzim.net'.{host}"
2024-10-14 15:49:44 +00:00
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 = []
2024-10-14 20:11:20 +00:00
for host, data in hosts.items():
result.append({
'key': host_key(host),
'name': host,
})
2024-10-14 15:49:44 +00:00
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 = []
2024-10-14 20:11:20 +00:00
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)
2024-10-14 15:49:44 +00:00
return result
if __name__ == '__main__':
hosts = []
services = []
data = json.loads(sys.stdin.read())
2024-10-14 20:11:20 +00:00
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)