chore(util): Refactor Docker-related scripts for better modularity

Refactored Docker-related scripts to improve modularity and readability. Introduced support for loading configuration from `.mod_env.json` files, enhancing script flexibility. Added debug flag to enable detailed logging during script execution for better troubleshooting. Fixed file path issues related to script operations.
This commit is contained in:
Yves-Marie Haussonne 2024-10-10 14:41:48 +02:00
parent 1d4681417a
commit 0a2e282dae
9 changed files with 71 additions and 34 deletions

3
.envrc
View File

@ -1,2 +1,3 @@
dotenv_if_exists .mod_env
dotenv_if_exists

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
*.tar.gz
dist
!.util/docker-compose.yaml
.util/docker-compose*.yaml

View File

@ -1,4 +0,0 @@
CURRENT_MOD=test_harness
DEPENDENCIES=[{ "url":"https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz", "path":"/usr/local/share/minetest/games/minetest", "strip":1 }]
MINETEST_ADD_CONF=
CLIENT_NB=1

12
.mod_env.json Normal file
View File

@ -0,0 +1,12 @@
{
"current_mod": "test_harness",
"dependencies": [
{
"url":"https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz",
"path":"/usr/local/share/minetest/games/minetest",
"strip":1
}
],
"minetest-add-conf": "",
"client_nb": 1
}

13
.util/.mod_env.json.tmpl Normal file
View File

@ -0,0 +1,13 @@
{
"current_mod": "test_harness",
"dependencies": [
{
"url":"https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz",
"path":"/usr/local/share/minetest/games/minetest",
"strip":1
}
],
"minetest_add_conf": "",
"client_nb": 1,
"additional_mods": ""
}

View File

@ -1,4 +0,0 @@
CURRENT_MOD=test_harness
CLIENT_NB=1
DEPENDENCIES=[{ "url":"https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz", "path":"/usr/local/share/minetest/games/minetest", "strip":1 }, { "url":"https://forge.apps.education.fr/iri/minetest/docker_test_harness/-/archive/main/docker_test_harness-main.tar.gz", "path":"/usr/local/share/minetest/games/minetest/mods/test_harness", "strip":1 }]
MINETEST_ADD_CONF=

View File

@ -1,10 +1,8 @@
#
FROM ghcr.io/minetest/minetest:latest as builder
ARG DEPENDENCIES
ARG MINETEST_ADD_CONF=""
FROM ghcr.io/minetest/minetest:latest as builder
ENV STOP_SERVER=true
ENV FAILFAST=false
ENV MINETEST_GAME_PATH=/usr/local/share/minetest/games
@ -19,6 +17,7 @@ RUN mkdir -p /usr/local/share/minetest/games && \
# WORKDIR /config/.minetest/games/devtest/mods
RUN <<EOF
echo "DEPENDENCIES: $DEPENDENCIES"
while read dep; do
url=$(echo "$dep" | jq -r ".url")
dep_path=$(echo "$dep" | jq -r ".path")

View File

@ -17,4 +17,4 @@ services:
STOP_SERVER: ${STOP_SERVER}
FAILFAST: ${FAILFAST}
CURRENT_MOD: "${CURRENT_MOD}"
ADDITIONAL_MODS: "${ADDITIONAL_MODS:-}"
ADDITIONAL_MODS: "${ADDITIONAL_MODS}"

View File

