41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""清理所有离线的新Runner,只留在线的"""
|
|
import urllib.request
|
|
import json
|
|
|
|
TOKEN = "1f8058d097e3942a9ed31c44382baf7f08311272"
|
|
BASE = "https://git.xiaoxiajianji.com/api/v1/repos/xiaoxia/xiaoxia-saas/actions/runners"
|
|
|
|
req = urllib.request.Request(BASE, headers={"Authorization": f"token {TOKEN}"})
|
|
with urllib.request.urlopen(req) as resp:
|
|
data = json.loads(resp.read())
|
|
runners = data.get('runners', [])
|
|
offline = [r for r in runners if r.get('status') != 'online']
|
|
print(f"离线Runner {len(offline)}个:")
|
|
for r in offline:
|
|
print(f" ID:{r['id']} {r['name']}")
|
|
|
|
# 删除所有离线的新Runner(ID>=58的旧的都清掉,只保留42,46,47老的在线)
|
|
for r in offline:
|
|
if r['id'] >= 58: # 只清理新的,不动老的
|
|
del_url = f"{BASE}/{r['id']}"
|
|
req = urllib.request.Request(del_url, headers={"Authorization": f"token {TOKEN}"}, method="DELETE")
|
|
try:
|
|
with urllib.request.urlopen(req) as resp:
|
|
print(f" 删除 {r['id']} {r['name']}: 成功")
|
|
except Exception as e:
|
|
print(f" 删除 {r['id']} {r['name']}: 失败 - {e}")
|
|
|
|
# 再次验证
|
|
req = urllib.request.Request(BASE, headers={"Authorization": f"token {TOKEN}"})
|
|
with urllib.request.urlopen(req) as resp:
|
|
data = json.loads(resp.read())
|
|
runners = data.get('runners', [])
|
|
online = [r for r in runners if r.get('status') == 'online']
|
|
offline = [r for r in runners if r.get('status') != 'online']
|
|
print(f"\n最终: 共{len(runners)}个,在线{len(online)}个,离线{len(offline)}个")
|
|
for r in sorted(runners, key=lambda x: x['id']):
|
|
s = "✅在线" if r.get('status') == 'online' else "❌离线"
|
|
labels = [l['name'] for l in r.get('labels', [])]
|
|
print(f" ID:{r['id']:2d} {r['name']:30s} {s} 标签:{labels}")
|