Files
SnarfCode/scripts/update_grafana_manifest.py

98 lines
3.2 KiB
Python

import yaml
import json
def str_presenter(dumper, data):
if '\n' in data:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
yaml.add_representer(str, str_presenter)
yaml.representer.SafeRepresenter.add_representer(str, str_presenter)
with open("homelab-gitops-repo/manifests/aerodrome/grafana/grafana.yaml", "r", encoding="utf-8") as f:
docs = list(yaml.safe_load_all(f.read()))
with open("homelab-gitops-repo/manifests/aerodrome/grafana/systems-dashboard.json", "r", encoding="utf-8") as f:
systems_json = f.read()
with open("homelab-gitops-repo/manifests/aerodrome/grafana/container-metrics-dashboard.json", "r", encoding="utf-8") as f:
containers_json = f.read()
# Deployment is doc 0
deploy = docs[0]
pod_spec = deploy['spec']['template']['spec']
container = pod_spec['containers'][0]
# Add volume mounts if not present
mount_names = [m['name'] for m in container['volumeMounts']]
if 'systems-dashboard' not in mount_names:
container['volumeMounts'].append({
'mountPath': '/var/lib/grafana-dashboards/systems-dashboard.json',
'name': 'systems-dashboard',
'subPath': 'systems-dashboard.json'
})
if 'container-metrics-dashboard' not in mount_names:
container['volumeMounts'].append({
'mountPath': '/var/lib/grafana-dashboards/container-metrics-dashboard.json',
'name': 'container-metrics-dashboard',
'subPath': 'container-metrics-dashboard.json'
})
vol_names = [v['name'] for v in pod_spec['volumes']]
if 'systems-dashboard' not in vol_names:
pod_spec['volumes'].append({
'configMap': {'name': 'grafana-dashboard-systems'},
'name': 'systems-dashboard'
})
if 'container-metrics-dashboard' not in vol_names:
pod_spec['volumes'].append({
'configMap': {'name': 'grafana-dashboard-containers'},
'name': 'container-metrics-dashboard'
})
# Remove existing configmaps with these names if present in docs to update them
docs = [d for d in docs if not (d and d.get('kind') == 'ConfigMap' and d.get('metadata', {}).get('name') in ['grafana-dashboard-systems', 'grafana-dashboard-containers'])]
cm_systems = {
'apiVersion': 'v1',
'kind': 'ConfigMap',
'metadata': {
'name': 'grafana-dashboard-systems',
'namespace': 'default',
'labels': {
'app.kubernetes.io/name': 'grafana',
'app.kubernetes.io/instance': 'grafana'
}
},
'data': {
'systems-dashboard.json': systems_json
}
}
cm_containers = {
'apiVersion': 'v1',
'kind': 'ConfigMap',
'metadata': {
'name': 'grafana-dashboard-containers',
'namespace': 'default',
'labels': {
'app.kubernetes.io/name': 'grafana',
'app.kubernetes.io/instance': 'grafana'
}
},
'data': {
'container-metrics-dashboard.json': containers_json
}
}
docs.append(cm_systems)
docs.append(cm_containers)
with open("homelab-gitops-repo/manifests/aerodrome/grafana/grafana.yaml", "w", encoding="utf-8") as f:
yaml.dump_all(docs, f, sort_keys=False, default_flow_style=False, allow_unicode=True)
print("Updated manifests/aerodrome/grafana/grafana.yaml successfully!")