Files
SnarfCode/scripts/fix_snarfhome_redirection.py

70 lines
2.9 KiB
Python

import urllib.request
import urllib.parse
import ssl
import json
import paramiko
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# 1. Add DNS A records for snarfhome.snarfnet.net -> 14.4.4.20 on Technitium DNS
for dns_host in ["dns1.snarfnet.net", "dns2.snarfnet.net"]:
try:
login_url = f"https://{dns_host}/api/user/login?user=admin&pass=snarfnetadmin&includeInfo=true"
with urllib.request.urlopen(urllib.request.Request(login_url), context=ctx, timeout=8) as resp:
token = json.loads(resp.read().decode())['token']
for name in ["snarfhome", "homeassistant"]:
params = {
"token": token,
"domain": f"{name}.snarfnet.net",
"zone": "snarfnet.net",
"type": "A",
"ipAddress": "14.4.4.20",
"ptr": "true",
"overwrite": "true",
"ttl": 3600
}
add_url = f"https://{dns_host}/api/zones/records/add?{urllib.parse.urlencode(params)}"
with urllib.request.urlopen(urllib.request.Request(add_url), context=ctx, timeout=8) as resp:
result = json.loads(resp.read().decode())
print(f"[{dns_host}] Added {name}.snarfnet.net -> 14.4.4.20: {result.get('status')}")
except Exception as e:
print(f"[{dns_host}] Error: {e}")
# 2. Disable Redirection Host 1 in NPM DB and remove 1.conf
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("140.44.4.71", username="root", key_filename=r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet", timeout=10)
npm_cleanup = """python3 - << 'EOF'
import sqlite3, os
db = '/var/lib/docker/volumes/nginx-proxy-manager_nginx-data/_data/database.sqlite'
conn = sqlite3.connect(db)
conn.execute("UPDATE redirection_host SET enabled = 0, is_deleted = 1 WHERE id = 1")
conn.commit()
print("Updated database: redirection_host id 1 disabled and marked deleted")
conf_file = '/var/lib/docker/volumes/nginx-proxy-manager_nginx-data/_data/nginx/redirection_host/1.conf'
if os.path.exists(conf_file):
os.remove(conf_file)
print("Removed 1.conf")
EOF
"""
stdin, stdout, stderr = client.exec_command(npm_cleanup)
print(stdout.read().decode())
print(stderr.read().decode())
# 3. Reload nginx on both Swarm NPM nodes
client.exec_command("for id in $(docker ps -q -f name=nginx-proxy-manager_app); do docker exec $id nginx -s reload; done")
client.close()
client2 = paramiko.SSHClient()
client2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client2.connect("140.44.4.72", username="root", key_filename=r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet", timeout=10)
client2.exec_command("for id in $(docker ps -q -f name=nginx-proxy-manager_app); do docker exec $id nginx -s reload; done")
client2.close()
print("Reloaded Nginx across NPM swarm replicas")