Improve visualize.py to load data directly

This commit is contained in:
Tobias Reisinger 2024-10-19 18:41:29 +02:00
parent fd2eec6683
commit 0347efcb38
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 12 additions and 14 deletions

View file

@ -55,8 +55,5 @@ all:
ansible-playbook ./playbooks/serguzim.net.yml -t $(TAGS) ansible-playbook ./playbooks/serguzim.net.yml -t $(TAGS)
visualize: visualize:
tofu output --json \ ./visualize.py | d2 - infrastructure.svg
| jq 'with_entries(.value |= .value)' \
| ./visualize.py \
| d2 - infrastructure.svg

View file

@ -7,5 +7,6 @@ mkShell {
dnscontrol dnscontrol
opentofu opentofu
python3Packages.jinja2 python3Packages.jinja2
python3Packages.bc-python-hcl2
]; ];
} }

View file

@ -29,7 +29,7 @@ external: {
} }
{% for backup in svc.backup or [] %} {% for backup in svc.backup or [] %}
{{ svc.key }} -> external.restic.{{ svc.host }}: backup.name { {{ svc.key }} -> external.restic.{{ svc.host }}: {{ backup.name }} {
style: { style: {
stroke: "#0f0" stroke: "#0f0"
stroke-dash: 3 stroke-dash: 3

View file

@ -4,6 +4,7 @@ import json
import sys import sys
import jinja2 import jinja2
import hcl2
icon_overrides = { icon_overrides = {
"acme_dns": "lets-encrypt", "acme_dns": "lets-encrypt",
@ -38,9 +39,8 @@ def service_key(svc, data, hosts):
def service_key_find(svc_name, services, hosts): def service_key_find(svc_name, services, hosts):
for svc, data in services.items(): if svc := services.get(svc_name):
if svc == svc_name: return service_key(svc_name, svc, hosts)
return service_key(svc, data, hosts)
return None return None
def parse_hosts(hosts): def parse_hosts(hosts):
@ -74,13 +74,13 @@ def parse_services(services, hosts):
return result return result
if __name__ == '__main__': if __name__ == '__main__':
hosts = [] hosts = {}
services = [] services = {}
data = json.loads(sys.stdin.read()) with open('./hosts.auto.tfvars', 'r') as file:
hosts = hcl2.load(file)["hosts"][0]
services = data["services"] with open('./services.auto.tfvars', 'r') as file:
hosts = data["hosts"] services = hcl2.load(file)["services"][0]
db_key = service_key_find("postgresql", services, hosts) db_key = service_key_find("postgresql", services, hosts)
auth_key = service_key_find("authentik", services, hosts) auth_key = service_key_find("authentik", services, hosts)