Improve visualize.py to include provider

This commit is contained in:
Tobias Reisinger 2024-10-14 22:53:08 +02:00
parent 20bb2c8fd7
commit b5ddefbd90
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
2 changed files with 22 additions and 19 deletions

View file

@ -3,6 +3,7 @@ output "hosts" {
for key, host in var.hosts : for key, host in var.hosts :
key => { key => {
"hostname" = host.hostname "hostname" = host.hostname
"provider" = host.provider
"fqdn" = "${host.hostname}.serguzim.net" "fqdn" = "${host.hostname}.serguzim.net"
"fqdn_vpn" = "${host.hostname}.vpn.serguzim.net" "fqdn_vpn" = "${host.hostname}.vpn.serguzim.net"
"ipv4_address" = try( "ipv4_address" = try(

View file

@ -31,35 +31,35 @@ def get_icon(svc):
fmt = icon_format.get(svc, 'svg') fmt = icon_format.get(svc, 'svg')
return f'https://cdn.jsdelivr.net/gh/selfhst/icons/{fmt}/{svc}.{fmt}' return f'https://cdn.jsdelivr.net/gh/selfhst/icons/{fmt}/{svc}.{fmt}'
def service_key(svc, data): def host_key(host, hosts):
return f"'serguzim.net'.{data['host']}.{svc}" return f"'serguzim.net'.{hosts[host]["provider"]}.{host}"
def host_key(host): def service_key(svc, data, hosts):
return f"'serguzim.net'.{host}" return f"{host_key(data["host"], hosts)}.{svc}"
def service_key_find(svc_name, services): def service_key_find(svc_name, services, hosts):
for svc, data in services.items(): for svc, data in services.items():
if svc == svc_name: if svc == svc_name:
return service_key(svc, data) return service_key(svc, data, hosts)
return None return None
def parse_hosts(hosts): def parse_hosts(hosts):
result = [] result = []
for host, data in hosts.items(): for host, data in hosts.items():
result.append({ result.append({
'key': host_key(host), 'key': host_key(host, hosts),
'name': host, 'name': host,
}) })
return result return result
def parse_services(services): def parse_services(services, hosts):
result = [] result = []
postgresql_key = service_key_find("postgresql", services) postgresql_key = service_key_find("postgresql", services, hosts)
authentik_key = service_key_find("authentik", services) authentik_key = service_key_find("authentik", services, hosts)
for svc, data in services.items(): for svc, data in services.items():
svc_key = service_key(svc, data) svc_key = service_key(svc, data, hosts)
domains = [] domains = []
for dns in data.get("dns") or []: for dns in data.get("dns") or []:
@ -83,19 +83,21 @@ if __name__ == '__main__':
data = json.loads(sys.stdin.read()) data = json.loads(sys.stdin.read())
db_key = service_key_find("postgresql", data["services"]) services = data["services"]
auth_key = service_key_find("authentik", data["services"]) hosts = data["hosts"]
monitoring_key = service_key_find("gatus", data["services"])
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_loader = jinja2.FileSystemLoader(searchpath="./templates")
jinja_env = jinja2.Environment(loader=jinja_loader) jinja_env = jinja2.Environment(loader=jinja_loader)
template = jinja_env.get_template("infrastructure.d2.j2") template = jinja_env.get_template("infrastructure.d2.j2")
text = template.render( print(template.render(
grid_svcs=[db_key, auth_key], grid_svcs=[db_key, auth_key],
svcs=parse_services(data["services"]), svcs=parse_services(services, hosts),
hosts=parse_hosts(data["hosts"]), hosts=parse_hosts(hosts),
db_key=db_key, db_key=db_key,
auth_key=auth_key, auth_key=auth_key,
monitoring_key=monitoring_key, monitoring_key=monitoring_key,
) ))
print(text)