Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare run_in_docker script to use podman or docker #602

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ services:
environment:
- KAFKA_ADVERTISED_HOST_NAME=kafka
- KAFKA_CREATE_TOPICS="platform.notifications.ingress:1:1"
healthcheck:
test: ["CMD", "bash", "-c", "echo > /dev/tcp/localhost/9092"]
interval: 5s
timeout: 2s
retries: 5
start_period: 5s

minio:
profiles:
Expand Down
53 changes: 37 additions & 16 deletions run_in_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@

# This script assumes that a Go service is already built, and the Python service can be interpreted without errors

# Function to get docker compose profile to add based on service name specified by the user
# Function to check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}


# Check if Docker is installed
if command_exists docker; then
CONTAINER_TOOL="docker"
COMPOSER="docker compose"
# Check if Podman is installed
elif command_exists podman; then
CONTAINER_TOOL="podman"
COMPOSER="podman-compose"
else
echo "Neither Docker nor Podman are installed on this system."
exit 1
fi

# Function to get the correct profile to add based on service name specified by the user
with_profile() {
local target="$1"
case "$target" in
Expand Down Expand Up @@ -43,22 +62,23 @@ copy_go_executable() {
local cid="$1"
local path_to_service="$2"
local executable_name="$3"
docker cp "$path_to_service/$executable_name" "$cid:$(docker exec "$cid" bash -c 'echo "$VIRTUAL_ENV_BIN"')"
docker exec -u root "$cid" /bin/bash -c "chmod +x \$VIRTUAL_ENV_BIN/$executable_name"
# shellcheck disable=SC2016
"$CONTAINER_TOOL" cp "$path_to_service/$executable_name" "$cid:$("$CONTAINER_TOOL" exec "$cid" bash -c 'echo "$VIRTUAL_ENV_BIN"')"
"$CONTAINER_TOOL" exec -u root "$cid" /bin/bash -c "chmod +x \$VIRTUAL_ENV_BIN/$executable_name"
}

copy_python_project() {
local cid="$1"
local path_to_service="$2"
docker cp "$path_to_service" "$cid:/."
"$CONTAINER_TOOL" cp "$path_to_service" "$cid:/."
# service will be pip-installed and executed, so user will need write and exec permissions
docker exec -u root "$cid" /bin/bash -c "chmod -R 777 /$(basename "$path_to_service")"
"$CONTAINER_TOOL" exec -u root "$cid" /bin/bash -c "chmod -R 777 /$(basename "$path_to_service")"
}

copy_openapi_spec() {
local cid="$1"
local path_to_service="$2"
docker cp "$path_to_service/openapi.json" "$cid:$(docker exec "$cid" bash -c 'echo "$HOME"')"
"$CONTAINER_TOOL" cp "$path_to_service/openapi.json" "$REMOTE_PATH_FOR_SERVICE"
}

# Function to copy files based on the make target
Expand All @@ -70,11 +90,11 @@ copy_files() {
case "$target" in
"aggregator-tests")
copy_go_executable "$cid" "$path_to_service" "insights-results-aggregator"
docker cp "$path_to_service/openapi.json" "$cid":"$(docker exec "$cid" bash -c 'echo "$HOME"')"
"$CONTAINER_TOOL" cp "$path_to_service/openapi.json" "$REMOTE_PATH_FOR_SERVICE"
;;
"aggregator-mock-tests")
copy_go_executable "$cid" "$path_to_service" "insights-results-aggregator-mock"
docker cp "$path_to_service" "$cid:$(docker exec "$cid" bash -c 'echo "$HOME"')/mock_server"
"$CONTAINER_TOOL" cp "$path_to_service" "$REMOTE_PATH_FOR_SERVICE/mock_server"
;;
"cleaner-tests")
copy_go_executable "$cid" "$path_to_service" "insights-results-aggregator-cleaner"
Expand All @@ -98,7 +118,7 @@ copy_files() {
"insights-content-service-tests")
echo -e "\033[33mWARNING! Content service should include test-rules for these tests to run properly.\033[0m"
echo -e "\033[33mPlease build using './build.sh --test-rules-only' or './build.sh --include-test-rules'\033[0m"
docker cp "$path_to_service" "$cid":"$(docker exec "$cid" bash -c 'echo "$HOME"')"
"$CONTAINER_TOOL" cp "$path_to_service" "$REMOTE_PATH_FOR_SERVICE"
;;
"insights-content-template-renderer-tests")
copy_python_project "$cid" "$path_to_service"
Expand All @@ -117,7 +137,7 @@ copy_files() {
;;
"parquet-factory-tests")
copy_go_executable "$cid" "$path_to_service" "parquet-factory"
docker cp "$path_to_service"/config.toml "$cid":"$(docker exec "$cid" bash -c 'echo "$HOME"')"
"$CONTAINER_TOOL" cp "$path_to_service"/config.toml "$REMOTE_PATH_FOR_SERVICE"
;;
*)
echo "Unexpected target: $target. Does it exist in Makefile?"
Expand All @@ -141,16 +161,17 @@ fi

# Step 4: Launch containers
# shellcheck disable=SC2046
POSTGRES_DB_NAME="$db_name" docker compose $(with_profile "$1") $(with_no_mock "$3") up -d
POSTGRES_DB_NAME="$db_name" "$COMPOSER" $(with_profile "$1") $(with_no_mock "$3") up -d

# Step 5: Find the container ID of the insights-behavioral-spec container
cid=$(docker ps | grep 'insights-behavioral-spec:latest' | cut -d ' ' -f 1)
cid=$("$CONTAINER_TOOL" ps | grep 'insights-behavioral-spec:latest' | cut -d ' ' -f 1)

# shellcheck disable=SC2016
REMOTE_PATH_FOR_SERVICE="$cid:$("$CONTAINER_TOOL" exec "$cid" bash -c 'echo "$HOME"')"

# Step 6: Copy the executable and needed dependencies or Python service into the container
# TODO: Discuss including archives in compiled Go executables for testing
copy_files "$cid" "$tests_target" "$path_to_service"

# Step 9: Execute the specified make target


docker exec "$cid" /bin/bash -c "source \$VIRTUAL_ENV/bin/activate && env && $(with_mocked_dependencies "$3") make $tests_target"
# Step 7: Execute the specified make target
"$CONTAINER_TOOL" exec "$cid" /bin/bash -c "source \$VIRTUAL_ENV/bin/activate && env && $(with_mocked_dependencies "$3") make $tests_target"
Loading