feat: add scripts for testing and automating Rancher upgrades and chart checks
This commit is contained in:
25
scripts/check_apis.py
Normal file
25
scripts/check_apis.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = """
|
||||
cd /tmp/rancher-test
|
||||
KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm template rancher ./rancher --namespace cattle-system --set hostname=aerodrome.snarfnet.net --set ingress.tls.source=secret | grep "apiVersion:" | sort | uniq -c
|
||||
"""
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
print("APIs used:")
|
||||
print(out)
|
||||
if err:
|
||||
print("STDERR:", err)
|
||||
|
||||
client.close()
|
||||
21
scripts/check_chart_kubeversions.py
Normal file
21
scripts/check_chart_kubeversions.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
versions = [
|
||||
"2.7.10", "2.8.5", "2.9.3", "2.10.3", "2.11.3", "2.12.3", "2.13.3", "2.14.3", "2.15.3"
|
||||
]
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
for ver in versions:
|
||||
stdin, stdout, stderr = client.exec_command(f"KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm show chart rancher-stable/rancher --version {ver} | grep kubeVersion")
|
||||
out = stdout.read().decode('utf-8').strip()
|
||||
print(f"Rancher {ver}: {out}")
|
||||
|
||||
client.close()
|
||||
20
scripts/check_psp.py
Normal file
20
scripts/check_psp.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = """
|
||||
cd /tmp/rancher-test
|
||||
KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm template rancher ./rancher --namespace cattle-system --set hostname=aerodrome.snarfnet.net --set ingress.tls.source=secret | grep -B 2 -A 8 "policy/v1beta1"
|
||||
"""
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
print(out)
|
||||
client.close()
|
||||
23
scripts/test_dryrun_215.py
Normal file
23
scripts/test_dryrun_215.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = (
|
||||
"KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm upgrade rancher rancher-stable/rancher "
|
||||
"--namespace cattle-system --version 2.15.3 --reuse-values --dry-run"
|
||||
)
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
print("STDOUT:", out[:500])
|
||||
print("STDERR:", err)
|
||||
|
||||
client.close()
|
||||
26
scripts/test_dryrun_285.py
Normal file
26
scripts/test_dryrun_285.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = """
|
||||
rm -rf /tmp/rancher-285 && mkdir -p /tmp/rancher-285 && cd /tmp/rancher-285
|
||||
KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm pull rancher-stable/rancher --version 2.8.5 --untar
|
||||
sed -i '/kubeVersion:/d' rancher/Chart.yaml
|
||||
KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm upgrade rancher ./rancher --namespace cattle-system \
|
||||
--set hostname=aerodrome.snarfnet.net --set ingress.tls.source=secret --dry-run
|
||||
"""
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
print("STDOUT:", out[:400])
|
||||
print("STDERR:", err)
|
||||
|
||||
client.close()
|
||||
23
scripts/test_dryrun_kubeversion.py
Normal file
23
scripts/test_dryrun_kubeversion.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = (
|
||||
"KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm upgrade rancher rancher-stable/rancher "
|
||||
"--namespace cattle-system --version 2.7.10 --reuse-values --dry-run --kube-version 1.26.0"
|
||||
)
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
print("STDOUT:", out[:300])
|
||||
print("STDERR:", err)
|
||||
|
||||
client.close()
|
||||
24
scripts/test_dryrun_resetvalues.py
Normal file
24
scripts/test_dryrun_resetvalues.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = (
|
||||
"KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm upgrade rancher rancher-stable/rancher "
|
||||
"--namespace cattle-system --version 2.15.3 "
|
||||
"--reset-values --set hostname=aerodrome.snarfnet.net --set ingress.tls.source=secret --dry-run"
|
||||
)
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
print("STDOUT:", out[:500])
|
||||
print("STDERR:", err)
|
||||
|
||||
client.close()
|
||||
25
scripts/test_patch_chart.py
Normal file
25
scripts/test_patch_chart.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import paramiko
|
||||
import sys
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
|
||||
client.connect("140.44.4.11", username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
test_cmd = """
|
||||
rm -rf /tmp/rancher-test && mkdir -p /tmp/rancher-test && cd /tmp/rancher-test
|
||||
KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm pull rancher-stable/rancher --version 2.7.10 --untar
|
||||
sed -i '/kubeVersion:/d' rancher/Chart.yaml
|
||||
KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm upgrade rancher ./rancher --namespace cattle-system --reuse-values --dry-run
|
||||
"""
|
||||
|
||||
stdin, stdout, stderr = client.exec_command(test_cmd)
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
print("STDOUT:", out[:400])
|
||||
print("STDERR:", err)
|
||||
|
||||
client.close()
|
||||
67
scripts/upgrade_rancher_step.py
Normal file
67
scripts/upgrade_rancher_step.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import paramiko
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
target_version = sys.argv[1] if len(sys.argv) > 1 else "2.7.10"
|
||||
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
master_ip = "140.44.4.11"
|
||||
|
||||
print(f"==================================================")
|
||||
print(f"Starting Rancher upgrade to version {target_version}")
|
||||
print(f"==================================================")
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
def run_cmd(cmd, check=True):
|
||||
print(f"\n>>> Running: {cmd}")
|
||||
stdin, stdout, stderr = client.exec_command(f"KUBECONFIG=/etc/rancher/k3s/k3s.yaml {cmd}")
|
||||
out = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8')
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
if out:
|
||||
print(out.strip())
|
||||
if err:
|
||||
print(f"STDERR: {err.strip()}")
|
||||
if check and exit_status != 0:
|
||||
raise RuntimeError(f"Command failed with exit code {exit_status}")
|
||||
return out, err, exit_status
|
||||
|
||||
try:
|
||||
client.connect(master_ip, username="root", key_filename=key_path, timeout=15)
|
||||
|
||||
# 1. Take snapshot
|
||||
snap_name = f"pre-rancher-{target_version}-{int(time.time())}"
|
||||
print(f"\n--- Taking etcd snapshot: {snap_name} ---")
|
||||
run_cmd(f"k3s etcd-snapshot save --name {snap_name}")
|
||||
|
||||
# 2. Perform Helm upgrade
|
||||
print(f"\n--- Upgrading Rancher chart to {target_version} ---")
|
||||
upgrade_cmd = (
|
||||
f"helm upgrade rancher rancher-stable/rancher "
|
||||
f"--namespace cattle-system "
|
||||
f"--version {target_version} "
|
||||
f"--reuse-values"
|
||||
)
|
||||
run_cmd(upgrade_cmd)
|
||||
|
||||
# 3. Wait for deployment rollout
|
||||
print("\n--- Waiting for deployment/rancher rollout status ---")
|
||||
run_cmd("kubectl -n cattle-system rollout status deployment/rancher --timeout=360s")
|
||||
|
||||
# 4. Check cattle-system pods
|
||||
print("\n--- Checking cattle-system pods ---")
|
||||
run_cmd("kubectl -n cattle-system get pods -o wide")
|
||||
|
||||
# 5. Check ingress / HTTP response
|
||||
print("\n--- Checking Rancher endpoint ---")
|
||||
run_cmd("curl -k -I -s https://aerodrome.snarfnet.net | head -n 10", check=False)
|
||||
|
||||
client.close()
|
||||
print(f"\nSUCCESS: Rancher upgraded to {target_version} successfully!")
|
||||
|
||||
except Exception as ex:
|
||||
print(f"\nFAILED: {ex}")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user