import paramiko c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect('140.44.4.11', username='root', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10) # 1. Get the prometheus.yml content from local file with open(r'c:\Users\Snarf\OneDrive\Desktop\code\SnarfCode\homelab-gitops-repo\manifests\aerodrome\prometheus\prometheus.yaml', 'r', encoding='utf-8') as f: text = f.read() # Extract prometheus.yml # It is between "prometheus.yml: |" and next "---" start_marker = "prometheus.yml: |" start_idx = text.find(start_marker) end_idx = text.find("\n---", start_idx) raw_yml = text[start_idx + len(start_marker):end_idx] # Unindent 4 spaces from raw_yml unindented_lines = [] for line in raw_yml.splitlines(): if line.startswith(" "): unindented_lines.append(line[4:]) elif not line.strip(): unindented_lines.append("") else: unindented_lines.append(line) prom_clean_content = "\n".join(unindented_lines).strip() + "\n" # Write directly to /tmp/prometheus_new.yml stdin, stdout, stderr = c.exec_command("cat > /tmp/prometheus_new.yml") stdin.write(prom_clean_content) stdin.close() stdout.channel.recv_exit_status() # Update configmap using kubectl create ... --dry-run=client -o yaml | kubectl apply -f - cmd = "kubectl create configmap prometheus-config --from-file=prometheus.yml=/tmp/prometheus_new.yml --dry-run=client -o yaml | kubectl apply -f -" _, o, e = c.exec_command(cmd) print("Create/Apply CM result:\n", o.read().decode().strip()) print("Create/Apply CM err:\n", e.read().decode().strip()) # Verify _, o, _ = c.exec_command("kubectl get configmap prometheus-config -o json") import json data = json.loads(o.read().decode().strip()) prom_yml = data.get("data", {}).get("prometheus.yml", "") print("\nUpdated CM contains shipyard:", "shipyard" in prom_yml) print("Updated CM length:", len(prom_yml)) # Reload prometheus _, o, _ = c.exec_command("kubectl exec deploy/prometheus -- wget -qO- --post-action='' http://localhost:9090/-/reload") print("Reload endpoint:", o.read().decode().strip()) c.close()