-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhf.table.sh
More file actions
executable file
·150 lines (131 loc) · 5.23 KB
/
hf.table.sh
File metadata and controls
executable file
·150 lines (131 loc) · 5.23 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# List all clusters with their nodepools in a combined table
source "$(dirname "$(realpath "$0")")/hf.lib.sh"
hf_require_config api-url api-version
hf_require_jq
GREEN=$(printf '\033[32m')
RED=$(printf '\033[31m')
YELLOW=$(printf '\033[33m')
RESET=$(printf '\033[0m')
CLUSTERS_JSON=$(hf_get "/clusters")
CLUSTER_STATUSES_MAP='{}'
NP_MAP='{}'
NP_STATUSES_MAP='{}'
while IFS= read -r CID; do
NP_JSON=$(hf_get "/clusters/${CID}/nodepools" 2>/dev/null || echo '{"items":[]}')
C_STATUSES=$(hf_get "/clusters/${CID}/statuses" 2>/dev/null || echo '{"items":[]}')
CLUSTER_STATUSES_MAP=$(jq -n \
--argjson m "$CLUSTER_STATUSES_MAP" --arg id "$CID" --argjson s "$C_STATUSES" \
'$m + {($id): ($s.items // [])}')
NP_MAP=$(jq -n \
--argjson m "$NP_MAP" --arg id "$CID" --argjson np "$NP_JSON" \
'$m + {($id): ($np.items // [])}')
while IFS= read -r NPID; do
NP_STAT=$(hf_get "/clusters/${CID}/nodepools/${NPID}/statuses" 2>/dev/null || echo '{"items":[]}')
NP_STATUSES_MAP=$(jq -n \
--argjson m "$NP_STATUSES_MAP" --arg id "$NPID" --argjson s "$NP_STAT" \
'$m + {($id): ($s.items // [])}')
done < <(echo "$NP_JSON" | jq -r '(.items // [])[].id')
done < <(echo "$CLUSTERS_JSON" | jq -r '.items[].id')
jq -n -r \
--argjson clusters "$CLUSTERS_JSON" \
--argjson cluster_statuses "$CLUSTER_STATUSES_MAP" \
--argjson np_map "$NP_MAP" \
--argjson np_statuses "$NP_STATUSES_MAP" \
--arg green "$GREEN" --arg red "$RED" --arg yellow "$YELLOW" --arg reset "$RESET" '
def fmt_cond:
if . == null then "-"
else
(.observed_generation | if . != null then tostring else "" end) as $gen |
if .status == "True" then "" + $gen
elif .status == "False" then "" + $gen
elif .status == "Unknown" then "" + $gen
elif .status == "" or .status == null then "-"
else .status end
end;
def fmt_adapter(ctype):
if . == null then "-"
else
(.observed_generation | if . != null then tostring else "" end) as $gen |
(.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;
$clusters.items as $citems |
# Condition types from clusters and nodepools (excluding *Successful)
([ $citems[].status.conditions[]?.type,
($np_map | to_entries[].value[].status.conditions[]?.type) ]
| unique | map(select(endswith("Successful") | not))) as $ctypes |
# Adapter names from all statuses
([ ($cluster_statuses | to_entries[].value[].adapter),
($np_statuses | to_entries[].value[].adapter) ]
| map(select(. != null)) | unique) as $adapters |
# Header
(["ID", "NAME", "GEN"] + $ctypes + $adapters | @tsv),
(["---", "---", "---"] + ($ctypes | map("---")) + ($adapters | map("---")) | @tsv),
# Rows: cluster row followed by its nodepool rows
($citems[] |
. as $cluster |
($cluster_statuses[$cluster.id] // []) as $cstatus |
(if $cluster.deleted_time != null then "Finalized" else "Available" end) as $ctype |
([ .id, .name,
((.generation // 0 | tostring) + (if .deleted_time != null then "" else "" end)) ] +
[ $ctypes[] as $t | $cluster.status.conditions // [] | map(select(.type == $t)) | .[0] | fmt_cond ] +
[ $adapters[] as $a | $cstatus | map(select(.adapter == $a)) | .[0] | fmt_adapter($ctype) ]
| @tsv),
($np_map[$cluster.id] // [] | .[] |
. as $np |
($np_statuses[$np.id] // []) as $npstatus |
(if $np.deleted_time != null then "Finalized" else "Available" end) as $nptype |
([ (" " + .id), (" " + .name),
((.generation // 0 | tostring) + (if .deleted_time != null then "" else "" end)) ] +
[ $ctypes[] as $t | $np.status.conditions // [] | map(select(.type == $t)) | .[0] | fmt_cond ] +
[ $adapters[] as $a | $npstatus | map(select(.adapter == $a)) | .[0] | fmt_adapter($nptype) ]
| @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"
}
}'