-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
154 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,199 @@ | ||
#!/bin/bash | ||
|
||
TEST_MACHINES=$(cat << 'HEREDOC' | ||
TEST_MACHINES=$( | ||
cat <<'HEREDOC' | ||
lrineau@bonnard | ||
lrineau@cgal | ||
cgaltest@friedrich | ||
lrineau@rubens | ||
HEREDOC | ||
) | ||
|
||
cat << HEREDOC | ||
cat <<HEREDOC | ||
# Test runner machines # | ||
The following machines are used to run the tests: | ||
HEREDOC | ||
|
||
machine_title () { | ||
machine_title() { | ||
printf '\n## %s ##\n' $1 | ||
} | ||
|
||
machine_tested_images () { | ||
machine_tested_images() { | ||
echo | ||
echo '```plain' | ||
ssh $1 cat /home/$2/.config/CGAL/test_cgal_docker_images | ||
echo '```' | ||
} | ||
|
||
test_docker_is_active_cmd () { | ||
echo 'systemctl is-active -q docker' | ||
docker_is_active_cmd() { | ||
systemctl is-active -q docker | ||
return $? | ||
} | ||
declare -xf docker_is_active_cmd | ||
|
||
docker_ps_filter_option () { | ||
echo '--filter name="CGAL-"' | ||
docker_cmd() { | ||
if docker_is_active_cmd; then | ||
docker $@ | ||
else | ||
podman -r $@ | ||
fi | ||
} | ||
declare -xf docker_cmd | ||
|
||
docker_ps_format_option () { | ||
echo '--format "table {{.Names}}\t{{.Image}}\t{{.CreatedAt}}\t{{.Status}}"' | ||
list_of_containers_cmd() { | ||
docker_cmd ps -a --format '{{.Names}}' --filter name="CGAL-" | ||
} | ||
declare -xf list_of_containers_cmd | ||
|
||
docker_ps_cmd () { | ||
printf 'docker ps -a %s %s' "$(docker_ps_filter_option)" "$(docker_ps_format_option)" | ||
container_status_cmd() { | ||
docker_cmd inspect --format '{{.State.Status}}' $1 | ||
} | ||
declare -xf container_status_cmd | ||
|
||
podman_ps_cmd () { | ||
printf 'podman -r ps -a %s %s' "$(docker_ps_filter_option)" "$(docker_ps_format_option)" | ||
container_human_readable_status_cmd() { | ||
docker_cmd ps --all --filter name=$1 --format '{{.Status}}' | ||
} | ||
declare -xf container_human_readable_status_cmd | ||
|
||
docker_or_podman_ps_cmd () { | ||
printf '%s && %s || %s' "$(test_docker_is_active_cmd)" "$(docker_ps_cmd)" "$(podman_ps_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 | ||
|
||
machine_list_cgal_test_container () { | ||
cmd=$(docker_or_podman_ps_cmd) | ||
echo | ||
echo '```plain' | ||
printf '%s\n' "$cmd" | ssh $1 bash -s | ||
echo '```' | ||
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' | ||
} | ||
|
||
command -v sed >/dev/null || { | ||
echo 'sed is required' | ||
exit 1 | ||
} | ||
|
||
if [[ $1 == --table ]] && ! command -v pandoc >/dev/null; then | ||
echo 'pandoc is required for the option --table' | ||
exit 1 | ||
fi | ||
if [[ $1 == --column ]] && ! command -v column >/dev/null; then | ||
echo 'column is required for the option --column' | ||
exit 1 | ||
fi | ||
if [[ $1 == --bat ]] && ! command -v bat >/dev/null; then | ||
echo 'bat is required for the option --bat' | ||
exit 1 | ||
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 | ||
else | ||
set_pretty_csv_to_cat | ||
fi | ||
;; | ||
*) | ||
echo "Unknown option $1" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
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 | ||
machine_list_cgal_test_container $HOST $USER | pretty_csv | ||
done |