-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhf.nodepool.table.sh
More file actions
executable file
·119 lines (106 loc) · 4.12 KB
/
hf.nodepool.table.sh
File metadata and controls
executable file
·119 lines (106 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# List all NodePools for the current cluster in table format
# Usage: hf.nodepool.table.sh [cluster_id]
source "$(dirname "$(realpath "$0")")/hf.lib.sh"
hf_require_config api-url api-version cluster-id
hf_require_jq
CLUSTER_ID=$(hf_cluster_id "${1:-}")
GREEN=$(printf '\033[32m')
RED=$(printf '\033[31m')
YELLOW=$(printf '\033[33m')
RESET=$(printf '\033[0m')
NODEPOOLS=$(hf_get "/clusters/${CLUSTER_ID}/nodepools")
STATUSES_MAP='{}'
while IFS= read -r NP_ID; do
S=$(hf_get "/clusters/${CLUSTER_ID}/nodepools/$NP_ID/statuses" 2>/dev/null || echo '{"items":[]}')
STATUSES_MAP=$(jq -n --argjson m "$STATUSES_MAP" --arg id "$NP_ID" --argjson s "$S" \
'$m + {($id): ($s.items // [])}')
done < <(echo "$NODEPOOLS" | jq -r '.items[].id')
jq -n -r \
--argjson nodepools "$NODEPOOLS" \
--argjson statuses "$STATUSES_MAP" \
--arg green "$GREEN" --arg red "$RED" --arg yellow "$YELLOW" --arg reset "$RESET" '
$nodepools.items as $items |
# Condition types from status.conditions
([$nodepools.items[].status.conditions[]?.type] | unique | map(select(endswith("Successful") | not))) as $ctypes |
# Adapter names from statuses
([($statuses | to_entries[].value[]) | .adapter] | unique) as $adapters |
# Build header row
(["ID", "NAME", "REPLICAS", "TYPE", "GEN"] + $ctypes + $adapters | @tsv),
# Build separator row
(["---", "---", "---", "---", "---"] + ($ctypes | map("---")) + ($adapters | map("---")) | @tsv),
# Build data rows
($items[] |
. as $np |
($statuses[$np.id] // []) as $npstatus |
(if $np.deleted_time != null then "Finalized" else "Available" end) as $ctype |
[.id, .name, (.spec.replicas | tostring), (.spec.platform.type // "-"), ((.generation // 0 | tostring) + (if .deleted_time != null then "\u0004" else "" end))] +
# Condition columns from status.conditions
[$ctypes[] as $t |
(($np.status.conditions // []) | map(select(.type == $t)) | .[0]) as $cond |
if $cond == null then "-"
else
($cond.observed_generation | if . != null then tostring else "" end) as $gen |
if $cond.status == "True" then "" + $gen
elif $cond.status == "False" then "" + $gen
elif $cond.status == "Unknown" then "" + $gen
elif $cond.status == "" or $cond.status == null then "-"
else $cond.status end
end] +
# Adapter columns from statuses
[$adapters[] as $a |
($npstatus | map(select(.adapter == $a)) | .[0]) as $astat |
if $astat == null then "-"
else
($astat.observed_generation | if . != null then tostring else "" end) as $gen |
($astat.conditions | map(select(.type == $ctype)) | .[0].status) as $s |
if $s == "True" then "" + $gen
elif $s == "False" then "" + $gen
elif $s == "Unknown" then "" + $gen
elif $s == null then "-"
else $s end
end]
| @tsv
)
' | awk -v green="$GREEN" -v red="$RED" -v yellow="$YELLOW" -v reset="$RESET" '
BEGIN { FS = "\t" }
function dw(cell, c, gen, pos) {
c = substr(cell, 1, 1)
if (c == "\001" || c == "\002" || c == "\003") {
gen = substr(cell, 2)
return 1 + (gen != "" ? 1 + length(gen) : 0)
}
pos = index(cell, "\004")
if (pos > 0) return (pos - 1) + 3
return length(cell)
}
function render(cell, c, gen, pos) {
c = substr(cell, 1, 1)
if (c == "\001") { gen = substr(cell, 2); return green "●" reset (gen != "" ? " " gen : "") }
if (c == "\002") { gen = substr(cell, 2); return red "●" reset (gen != "" ? " " gen : "") }
if (c == "\003") { gen = substr(cell, 2); return yellow "●" reset (gen != "" ? " " gen : "") }
pos = index(cell, "\004")
if (pos > 0) return substr(cell, 1, pos - 1) " " red "❌" reset
return cell
}
{
row[NR] = $0
n = split($0, f, "\t")
if (n > ncols) ncols = n
for (i = 1; i <= n; i++) {
w = dw(f[i])
if (w > cw[i]) cw[i] = w
}
}
END {
for (r = 1; r <= NR; r++) {
n = split(row[r], f, "\t")
for (i = 1; i <= ncols; i++) {
cell = (i <= n) ? f[i] : ""
pad = cw[i] - dw(cell)
if (i < ncols) printf "%s%*s ", render(cell), pad, ""
else printf "%s", render(cell)
}
printf "\n"
}
}'