Files
SnarfCode/scripts/apply_and_set_pihole_pw.py

53 lines
2.0 KiB
Python

import paramiko
import time
import sys
sys.stdout.reconfigure(encoding='utf-8')
key_path = r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet'
def apply_and_update(ip, name, manifest_path):
print(f"\n=======================================================")
print(f"Applying to {name} ({ip})...")
with open(manifest_path, 'r', encoding='utf-8') as f:
content = f.read()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username='root', key_filename=key_path, timeout=10)
stdin, stdout, stderr = ssh.exec_command("k3s kubectl apply -f -")
stdin.write(content)
stdin.channel.shutdown_write()
print("Apply output:", stdout.read().decode())
err = stderr.read().decode()
if err:
print("Apply stderr:", err)
print(f"Waiting for Pi-hole deployment rollout on {name}...")
stdin, stdout, stderr = ssh.exec_command("k3s kubectl rollout status deployment/pihole -n dns --timeout=60s")
print(stdout.read().decode())
# Get new pod name
stdin, stdout, stderr = ssh.exec_command("k3s kubectl get pods -n dns -l app=pihole -o jsonpath='{.items[0].metadata.name}'")
pod_name = stdout.read().decode().strip()
print(f"New Pi-hole pod: {pod_name}")
# Set password to snarfnetadmin
print("Setting Pi-hole password to 'snarfnetadmin'...")
stdin, stdout, stderr = ssh.exec_command(f"k3s kubectl exec -n dns {pod_name} -- pihole setpassword snarfnetadmin")
out = stdout.read().decode()
err = stderr.read().decode()
print("Set password output:", out)
if err:
print("Set password stderr:", err)
ssh.close()
aero_manifest = r"C:\Users\Snarf\OneDrive\Desktop\code\SnarfCode\homelab-gitops-repo\manifests\aerodrome\dns\dns-stack.yaml"
space_manifest = r"C:\Users\Snarf\OneDrive\Desktop\code\SnarfCode\homelab-gitops-repo\manifests\spaceport\dns\dns-stack.yaml"
apply_and_update("140.44.4.11", "Aerodrome", aero_manifest)
apply_and_update("140.44.4.41", "Spaceport", space_manifest)
print("\nDone!")