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)
visualize:
tofu output --json \
| jq 'with_entries(.value |= .value)' \
| ./visualize.py \
| d2 - infrastructure.svg
./visualize.py | d2 - infrastructure.svg

View file

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

View file

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

View file

@ -4,6 +4,7 @@ import json
import sys
import jinja2
import hcl2
icon_overrides = {
"acme_dns": "lets-encrypt",
@ -38,9 +39,8 @@ def service_key(svc, data, hosts):
def service_key_find(svc_name, services, hosts):
for svc, data in services.items():
if svc == svc_name:
return service_key(svc, data, hosts)
if svc := services.get(svc_name):
return service_key(svc_name, svc, hosts)
return None
def parse_hosts(hosts):
@ -74,13 +74,13 @@ def parse_services(services, hosts):
return result
if __name__ == '__main__':
hosts = []
services = []
hosts = {}
services = {}
data = json.loads(sys.stdin.read())
services = data["services"]
hosts = data["hosts"]
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)