-
Notifications
You must be signed in to change notification settings - Fork 9
/
list_test_runner_machines
executable file
·237 lines (205 loc) · 6.23 KB
/
list_test_runner_machines
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
TEST_MACHINES=$(
cat <<'HEREDOC'
lrineau@bonnard
lrineau@cgal
cgaltest@friedrich
lrineau@rubens
HEREDOC
)
machine_title() {
printf '\n## %s ##\n' $1
}
machine_tested_images() {
echo
echo '```plain'
ssh $1 cat /home/$2/.config/CGAL/test_cgal_docker_images
echo '```'
}
docker_is_active_cmd() {
systemctl is-active -q docker
return $?
}
declare -xf docker_is_active_cmd
docker_cmd() {
if docker_is_active_cmd; then
docker $@
else
podman --url unix:/var/run/podman/podman.sock $@
fi
}
declare -xf docker_cmd
list_of_containers_cmd() {
docker_cmd ps -a --format '{{.Names}}' --filter name="CGAL-"
}
declare -xf list_of_containers_cmd
container_status_cmd() {
docker_cmd inspect --format '{{.State.Status}}' $1
}
declare -xf container_status_cmd
container_human_readable_status_cmd() {
docker_cmd ps --all --filter name=$1 --format '{{.Status}}'
}
declare -xf container_human_readable_status_cmd
simplify_date_cmd() {
date=$1
pattern=' \+[0-9]{4} [A-Z]{3,}$'
if [[ $date =~ $pattern ]]; then
date=${date% *}
fi
echo "$date"
}
declare -xf simplify_date_cmd
container_start_time_cmd() {
simplify_date_cmd "$(docker_cmd inspect --format '{{.State.StartedAt}}' $1)"
}
declare -xf container_start_time_cmd
container_end_time_cmd() {
simplify_date_cmd "$(docker_cmd inspect --format '{{.State.FinishedAt}}' $1)"
}
declare -xf container_end_time_cmd
container_running_time_cmd() {
start_time=$(container_start_time_cmd $1)
end_time=$(container_end_time_cmd $1)
status=$(container_status_cmd $1)
if [ "$status" = "running" ]; then
end_time=$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')
fi
secs=$(($(date -d "$end_time" +%s) - $(date -d "$start_time" +%s)))
printf '%02dh:%02dm:%02ds\n' $((secs / 3600)) $((secs % 3600 / 60)) $((secs % 60))
}
declare -xf container_running_time_cmd
display_one_container_line_cmd() {
printf '%s\t%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" "$5"
}
declare -xf display_one_container_line_cmd
list_cgal_test_container_cmd() {
# docker_cmd ps -a --filter name=CGAL-
display_one_container_line_cmd "CONTAINER" "START TIME" "END TIME" "RUNNING TIME" "STATUS"
for container in $(list_of_containers_cmd); do
start_time="$(container_start_time_cmd $container)"
end_time="$(container_end_time_cmd $container)"
dur=$(container_running_time_cmd $container)
status="$(container_status_cmd $container) - $(container_human_readable_status_cmd $container)"
display_one_container_line_cmd "$container" "$start_time" "$end_time" "$dur" "$status"
done
}
declare -xf list_cgal_test_container_cmd
display_all_exported_cmd_functions() {
funcs=$(declare -F | awk '/ -fx .*_cmd$/ {print $3}')
for func in $funcs; do
declare -f $func
done
}
machine_list_cgal_test_container() {
printf '\n```tsv\n'
remote_script=$(
display_all_exported_cmd_functions
echo list_cgal_test_container_cmd
)
ssh $1 bash -s <<<"$remote_script"
printf '```\n'
}
help() {
cat <<HEREDOC
Usage: $0 [OPTION]
List the test runner machines and the containers running on them.
Options:
--table output in markdown table format
--column output in column format
--bat output with bat
--plain output in plain text
If no option is given, the script will try to use bat, then column, and finally
plain text.
HEREDOC
}
error_out() {
echo "ERROR: $1"
echo
help
exit 1
}
command -v sed >/dev/null || {
error_out 'sed is required'
}
if [[ $1 == --table ]] && ! command -v pandoc >/dev/null; then
error_out 'pandoc is required for the option --table'
fi
if [[ $1 == --column ]] && ! command -v column >/dev/null; then
error_out 'column is required for the option --column'
fi
if [[ $1 == --bat ]] && ! command -v bat >/dev/null; then
error_out 'bat is required for the option --bat'
fi
set_pretty_csv_to_md_table() {
pretty_csv() (
echo
sed '/```/ d; /^$/ d' | pandoc -f tsv -t gfm
)
}
set_pretty_csv_to_column() {
pretty_csv() {
echo
column -t -s $'\t' -o $'\t' | sed 's/^\(```[^ ]*\) *\t.*/\1/'
}
}
set_pretty_csv_to_bat() {
pretty_csv() {
bat --tabs=50 --paging=never --plain -l csv
}
}
set_pretty_csv_to_cat() {
pretty_csv() {
cat
}
}
case "$1" in
--table) set_pretty_csv_to_md_table ;;
--column) set_pretty_csv_to_column ;;
--bat) set_pretty_csv_to_bat ;;
--plain) set_pretty_csv_to_cat ;;
'')
if command -v bat >/dev/null; then
set_pretty_csv_to_bat
elif command -v column >/dev/null; then
set_pretty_csv_to_column
elif command -v pandoc >/dev/null; then
set_pretty_csv_to_md_table
else
set_pretty_csv_to_cat
fi
;;
*)
error_out "Unknown option $1"
;;
esac
ERROR_MACHINES=""
for machine in $TEST_MACHINES; do
USER=${machine%@*}
HOST=${machine#*@}
ssh $HOST test -f /home/$USER/.config/CGAL/test_cgal_docker_images || {
ERROR_MACHINES="$ERROR_MACHINES $machine"
}
done
if [ -n "$ERROR_MACHINES" ]; then
for machine in $ERROR_MACHINES; do
USER=${machine%@*}
HOST=${machine#*@}
printf 'ERROR: cannot read file `/home/%s/.config/CGAL/test_cgal_docker_images` on ssh host `%s`\n' $USER $HOST
done
exit 1
fi
cat <<HEREDOC
# Test runner machines #
The following machines are used to run the tests:
HEREDOC
for machine in $TEST_MACHINES; do
USER=${machine%@*}
HOST=${machine#*@}
machine_title $machine
printf '\nusing `%s`\n' "$(ssh $HOST docker --version)"
printf '\nTested images:\n'
machine_tested_images $HOST $USER
printf '\nCGAL test containers:\n'
machine_list_cgal_test_container $HOST $USER | pretty_csv
done