From 0a2e282dae2d845a7bee0be57bcfd90ad824fa20 Mon Sep 17 00:00:00 2001 From: Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:41:48 +0200 Subject: [PATCH] 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. --- .envrc | 3 +- .gitignore | 4 ++- .mod_env | 4 --- .mod_env.json | 12 ++++++++ .util/.mod_env.json.tmpl | 13 +++++++++ .util/.mod_env.tmpl | 4 --- .util/Dockerfile | 5 ++-- .util/docker-compose.yaml | 2 +- .util/run_tests.sh | 58 +++++++++++++++++++++++++-------------- 9 files changed, 71 insertions(+), 34 deletions(-) delete mode 100644 .mod_env create mode 100644 .mod_env.json create mode 100644 .util/.mod_env.json.tmpl delete mode 100644 .util/.mod_env.tmpl diff --git a/.envrc b/.envrc index d57593d..d1e7b62 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,3 @@ -dotenv_if_exists .mod_env +dotenv_if_exists + diff --git a/.gitignore b/.gitignore index 0802dec..dd30962 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.tar.gz -dist \ No newline at end of file +dist +!.util/docker-compose.yaml +.util/docker-compose*.yaml \ No newline at end of file diff --git a/.mod_env b/.mod_env deleted file mode 100644 index 3b92f05..0000000 --- a/.mod_env +++ /dev/null @@ -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 \ No newline at end of file diff --git a/.mod_env.json b/.mod_env.json new file mode 100644 index 0000000..deac0eb --- /dev/null +++ b/.mod_env.json @@ -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 +} diff --git a/.util/.mod_env.json.tmpl b/.util/.mod_env.json.tmpl new file mode 100644 index 0000000..6263052 --- /dev/null +++ b/.util/.mod_env.json.tmpl @@ -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": "" +} diff --git a/.util/.mod_env.tmpl b/.util/.mod_env.tmpl deleted file mode 100644 index c1f539a..0000000 --- a/.util/.mod_env.tmpl +++ /dev/null @@ -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= \ No newline at end of file diff --git a/.util/Dockerfile b/.util/Dockerfile index 286d155..3591258 100644 --- a/.util/Dockerfile +++ b/.util/Dockerfile @@ -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 </dev/null && pwd -P) usage() { cat < /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 <> $docker_compose_file + cat <> "$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 \ No newline at end of file