From 0347efcb386f3fdc89c349e20ab52fbfda8b1872 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sat, 19 Oct 2024 18:41:29 +0200 Subject: [PATCH] Improve visualize.py to load data directly --- Makefile | 5 +---- shell.nix | 1 + templates/infrastructure.d2.j2 | 2 +- visualize.py | 18 +++++++++--------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 2aa76de..98d3075 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/shell.nix b/shell.nix index ddd2abf..77b4051 100644 --- a/shell.nix +++ b/shell.nix @@ -7,5 +7,6 @@ mkShell { dnscontrol opentofu python3Packages.jinja2 + python3Packages.bc-python-hcl2 ]; } diff --git a/templates/infrastructure.d2.j2 b/templates/infrastructure.d2.j2 index 3e720f1..f898a7c 100644 --- a/templates/infrastructure.d2.j2 +++ b/templates/infrastructure.d2.j2 @@ -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 diff --git a/visualize.py b/visualize.py index 8b1c94d..9b907b5 100755 --- a/visualize.py +++ b/visualize.py @@ -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)