ci: cleanup honours PVE_TARGET_NODE and the ISO storage var

A pool-scoped token cannot list /nodes, so node discovery fell back to 'pve'
and force-cleanup silently found no VMs to destroy.
This commit is contained in:
Clint Branham
2026-08-23 09:27:27 -05:00
parent 5b896b4681
commit 2897feb43e
@@ -13,9 +13,13 @@ ISO_FILENAME="$4"
TF_DIR="$5"
API_BASE="${PVE_ENDPOINT}/api2/json"
# Discover node name
NODES_JSON=$(curl -sk -H "Authorization: PVEAPIToken=${API_TOKEN}" "${API_BASE}/nodes" 2>/dev/null)
NODE=$(echo "$NODES_JSON" | python3 -c "import json,sys; print(json.load(sys.stdin)['data'][0]['node'])" 2>/dev/null || echo "pve")
# Node: PVE_TARGET_NODE when set (a pool-scoped token cannot list /nodes), else the first node.
NODE="${PVE_TARGET_NODE:-}"
if [ -z "$NODE" ]; then
NODES_JSON=$(curl -sk -H "Authorization: PVEAPIToken=${API_TOKEN}" "${API_BASE}/nodes" 2>/dev/null)
NODE=$(echo "$NODES_JSON" | python3 -c "import json,sys; print(json.load(sys.stdin)['data'][0]['node'])" 2>/dev/null || echo "pve")
fi
ISO_STORAGE="${TF_VAR_iso_storage:-local}"
echo "=== Pre-flight cleanup (node: ${NODE}, vmid: ${VM_ID}) ==="
@@ -53,7 +57,7 @@ if [ -z "$ISO_FILENAME" ]; then
echo "No ISO filename specified, skipping ISO cleanup"
else
ISO_EXISTS=$(curl -sk -H "Authorization: PVEAPIToken=${API_TOKEN}" \
"${API_BASE}/nodes/${NODE}/storage/local/content" 2>/dev/null \
"${API_BASE}/nodes/${NODE}/storage/${ISO_STORAGE}/content" 2>/dev/null \
| python3 -c "
import json, sys
data = json.load(sys.stdin).get('data', [])
@@ -68,7 +72,7 @@ if [ -n "$ISO_EXISTS" ]; then
echo " Deleting..."
ENCODED=$(python3 -c "import urllib.parse; print(urllib.parse.quote('${ISO_EXISTS}', safe=''))")
curl -sk -X DELETE -H "Authorization: PVEAPIToken=${API_TOKEN}" \
"${API_BASE}/nodes/${NODE}/storage/local/content/${ENCODED}" >/dev/null 2>&1
"${API_BASE}/nodes/${NODE}/storage/${ISO_STORAGE}/content/${ENCODED}" >/dev/null 2>&1
sleep 2
echo " ISO cleanup done"
else