Skip to content

Commit

Permalink
Fixed various Docker-related project control code issues.
Browse files Browse the repository at this point in the history
Fixed variable typos.
Fixed wrong return code propagation for docker build commands.
Improved Python templates related to Docker interactive testing.
  • Loading branch information
kilo52 committed Jun 9, 2024
1 parent 2aeef7e commit 2b218b3
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 112 deletions.
7 changes: 4 additions & 3 deletions js/02_server-nodejs/source/.docker/controls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
###############################################################################

# The Dockerfile to be used for creating images for isolated runs
ROJECT_CONTAINER_BUILD_DOCKERFILE="Dockerfile-run";
PROJECT_CONTAINER_BUILD_DOCKERFILE="Dockerfile-run";
# The name given to the image and container
PROJECT_CONTAINER_BUILD_NAME="${{VAR_PROJECT_NAME_LOWER}}";
# The version tag given to the image and container
Expand Down Expand Up @@ -74,8 +74,9 @@ function project_run_isolated() {
--tag "$name_tag" \
--file .docker/${PROJECT_CONTAINER_BUILD_DOCKERFILE} .

if (( $? != 0 )); then
return $?;
local docker_status=$?;
if (( docker_status != 0 )); then
return $docker_status;
fi
fi

Expand Down
85 changes: 39 additions & 46 deletions python/06_server-cherrypy/source/.docker/controls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
# This shell script provides functions to handle isolated executions #
# via Docker. Users may source this file and call the provided functions to #
# either build or test the underlying project inside a Docker container. #
# This can also be used to run arbitrary commands inside an isolated #
# container or to get an interactive shell. Some execution parameters #
# can be controlled by setting various PROJECT_CONTAINER_* variables. #
# #
###############################################################################

# The Dockerfile to be used for creating images for isolated builds
readonly CONTAINER_BUILD_DOCKERFILE="Dockerfile-build";
PROJECT_CONTAINER_BUILD_DOCKERFILE="Dockerfile-build";
# The name given to the image and container
readonly CONTAINER_BUILD_NAME="${{VAR_PROJECT_NAME_LOWER}}-build";
PROJECT_CONTAINER_BUILD_NAME="${{VAR_PROJECT_NAME_LOWER}}-build";
# The version tag given to the image and container
readonly CONTAINER_BUILD_VERSION="0.1";
PROJECT_CONTAINER_BUILD_VERSION="0.1";
# The project name inside the container
readonly CONTAINER_PROJECT_NAME="${{VAR_PROJECT_NAME_LOWER}}";
PROJECT_CONTAINER_PROJECT_NAME="${{VAR_PROJECT_NAME_LOWER}}";


# Checks that the docker command is available.
Expand All @@ -41,13 +44,19 @@ function _check_docker() {

# Builds the Docker image.
#
# The building of the Docker image can be suppressed by setting the
# environment variable PROJECT_CONTAINER_IMAGE_BUILD to the value "0".
#
# Returns:
# The exit status of the docker build command.
#
function _docker_build() {
if [[ "$PROJECT_CONTAINER_IMAGE_BUILD" == "0" ]]; then
return 0;
fi
local uid=0;
local gid=0;
local workdir="/root/${CONTAINER_PROJECT_NAME}";
local workdir="/root/${PROJECT_CONTAINER_PROJECT_NAME}";
# When using non-rootless Docker, the user inside the container should be a
# regular user. We assign him the same UID and GID as the underlying host
# user so that there are no conflicts when bind-mounting the source tree.
Expand All @@ -57,14 +66,15 @@ function _docker_build() {
if ! docker info 2>/dev/null |grep -q "rootless"; then
uid=$(id -u);
gid=$(id -g);
workdir="/home/user/${CONTAINER_PROJECT_NAME}";
workdir="/home/user/${PROJECT_CONTAINER_PROJECT_NAME}";
fi
local name_tag="${PROJECT_CONTAINER_BUILD_NAME}:${PROJECT_CONTAINER_BUILD_VERSION}";
logI "Building Docker image";
docker build --build-arg UID=${uid} \
--build-arg GID=${gid} \
--build-arg DWORKDIR="${workdir}" \
--tag ${CONTAINER_BUILD_NAME}:${CONTAINER_BUILD_VERSION} \
--file .docker/${CONTAINER_BUILD_DOCKERFILE} .
docker build --build-arg UID=${uid} \
--build-arg GID=${gid} \
--build-arg DWORKDIR="${workdir}" \
--tag "${name_tag}" \
--file .docker/${PROJECT_CONTAINER_BUILD_DOCKERFILE} .

return $?;
}
Expand All @@ -82,10 +92,10 @@ function _docker_build() {
# Either the exit status of the Docker-related commands, if unsuccessful,
# or the exit status of the specified command.
#
function _run_isolated() {
local run_type="$1";
function project_run_isolated() {
local isolated_command="$1";
shift;
if ! _check_docker "If you want the $run_type to be executed in an isolated environment," \
if ! _check_docker "If you want a command to be executed in an isolated environment," \
"please make sure that Docker is correctly installed."; then
return 1;
fi
Expand All @@ -96,20 +106,22 @@ function _run_isolated() {

local uid=0;
local gid=0;
local workdir="/root/${CONTAINER_PROJECT_NAME}";
local workdir="/root/${PROJECT_CONTAINER_PROJECT_NAME}";
if ! docker info 2>/dev/null |grep -q "rootless"; then
uid=$(id -u);
gid=$(id -g);
workdir="/home/user/${CONTAINER_PROJECT_NAME}";
workdir="/home/user/${PROJECT_CONTAINER_PROJECT_NAME}";
fi
logI "Executing isolated $run_type";
docker run --name ${CONTAINER_BUILD_NAME} \
local name_tag="${PROJECT_CONTAINER_BUILD_NAME}:${PROJECT_CONTAINER_BUILD_VERSION}";
logI "Executing isolated $isolated_command";
docker run --name ${PROJECT_CONTAINER_BUILD_NAME} \
--mount type=bind,source="${PWD}",target="${workdir}" \
--user ${uid}:${gid} \
--rm \
--tty \
${CONTAINER_BUILD_NAME}:${CONTAINER_BUILD_VERSION} \
"$run_type" \
--interactive \
"$name_tag" \
"$isolated_command" \
"$@";

return $?;
Expand Down Expand Up @@ -141,48 +153,29 @@ function _start_interactive_isolated_tests() {

local uid=0;
local gid=0;
local workdir="/root/${CONTAINER_PROJECT_NAME}";
local workdir="/root/${PROJECT_CONTAINER_PROJECT_NAME}";
if ! docker info 2>/dev/null |grep -q "rootless"; then
uid=$(id -u);
gid=$(id -g);
workdir="/home/user/${CONTAINER_PROJECT_NAME}";
workdir="/home/user/${PROJECT_CONTAINER_PROJECT_NAME}";
fi
local name_tag="${PROJECT_CONTAINER_BUILD_NAME}:${PROJECT_CONTAINER_BUILD_VERSION}";
logI "Starting isolated Docker container for interactive testing";
docker run --name ${CONTAINER_BUILD_NAME} \
docker run --name ${PROJECT_CONTAINER_BUILD_NAME} \
--interactive \
--tty \
--init \
--mount type=bind,source="${PWD}",target="${workdir}" \
--user ${uid}:${gid} \
--rm \
--publish 8080:8080 \
${CONTAINER_BUILD_NAME}:${CONTAINER_BUILD_VERSION} \
"$name_tag" \
"tests" \
"$@";

return $?;
}

# Executes an isolated build process inside a Docker container.
#
# This function executes a 'docker build' followed by a 'docker run' command.
# Returns the exit status of 'docker build', if it was not successful,
# otherwise returns the exit status of 'docker run'.
#
# Args:
# $@ - Arguments to be passed to the project's build.sh script
# to customise the build.
#
# Returns:
# The exit status of the Docker-related commands. If the Docker commands are
# all successful, then the exit status of the executed build.sh script
# is returned instead.
#
function run_isolated_build() {
_run_isolated "build" "$@";
return $?;
}

# Executes an isolated test process inside a Docker container.
#
# This function is used for two purposes. It either executes a 'docker build'
Expand All @@ -207,7 +200,7 @@ function run_isolated_build() {
# Returns the exit status of the server application process when using an
# interactive test session.
#
function run_isolated_tests() {
function project_run_isolated_tests() {
local arg="";
local run_interactive_session=false;
for arg in "$@"; do
Expand All @@ -220,7 +213,7 @@ function run_isolated_tests() {
if [[ $run_interactive_session == true ]]; then
_start_interactive_isolated_tests "$@";
else
_run_isolated "tests" "$@";
project_run_isolated "tests" "$@";
fi
return $?;
}
5 changes: 5 additions & 0 deletions python/06_server-cherrypy/var/script_test_isolated_main
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if [[ $ARG_ISOLATED == true ]]; then
source ".docker/controls.sh";
project_run_isolated_tests "${ARGS_ISOLATED[@]}";
exit $?;
fi
85 changes: 38 additions & 47 deletions python/07_odoo_module/source/.docker/controls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@
# This shell script provides functions to handle isolated executions #
# via Docker. Users may source this file and call the provided functions to #
# either build or test the underlying project inside a Docker container. #
# This can also be used to run arbitrary commands inside an isolated #
# container or to get an interactive shell. Some execution parameters #
# can be controlled by setting various PROJECT_CONTAINER_* variables. #
# #
###############################################################################

# The Dockerfile to be used for creating images for isolated builds
readonly CONTAINER_BUILD_DOCKERFILE="Dockerfile-build";
PROJECT_CONTAINER_BUILD_DOCKERFILE="Dockerfile-build";
# The name given to the image and container
readonly CONTAINER_BUILD_NAME="${{VAR_PROJECT_NAME_LOWER}}-build";
PROJECT_CONTAINER_BUILD_NAME="${{VAR_PROJECT_NAME_LOWER}}-build";
# The version tag given to the image and container
readonly CONTAINER_BUILD_VERSION="0.1";
PROJECT_CONTAINER_BUILD_VERSION="0.1";

# The docker-compose file to be used for isolated interactive tests
readonly CONTAINER_TEST_DOCKER_COMPOSE="docker-compose-test.yaml";
PROJECT_CONTAINER_TEST_DOCKER_COMPOSE="docker-compose-test.yaml";


# Executes an isolated command inside a Docker container.
#
# This function builds the image, creates a container from it
# and then runs that container. The container is removed after
# the execution has completed
# the execution has completed.
#
# The building of the Docker image can be suppressed by setting the
# environment variable PROJECT_CONTAINER_IMAGE_BUILD to the value "0".
#
# Args:
# $@ - The arguments to be passed to the container's entrypoint.
Expand All @@ -33,12 +39,12 @@ readonly CONTAINER_TEST_DOCKER_COMPOSE="docker-compose-test.yaml";
# Either the exit status of the Docker-related commands, if unsuccessful,
# or the exit status of the specified command.
#
function _run_isolated() {
local run_type="$1";
function project_run_isolated() {
local isolated_command="$1";
shift;
if ! command -v "docker" &> /dev/null; then
logE "ERROR: Could not find the 'docker' executable.";
logE "If you want the $run_type to be executed in an isolated environment,";
logE "If you want a command to be executed in an isolated environment,";
logE "please make sure that Docker is correctly installed";
return 1;
fi
Expand All @@ -58,25 +64,30 @@ function _run_isolated() {
gid=$(id -g);
workdir="/home/user/${workdir_name}";
fi
logI "Building Docker image";
docker build --build-arg UID=${uid} \
--build-arg GID=${gid} \
--build-arg DWORKDIR="${workdir}" \
--tag ${CONTAINER_BUILD_NAME}:${CONTAINER_BUILD_VERSION} \
--file .docker/${CONTAINER_BUILD_DOCKERFILE} .
local name_tag="${PROJECT_CONTAINER_BUILD_NAME}:${PROJECT_CONTAINER_BUILD_VERSION}";
if [[ "$PROJECT_CONTAINER_IMAGE_BUILD" != "0" ]]; then
logI "Building Docker image";
docker build --build-arg UID=${uid} \
--build-arg GID=${gid} \
--build-arg DWORKDIR="${workdir}" \
--tag "$name_tag" \
--file .docker/${PROJECT_CONTAINER_BUILD_DOCKERFILE} .

if (( $? != 0 )); then
return $?;
local docker_status=$?;
if (( docker_status != 0 )); then
return $docker_status;
fi
fi

logI "Executing isolated $run_type";
docker run --name ${CONTAINER_BUILD_NAME} \
logI "Executing isolated $isolated_command";
docker run --name ${PROJECT_CONTAINER_BUILD_NAME} \
--mount type=bind,source="${PWD}",target="${workdir}" \
--user ${uid}:${gid} \
--rm \
--tty \
${CONTAINER_BUILD_NAME}:${CONTAINER_BUILD_VERSION} \
"$run_type" \
--interactive \
"$name_tag" \
"$isolated_command" \
"$@";

return $?;
Expand All @@ -103,36 +114,16 @@ function _start_interactive_isolated_tests() {
return 1;
fi
logI "Starting isolated Docker containers for interactive testing";
docker compose --file .docker/${CONTAINER_TEST_DOCKER_COMPOSE} \
--project-directory "${PWD}" \
run \
--service-ports \
web \
docker compose --file .docker/${PROJECT_CONTAINER_TEST_DOCKER_COMPOSE} \
--project-directory "${PWD}" \
run \
--service-ports \
web \
"$@";

return $?;
}

# Executes an isolated build process inside a Docker container.
#
# This function executes a 'docker build' followed by a 'docker run' command.
# Returns the exit status of 'docker build', if it was not successful,
# otherwise returns the exit status of 'docker run'.
#
# Args:
# $@ - Arguments to be passed to the project's build.sh script
# to customise the build.
#
# Returns:
# The exit status of the Docker-related commands. If the Docker commands are
# all successful, then the exit status of the executed build.sh script
# is returned instead.
#
function run_isolated_build() {
_run_isolated "build" "$@";
return $?;
}

# Executes an isolated test process inside a Docker container.
#
# This function is used for two purposes. It either executes a 'docker build'
Expand All @@ -157,7 +148,7 @@ function run_isolated_build() {
# Returns the exit status of the odoo process when using an
# interactive test session.
#
function run_isolated_tests() {
function project_run_isolated_tests() {
local arg="";
local run_interactive_session=false;
local odoo_args=();
Expand All @@ -177,7 +168,7 @@ function run_isolated_tests() {
if [[ $run_interactive_session == true ]]; then
_start_interactive_isolated_tests "${odoo_args[@]}";
else
_run_isolated "tests" "$@";
project_run_isolated "tests" "$@";
fi
return $?;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.1'
services:
web:
image: odoo:16
Expand Down
5 changes: 5 additions & 0 deletions python/07_odoo_module/var/script_test_isolated_main
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if [[ $ARG_ISOLATED == true ]]; then
source ".docker/controls.sh";
project_run_isolated_tests "${ARGS_ISOLATED[@]}";
exit $?;
fi
5 changes: 3 additions & 2 deletions share/c/docker/controls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ function project_run_isolated() {
--tag "$name_tag" \
--file .docker/${PROJECT_CONTAINER_BUILD_DOCKERFILE} .

if (( $? != 0 )); then
return $?;
local docker_status=$?;
if (( docker_status != 0 )); then
return $docker_status;
fi
fi

Expand Down
Loading

0 comments on commit 2b218b3

Please sign in to comment.