update
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
TAPM_HA_NODE_IN_MAINTENANCE() {
|
||||
local node="${1:-}"
|
||||
local status_file="${2:-/etc/pve/ha/manager_status}"
|
||||
|
||||
[[ "$node" =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$ ]] || return 2
|
||||
[[ -r "$status_file" ]] || return 2
|
||||
TAPM_HA_NODE="$node" python3 - "$status_file" <<'PY'
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
with open(sys.argv[1], encoding="utf-8") as status_handle:
|
||||
status = json.load(status_handle)
|
||||
except (OSError, TypeError, ValueError):
|
||||
raise SystemExit(2)
|
||||
|
||||
node = os.environ["TAPM_HA_NODE"].strip().lower()
|
||||
|
||||
def contains_maintenance(value):
|
||||
if isinstance(value, str):
|
||||
return "maintenance" in value.lower()
|
||||
if isinstance(value, dict):
|
||||
return any(contains_maintenance(item) for item in value.values())
|
||||
if isinstance(value, list):
|
||||
return any(contains_maintenance(item) for item in value)
|
||||
return False
|
||||
|
||||
def node_is_in_maintenance(value):
|
||||
if isinstance(value, dict):
|
||||
for key, item in value.items():
|
||||
if str(key).strip().lower() == node and contains_maintenance(item):
|
||||
return True
|
||||
identity = next(
|
||||
(
|
||||
value.get(key)
|
||||
for key in ("node", "name", "id")
|
||||
if isinstance(value.get(key), str)
|
||||
),
|
||||
"",
|
||||
)
|
||||
if identity.strip().lower() == node and contains_maintenance(value):
|
||||
return True
|
||||
return any(node_is_in_maintenance(item) for item in value.values())
|
||||
if isinstance(value, list):
|
||||
return any(node_is_in_maintenance(item) for item in value)
|
||||
return False
|
||||
|
||||
raise SystemExit(0 if node_is_in_maintenance(status) else 1)
|
||||
PY
|
||||
}
|
||||
Reference in New Issue
Block a user