62 lines
1.8 KiB
Python
Executable File
62 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
"""
|
|
Mermaid Diagram Generator for Homelab Nodes
|
|
This script generates mermaid.js diagrams from node data
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def generate_mermaid_diagram(nodes_data, output_file):
|
|
"""Generate a mermaid diagram from node data"""
|
|
|
|
# Start the mermaid diagram
|
|
mermaid_content = "graph TD\n"
|
|
mermaid_content += " %% Network Infrastructure\n"
|
|
mermaid_content += " subgraph \"Homelab Network\"\n"
|
|
|
|
# Add nodes
|
|
for node in nodes_data.get('nodes', []):
|
|
node_name = node.get('name', 'Unknown')
|
|
ip = node.get('ip', 'Unknown')
|
|
node_type = node.get('type', 'Generic')
|
|
node_id = node_name.replace(' ', '').replace('.', '')
|
|
|
|
mermaid_content += f" {node_id}[{node_name}<br/>{ip}]\n"
|
|
|
|
# Add connections
|
|
for connection in nodes_data.get('connections', []):
|
|
source = connection.get('source', '').replace(' ', '').replace('.', '')
|
|
target = connection.get('target', '').replace(' ', '').replace('.', '')
|
|
description = connection.get('description', 'Connection')
|
|
|
|
mermaid_content += f" {source} -->|{description}| {target}\n"
|
|
|
|
# End the diagram
|
|
mermaid_content += " end\n"
|
|
|
|
# Write to file
|
|
with open(output_file, 'w') as f:
|
|
f.write(mermaid_content)
|
|
|
|
print(f"Mermaid diagram generated: {output_file}")
|
|
|
|
def main():
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python3 generate-mermaid.py <input.json> <output.mmd>")
|
|
return
|
|
|
|
input_file = sys.argv[1]
|
|
output_file = sys.argv[2]
|
|
|
|
# Read input data
|
|
with open(input_file, 'r') as f:
|
|
nodes_data = json.load(f)
|
|
|
|
# Generate diagram
|
|
generate_mermaid_diagram(nodes_data, output_file)
|
|
|
|
if __name__ == "__main__":
|
|
main() |