@ -7,9 +7,10 @@ script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-D] [-f/--failfast] [-k/--keep-server]
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-D] [-f/--failfast] [-k/--keep-server] [-d/--debug]
[-p/--port port_server] [--dependencies mod_dependencies]
[-c/--clients nb_of_client] [-i/--client-image client_docker_image]
[--docker-cmd docker_command]
Start tests.
@ -17,7 +18,7 @@ Start tests.
Available options:
-h, --help Print this help and exit
-d, --docker-cmd The container engine (docker or podman)
--docker-cmd The container engine (docker or podman)
--podman Use podman as container engine
--docker Use docker as container engine
-D, --show-debug Display debug.txt content on exit
@ -25,19 +26,20 @@ Available options:
-k, --keep-server Keep the server running
-p, --port Server port
-c, --clients Number of client, default to \$CLIENT_NB
--dependencies mod dependencies
--dependencies Mod dependencies
-i, --client-image Client docker image (default to registry.apps.education.fr/iri/minetest/docker_test_harness/client:latest)
-d, --debug Debug the script
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
if [-f "${docker_compose_file:-}" ] && ![-z "$docker_cmd"]; then
eval $docker_cmd compose -f "${docker_compose_file}" down
if [ -f "$PWD/${docker_compose_file:-}" ] && ! [ -z "$docker_cmd" ]; then
eval $docker_cmd compose -f "$PWD/${docker_compose_file}" down || true
fi
if [-f "${docker_compose_file:-}" ]; then
\rm "${docker_compose_file}"
if [ -f "$PWD/${docker_compose_file:-}" ]; then
\rm "$PWD/${docker_compose_file}"
fi
}
@ -86,10 +88,11 @@ show_debug="false"
run_tests="all"
dependencies="${DEPENDENCIES:-}"
docker_compose_file=""
debug=false
parse_params() {
# init switch flags
args=$(getopt -a -o Dfhkc:p:d:t:i: --long show-debug,failfast,keep-server,help,podman,docker,clients:,client-image:,port:,docker-cmd:,tests:,dependencies: -- "$@")
args=$(getopt -a -o Dfhkdc:p:t:i: --long show-debug,failfast,keep-server,help,podman,docker,debug,clients:,client-image:,port:,docker-cmd:,tests:,dependencies: -- "$@")
if [[ $? -gt 0 ]]; then
usage
fi
@ -121,7 +124,11 @@ parse_params() {
show_debug="true"
shift
;;
-d | --docker-cmd)
-d | --debug)
debug=true
shift
;;
--docker-cmd)
docker_cmd=$2
shift 2
;;
@ -160,7 +167,8 @@ parse_params() {
shift $((OPTIND - 1))
# check required params and arguments
if [ "$docker_cmd" == "docker" ]; then docker_build_cmd="buildx build"; fi
if [ "$debug" = "true" ]; then set -x; fi
if [[ "$docker_cmd" == "docker" ]]; then docker_build_cmd="buildx build"; fi
if ! exists_in_list "docker podman", " ", "$docker_cmd"; then die "docker command must be in docker or podman"; fi
if ! exists_in_list "all simple players", " ", "$run_tests"; then die "run tests (-t) must be in (all,simple,players)"; fi
if ! isuint $client_nb; then die "Number of clients must be an integer"; fi
@ -207,19 +215,24 @@ export STOP_SERVER="${stop_server:-true}"
export FAILFAST="${failfast:-false}"
export DEPENDENCIES="${dependencies}"
export CLIENT_IMAGE="${client_image:-registry.apps.education.fr/iri/minetest/docker_test_harness/client:latest}"
if [ -z "$($docker_cmd images -q $CLIENT_IMAGE 2> /dev/null)" ]; then
if ($docker_cmd manifest inspect $CLIENT_IMAGE 2>&1 /dev/null); then
die "CLIENT IMAGE $CLIENT_IMAGE does not exists"
fi
export ENV_FILE=""
if [ -f "$PWD/.mod_env" ]; then
ENV_FILE="--env-file \"$PWD/.mod_env\""
export CURRENT_MOD=""
export ADDITIONAL_MODS=""
if [ -f "$PWD/.mod_env.json" ]; then
DEPENDENCIES="$(cat "$PWD/.mod_env.json" | jq -c ".dependencies//\"\"")"
CURRENT_MOD="$(cat "$PWD/.mod_env.json" | jq -r ".current_mod//\"\"")"
CLIENT_NB="$(cat "$PWD/.mod_env.json" | jq -r ".client_nb//0")"
ADDITIONAL_MODS="$(cat "$PWD/.mod_env.json" | jq -r ".additional_mods//\"\"")"
fi
docker_compose_file=`mktemp --suffix=.yaml docker-composeXXXXXX`
docker_compose_file=`mktemp --suffix=.yaml docker-composeXXXXXX --tmpdir=.util`
cat "${script_dir}/docker-compose.yaml" > $docker_compose_file
cat "${script_dir}/docker-compose.yaml" > "$PWD/$docker_compose_file"
for (( clienti=1; clienti<=$client_nb; clienti++ )); do
cat <<EOF >> $docker_compose_file
cat <<EOF >> "$PWD/$docker_compose_file"
client${clienti}:
image: \${CLIENT_IMAGE}
restart: on-failure:10
@ -235,11 +248,16 @@ EOF
done
if [ "$debug" = "true" ]; then
echo -e "$GREEN------------------ $docker_compose_file ----------------------------------$NOFORMAT"
cat "$PWD/$docker_compose_file"
echo -e "$GREEN--------------------------------------------------------------------------$NOFORMAT"
fi
eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" down
eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" pull
eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" build --pull
eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" up --force-recreate --exit-code-from server --abort-on-container-exit
eval $docker_cmd compose $ENV_FILE -f "$PWD/${docker_compose_file}" down
eval $docker_cmd compose $ENV_FILE -f "$PWD/${docker_compose_file}" pull
eval $docker_cmd compose $ENV_FILE -f "$PWD/${docker_compose_file}" build --pull
eval $docker_cmd compose $ENV_FILE -f "$PWD/${docker_compose_file}" up --force-recreate --exit-code-from server --abort-on-container-exit
exit_code=$?
exit $exit_code