#!/usr/bin/env python import json import sys import jinja2 import hcl2 icon_overrides = { "acme_dns": "lets-encrypt", "backup": "restic", "dokku": None, "extra_services": None, "forgejo_runner": "forgejo", "healthcheck": "healthchecks", "lego": "lets-encrypt", "mailcowdockerized": "mailcow", "reitanlage_oranienburg": "grav", "tandoor": "tandoor-recipes", "teamspeak_fallback": None, "tinytinyrss": "tiny-tiny-rss", "wiki_js": "wiki-js", "woodpecker": None, } icon_format = { "restic": "webp", "linkwarden": "webp", "telegraf": "webp", "tiny-tiny-rss": "webp", "watchtower": "webp", # TODO revert when icon is fixed } 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 host_key(host, hosts): return f"'serguzim.net'.{hosts[host]["provider"]}.{host}" def service_key(svc, data, hosts): return f"{host_key(data["host"], hosts)}.{svc}" def service_key_find(svc_name, services, hosts): if svc := services.get(svc_name): return service_key(svc_name, svc, hosts) return None def parse_hosts(hosts): result = [] for host, data in hosts.items(): result.append({ 'key': host_key(host, hosts), 'name': host, }) return result def parse_service(svc, data, hosts): svc_key = service_key(svc, data, hosts) domains = [] for dns in data.get("dns") or []: domains.append(f"- {dns['domain']}") data['key'] = svc_key data['host_key'] = host_key(data["host"], hosts) data['label'] = "\\n".join([svc] + domains) data['icon'] = get_icon(svc) return dict(data) def parse_services(services, hosts): result = [] postgresql_key = service_key_find("postgresql", services, hosts) authentik_key = service_key_find("authentik", services, hosts) for svc, data in services.items(): if data["host"] == "*": for host in hosts.keys(): data["host"] = host result.append(parse_service(svc, data, hosts)) else: result.append(parse_service(svc, data, hosts)) return result if __name__ == '__main__': hosts = {} services = {} with open('./hosts.auto.tfvars', 'r') as file: hosts = hcl2.load(file)["hosts"][0] with open('./services.auto.tfvars', 'r') as file: services = hcl2.load(file)["services"][0] db_key = service_key_find("postgresql", services, hosts) auth_key = service_key_find("authentik", services, hosts) monitoring_key = service_key_find("gatus", services, hosts) jinja_loader = jinja2.FileSystemLoader(searchpath="./templates") jinja_env = jinja2.Environment(loader=jinja_loader) template = jinja_env.get_template("infrastructure.d2.j2") print(template.render( grid_svcs=[db_key, auth_key], svcs=parse_services(services, hosts), hosts=parse_hosts(hosts), db_key=db_key, auth_key=auth_key, monitoring_key=monitoring_key, ))