feat: add various infrastructure check and automation scripts
This commit is contained in:
40
scripts/salvage_api.py
Normal file
40
scripts/salvage_api.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import paramiko
|
||||
import json
|
||||
|
||||
SSH_KEY = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
|
||||
c = paramiko.SSHClient()
|
||||
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
c.connect("140.44.4.90", username="rancher", key_filename=SSH_KEY, timeout=10)
|
||||
|
||||
def k8s(cmd):
|
||||
full_cmd = f"echo Katzir476! | sudo -S sh -c 'export KUBECONFIG=/etc/rancher/rke2/rke2.yaml; PATH=$PATH:/var/lib/rancher/rke2/bin; {cmd}'"
|
||||
_, o, e = c.exec_command(full_cmd)
|
||||
return o.read().decode().strip(), e.read().decode().strip()
|
||||
|
||||
print("Longhorn services:")
|
||||
out, _ = k8s("kubectl get svc -n longhorn-system")
|
||||
print(out)
|
||||
|
||||
# Call Longhorn API directly to salvage volumes!
|
||||
# Longhorn manager backend exposes port 9500 (longhorn-backend service)
|
||||
out, _ = k8s("kubectl exec -n longhorn-system $(kubectl get pods -n longhorn-system -l app=longhorn-manager -o jsonpath='{.items[0].metadata.name}') -c longhorn-manager -- curl -s http://127.0.0.1:9500/v1/volumes")
|
||||
try:
|
||||
vols = json.loads(out)
|
||||
for v in vols.get("data", []):
|
||||
vname = v["id"]
|
||||
robustness = v.get("robustness")
|
||||
actions = list(v.get("actions", {}).keys())
|
||||
print(f"Volume: {vname}, Robustness: {robustness}, Actions: {actions}")
|
||||
if "salvage" in actions:
|
||||
print(f" --> Executing salvage on {vname}...")
|
||||
# Get replica names
|
||||
reps = [r["name"] for r in v.get("replicas", [])]
|
||||
salvage_payload = json.dumps({"names": reps})
|
||||
salv_cmd = f"kubectl exec -n longhorn-system $(kubectl get pods -n longhorn-system -l app=longhorn-manager -o jsonpath='{{.items[0].metadata.name}}') -c longhorn-manager -- curl -s -X POST -H 'Content-Type: application/json' -d '{salvage_payload}' http://127.0.0.1:9500/v1/volumes/{vname}?action=salvage"
|
||||
s_out, _ = k8s(salv_cmd)
|
||||
print(" Salvage result:", s_out[:200])
|
||||
except Exception as e:
|
||||
print("Error querying Longhorn API:", e, out[:300])
|
||||
|
||||
c.close()
|
||||
|
||||
Reference in New Issue
Block a user