From 0782f49505f7c6236a927a87dcd9e098bff95a26 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Wed, 24 Apr 2024 15:39:32 -0700 Subject: [PATCH 01/52] Fix for graceful shutdown option. Don't call docker rm --force until stop has returned --- lib/run.bash | 24 +++++++++++++++++------- plugin.yml | 2 ++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/run.bash b/lib/run.bash index bd057ab7..bb6ce8c7 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -1,14 +1,14 @@ #!/bin/bash -compose_cleanup() { - if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "false" ]]; then - # Send all containers a SIGKILL - run_docker_compose kill || true - else - # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period - run_docker_compose stop || true +kill_or_wait_for_stop() { + + if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then + # This will block until the container exits + run_docker_compose wait + container_exit_code=$? fi + # This will kill the container if it hasn't exited yet # `compose down` doesn't support force removing images if [[ "$(plugin_read_config LEAVE_VOLUMES 'false')" == "false" ]]; then run_docker_compose rm --force -v || true @@ -24,6 +24,16 @@ compose_cleanup() { fi } +compose_cleanup() { + kill_or_wait_for_stop & + + # No need to call kill directly for GRACEFUL_SHUTDOWN == false since rm --force will send the same kill signal + if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then + # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period + run_docker_compose stop || true + fi +} + # Checks for failed containers and writes logs for them the the provided dir check_linked_containers_and_save_logs() { local service="$1" diff --git a/plugin.yml b/plugin.yml index 39db0041..8d08e80c 100644 --- a/plugin.yml +++ b/plugin.yml @@ -50,6 +50,8 @@ configuration: type: array skip-checkout: type: boolean + stop-signal: + type: string leave-volumes: type: boolean no-cache: From af547804b30297d0946953c6c124b02b2467a7f6 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 25 Apr 2024 12:31:47 -0700 Subject: [PATCH 02/52] working on testing graceful shutdown fix --- docker-compose.yml | 1 + lib/run.bash | 14 ++++++++++++++ tests/cleanup.bats | 0 tests/docker-compose-cleanup.bats | 17 ++++++++--------- 4 files changed, 23 insertions(+), 9 deletions(-) mode change 100644 => 100755 tests/cleanup.bats diff --git a/docker-compose.yml b/docker-compose.yml index 10c6e0b6..f1401d7d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,5 +2,6 @@ version: '2' services: tests: image: buildkite/plugin-tester:v4.1.1 + command: ["bats", "tests/docker-compose-cleanup.bats", "tests/cleanup.bats"] volumes: - ".:/plugin" diff --git a/lib/run.bash b/lib/run.bash index 7ef0d6d6..69e5b16d 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -6,6 +6,9 @@ kill_or_wait_for_stop() { # This will block until the container exits run_docker_compose wait container_exit_code=$? + echo "container exited with code $container_exit_code" + else + echo "no need to wait, GRACEFUL_SHUTDOWN false" fi # This will kill the container if it hasn't exited yet @@ -26,11 +29,22 @@ kill_or_wait_for_stop() { compose_cleanup() { kill_or_wait_for_stop & + + if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "false" ]]; then + echo "graceful shutdown was false" + fi + + if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then + echo "graceful shutdown was true" + fi # No need to call kill directly for GRACEFUL_SHUTDOWN == false since rm --force will send the same kill signal if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period + echo "stopping gracefully" run_docker_compose stop || true + else + echo "no need to stop, GRACEFUL_SHUTDOWN false" fi } diff --git a/tests/cleanup.bats b/tests/cleanup.bats old mode 100644 new mode 100755 diff --git a/tests/docker-compose-cleanup.bats b/tests/docker-compose-cleanup.bats index a8843434..c0c3fb9d 100644 --- a/tests/docker-compose-cleanup.bats +++ b/tests/docker-compose-cleanup.bats @@ -15,9 +15,9 @@ setup () { run compose_cleanup assert_success - assert_equal "${lines[0]}" "kill" - assert_equal "${lines[1]}" "rm --force -v" - assert_equal "${lines[2]}" "down --remove-orphans --volumes" + assert_equal "${lines[0]}" "graceful shutdown was false" + # assert_equal "${lines[0]}" "rm --force -v" + # assert_equal "${lines[1]}" "down --remove-orphans --volumes" } @test "Possible to gracefully shutdown containers in docker-compose cleanup" { @@ -25,9 +25,9 @@ setup () { run compose_cleanup assert_success - assert_equal "${lines[0]}" "stop" - assert_equal "${lines[1]}" "rm --force -v" - assert_equal "${lines[2]}" "down --remove-orphans --volumes" + assert_equal "${lines[0]}" "graceful shutdown was true" + # assert_equal "${lines[0]}" "stop" + # assert_equal "${lines[1]}" "down --remove-orphans --volumes" } @test "Possible to skip volume destruction in docker-compose cleanup" { @@ -35,7 +35,6 @@ setup () { run compose_cleanup assert_success - assert_equal "${lines[0]}" "kill" - assert_equal "${lines[1]}" "rm --force" - assert_equal "${lines[2]}" "down --remove-orphans" + assert_equal "${lines[0]}" "rm --force" + assert_equal "${lines[1]}" "down --remove-orphans" } From bebc54fcd8668d985f7f2fae12d5cbd264135c2a Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 25 Apr 2024 12:46:56 -0700 Subject: [PATCH 03/52] fixed broken tests --- lib/run.bash | 16 ++-------------- tests/cleanup.bats | 3 +-- tests/docker-compose-cleanup.bats | 14 +++++++------- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/lib/run.bash b/lib/run.bash index 69e5b16d..91a8b073 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -6,9 +6,7 @@ kill_or_wait_for_stop() { # This will block until the container exits run_docker_compose wait container_exit_code=$? - echo "container exited with code $container_exit_code" - else - echo "no need to wait, GRACEFUL_SHUTDOWN false" + echo "exit code was $container_exit_code" fi # This will kill the container if it hasn't exited yet @@ -29,22 +27,12 @@ kill_or_wait_for_stop() { compose_cleanup() { kill_or_wait_for_stop & - - if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "false" ]]; then - echo "graceful shutdown was false" - fi - - if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then - echo "graceful shutdown was true" - fi + sleep 1 # No need to call kill directly for GRACEFUL_SHUTDOWN == false since rm --force will send the same kill signal if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period - echo "stopping gracefully" run_docker_compose stop || true - else - echo "no need to stop, GRACEFUL_SHUTDOWN false" fi } diff --git a/tests/cleanup.bats b/tests/cleanup.bats index 77d55bb1..5a2c601e 100755 --- a/tests/cleanup.bats +++ b/tests/cleanup.bats @@ -17,8 +17,7 @@ load '../lib/run' export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=true stub docker \ - "compose -f docker-compose.yml -p buildkite1111 kill : echo killing containers" \ - "compose -f docker-compose.yml -p buildkite1111 rm --force -v : echo removing stopped containers" \ + "compose -f docker-compose.yml -p buildkite1111 rm --force -v : echo killing and removing stopped containers" \ "compose -f docker-compose.yml -p buildkite1111 down --remove-orphans --volumes : echo removing everything" run "$PWD"/hooks/pre-exit diff --git a/tests/docker-compose-cleanup.bats b/tests/docker-compose-cleanup.bats index c0c3fb9d..69a10e0a 100644 --- a/tests/docker-compose-cleanup.bats +++ b/tests/docker-compose-cleanup.bats @@ -15,19 +15,19 @@ setup () { run compose_cleanup assert_success - assert_equal "${lines[0]}" "graceful shutdown was false" - # assert_equal "${lines[0]}" "rm --force -v" - # assert_equal "${lines[1]}" "down --remove-orphans --volumes" + assert_equal "${lines[0]}" "rm --force -v" + assert_equal "${lines[1]}" "down --remove-orphans --volumes" } @test "Possible to gracefully shutdown containers in docker-compose cleanup" { - export BUILDKITE_PLUGIN_DOCKER_COMPOSE_GRACEFUL_SHUTDOWN=1 + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_GRACEFUL_SHUTDOWN="true" run compose_cleanup assert_success - assert_equal "${lines[0]}" "graceful shutdown was true" - # assert_equal "${lines[0]}" "stop" - # assert_equal "${lines[1]}" "down --remove-orphans --volumes" + assert_equal "${lines[0]}" "wait" + assert_equal "${lines[1]}" "exit code was 0" + assert_equal "${lines[2]}" "rm --force -v" + assert_equal "${lines[3]}" "down --remove-orphans --volumes" } @test "Possible to skip volume destruction in docker-compose cleanup" { From 1b6df2304cf7610a4c2cae1f7ed54e93c537a918 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Mon, 29 Apr 2024 15:13:47 -0700 Subject: [PATCH 04/52] try running docker shell arg in [] to use exec mode --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 4c912854..20501a87 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -466,7 +466,7 @@ fi set +e ( # subshell is necessary to trap signals (compose v2 fails to stop otherwise) echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 - run_docker_compose "${run_params[@]}" + run_docker_compose ["${run_params[@]}"] ) exitcode=$? From dc72a0f66a7f80ce3abec08f5cbcba44dd3a6cc8 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 11:30:03 -0700 Subject: [PATCH 05/52] try graceful compose shutdown --- commands/run.sh | 9 ++++++--- docker-compose.yml | 1 - 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 20501a87..769b62f0 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -1,6 +1,8 @@ #!/bin/bash set -ueo pipefail +. "$DIR/../lib/run.bash" + # Run takes a service name, pulls down any pre-built image for that name # and then runs docker-compose run a generated project name @@ -447,8 +449,9 @@ elif [[ ${#command[@]} -gt 0 ]] ; then fi ensure_stopped() { - echo '+++ :warning: Signal received, stopping container' - docker stop "${container_name}" || true + echo '+++ :warning: Signal received, stopping container gracefully' + # docker stop "${container_name}" || true + compose_cleanup echo '~~~ Last log lines that may be missing above (if container was not already removed)' docker logs "${container_name}" || true exitcode='TRAP' @@ -466,7 +469,7 @@ fi set +e ( # subshell is necessary to trap signals (compose v2 fails to stop otherwise) echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 - run_docker_compose ["${run_params[@]}"] + run_docker_compose "${run_params[@]}" ) exitcode=$? diff --git a/docker-compose.yml b/docker-compose.yml index f1401d7d..10c6e0b6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,5 @@ version: '2' services: tests: image: buildkite/plugin-tester:v4.1.1 - command: ["bats", "tests/docker-compose-cleanup.bats", "tests/cleanup.bats"] volumes: - ".:/plugin" From 553a8d6cc13b602b705f6e4c365cff1ec3cdf6ca Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 12:40:03 -0700 Subject: [PATCH 06/52] pass service name arg to stop/wait --- commands/run.sh | 2 +- hooks/pre-exit | 2 +- lib/run.bash | 7 ++++--- tests/docker-compose-cleanup.bats | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 769b62f0..5cd89170 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -451,7 +451,7 @@ fi ensure_stopped() { echo '+++ :warning: Signal received, stopping container gracefully' # docker stop "${container_name}" || true - compose_cleanup + compose_cleanup ${run_service} echo '~~~ Last log lines that may be missing above (if container was not already removed)' docker logs "${container_name}" || true exitcode='TRAP' diff --git a/hooks/pre-exit b/hooks/pre-exit index 8278e338..37b59f03 100755 --- a/hooks/pre-exit +++ b/hooks/pre-exit @@ -18,5 +18,5 @@ if [[ -n "$(plugin_read_list RUN)" ]] && [[ "$(plugin_read_config CLEANUP "true" . "$DIR/../lib/run.bash" echo "~~~ :docker: Cleaning up after docker-compose" >&2 - compose_cleanup + compose_cleanup "" fi diff --git a/lib/run.bash b/lib/run.bash index 91a8b073..522ef069 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -4,7 +4,7 @@ kill_or_wait_for_stop() { if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then # This will block until the container exits - run_docker_compose wait + run_docker_compose wait "$1" container_exit_code=$? echo "exit code was $container_exit_code" fi @@ -26,13 +26,14 @@ kill_or_wait_for_stop() { } compose_cleanup() { - kill_or_wait_for_stop & + kill_or_wait_for_stop "$1" & sleep 1 # No need to call kill directly for GRACEFUL_SHUTDOWN == false since rm --force will send the same kill signal if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then + echo "graceful shutdown was true, stopping ${1}" # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period - run_docker_compose stop || true + run_docker_compose stop "$1" || true fi } diff --git a/tests/docker-compose-cleanup.bats b/tests/docker-compose-cleanup.bats index 69a10e0a..7cd3ada6 100644 --- a/tests/docker-compose-cleanup.bats +++ b/tests/docker-compose-cleanup.bats @@ -24,7 +24,7 @@ setup () { run compose_cleanup assert_success - assert_equal "${lines[0]}" "wait" + assert_output --partial "wait" assert_equal "${lines[1]}" "exit code was 0" assert_equal "${lines[2]}" "rm --force -v" assert_equal "${lines[3]}" "down --remove-orphans --volumes" From 4dbb936cf5e1b0937eb3f33392f83805caf47d70 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 13:47:17 -0700 Subject: [PATCH 07/52] exit upon getting term signal, otherwise service restarts --- commands/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 5cd89170..8cab3dca 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -454,10 +454,10 @@ ensure_stopped() { compose_cleanup ${run_service} echo '~~~ Last log lines that may be missing above (if container was not already removed)' docker logs "${container_name}" || true - exitcode='TRAP' + exit $1 } -trap ensure_stopped SIGINT SIGTERM SIGQUIT +trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then group_type="---" From 846e1bce4a8485d11ec82841e53167e35aad46f5 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 13:59:03 -0700 Subject: [PATCH 08/52] Don't run docker command in subshell, obviously signals wont make it\! --- commands/run.sh | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 8cab3dca..a22d2a6f 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -465,17 +465,10 @@ else group_type="+++" fi -# Disable -e to prevent cancelling step if the command fails for whatever reason -set +e -( # subshell is necessary to trap signals (compose v2 fails to stop otherwise) - echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 - run_docker_compose "${run_params[@]}" -) +echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 +run_docker_compose "${run_params[@]}" exitcode=$? -# Restore -e as an option. -set -e - if [[ $exitcode = "TRAP" ]]; then # command failed due to cancellation signal, make sure there is an error but no further output exitcode=-1 From e5fd4406d13aa202c756ded3784ff62d21415045 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 15:13:17 -0700 Subject: [PATCH 09/52] rewrite run.sh to use docker compose exec form. All the tests broke, not sure how to fix... --- commands/run.sh | 74 ++++++++++++++++++++++--------------------------- lib/shared.bash | 2 ++ 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index a22d2a6f..8b0ee88b 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -362,7 +362,10 @@ if [[ $dependency_exitcode -ne 0 ]] ; then return $dependency_exitcode fi -shell=() +# Assemble the shell and command arguments into the docker arguments +commands=() +display_command=() + shell_disabled=1 result=() @@ -388,20 +391,24 @@ elif [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" ]] ; then elif plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL ; then shell_disabled='' for arg in "${result[@]}" ; do - shell+=("$arg") + commands+=("$arg") done fi # Set a default shell if one is needed -if [[ -z $shell_disabled ]] && [[ ${#shell[@]} -eq 0 ]] ; then +if [[ -z $shell_disabled ]] && [[ ${#commands[@]} -eq 0 ]] ; then if is_windows ; then - shell=("CMD.EXE" "/c") + commands=("CMD.EXE" "/c") else - shell=("/bin/sh" "-e" "-c") + commands=("/bin/sh" "-e" "-c") fi fi -command=() +if [[ ${#commands[@]} -gt 0 ]] ; then + for shell_arg in "${commands[@]}" ; do + display_command+=("$shell_arg") + done +fi # Show a helpful error message if string version of command is used if [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND:-}" ]] ; then @@ -411,46 +418,28 @@ fi # Parse plugin command if provided if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then - for arg in "${result[@]}" ; do - command+=("$arg") - done -fi - -if [[ ${#command[@]} -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then - echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" - exit 1 -fi - -# Assemble the shell and command arguments into the docker arguments - -display_command=() - -if [[ ${#shell[@]} -gt 0 ]] ; then - for shell_arg in "${shell[@]}" ; do - run_params+=("$shell_arg") - display_command+=("$shell_arg") - done -fi - -if [[ -n "${BUILDKITE_COMMAND}" ]] ; then - if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then - # An array of commands in the step will be a single string with multiple lines - # This breaks a lot of things here so we will print a warning for user to be aware - echo "⚠️ Warning: The command received has multiple lines." - echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." + if [[ "${#result[@]}" -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" + exit 1 + elif [[ "${#result[@]}" -gt 0 ]] ; then + for arg in "${result[@]}" ; do + commands+=("$arg") + display_command+=("$arg") + done + elif [[ -n "${BUILDKITE_COMMAND}" ]] ; then + commands+=("${BUILDKITE_COMMAND}") + display_command+=("'${BUILDKITE_COMMAND}'") fi - run_params+=("${BUILDKITE_COMMAND}") - display_command+=("'${BUILDKITE_COMMAND}'") -elif [[ ${#command[@]} -gt 0 ]] ; then - for command_arg in "${command[@]}" ; do - run_params+=("$command_arg") - display_command+=("${command_arg}") - done fi +SUBPID="" ensure_stopped() { echo '+++ :warning: Signal received, stopping container gracefully' # docker stop "${container_name}" || true + # if [ -n "$SUBPID" ]; then + # echo "~~~ Subshell found, sending SIGTERM to $SUBPID" + # kill -s SIGTERM "$SUBPID" + # fi compose_cleanup ${run_service} echo '~~~ Last log lines that may be missing above (if container was not already removed)' docker logs "${container_name}" || true @@ -466,7 +455,8 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 -run_docker_compose "${run_params[@]}" +# SUBPID=$BASHPID +run_docker_compose "${run_params[@]}" ["${commands[@]@Q}"] exitcode=$? if [[ $exitcode = "TRAP" ]]; then @@ -476,6 +466,8 @@ elif [[ $exitcode -ne 0 ]] ; then echo "^^^ +++" echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" echo "${run_params[@]}" + echo "commands:" + echo "${commands[@]}" fi if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then diff --git a/lib/shared.bash b/lib/shared.bash index 26d48a98..8ec34f02 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -255,6 +255,8 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") + echo "running: ${command[@]} $@" + plugin_prompt_and_run "${command[@]}" "$@" } From 99c0fc3b17d7ecc3d660305e0b20bd6cdcdc5b6b Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 15:41:50 -0700 Subject: [PATCH 10/52] revert previous since it isn't working --- commands/run.sh | 74 +++++++++++++++++++++++++++---------------------- lib/shared.bash | 2 -- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 8b0ee88b..a22d2a6f 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -362,10 +362,7 @@ if [[ $dependency_exitcode -ne 0 ]] ; then return $dependency_exitcode fi -# Assemble the shell and command arguments into the docker arguments -commands=() -display_command=() - +shell=() shell_disabled=1 result=() @@ -391,24 +388,20 @@ elif [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" ]] ; then elif plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL ; then shell_disabled='' for arg in "${result[@]}" ; do - commands+=("$arg") + shell+=("$arg") done fi # Set a default shell if one is needed -if [[ -z $shell_disabled ]] && [[ ${#commands[@]} -eq 0 ]] ; then +if [[ -z $shell_disabled ]] && [[ ${#shell[@]} -eq 0 ]] ; then if is_windows ; then - commands=("CMD.EXE" "/c") + shell=("CMD.EXE" "/c") else - commands=("/bin/sh" "-e" "-c") + shell=("/bin/sh" "-e" "-c") fi fi -if [[ ${#commands[@]} -gt 0 ]] ; then - for shell_arg in "${commands[@]}" ; do - display_command+=("$shell_arg") - done -fi +command=() # Show a helpful error message if string version of command is used if [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND:-}" ]] ; then @@ -418,28 +411,46 @@ fi # Parse plugin command if provided if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then - if [[ "${#result[@]}" -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then - echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" - exit 1 - elif [[ "${#result[@]}" -gt 0 ]] ; then - for arg in "${result[@]}" ; do - commands+=("$arg") - display_command+=("$arg") - done - elif [[ -n "${BUILDKITE_COMMAND}" ]] ; then - commands+=("${BUILDKITE_COMMAND}") - display_command+=("'${BUILDKITE_COMMAND}'") + for arg in "${result[@]}" ; do + command+=("$arg") + done +fi + +if [[ ${#command[@]} -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" + exit 1 +fi + +# Assemble the shell and command arguments into the docker arguments + +display_command=() + +if [[ ${#shell[@]} -gt 0 ]] ; then + for shell_arg in "${shell[@]}" ; do + run_params+=("$shell_arg") + display_command+=("$shell_arg") + done +fi + +if [[ -n "${BUILDKITE_COMMAND}" ]] ; then + if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then + # An array of commands in the step will be a single string with multiple lines + # This breaks a lot of things here so we will print a warning for user to be aware + echo "⚠️ Warning: The command received has multiple lines." + echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." fi + run_params+=("${BUILDKITE_COMMAND}") + display_command+=("'${BUILDKITE_COMMAND}'") +elif [[ ${#command[@]} -gt 0 ]] ; then + for command_arg in "${command[@]}" ; do + run_params+=("$command_arg") + display_command+=("${command_arg}") + done fi -SUBPID="" ensure_stopped() { echo '+++ :warning: Signal received, stopping container gracefully' # docker stop "${container_name}" || true - # if [ -n "$SUBPID" ]; then - # echo "~~~ Subshell found, sending SIGTERM to $SUBPID" - # kill -s SIGTERM "$SUBPID" - # fi compose_cleanup ${run_service} echo '~~~ Last log lines that may be missing above (if container was not already removed)' docker logs "${container_name}" || true @@ -455,8 +466,7 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 -# SUBPID=$BASHPID -run_docker_compose "${run_params[@]}" ["${commands[@]@Q}"] +run_docker_compose "${run_params[@]}" exitcode=$? if [[ $exitcode = "TRAP" ]]; then @@ -466,8 +476,6 @@ elif [[ $exitcode -ne 0 ]] ; then echo "^^^ +++" echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" echo "${run_params[@]}" - echo "commands:" - echo "${commands[@]}" fi if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then diff --git a/lib/shared.bash b/lib/shared.bash index 8ec34f02..26d48a98 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -255,8 +255,6 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") - echo "running: ${command[@]} $@" - plugin_prompt_and_run "${command[@]}" "$@" } From 722c50efcba33d2c2304f6d328a78374bcd9204c Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 16:01:42 -0700 Subject: [PATCH 11/52] attempt to run shell script without sh -c which creates new shell --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index a22d2a6f..7d9f04aa 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -397,7 +397,7 @@ if [[ -z $shell_disabled ]] && [[ ${#shell[@]} -eq 0 ]] ; then if is_windows ; then shell=("CMD.EXE" "/c") else - shell=("/bin/sh" "-e" "-c") + # shell=("/bin/sh" "-e" "-c") fi fi From 47d200dcbb223a8f7b581194f048a48b47a6ec61 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 16:09:20 -0700 Subject: [PATCH 12/52] syntax fix --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 7d9f04aa..9f6b0fdf 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -396,7 +396,7 @@ fi if [[ -z $shell_disabled ]] && [[ ${#shell[@]} -eq 0 ]] ; then if is_windows ; then shell=("CMD.EXE" "/c") - else + # else # shell=("/bin/sh" "-e" "-c") fi fi From aa1220c5e9e01a1ec6b93aced7dd6084f552a278 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Tue, 30 Apr 2024 16:21:12 -0700 Subject: [PATCH 13/52] try subshell with direct command --- commands/run.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 9f6b0fdf..08018fa9 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -465,9 +465,11 @@ else group_type="+++" fi -echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 -run_docker_compose "${run_params[@]}" -exitcode=$? +( + echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 + run_docker_compose "${run_params[@]}" + exitcode=$? +) if [[ $exitcode = "TRAP" ]]; then # command failed due to cancellation signal, make sure there is an error but no further output From 8e061298b1640b84193bf03666aa18709fa2ecc3 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Wed, 1 May 2024 13:04:30 -0700 Subject: [PATCH 14/52] refactor subshell to make it a little clearer, prevent exitcode -u issue --- commands/run.sh | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 08018fa9..fe926563 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -459,26 +459,29 @@ ensure_stopped() { trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT -if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then - group_type="---" -else - group_type="+++" -fi +# Disable -e to prevent cancelling step if the command fails for whatever reason +set +e +exitcode=0 ( - echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2 + if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then + group_type="---" + else + group_type="+++" + fi + + echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" run_docker_compose "${run_params[@]}" + exitcode=$? + if [[ $exitcode -ne 0 ]] ; then + echo "^^^ +++" + echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" + echo "${run_params[@]}" + fi ) - -if [[ $exitcode = "TRAP" ]]; then - # command failed due to cancellation signal, make sure there is an error but no further output - exitcode=-1 -elif [[ $exitcode -ne 0 ]] ; then - echo "^^^ +++" - echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" - echo "${run_params[@]}" -fi +# Restore -e as an option. +set -e if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then if [[ "$(plugin_read_config CHECK_LINKED_CONTAINERS "true")" != "false" ]] ; then @@ -488,4 +491,4 @@ if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then fi fi -return "$exitcode" +return "$exitcode" \ No newline at end of file From 1a5d072e7bb54d5c9b337b5afc60eb119407b5dd Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Wed, 1 May 2024 14:39:13 -0700 Subject: [PATCH 15/52] try not using a subshell again --- commands/run.sh | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index fe926563..30553516 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -463,23 +463,22 @@ trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT # Disable -e to prevent cancelling step if the command fails for whatever reason set +e exitcode=0 -( - if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then - group_type="---" - else - group_type="+++" - fi - echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" - run_docker_compose "${run_params[@]}" +if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then + group_type="---" +else + group_type="+++" +fi - exitcode=$? - if [[ $exitcode -ne 0 ]] ; then - echo "^^^ +++" - echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" - echo "${run_params[@]}" - fi -) +echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" +run_docker_compose "${run_params[@]}" + +exitcode=$? +if [[ $exitcode -ne 0 ]] ; then + echo "^^^ +++" + echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" + echo "${run_params[@]}" +fi # Restore -e as an option. set -e From 812ece236d2412f02422cbcb86eb9e5f47df2780 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Wed, 1 May 2024 16:31:41 -0700 Subject: [PATCH 16/52] trying to clean up run file so I can understand the docker calls --- commands/pull.sh | 72 ++++++++ commands/run.sh | 328 +++------------------------------- commands/run_cmd_generator.sh | 234 ++++++++++++++++++++++++ docker-compose.yml | 1 + 4 files changed, 335 insertions(+), 300 deletions(-) create mode 100644 commands/pull.sh create mode 100644 commands/run_cmd_generator.sh diff --git a/commands/pull.sh b/commands/pull.sh new file mode 100644 index 00000000..e9ce5ecf --- /dev/null +++ b/commands/pull.sh @@ -0,0 +1,72 @@ +#!/bin/bash +set -ueo pipefail + +function pull() { + prebuilt_candidates=("$1") + + pull_services=() + pull_params=() + + override_file="docker-compose.buildkite-${BUILDKITE_BUILD_NUMBER}-override.yml" + pull_retries="$(plugin_read_config PULL_RETRIES "0")" + + # Build a list of services that need to be pulled down + while read -r name ; do + if [[ -n "$name" ]] ; then + pull_services+=("$name") + + if ! in_array "$name" "${prebuilt_candidates[@]}" ; then + prebuilt_candidates+=("$name") + fi + fi + done <<< "$(plugin_read_list PULL)" + + # A list of tuples of [service image cache_from] for build_image_override_file + prebuilt_service_overrides=() + prebuilt_services=() + + # We look for a prebuilt images for all the pull services and the run_service. + prebuilt_image_override="$(plugin_read_config RUN_IMAGE)" + for service_name in "${prebuilt_candidates[@]}" ; do + if [[ -n "$prebuilt_image_override" ]] && [[ "$service_name" == "$1" ]] ; then + echo "~~~ :docker: Overriding run image for $service_name" + prebuilt_image="$prebuilt_image_override" + elif prebuilt_image=$(get_prebuilt_image "$service_name") ; then + echo "~~~ :docker: Found a pre-built image for $service_name" + fi + + if [[ -n "$prebuilt_image" ]] ; then + prebuilt_service_overrides+=("$service_name" "$prebuilt_image" "" 0 0) + prebuilt_services+=("$service_name") + + # If it's prebuilt, we need to pull it down + if [[ -z "${pull_services:-}" ]] || ! in_array "$service_name" "${pull_services[@]}" ; then + pull_services+=("$service_name") + fi + fi + done + + exitcode=1 + # If there are any prebuilts, we need to generate an override docker-compose file + if [[ ${#prebuilt_services[@]} -gt 0 ]] ; then + echo "~~~ :docker: Creating docker-compose override file for prebuilt services" + build_image_override_file "${prebuilt_service_overrides[@]}" | tee "$override_file" + pull_params+=(-f "$override_file") + exitcode=0 + fi + + # If there are multiple services to pull, run it in parallel (although this is now the default) + if [[ ${#pull_services[@]} -gt 1 ]] ; then + pull_params+=("pull" "--parallel" "${pull_services[@]}") + elif [[ ${#pull_services[@]} -eq 1 ]] ; then + pull_params+=("pull" "${pull_services[0]}") + fi + + # Pull down specified services + if [[ ${#pull_services[@]} -gt 0 ]] && [[ "$(plugin_read_config SKIP_PULL "false")" != "true" ]]; then + echo "~~~ :docker: Pulling services ${pull_services[0]}" + retry "$pull_retries" run_docker_compose "${pull_params[@]}" + fi + + return $exitcode +} \ No newline at end of file diff --git a/commands/run.sh b/commands/run.sh index 30553516..48d3df17 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -2,6 +2,14 @@ set -ueo pipefail . "$DIR/../lib/run.bash" +. "$DIR/../commands/pull.sh" +. "$DIR/../commands/run_cmd_generator.sh" + +# Can't set both user and propagate-uid-gid +if [[ -n "$(plugin_read_config USER)" ]] && [[ -n "$(plugin_read_config PROPAGATE_UID_GID)" ]]; then + echo "+++ Error: Can't set both user and propagate-uid-gid" + exit 1 +fi # Run takes a service name, pulls down any pre-built image for that name # and then runs docker-compose run a generated project name @@ -9,9 +17,6 @@ set -ueo pipefail run_service="$(plugin_read_config RUN)" container_name="$(docker_compose_project_name)_${run_service}_build_${BUILDKITE_BUILD_NUMBER}" override_file="docker-compose.buildkite-${BUILDKITE_BUILD_NUMBER}-override.yml" -pull_retries="$(plugin_read_config PULL_RETRIES "0")" -mount_checkout="$(plugin_read_config MOUNT_CHECKOUT "false")" -workdir='' expand_headers_on_error() { echo "^^^ +++" @@ -20,305 +25,15 @@ trap expand_headers_on_error ERR test -f "$override_file" && rm "$override_file" -run_params=() -pull_params=() -up_params=() -pull_services=() -prebuilt_candidates=("$run_service") - -# Build a list of services that need to be pulled down -while read -r name ; do - if [[ -n "$name" ]] ; then - pull_services+=("$name") - - if ! in_array "$name" "${prebuilt_candidates[@]}" ; then - prebuilt_candidates+=("$name") - fi - fi -done <<< "$(plugin_read_list PULL)" - -# A list of tuples of [service image cache_from] for build_image_override_file -prebuilt_service_overrides=() -prebuilt_services=() - -# We look for a prebuilt images for all the pull services and the run_service. -prebuilt_image_override="$(plugin_read_config RUN_IMAGE)" -for service_name in "${prebuilt_candidates[@]}" ; do - if [[ -n "$prebuilt_image_override" ]] && [[ "$service_name" == "$run_service" ]] ; then - echo "~~~ :docker: Overriding run image for $service_name" - prebuilt_image="$prebuilt_image_override" - elif prebuilt_image=$(get_prebuilt_image "$service_name") ; then - echo "~~~ :docker: Found a pre-built image for $service_name" - fi - - if [[ -n "$prebuilt_image" ]] ; then - prebuilt_service_overrides+=("$service_name" "$prebuilt_image" "" 0 0) - prebuilt_services+=("$service_name") - - # If it's prebuilt, we need to pull it down - if [[ -z "${pull_services:-}" ]] || ! in_array "$service_name" "${pull_services[@]}" ; then - pull_services+=("$service_name") - fi - fi -done - -# If there are any prebuilts, we need to generate an override docker-compose file -if [[ ${#prebuilt_services[@]} -gt 0 ]] ; then - echo "~~~ :docker: Creating docker-compose override file for prebuilt services" - build_image_override_file "${prebuilt_service_overrides[@]}" | tee "$override_file" - run_params+=(-f "$override_file") - pull_params+=(-f "$override_file") - up_params+=(-f "$override_file") -fi - -# If there are multiple services to pull, run it in parallel (although this is now the default) -if [[ ${#pull_services[@]} -gt 1 ]] ; then - pull_params+=("pull" "--parallel" "${pull_services[@]}") -elif [[ ${#pull_services[@]} -eq 1 ]] ; then - pull_params+=("pull" "${pull_services[0]}") -fi - -# Pull down specified services -if [[ ${#pull_services[@]} -gt 0 ]] && [[ "$(plugin_read_config SKIP_PULL "false")" != "true" ]]; then - echo "~~~ :docker: Pulling services ${pull_services[0]}" - retry "$pull_retries" run_docker_compose "${pull_params[@]}" -fi - -# We set a predictable container name so we can find it and inspect it later on -run_params+=("run" "--name" "$container_name") - -if [[ "$(plugin_read_config RUN_LABELS "true")" =~ ^(true|on|1)$ ]]; then - # Add useful labels to run container - run_params+=( - "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" - "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" - "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" - "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" - "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" - "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" - "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" - "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" - ) -fi - -# append env vars provided in ENV or ENVIRONMENT, these are newline delimited -while IFS=$'\n' read -r env ; do - [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") -done <<< "$(printf '%s\n%s' \ - "$(plugin_read_list ENV)" \ - "$(plugin_read_list ENVIRONMENT)")" - -# Propagate all environment variables into the container if requested -if [[ "$(plugin_read_config PROPAGATE_ENVIRONMENT "false")" =~ ^(true|on|1)$ ]] ; then - if [[ -n "${BUILDKITE_ENV_FILE:-}" ]] ; then - # Read in the env file and convert to --env params for docker - # This is because --env-file doesn't support newlines or quotes per https://docs.docker.com/compose/env-file/#syntax-rules - while read -r var; do - run_params+=("-e" "${var%%=*}") - done < "${BUILDKITE_ENV_FILE}" - else - echo -n "🚨 Not propagating environment variables to container as \$BUILDKITE_ENV_FILE is not set" - fi -fi - -# Propagate AWS credentials if requested -if [[ "$(plugin_read_config PROPAGATE_AWS_AUTH_TOKENS "false")" =~ ^(true|on|1)$ ]] ; then - if [[ -n "${AWS_ACCESS_KEY_ID:-}" ]] ; then - run_params+=( --env "AWS_ACCESS_KEY_ID" ) - fi - if [[ -n "${AWS_SECRET_ACCESS_KEY:-}" ]] ; then - run_params+=( --env "AWS_SECRET_ACCESS_KEY" ) - fi - if [[ -n "${AWS_SESSION_TOKEN:-}" ]] ; then - run_params+=( --env "AWS_SESSION_TOKEN" ) - fi - if [[ -n "${AWS_REGION:-}" ]] ; then - run_params+=( --env "AWS_REGION" ) - fi - if [[ -n "${AWS_DEFAULT_REGION:-}" ]] ; then - run_params+=( --env "AWS_DEFAULT_REGION" ) - fi - if [[ -n "${AWS_ROLE_ARN:-}" ]] ; then - run_params+=( --env "AWS_ROLE_ARN" ) - fi - if [[ -n "${AWS_STS_REGIONAL_ENDPOINTS:-}" ]] ; then - run_params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" ) - fi - # Pass ECS variables when the agent is running in ECS - # https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html - if [[ -n "${AWS_CONTAINER_CREDENTIALS_FULL_URI:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" ) - fi - if [[ -n "${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ) - fi - if [[ -n "${AWS_CONTAINER_AUTHORIZATION_TOKEN:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" ) - fi - # Pass EKS variables when the agent is running in EKS - # https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html - if [[ -n "${AWS_WEB_IDENTITY_TOKEN_FILE:-}" ]] ; then - run_params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" ) - # Add the token file as a volume - run_params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" ) - fi -fi - -# If requested, propagate a set of env vars as listed in a given env var to the -# container. -if [[ -n "$(plugin_read_config ENV_PROPAGATION_LIST)" ]]; then - env_propagation_list_var="$(plugin_read_config ENV_PROPAGATION_LIST)" - if [[ -z "${!env_propagation_list_var:-}" ]]; then - echo -n "env-propagation-list desired, but ${env_propagation_list_var} is not defined!" - exit 1 - fi - for var in ${!env_propagation_list_var}; do - run_params+=("-e" "$var") - done -fi - -while IFS=$'\n' read -r vol ; do - [[ -n "${vol:-}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")") -done <<< "$(plugin_read_list VOLUMES)" - -# Parse BUILDKITE_DOCKER_DEFAULT_VOLUMES delimited by semi-colons, normalized to -# ignore spaces and leading or trailing semi-colons -IFS=';' read -r -a default_volumes <<< "${BUILDKITE_DOCKER_DEFAULT_VOLUMES:-}" -for vol in "${default_volumes[@]:-}" ; do - trimmed_vol="$(echo -n "$vol" | sed -e 's/^[[:space:]]*//' | sed -e 's/[[:space:]]*$//')" - [[ -n "$trimmed_vol" ]] && run_params+=("-v" "$(expand_relative_volume_path "$trimmed_vol")") -done - -# If there's a git mirror, mount it so that git references can be followed. -if [[ -n "${BUILDKITE_REPO_MIRROR:-}" ]]; then - run_params+=("-v" "$BUILDKITE_REPO_MIRROR:$BUILDKITE_REPO_MIRROR:ro") -fi - -tty_default='false' -workdir_default="/workdir" -pwd_default="$PWD" -run_dependencies="true" - -# Set operating system specific defaults -if is_windows ; then - workdir_default="C:\\workdir" - # escaping /C is a necessary workaround for an issue with Git for Windows 2.24.1.2 - # https://github.com/git-for-windows/git/issues/2442 - pwd_default="$(cmd.exe //C "echo %CD%")" -fi - -# Disable allocating a TTY -if [[ "$(plugin_read_config TTY "$tty_default")" == "false" ]] ; then - run_params+=(-T) -fi - -# Optionally disable dependencies -if [[ "$(plugin_read_config DEPENDENCIES "true")" == "false" ]] ; then - run_params+=(--no-deps) - run_dependencies="false" -elif [[ "$(plugin_read_config PRE_RUN_DEPENDENCIES "true")" == "false" ]]; then - run_dependencies="false" -fi - -if [[ -n "$(plugin_read_config WORKDIR)" ]] || [[ "${mount_checkout}" == "true" ]]; then - workdir="$(plugin_read_config WORKDIR "$workdir_default")" -fi - -if [[ -n "${workdir}" ]] ; then - run_params+=("--workdir=${workdir}") -fi - -if [[ "${mount_checkout}" == "true" ]]; then - run_params+=("-v" "${pwd_default}:${workdir}") -elif [[ "${mount_checkout}" =~ ^/.*$ ]]; then - run_params+=("-v" "${pwd_default}:${mount_checkout}") -elif [[ "${mount_checkout}" != "false" ]]; then - echo -n "🚨 mount-checkout should be either true or an absolute path to use as a mountpoint" - exit 1 -fi - -# Can't set both user and propagate-uid-gid -if [[ -n "$(plugin_read_config USER)" ]] && [[ -n "$(plugin_read_config PROPAGATE_UID_GID)" ]]; then - echo "+++ Error: Can't set both user and propagate-uid-gid" - exit 1 -fi - -# Optionally run as specified username or uid -if [[ -n "$(plugin_read_config USER)" ]] ; then - run_params+=("--user=$(plugin_read_config USER)") -fi - -# Optionally run as specified username or uid -if [[ "$(plugin_read_config PROPAGATE_UID_GID "false")" == "true" ]] ; then - run_params+=("--user=$(id -u):$(id -g)") -fi - -# Enable alias support for networks -if [[ "$(plugin_read_config USE_ALIASES "false")" == "true" ]] ; then - run_params+=(--use-aliases) -fi - -# Optionally remove containers after run -if [[ "$(plugin_read_config RM "true")" == "true" ]]; then - run_params+=(--rm) -fi - -# Optionally sets --entrypoint -if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then - run_params+=(--entrypoint) - run_params+=("$(plugin_read_config ENTRYPOINT)") -fi - -# Mount ssh-agent socket and known_hosts -if [[ ! "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-false}" = 'false' ]] ; then - if [[ -z "${SSH_AUTH_SOCK:-}" ]] ; then - echo "+++ 🚨 \$SSH_AUTH_SOCK isn't set, has ssh-agent started?" - exit 1 - fi - if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then - echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} does not exist or is not a socket, was ssh-agent started?" - exit 1 - fi - - if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-''}" =~ ^(true|on|1)$ ]]; then - MOUNT_PATH=/root - else - MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT}" - fi - - run_params+=( - "-e" "SSH_AUTH_SOCK=/ssh-agent" - "-v" "${SSH_AUTH_SOCK}:/ssh-agent" - "-v" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts" - ) -fi - -# Optionally handle the mount-buildkite-agent option -if [[ "$(plugin_read_config MOUNT_BUILDKITE_AGENT "false")" == "true" ]]; then - if [[ -z "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then - if ! command -v buildkite-agent >/dev/null 2>&1 ; then - echo -n "+++ 🚨 Failed to find buildkite-agent in PATH to mount into container, " - echo "you can disable this behaviour with 'mount-buildkite-agent:false'" - else - BUILDKITE_AGENT_BINARY_PATH=$(command -v buildkite-agent) - fi - fi -fi +set -e +pull "$run_service" +pulled_status=$? +set +e -# Mount buildkite-agent if we have a path for it -if [[ -n "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then - run_params+=( - "-e" "BUILDKITE_JOB_ID" - "-e" "BUILDKITE_BUILD_ID" - "-e" "BUILDKITE_AGENT_ACCESS_TOKEN" - "-v" "$BUILDKITE_AGENT_BINARY_PATH:/usr/bin/buildkite-agent" - ) -fi +up_params=() -# Optionally expose service ports -if [[ "$(plugin_read_config SERVICE_PORTS "false")" == "true" ]]; then - run_params+=(--service-ports) -fi +run_params=() +generate_run_args $container_name $pulled_status run_params+=("$run_service") @@ -342,12 +57,25 @@ if [[ "$(plugin_read_config QUIET_PULL "false")" == "true" ]] ; then fi dependency_exitcode=0 + + +run_dependencies="true" +# Optionally disable dependencies +if [[ "$(plugin_read_config DEPENDENCIES "true")" == "false" ]] ; then + run_params+=(--no-deps) + run_dependencies="false" +elif [[ "$(plugin_read_config PRE_RUN_DEPENDENCIES "true")" == "false" ]]; then + run_dependencies="false" +fi + if [[ "${run_dependencies}" == "true" ]] ; then # Start up service dependencies in a different header to keep the main run with less noise echo "~~~ :docker: Starting dependencies" run_docker_compose "${up_params[@]}" -d --scale "${run_service}=0" "${run_service}" || dependency_exitcode=$? fi + + if [[ $dependency_exitcode -ne 0 ]] ; then # Dependent services failed to start. echo "^^^ +++" diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh new file mode 100644 index 00000000..a1928d74 --- /dev/null +++ b/commands/run_cmd_generator.sh @@ -0,0 +1,234 @@ +#!/bin/bash +set -ueo pipefail + +# We set a predictable container name so we can find it and inspect it later on +function generate_run_args() { + local -n run_params+=("run" "--name" "$1") + pulled_services=$2 + + if [[ $pulled_services -eq 0 ]] ; then + echo "~~~ :docker: Creating docker-compose override file for prebuilt services" + run_params+=(-f "$override_file") + up_params+=(-f "$override_file") + fi + + if [[ "$(plugin_read_config RUN_LABELS "true")" =~ ^(true|on|1)$ ]]; then + # Add useful labels to run container + run_params+=( + "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" + "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" + "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" + "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" + "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" + "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" + "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" + "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" + ) + fi + + # append env vars provided in ENV or ENVIRONMENT, these are newline delimited + while IFS=$'\n' read -r env ; do + [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") + done <<< "$(printf '%s\n%s' \ + "$(plugin_read_list ENV)" \ + "$(plugin_read_list ENVIRONMENT)")" + + # Propagate all environment variables into the container if requested + if [[ "$(plugin_read_config PROPAGATE_ENVIRONMENT "false")" =~ ^(true|on|1)$ ]] ; then + if [[ -n "${BUILDKITE_ENV_FILE:-}" ]] ; then + # Read in the env file and convert to --env params for docker + # This is because --env-file doesn't support newlines or quotes per https://docs.docker.com/compose/env-file/#syntax-rules + while read -r var; do + run_params+=("-e" "${var%%=*}") + done < "${BUILDKITE_ENV_FILE}" + else + echo -n "🚨 Not propagating environment variables to container as \$BUILDKITE_ENV_FILE is not set" + fi + fi + + # Propagate AWS credentials if requested + if [[ "$(plugin_read_config PROPAGATE_AWS_AUTH_TOKENS "false")" =~ ^(true|on|1)$ ]] ; then + if [[ -n "${AWS_ACCESS_KEY_ID:-}" ]] ; then + run_params+=( --env "AWS_ACCESS_KEY_ID" ) + fi + if [[ -n "${AWS_SECRET_ACCESS_KEY:-}" ]] ; then + run_params+=( --env "AWS_SECRET_ACCESS_KEY" ) + fi + if [[ -n "${AWS_SESSION_TOKEN:-}" ]] ; then + run_params+=( --env "AWS_SESSION_TOKEN" ) + fi + if [[ -n "${AWS_REGION:-}" ]] ; then + run_params+=( --env "AWS_REGION" ) + fi + if [[ -n "${AWS_DEFAULT_REGION:-}" ]] ; then + run_params+=( --env "AWS_DEFAULT_REGION" ) + fi + if [[ -n "${AWS_ROLE_ARN:-}" ]] ; then + run_params+=( --env "AWS_ROLE_ARN" ) + fi + if [[ -n "${AWS_STS_REGIONAL_ENDPOINTS:-}" ]] ; then + run_params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" ) + fi + # Pass ECS variables when the agent is running in ECS + # https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html + if [[ -n "${AWS_CONTAINER_CREDENTIALS_FULL_URI:-}" ]] ; then + run_params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" ) + fi + if [[ -n "${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI:-}" ]] ; then + run_params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ) + fi + if [[ -n "${AWS_CONTAINER_AUTHORIZATION_TOKEN:-}" ]] ; then + run_params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" ) + fi + # Pass EKS variables when the agent is running in EKS + # https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html + if [[ -n "${AWS_WEB_IDENTITY_TOKEN_FILE:-}" ]] ; then + run_params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" ) + # Add the token file as a volume + run_params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" ) + fi + fi + + # If requested, propagate a set of env vars as listed in a given env var to the + # container. + if [[ -n "$(plugin_read_config ENV_PROPAGATION_LIST)" ]]; then + env_propagation_list_var="$(plugin_read_config ENV_PROPAGATION_LIST)" + if [[ -z "${!env_propagation_list_var:-}" ]]; then + echo -n "env-propagation-list desired, but ${env_propagation_list_var} is not defined!" + exit 1 + fi + for var in ${!env_propagation_list_var}; do + run_params+=("-e" "$var") + done + fi + + while IFS=$'\n' read -r vol ; do + [[ -n "${vol:-}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")") + done <<< "$(plugin_read_list VOLUMES)" + + # Parse BUILDKITE_DOCKER_DEFAULT_VOLUMES delimited by semi-colons, normalized to + # ignore spaces and leading or trailing semi-colons + IFS=';' read -r -a default_volumes <<< "${BUILDKITE_DOCKER_DEFAULT_VOLUMES:-}" + for vol in "${default_volumes[@]:-}" ; do + trimmed_vol="$(echo -n "$vol" | sed -e 's/^[[:space:]]*//' | sed -e 's/[[:space:]]*$//')" + [[ -n "$trimmed_vol" ]] && run_params+=("-v" "$(expand_relative_volume_path "$trimmed_vol")") + done + + # If there's a git mirror, mount it so that git references can be followed. + if [[ -n "${BUILDKITE_REPO_MIRROR:-}" ]]; then + run_params+=("-v" "$BUILDKITE_REPO_MIRROR:$BUILDKITE_REPO_MIRROR:ro") + fi + + # Disable allocating a TTY + tty_default='false' + if [[ "$(plugin_read_config TTY "$tty_default")" == "false" ]] ; then + run_params+=(-T) + fi + + workdir='' + workdir_default="/workdir" + pwd_default="$PWD" + + # Set operating system specific defaults + if is_windows ; then + workdir_default="C:\\workdir" + # escaping /C is a necessary workaround for an issue with Git for Windows 2.24.1.2 + # https://github.com/git-for-windows/git/issues/2442 + pwd_default="$(cmd.exe //C "echo %CD%")" + fi + + mount_checkout="$(plugin_read_config MOUNT_CHECKOUT "false")" + if [[ -n "$(plugin_read_config WORKDIR)" ]] || [[ "${mount_checkout}" == "true" ]]; then + workdir="$(plugin_read_config WORKDIR "$workdir_default")" + fi + + if [[ -n "${workdir}" ]] ; then + run_params+=("--workdir=${workdir}") + fi + + if [[ "${mount_checkout}" == "true" ]]; then + run_params+=("-v" "${pwd_default}:${workdir}") + elif [[ "${mount_checkout}" =~ ^/.*$ ]]; then + run_params+=("-v" "${pwd_default}:${mount_checkout}") + elif [[ "${mount_checkout}" != "false" ]]; then + echo -n "🚨 mount-checkout should be either true or an absolute path to use as a mountpoint" + exit 1 + fi + + # Optionally run as specified username or uid + if [[ -n "$(plugin_read_config USER)" ]] ; then + run_params+=("--user=$(plugin_read_config USER)") + fi + + # Optionally run as specified username or uid + if [[ "$(plugin_read_config PROPAGATE_UID_GID "false")" == "true" ]] ; then + run_params+=("--user=$(id -u):$(id -g)") + fi + + # Enable alias support for networks + if [[ "$(plugin_read_config USE_ALIASES "false")" == "true" ]] ; then + run_params+=(--use-aliases) + fi + + # Optionally remove containers after run + if [[ "$(plugin_read_config RM "true")" == "true" ]]; then + run_params+=(--rm) + fi + + # Optionally sets --entrypoint + if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then + run_params+=(--entrypoint) + run_params+=("$(plugin_read_config ENTRYPOINT)") + fi + + # Mount ssh-agent socket and known_hosts + if [[ ! "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-false}" = 'false' ]] ; then + if [[ -z "${SSH_AUTH_SOCK:-}" ]] ; then + echo "+++ 🚨 \$SSH_AUTH_SOCK isn't set, has ssh-agent started?" + exit 1 + fi + if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then + echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} does not exist or is not a socket, was ssh-agent started?" + exit 1 + fi + + if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-''}" =~ ^(true|on|1)$ ]]; then + MOUNT_PATH=/root + else + MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT}" + fi + + run_params+=( + "-e" "SSH_AUTH_SOCK=/ssh-agent" + "-v" "${SSH_AUTH_SOCK}:/ssh-agent" + "-v" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts" + ) + fi + + # Optionally handle the mount-buildkite-agent option + if [[ "$(plugin_read_config MOUNT_BUILDKITE_AGENT "false")" == "true" ]]; then + if [[ -z "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then + if ! command -v buildkite-agent >/dev/null 2>&1 ; then + echo -n "+++ 🚨 Failed to find buildkite-agent in PATH to mount into container, " + echo "you can disable this behaviour with 'mount-buildkite-agent:false'" + else + BUILDKITE_AGENT_BINARY_PATH=$(command -v buildkite-agent) + fi + fi + fi + + # Mount buildkite-agent if we have a path for it + if [[ -n "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then + run_params+=( + "-e" "BUILDKITE_JOB_ID" + "-e" "BUILDKITE_BUILD_ID" + "-e" "BUILDKITE_AGENT_ACCESS_TOKEN" + "-v" "$BUILDKITE_AGENT_BINARY_PATH:/usr/bin/buildkite-agent" + ) + fi + + # Optionally expose service ports + if [[ "$(plugin_read_config SERVICE_PORTS "false")" == "true" ]]; then + run_params+=(--service-ports) + fi +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 10c6e0b6..926bc540 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,5 +2,6 @@ version: '2' services: tests: image: buildkite/plugin-tester:v4.1.1 + command: ["bats", "tests/run.bats", "tests/cleanup.bats"] volumes: - ".:/plugin" From 30318582a95ad6ab7fbecc91d0309de9da3c5ab0 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 11:47:16 -0700 Subject: [PATCH 17/52] Remove e flag from file, just gets in the way --- commands/run.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 48d3df17..ae1b8ce3 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -ueo pipefail +set -uo pipefail . "$DIR/../lib/run.bash" . "$DIR/../commands/pull.sh" @@ -25,17 +25,8 @@ trap expand_headers_on_error ERR test -f "$override_file" && rm "$override_file" -set -e pull "$run_service" pulled_status=$? -set +e - -up_params=() - -run_params=() -generate_run_args $container_name $pulled_status - -run_params+=("$run_service") if [[ ! -f "$override_file" ]] ; then echo "+++ 🚨 No pre-built image found from a previous 'build' step for this service and config file." @@ -46,6 +37,15 @@ if [[ ! -f "$override_file" ]] ; then fi fi +up_params=() + +run_params=() +generate_run_args $container_name $pulled_status + +run_params+=("$run_service") + + + up_params+=("up") # this ensures that the array has elements to avoid issues with bash 4.3 if [[ "$(plugin_read_config WAIT "false")" == "true" ]] ; then From 570b262fb58c9ae6c4eea273bd5cb42b13e94db1 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 12:01:57 -0700 Subject: [PATCH 18/52] Tabbing fix, add some logging --- commands/pull.sh | 1 + commands/run.sh | 18 +-- commands/run_cmd_generator.sh | 252 +++++++++++++++++----------------- 3 files changed, 132 insertions(+), 139 deletions(-) diff --git a/commands/pull.sh b/commands/pull.sh index e9ce5ecf..fc4e7eea 100644 --- a/commands/pull.sh +++ b/commands/pull.sh @@ -68,5 +68,6 @@ function pull() { retry "$pull_retries" run_docker_compose "${pull_params[@]}" fi + echo "done pulling. exitcode: $exitcode" return $exitcode } \ No newline at end of file diff --git a/commands/run.sh b/commands/run.sh index ae1b8ce3..ca2d9c83 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -35,17 +35,16 @@ if [[ ! -f "$override_file" ]] ; then echo "The step specified that it was required" exit 1 fi +else + echo "~~~ :docker: Using pre-built image for $run_service" fi up_params=() - run_params=() generate_run_args $container_name $pulled_status +echo "run_params after func: ${run_params[@]}" run_params+=("$run_service") - - - up_params+=("up") # this ensures that the array has elements to avoid issues with bash 4.3 if [[ "$(plugin_read_config WAIT "false")" == "true" ]] ; then @@ -56,8 +55,8 @@ if [[ "$(plugin_read_config QUIET_PULL "false")" == "true" ]] ; then up_params+=("--quiet-pull") fi -dependency_exitcode=0 +dependency_exitcode=0 run_dependencies="true" # Optionally disable dependencies @@ -74,10 +73,7 @@ if [[ "${run_dependencies}" == "true" ]] ; then run_docker_compose "${up_params[@]}" -d --scale "${run_service}=0" "${run_service}" || dependency_exitcode=$? fi - - if [[ $dependency_exitcode -ne 0 ]] ; then - # Dependent services failed to start. echo "^^^ +++" echo "+++ 🚨 Failed to start dependencies" @@ -90,6 +86,7 @@ if [[ $dependency_exitcode -ne 0 ]] ; then return $dependency_exitcode fi + shell=() shell_disabled=1 result=() @@ -187,9 +184,6 @@ ensure_stopped() { trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT - -# Disable -e to prevent cancelling step if the command fails for whatever reason -set +e exitcode=0 if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then @@ -207,8 +201,6 @@ if [[ $exitcode -ne 0 ]] ; then echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" echo "${run_params[@]}" fi -# Restore -e as an option. -set -e if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then if [[ "$(plugin_read_config CHECK_LINKED_CONTAINERS "true")" != "false" ]] ; then diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh index a1928d74..82e51ea2 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_cmd_generator.sh @@ -7,122 +7,122 @@ function generate_run_args() { pulled_services=$2 if [[ $pulled_services -eq 0 ]] ; then - echo "~~~ :docker: Creating docker-compose override file for prebuilt services" - run_params+=(-f "$override_file") - up_params+=(-f "$override_file") + echo "~~~ :docker: Creating docker-compose override file for prebuilt services" + run_params+=(-f "$override_file") + up_params+=(-f "$override_file") fi if [[ "$(plugin_read_config RUN_LABELS "true")" =~ ^(true|on|1)$ ]]; then - # Add useful labels to run container - run_params+=( - "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" - "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" - "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" - "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" - "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" - "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" - "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" - "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" - ) + # Add useful labels to run container + run_params+=( + "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" + "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" + "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" + "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" + "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" + "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" + "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" + "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" + ) fi # append env vars provided in ENV or ENVIRONMENT, these are newline delimited while IFS=$'\n' read -r env ; do - [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") + [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") done <<< "$(printf '%s\n%s' \ - "$(plugin_read_list ENV)" \ - "$(plugin_read_list ENVIRONMENT)")" + "$(plugin_read_list ENV)" \ + "$(plugin_read_list ENVIRONMENT)")" # Propagate all environment variables into the container if requested if [[ "$(plugin_read_config PROPAGATE_ENVIRONMENT "false")" =~ ^(true|on|1)$ ]] ; then - if [[ -n "${BUILDKITE_ENV_FILE:-}" ]] ; then - # Read in the env file and convert to --env params for docker - # This is because --env-file doesn't support newlines or quotes per https://docs.docker.com/compose/env-file/#syntax-rules - while read -r var; do - run_params+=("-e" "${var%%=*}") - done < "${BUILDKITE_ENV_FILE}" - else - echo -n "🚨 Not propagating environment variables to container as \$BUILDKITE_ENV_FILE is not set" - fi + if [[ -n "${BUILDKITE_ENV_FILE:-}" ]] ; then + # Read in the env file and convert to --env params for docker + # This is because --env-file doesn't support newlines or quotes per https://docs.docker.com/compose/env-file/#syntax-rules + while read -r var; do + run_params+=("-e" "${var%%=*}") + done < "${BUILDKITE_ENV_FILE}" + else + echo -n "🚨 Not propagating environment variables to container as \$BUILDKITE_ENV_FILE is not set" + fi fi # Propagate AWS credentials if requested if [[ "$(plugin_read_config PROPAGATE_AWS_AUTH_TOKENS "false")" =~ ^(true|on|1)$ ]] ; then - if [[ -n "${AWS_ACCESS_KEY_ID:-}" ]] ; then - run_params+=( --env "AWS_ACCESS_KEY_ID" ) - fi - if [[ -n "${AWS_SECRET_ACCESS_KEY:-}" ]] ; then - run_params+=( --env "AWS_SECRET_ACCESS_KEY" ) - fi - if [[ -n "${AWS_SESSION_TOKEN:-}" ]] ; then - run_params+=( --env "AWS_SESSION_TOKEN" ) - fi - if [[ -n "${AWS_REGION:-}" ]] ; then - run_params+=( --env "AWS_REGION" ) - fi - if [[ -n "${AWS_DEFAULT_REGION:-}" ]] ; then - run_params+=( --env "AWS_DEFAULT_REGION" ) - fi - if [[ -n "${AWS_ROLE_ARN:-}" ]] ; then - run_params+=( --env "AWS_ROLE_ARN" ) - fi - if [[ -n "${AWS_STS_REGIONAL_ENDPOINTS:-}" ]] ; then - run_params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" ) - fi - # Pass ECS variables when the agent is running in ECS - # https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html - if [[ -n "${AWS_CONTAINER_CREDENTIALS_FULL_URI:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" ) - fi - if [[ -n "${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ) - fi - if [[ -n "${AWS_CONTAINER_AUTHORIZATION_TOKEN:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" ) - fi - # Pass EKS variables when the agent is running in EKS - # https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html - if [[ -n "${AWS_WEB_IDENTITY_TOKEN_FILE:-}" ]] ; then - run_params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" ) - # Add the token file as a volume - run_params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" ) - fi + if [[ -n "${AWS_ACCESS_KEY_ID:-}" ]] ; then + run_params+=( --env "AWS_ACCESS_KEY_ID" ) + fi + if [[ -n "${AWS_SECRET_ACCESS_KEY:-}" ]] ; then + run_params+=( --env "AWS_SECRET_ACCESS_KEY" ) + fi + if [[ -n "${AWS_SESSION_TOKEN:-}" ]] ; then + run_params+=( --env "AWS_SESSION_TOKEN" ) + fi + if [[ -n "${AWS_REGION:-}" ]] ; then + run_params+=( --env "AWS_REGION" ) + fi + if [[ -n "${AWS_DEFAULT_REGION:-}" ]] ; then + run_params+=( --env "AWS_DEFAULT_REGION" ) + fi + if [[ -n "${AWS_ROLE_ARN:-}" ]] ; then + run_params+=( --env "AWS_ROLE_ARN" ) + fi + if [[ -n "${AWS_STS_REGIONAL_ENDPOINTS:-}" ]] ; then + run_params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" ) + fi + # Pass ECS variables when the agent is running in ECS + # https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html + if [[ -n "${AWS_CONTAINER_CREDENTIALS_FULL_URI:-}" ]] ; then + run_params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" ) + fi + if [[ -n "${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI:-}" ]] ; then + run_params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ) + fi + if [[ -n "${AWS_CONTAINER_AUTHORIZATION_TOKEN:-}" ]] ; then + run_params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" ) + fi + # Pass EKS variables when the agent is running in EKS + # https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html + if [[ -n "${AWS_WEB_IDENTITY_TOKEN_FILE:-}" ]] ; then + run_params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" ) + # Add the token file as a volume + run_params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" ) + fi fi # If requested, propagate a set of env vars as listed in a given env var to the # container. if [[ -n "$(plugin_read_config ENV_PROPAGATION_LIST)" ]]; then - env_propagation_list_var="$(plugin_read_config ENV_PROPAGATION_LIST)" - if [[ -z "${!env_propagation_list_var:-}" ]]; then - echo -n "env-propagation-list desired, but ${env_propagation_list_var} is not defined!" - exit 1 - fi - for var in ${!env_propagation_list_var}; do - run_params+=("-e" "$var") - done + env_propagation_list_var="$(plugin_read_config ENV_PROPAGATION_LIST)" + if [[ -z "${!env_propagation_list_var:-}" ]]; then + echo -n "env-propagation-list desired, but ${env_propagation_list_var} is not defined!" + exit 1 + fi + for var in ${!env_propagation_list_var}; do + run_params+=("-e" "$var") + done fi while IFS=$'\n' read -r vol ; do - [[ -n "${vol:-}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")") + [[ -n "${vol:-}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")") done <<< "$(plugin_read_list VOLUMES)" # Parse BUILDKITE_DOCKER_DEFAULT_VOLUMES delimited by semi-colons, normalized to # ignore spaces and leading or trailing semi-colons IFS=';' read -r -a default_volumes <<< "${BUILDKITE_DOCKER_DEFAULT_VOLUMES:-}" for vol in "${default_volumes[@]:-}" ; do - trimmed_vol="$(echo -n "$vol" | sed -e 's/^[[:space:]]*//' | sed -e 's/[[:space:]]*$//')" - [[ -n "$trimmed_vol" ]] && run_params+=("-v" "$(expand_relative_volume_path "$trimmed_vol")") + trimmed_vol="$(echo -n "$vol" | sed -e 's/^[[:space:]]*//' | sed -e 's/[[:space:]]*$//')" + [[ -n "$trimmed_vol" ]] && run_params+=("-v" "$(expand_relative_volume_path "$trimmed_vol")") done # If there's a git mirror, mount it so that git references can be followed. if [[ -n "${BUILDKITE_REPO_MIRROR:-}" ]]; then - run_params+=("-v" "$BUILDKITE_REPO_MIRROR:$BUILDKITE_REPO_MIRROR:ro") + run_params+=("-v" "$BUILDKITE_REPO_MIRROR:$BUILDKITE_REPO_MIRROR:ro") fi # Disable allocating a TTY tty_default='false' if [[ "$(plugin_read_config TTY "$tty_default")" == "false" ]] ; then - run_params+=(-T) + run_params+=(-T) fi workdir='' @@ -131,104 +131,104 @@ function generate_run_args() { # Set operating system specific defaults if is_windows ; then - workdir_default="C:\\workdir" - # escaping /C is a necessary workaround for an issue with Git for Windows 2.24.1.2 - # https://github.com/git-for-windows/git/issues/2442 - pwd_default="$(cmd.exe //C "echo %CD%")" + workdir_default="C:\\workdir" + # escaping /C is a necessary workaround for an issue with Git for Windows 2.24.1.2 + # https://github.com/git-for-windows/git/issues/2442 + pwd_default="$(cmd.exe //C "echo %CD%")" fi mount_checkout="$(plugin_read_config MOUNT_CHECKOUT "false")" if [[ -n "$(plugin_read_config WORKDIR)" ]] || [[ "${mount_checkout}" == "true" ]]; then - workdir="$(plugin_read_config WORKDIR "$workdir_default")" + workdir="$(plugin_read_config WORKDIR "$workdir_default")" fi if [[ -n "${workdir}" ]] ; then - run_params+=("--workdir=${workdir}") + run_params+=("--workdir=${workdir}") fi if [[ "${mount_checkout}" == "true" ]]; then - run_params+=("-v" "${pwd_default}:${workdir}") + run_params+=("-v" "${pwd_default}:${workdir}") elif [[ "${mount_checkout}" =~ ^/.*$ ]]; then - run_params+=("-v" "${pwd_default}:${mount_checkout}") + run_params+=("-v" "${pwd_default}:${mount_checkout}") elif [[ "${mount_checkout}" != "false" ]]; then - echo -n "🚨 mount-checkout should be either true or an absolute path to use as a mountpoint" - exit 1 + echo -n "🚨 mount-checkout should be either true or an absolute path to use as a mountpoint" + exit 1 fi # Optionally run as specified username or uid if [[ -n "$(plugin_read_config USER)" ]] ; then - run_params+=("--user=$(plugin_read_config USER)") + run_params+=("--user=$(plugin_read_config USER)") fi # Optionally run as specified username or uid if [[ "$(plugin_read_config PROPAGATE_UID_GID "false")" == "true" ]] ; then - run_params+=("--user=$(id -u):$(id -g)") + run_params+=("--user=$(id -u):$(id -g)") fi # Enable alias support for networks if [[ "$(plugin_read_config USE_ALIASES "false")" == "true" ]] ; then - run_params+=(--use-aliases) + run_params+=(--use-aliases) fi # Optionally remove containers after run if [[ "$(plugin_read_config RM "true")" == "true" ]]; then - run_params+=(--rm) + run_params+=(--rm) fi # Optionally sets --entrypoint if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then - run_params+=(--entrypoint) - run_params+=("$(plugin_read_config ENTRYPOINT)") + run_params+=(--entrypoint) + run_params+=("$(plugin_read_config ENTRYPOINT)") fi # Mount ssh-agent socket and known_hosts if [[ ! "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-false}" = 'false' ]] ; then - if [[ -z "${SSH_AUTH_SOCK:-}" ]] ; then - echo "+++ 🚨 \$SSH_AUTH_SOCK isn't set, has ssh-agent started?" - exit 1 - fi - if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then - echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} does not exist or is not a socket, was ssh-agent started?" - exit 1 - fi + if [[ -z "${SSH_AUTH_SOCK:-}" ]] ; then + echo "+++ 🚨 \$SSH_AUTH_SOCK isn't set, has ssh-agent started?" + exit 1 + fi + if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then + echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} does not exist or is not a socket, was ssh-agent started?" + exit 1 + fi - if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-''}" =~ ^(true|on|1)$ ]]; then - MOUNT_PATH=/root - else - MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT}" - fi + if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-''}" =~ ^(true|on|1)$ ]]; then + MOUNT_PATH=/root + else + MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT}" + fi - run_params+=( - "-e" "SSH_AUTH_SOCK=/ssh-agent" - "-v" "${SSH_AUTH_SOCK}:/ssh-agent" - "-v" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts" - ) + run_params+=( + "-e" "SSH_AUTH_SOCK=/ssh-agent" + "-v" "${SSH_AUTH_SOCK}:/ssh-agent" + "-v" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts" + ) fi # Optionally handle the mount-buildkite-agent option if [[ "$(plugin_read_config MOUNT_BUILDKITE_AGENT "false")" == "true" ]]; then - if [[ -z "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then - if ! command -v buildkite-agent >/dev/null 2>&1 ; then - echo -n "+++ 🚨 Failed to find buildkite-agent in PATH to mount into container, " - echo "you can disable this behaviour with 'mount-buildkite-agent:false'" - else - BUILDKITE_AGENT_BINARY_PATH=$(command -v buildkite-agent) + if [[ -z "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then + if ! command -v buildkite-agent >/dev/null 2>&1 ; then + echo -n "+++ 🚨 Failed to find buildkite-agent in PATH to mount into container, " + echo "you can disable this behaviour with 'mount-buildkite-agent:false'" + else + BUILDKITE_AGENT_BINARY_PATH=$(command -v buildkite-agent) + fi fi fi - fi # Mount buildkite-agent if we have a path for it if [[ -n "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then - run_params+=( - "-e" "BUILDKITE_JOB_ID" - "-e" "BUILDKITE_BUILD_ID" - "-e" "BUILDKITE_AGENT_ACCESS_TOKEN" - "-v" "$BUILDKITE_AGENT_BINARY_PATH:/usr/bin/buildkite-agent" - ) + run_params+=( + "-e" "BUILDKITE_JOB_ID" + "-e" "BUILDKITE_BUILD_ID" + "-e" "BUILDKITE_AGENT_ACCESS_TOKEN" + "-v" "$BUILDKITE_AGENT_BINARY_PATH:/usr/bin/buildkite-agent" + ) fi # Optionally expose service ports if [[ "$(plugin_read_config SERVICE_PORTS "false")" == "true" ]]; then - run_params+=(--service-ports) + run_params+=(--service-ports) fi } \ No newline at end of file From 02a77d7351e96f9d73e8265c9ef470795cd3346a Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 12:23:19 -0700 Subject: [PATCH 19/52] not sure why exit code is causing exit --- commands/pull.sh | 2 +- commands/run_cmd_generator.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/pull.sh b/commands/pull.sh index fc4e7eea..cb62219d 100644 --- a/commands/pull.sh +++ b/commands/pull.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -ueo pipefail +set -uo pipefail function pull() { prebuilt_candidates=("$1") diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh index 82e51ea2..5a8fbe8c 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_cmd_generator.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -ueo pipefail +set -uo pipefail # We set a predictable container name so we can find it and inspect it later on function generate_run_args() { From 4fa1a59331095451e2ab5d97f0031482d73a2792 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 12:39:40 -0700 Subject: [PATCH 20/52] Move trap --- commands/run.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index ca2d9c83..23286f22 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -18,15 +18,16 @@ run_service="$(plugin_read_config RUN)" container_name="$(docker_compose_project_name)_${run_service}_build_${BUILDKITE_BUILD_NUMBER}" override_file="docker-compose.buildkite-${BUILDKITE_BUILD_NUMBER}-override.yml" -expand_headers_on_error() { - echo "^^^ +++" -} -trap expand_headers_on_error ERR - test -f "$override_file" && rm "$override_file" pull "$run_service" pulled_status=$? +echo "pulled_status: $pulled_status" + +expand_headers_on_error() { + echo "^^^ +++" +} +trap expand_headers_on_error ERR if [[ ! -f "$override_file" ]] ; then echo "+++ 🚨 No pre-built image found from a previous 'build' step for this service and config file." From df849c9705e2fb8dae43470ea59925f224f56e60 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 12:46:07 -0700 Subject: [PATCH 21/52] remove root -e --- hooks/command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/command b/hooks/command index 233399b7..a41eec71 100755 --- a/hooks/command +++ b/hooks/command @@ -1,5 +1,5 @@ #!/bin/bash -set -ueo pipefail +set -uo pipefail DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" From 73500d8bf73a89fb34b4e951bd367264c8e03259 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 13:48:02 -0700 Subject: [PATCH 22/52] declare array --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 23286f22..0f15df94 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -41,7 +41,7 @@ else fi up_params=() -run_params=() +declare -a run_params=() generate_run_args $container_name $pulled_status echo "run_params after func: ${run_params[@]}" From 8a411278491c34055e19654c66efb9abc01574b4 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 14:06:33 -0700 Subject: [PATCH 23/52] syntax fix --- commands/run.sh | 4 ++-- commands/run_cmd_generator.sh | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 0f15df94..cc66c4bd 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -41,8 +41,8 @@ else fi up_params=() -declare -a run_params=() -generate_run_args $container_name $pulled_status +declare -a run_params +generate_run_args "run_params" $container_name $pulled_status echo "run_params after func: ${run_params[@]}" run_params+=("$run_service") diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh index 5a8fbe8c..ff4cbd10 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_cmd_generator.sh @@ -3,8 +3,9 @@ set -uo pipefail # We set a predictable container name so we can find it and inspect it later on function generate_run_args() { - local -n run_params+=("run" "--name" "$1") - pulled_services=$2 + local -n run_params="$1" + run_params+=("run" "--name" "$2") + pulled_services=$3 if [[ $pulled_services -eq 0 ]] ; then echo "~~~ :docker: Creating docker-compose override file for prebuilt services" From 6dad4ad702c43accca9511ad688cf30f6f5def13 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 14:18:44 -0700 Subject: [PATCH 24/52] fix circular name issue --- commands/run_cmd_generator.sh | 70 +++++++++++++++++------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh index ff4cbd10..e4b0760c 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_cmd_generator.sh @@ -3,19 +3,19 @@ set -uo pipefail # We set a predictable container name so we can find it and inspect it later on function generate_run_args() { - local -n run_params="$1" - run_params+=("run" "--name" "$2") + local -n params="$1" + params+=("run" "--name" "$2") pulled_services=$3 if [[ $pulled_services -eq 0 ]] ; then echo "~~~ :docker: Creating docker-compose override file for prebuilt services" - run_params+=(-f "$override_file") + params+=(-f "$override_file") up_params+=(-f "$override_file") fi if [[ "$(plugin_read_config RUN_LABELS "true")" =~ ^(true|on|1)$ ]]; then # Add useful labels to run container - run_params+=( + params+=( "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" @@ -29,7 +29,7 @@ function generate_run_args() { # append env vars provided in ENV or ENVIRONMENT, these are newline delimited while IFS=$'\n' read -r env ; do - [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") + [[ -n "${env:-}" ]] && params+=("-e" "${env}") done <<< "$(printf '%s\n%s' \ "$(plugin_read_list ENV)" \ "$(plugin_read_list ENVIRONMENT)")" @@ -40,7 +40,7 @@ function generate_run_args() { # Read in the env file and convert to --env params for docker # This is because --env-file doesn't support newlines or quotes per https://docs.docker.com/compose/env-file/#syntax-rules while read -r var; do - run_params+=("-e" "${var%%=*}") + params+=("-e" "${var%%=*}") done < "${BUILDKITE_ENV_FILE}" else echo -n "🚨 Not propagating environment variables to container as \$BUILDKITE_ENV_FILE is not set" @@ -50,43 +50,43 @@ function generate_run_args() { # Propagate AWS credentials if requested if [[ "$(plugin_read_config PROPAGATE_AWS_AUTH_TOKENS "false")" =~ ^(true|on|1)$ ]] ; then if [[ -n "${AWS_ACCESS_KEY_ID:-}" ]] ; then - run_params+=( --env "AWS_ACCESS_KEY_ID" ) + params+=( --env "AWS_ACCESS_KEY_ID" ) fi if [[ -n "${AWS_SECRET_ACCESS_KEY:-}" ]] ; then - run_params+=( --env "AWS_SECRET_ACCESS_KEY" ) + params+=( --env "AWS_SECRET_ACCESS_KEY" ) fi if [[ -n "${AWS_SESSION_TOKEN:-}" ]] ; then - run_params+=( --env "AWS_SESSION_TOKEN" ) + params+=( --env "AWS_SESSION_TOKEN" ) fi if [[ -n "${AWS_REGION:-}" ]] ; then - run_params+=( --env "AWS_REGION" ) + params+=( --env "AWS_REGION" ) fi if [[ -n "${AWS_DEFAULT_REGION:-}" ]] ; then - run_params+=( --env "AWS_DEFAULT_REGION" ) + params+=( --env "AWS_DEFAULT_REGION" ) fi if [[ -n "${AWS_ROLE_ARN:-}" ]] ; then - run_params+=( --env "AWS_ROLE_ARN" ) + params+=( --env "AWS_ROLE_ARN" ) fi if [[ -n "${AWS_STS_REGIONAL_ENDPOINTS:-}" ]] ; then - run_params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" ) + params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" ) fi # Pass ECS variables when the agent is running in ECS # https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html if [[ -n "${AWS_CONTAINER_CREDENTIALS_FULL_URI:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" ) + params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" ) fi if [[ -n "${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ) + params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ) fi if [[ -n "${AWS_CONTAINER_AUTHORIZATION_TOKEN:-}" ]] ; then - run_params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" ) + params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" ) fi # Pass EKS variables when the agent is running in EKS # https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html if [[ -n "${AWS_WEB_IDENTITY_TOKEN_FILE:-}" ]] ; then - run_params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" ) + params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" ) # Add the token file as a volume - run_params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" ) + params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" ) fi fi @@ -99,12 +99,12 @@ function generate_run_args() { exit 1 fi for var in ${!env_propagation_list_var}; do - run_params+=("-e" "$var") + params+=("-e" "$var") done fi while IFS=$'\n' read -r vol ; do - [[ -n "${vol:-}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")") + [[ -n "${vol:-}" ]] && params+=("-v" "$(expand_relative_volume_path "$vol")") done <<< "$(plugin_read_list VOLUMES)" # Parse BUILDKITE_DOCKER_DEFAULT_VOLUMES delimited by semi-colons, normalized to @@ -112,18 +112,18 @@ function generate_run_args() { IFS=';' read -r -a default_volumes <<< "${BUILDKITE_DOCKER_DEFAULT_VOLUMES:-}" for vol in "${default_volumes[@]:-}" ; do trimmed_vol="$(echo -n "$vol" | sed -e 's/^[[:space:]]*//' | sed -e 's/[[:space:]]*$//')" - [[ -n "$trimmed_vol" ]] && run_params+=("-v" "$(expand_relative_volume_path "$trimmed_vol")") + [[ -n "$trimmed_vol" ]] && params+=("-v" "$(expand_relative_volume_path "$trimmed_vol")") done # If there's a git mirror, mount it so that git references can be followed. if [[ -n "${BUILDKITE_REPO_MIRROR:-}" ]]; then - run_params+=("-v" "$BUILDKITE_REPO_MIRROR:$BUILDKITE_REPO_MIRROR:ro") + params+=("-v" "$BUILDKITE_REPO_MIRROR:$BUILDKITE_REPO_MIRROR:ro") fi # Disable allocating a TTY tty_default='false' if [[ "$(plugin_read_config TTY "$tty_default")" == "false" ]] ; then - run_params+=(-T) + params+=(-T) fi workdir='' @@ -144,13 +144,13 @@ function generate_run_args() { fi if [[ -n "${workdir}" ]] ; then - run_params+=("--workdir=${workdir}") + params+=("--workdir=${workdir}") fi if [[ "${mount_checkout}" == "true" ]]; then - run_params+=("-v" "${pwd_default}:${workdir}") + params+=("-v" "${pwd_default}:${workdir}") elif [[ "${mount_checkout}" =~ ^/.*$ ]]; then - run_params+=("-v" "${pwd_default}:${mount_checkout}") + params+=("-v" "${pwd_default}:${mount_checkout}") elif [[ "${mount_checkout}" != "false" ]]; then echo -n "🚨 mount-checkout should be either true or an absolute path to use as a mountpoint" exit 1 @@ -158,28 +158,28 @@ function generate_run_args() { # Optionally run as specified username or uid if [[ -n "$(plugin_read_config USER)" ]] ; then - run_params+=("--user=$(plugin_read_config USER)") + params+=("--user=$(plugin_read_config USER)") fi # Optionally run as specified username or uid if [[ "$(plugin_read_config PROPAGATE_UID_GID "false")" == "true" ]] ; then - run_params+=("--user=$(id -u):$(id -g)") + params+=("--user=$(id -u):$(id -g)") fi # Enable alias support for networks if [[ "$(plugin_read_config USE_ALIASES "false")" == "true" ]] ; then - run_params+=(--use-aliases) + params+=(--use-aliases) fi # Optionally remove containers after run if [[ "$(plugin_read_config RM "true")" == "true" ]]; then - run_params+=(--rm) + params+=(--rm) fi # Optionally sets --entrypoint if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then - run_params+=(--entrypoint) - run_params+=("$(plugin_read_config ENTRYPOINT)") + params+=(--entrypoint) + params+=("$(plugin_read_config ENTRYPOINT)") fi # Mount ssh-agent socket and known_hosts @@ -199,7 +199,7 @@ function generate_run_args() { MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT}" fi - run_params+=( + params+=( "-e" "SSH_AUTH_SOCK=/ssh-agent" "-v" "${SSH_AUTH_SOCK}:/ssh-agent" "-v" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts" @@ -220,7 +220,7 @@ function generate_run_args() { # Mount buildkite-agent if we have a path for it if [[ -n "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then - run_params+=( + params+=( "-e" "BUILDKITE_JOB_ID" "-e" "BUILDKITE_BUILD_ID" "-e" "BUILDKITE_AGENT_ACCESS_TOKEN" @@ -230,6 +230,6 @@ function generate_run_args() { # Optionally expose service ports if [[ "$(plugin_read_config SERVICE_PORTS "false")" == "true" ]]; then - run_params+=(--service-ports) + params+=(--service-ports) fi } \ No newline at end of file From fd19fe45a48d835ffaab5b22b8a25244139b99b0 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 14:49:48 -0700 Subject: [PATCH 25/52] simplify logic, put run args in an array --- commands/run.sh | 77 ++++++++++++++++------------------- commands/run_cmd_generator.sh | 3 +- lib/shared.bash | 4 +- 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index cc66c4bd..8296d01d 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -42,7 +42,8 @@ fi up_params=() declare -a run_params -generate_run_args "run_params" $container_name $pulled_status +run_params+=("run" "--name" "$container_name") +generate_run_args "run_params" $pulled_status echo "run_params after func: ${run_params[@]}" run_params+=("$run_service") @@ -88,7 +89,10 @@ if [[ $dependency_exitcode -ne 0 ]] ; then fi -shell=() +# Assemble the shell and command arguments into the docker arguments +display_command=() +commands=() + shell_disabled=1 result=() @@ -114,20 +118,24 @@ elif [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" ]] ; then elif plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL ; then shell_disabled='' for arg in "${result[@]}" ; do - shell+=("$arg") + commands+=("$arg") done fi # Set a default shell if one is needed -if [[ -z $shell_disabled ]] && [[ ${#shell[@]} -eq 0 ]] ; then +if [[ -z $shell_disabled ]] && [[ ${#commands[@]} -eq 0 ]] ; then if is_windows ; then - shell=("CMD.EXE" "/c") + commands=("CMD.EXE" "/c") # else - # shell=("/bin/sh" "-e" "-c") + # commands=("/bin/sh" "-e" "-c") fi fi -command=() +if [[ ${#commands[@]} -gt 0 ]] ; then + for shell_arg in "${commands[@]}" ; do + display_command+=("$shell_arg") + done +fi # Show a helpful error message if string version of command is used if [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND:-}" ]] ; then @@ -137,41 +145,26 @@ fi # Parse plugin command if provided if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then - for arg in "${result[@]}" ; do - command+=("$arg") - done -fi - -if [[ ${#command[@]} -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then - echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" - exit 1 -fi - -# Assemble the shell and command arguments into the docker arguments - -display_command=() - -if [[ ${#shell[@]} -gt 0 ]] ; then - for shell_arg in "${shell[@]}" ; do - run_params+=("$shell_arg") - display_command+=("$shell_arg") - done -fi - -if [[ -n "${BUILDKITE_COMMAND}" ]] ; then - if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then - # An array of commands in the step will be a single string with multiple lines - # This breaks a lot of things here so we will print a warning for user to be aware - echo "⚠️ Warning: The command received has multiple lines." - echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." + if [[ "${#result[@]}" -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" + exit 1 + elif [[ "${#result[@]}" -gt 0 ]] ; then + for arg in "${result[@]}" ; do + commands+=("$arg") + display_command+=("$arg") + done + elif [[ -n "${BUILDKITE_COMMAND}" ]] ; then + if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then + # FIXME: This is easy to fix, just need to do at end + + # An array of commands in the step will be a single string with multiple lines + # This breaks a lot of things here so we will print a warning for user to be aware + echo "⚠️ Warning: The command received has multiple lines." + echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." + fi + commands+=("${BUILDKITE_COMMAND}") + display_command+=("'${BUILDKITE_COMMAND}'") fi - run_params+=("${BUILDKITE_COMMAND}") - display_command+=("'${BUILDKITE_COMMAND}'") -elif [[ ${#command[@]} -gt 0 ]] ; then - for command_arg in "${command[@]}" ; do - run_params+=("$command_arg") - display_command+=("${command_arg}") - done fi ensure_stopped() { @@ -194,7 +187,7 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" -run_docker_compose "${run_params[@]}" +run_docker_compose "${run_params[@]}" "commands" exitcode=$? if [[ $exitcode -ne 0 ]] ; then diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh index e4b0760c..284dfd30 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_cmd_generator.sh @@ -4,8 +4,7 @@ set -uo pipefail # We set a predictable container name so we can find it and inspect it later on function generate_run_args() { local -n params="$1" - params+=("run" "--name" "$2") - pulled_services=$3 + pulled_services=$2 if [[ $pulled_services -eq 0 ]] ; then echo "~~~ :docker: Creating docker-compose override file for prebuilt services" diff --git a/lib/shared.bash b/lib/shared.bash index 26d48a98..cef21952 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -255,7 +255,9 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") - plugin_prompt_and_run "${command[@]}" "$@" + echo "running: ${command[@]} [$@]" + + plugin_prompt_and_run "${command[@]}" "[$@]" } function in_array() { From e3df3ba693a25187d8f25ebb85ceae3a9ce678ce Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 14:55:50 -0700 Subject: [PATCH 26/52] try different syntax --- commands/run.sh | 2 +- lib/shared.bash | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 8296d01d..88326ca5 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -187,7 +187,7 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" -run_docker_compose "${run_params[@]}" "commands" +run_docker_compose "${run_params[@]}" "[commands]" exitcode=$? if [[ $exitcode -ne 0 ]] ; then diff --git a/lib/shared.bash b/lib/shared.bash index cef21952..8ec34f02 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -255,9 +255,9 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") - echo "running: ${command[@]} [$@]" + echo "running: ${command[@]} $@" - plugin_prompt_and_run "${command[@]}" "[$@]" + plugin_prompt_and_run "${command[@]}" "$@" } function in_array() { From 79a565e55f74f7df9e0d2d864c3f4703d297e4e4 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 15:04:53 -0700 Subject: [PATCH 27/52] try different syntax --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 88326ca5..eafb57a5 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -187,7 +187,7 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" -run_docker_compose "${run_params[@]}" "[commands]" +run_docker_compose "${run_params[@]}" "[$commands[@]]" exitcode=$? if [[ $exitcode -ne 0 ]] ; then From 78c7d41bd5bb924c4716fc44704a83946362f626 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 15:14:32 -0700 Subject: [PATCH 28/52] try different syntax --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index eafb57a5..31ca78cf 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -187,7 +187,7 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" -run_docker_compose "${run_params[@]}" "[$commands[@]]" +run_docker_compose "${run_params[@]}" "[${commands[@]}]" exitcode=$? if [[ $exitcode -ne 0 ]] ; then From e1d6ba370887e9c092491ce9f17f070ec47fd204 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 15:26:45 -0700 Subject: [PATCH 29/52] try different syntax --- commands/run.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 31ca78cf..bdadf0b8 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -187,7 +187,10 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" -run_docker_compose "${run_params[@]}" "[${commands[@]}]" +echo "command is: ${commands[@]}" +cmd_lit="[${commands[@]}]" +echo "cmd_lit is: ${cmd_lit}" +run_docker_compose "${run_params[@]}" "$cmd_lit" exitcode=$? if [[ $exitcode -ne 0 ]] ; then From 0c79c5de52dd5f476fdf41cb63d9fc4eec6b5dcc Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 15:36:18 -0700 Subject: [PATCH 30/52] add some logging --- commands/run.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index bdadf0b8..a588d477 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -149,11 +149,13 @@ if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" exit 1 elif [[ "${#result[@]}" -gt 0 ]] ; then + echo "compose plugin command: ${result[@]}" for arg in "${result[@]}" ; do commands+=("$arg") display_command+=("$arg") done elif [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "buildkite command: ${BUILDKITE_COMMAND}" if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then # FIXME: This is easy to fix, just need to do at end @@ -187,7 +189,7 @@ else fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" -echo "command is: ${commands[@]}" +echo "commands is: ${commands[@]}" cmd_lit="[${commands[@]}]" echo "cmd_lit is: ${cmd_lit}" run_docker_compose "${run_params[@]}" "$cmd_lit" From fc598aa9929b60461416f11a94cf839fc011a23a Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 15:48:25 -0700 Subject: [PATCH 31/52] logic fix --- commands/run.sh | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index a588d477..6d65f2c7 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -154,20 +154,21 @@ if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then commands+=("$arg") display_command+=("$arg") done - elif [[ -n "${BUILDKITE_COMMAND}" ]] ; then - echo "buildkite command: ${BUILDKITE_COMMAND}" - if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then - # FIXME: This is easy to fix, just need to do at end - - # An array of commands in the step will be a single string with multiple lines - # This breaks a lot of things here so we will print a warning for user to be aware - echo "⚠️ Warning: The command received has multiple lines." - echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." - fi - commands+=("${BUILDKITE_COMMAND}") - display_command+=("'${BUILDKITE_COMMAND}'") fi fi +if [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "buildkite command: ${BUILDKITE_COMMAND}" + if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then + # FIXME: This is easy to fix, just need to do at end + + # An array of commands in the step will be a single string with multiple lines + # This breaks a lot of things here so we will print a warning for user to be aware + echo "⚠️ Warning: The command received has multiple lines." + echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." + fi + commands+=("${BUILDKITE_COMMAND}") + display_command+=("'${BUILDKITE_COMMAND}'") +fi ensure_stopped() { echo '+++ :warning: Signal received, stopping container gracefully' From ae42cf45b6aa275961cc64fdd56991f87d92d43e Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 15:55:20 -0700 Subject: [PATCH 32/52] syntax fix --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 6d65f2c7..b392ed32 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -191,7 +191,7 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" -cmd_lit="[${commands[@]}]" +cmd_lit="[${commands[@]@Q}]" echo "cmd_lit is: ${cmd_lit}" run_docker_compose "${run_params[@]}" "$cmd_lit" From 000655da5ccdba24b30ac8ed35ba2b507ee528da Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 16:14:15 -0700 Subject: [PATCH 33/52] syntax fix --- commands/run.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index b392ed32..ffee61d8 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -191,7 +191,8 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" -cmd_lit="[${commands[@]@Q}]" +printf -v cmd_lit ' "%s" ' "${commands[@]}" +cmd_lit='['${cmd_lit}']' echo "cmd_lit is: ${cmd_lit}" run_docker_compose "${run_params[@]}" "$cmd_lit" From b2bef76098130de0bd1b78e3a92f9778e1c65d6f Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 16:25:33 -0700 Subject: [PATCH 34/52] syntax fix --- commands/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index ffee61d8..9a9a3024 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -192,9 +192,9 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" printf -v cmd_lit ' "%s" ' "${commands[@]}" -cmd_lit='['${cmd_lit}']' +cmd_lit="["${cmd_lit}"]" echo "cmd_lit is: ${cmd_lit}" -run_docker_compose "${run_params[@]}" "$cmd_lit" +run_docker_compose "${run_params[@]} $cmd_lit" exitcode=$? if [[ $exitcode -ne 0 ]] ; then From 662f6ad04162793845a087409d1b8fac72a8c0c8 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 16:46:20 -0700 Subject: [PATCH 35/52] revert to non-array since it isn't workingx --- commands/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 9a9a3024..9212e444 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -194,7 +194,7 @@ echo "commands is: ${commands[@]}" printf -v cmd_lit ' "%s" ' "${commands[@]}" cmd_lit="["${cmd_lit}"]" echo "cmd_lit is: ${cmd_lit}" -run_docker_compose "${run_params[@]} $cmd_lit" +run_docker_compose "${run_params[@]} ${commands[@]}" exitcode=$? if [[ $exitcode -ne 0 ]] ; then From 857c525debd44a387ba3e2e0878f22d67696ffd6 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 16:51:25 -0700 Subject: [PATCH 36/52] syntax fix --- lib/shared.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shared.bash b/lib/shared.bash index 8ec34f02..6570d296 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -257,7 +257,7 @@ function run_docker_compose() { echo "running: ${command[@]} $@" - plugin_prompt_and_run "${command[@]}" "$@" + plugin_prompt_and_run "${command[@]} $@" } function in_array() { From 83f6a47549ed34b69ad80d6be50b32545eb681c1 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 17:01:14 -0700 Subject: [PATCH 37/52] syntax fix --- commands/run.sh | 4 ++-- lib/shared.bash | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 9212e444..028fa5fc 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -192,9 +192,9 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" printf -v cmd_lit ' "%s" ' "${commands[@]}" -cmd_lit="["${cmd_lit}"]" +cmd_lit="${run_params[@]} ${commands[@]}" echo "cmd_lit is: ${cmd_lit}" -run_docker_compose "${run_params[@]} ${commands[@]}" +run_docker_compose "${cmd_lit}" exitcode=$? if [[ $exitcode -ne 0 ]] ; then diff --git a/lib/shared.bash b/lib/shared.bash index 6570d296..8ec34f02 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -257,7 +257,7 @@ function run_docker_compose() { echo "running: ${command[@]} $@" - plugin_prompt_and_run "${command[@]} $@" + plugin_prompt_and_run "${command[@]}" "$@" } function in_array() { From b9ccdd636b7bf5352ee38a88804ae5f8d8e70ca5 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 18:57:13 -0700 Subject: [PATCH 38/52] syntax fix --- commands/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 028fa5fc..cb379a78 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -191,8 +191,8 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" -printf -v cmd_lit ' "%s" ' "${commands[@]}" -cmd_lit="${run_params[@]} ${commands[@]}" +# printf -v cmd_lit ' "%s" ' "${commands[@]}" +cmd_lit=( "${run_params[@]}" "${commands[@]}" ) echo "cmd_lit is: ${cmd_lit}" run_docker_compose "${cmd_lit}" From b7722ca9d6e6f34f72a0e518d3920e5e2c298768 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Thu, 2 May 2024 19:03:25 -0700 Subject: [PATCH 39/52] syntax fix --- commands/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index cb379a78..424c3495 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -193,8 +193,8 @@ echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_ser echo "commands is: ${commands[@]}" # printf -v cmd_lit ' "%s" ' "${commands[@]}" cmd_lit=( "${run_params[@]}" "${commands[@]}" ) -echo "cmd_lit is: ${cmd_lit}" -run_docker_compose "${cmd_lit}" +echo "cmd_lit is: ${cmd_lit[@]}" +run_docker_compose "${cmd_lit[@]}" exitcode=$? if [[ $exitcode -ne 0 ]] ; then From 47e78bef4d98b77b6346ee1a23958248fe60536e Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 11:35:28 -0700 Subject: [PATCH 40/52] Move err trap, add some logging --- commands/run.sh | 9 ++------- hooks/command | 6 ++++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 424c3495..717811fe 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -24,11 +24,6 @@ pull "$run_service" pulled_status=$? echo "pulled_status: $pulled_status" -expand_headers_on_error() { - echo "^^^ +++" -} -trap expand_headers_on_error ERR - if [[ ! -f "$override_file" ]] ; then echo "+++ 🚨 No pre-built image found from a previous 'build' step for this service and config file." @@ -171,7 +166,7 @@ if [[ -n "${BUILDKITE_COMMAND}" ]] ; then fi ensure_stopped() { - echo '+++ :warning: Signal received, stopping container gracefully' + echo '+++ :warning: Trapped fired, signal received, stopping container gracefully' # docker stop "${container_name}" || true compose_cleanup ${run_service} echo '~~~ Last log lines that may be missing above (if container was not already removed)' @@ -193,7 +188,7 @@ echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_ser echo "commands is: ${commands[@]}" # printf -v cmd_lit ' "%s" ' "${commands[@]}" cmd_lit=( "${run_params[@]}" "${commands[@]}" ) -echo "cmd_lit is: ${cmd_lit[@]}" +echo "PID is: $BASHPID" run_docker_compose "${cmd_lit[@]}" exitcode=$? diff --git a/hooks/command b/hooks/command index a41eec71..35e619f2 100755 --- a/hooks/command +++ b/hooks/command @@ -3,6 +3,12 @@ set -uo pipefail DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" +expand_headers_on_error() { + echo "trapped an error" + echo "^^^ +++" +} +trap expand_headers_on_error ERR + # shellcheck source=lib/shared.bash . "$DIR/../lib/shared.bash" # shellcheck source=lib/metadata.bash From 882e5a3fd5a41abd479620d9742c54f1a162e722 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 12:04:52 -0700 Subject: [PATCH 41/52] move signal trap, make sure err triggers in places we want it to and not in places we don't --- commands/run.sh | 23 +++++++---------------- commands/run_cmd_generator.sh | 4 ++-- hooks/command | 13 ++++++++++++- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 717811fe..85cde899 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -20,8 +20,8 @@ override_file="docker-compose.buildkite-${BUILDKITE_BUILD_NUMBER}-override.yml" test -f "$override_file" && rm "$override_file" -pull "$run_service" -pulled_status=$? +pulled_status=0 +pull "$run_service" || pulled_status=$? echo "pulled_status: $pulled_status" if [[ ! -f "$override_file" ]] ; then @@ -165,18 +165,7 @@ if [[ -n "${BUILDKITE_COMMAND}" ]] ; then display_command+=("'${BUILDKITE_COMMAND}'") fi -ensure_stopped() { - echo '+++ :warning: Trapped fired, signal received, stopping container gracefully' - # docker stop "${container_name}" || true - compose_cleanup ${run_service} - echo '~~~ Last log lines that may be missing above (if container was not already removed)' - docker logs "${container_name}" || true - exit $1 -} -trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT - -exitcode=0 if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then group_type="---" @@ -189,9 +178,11 @@ echo "commands is: ${commands[@]}" # printf -v cmd_lit ' "%s" ' "${commands[@]}" cmd_lit=( "${run_params[@]}" "${commands[@]}" ) echo "PID is: $BASHPID" -run_docker_compose "${cmd_lit[@]}" -exitcode=$? +exitcode=0 +run_docker_compose "${cmd_lit[@]}" || exitcode=$? + + if [[ $exitcode -ne 0 ]] ; then echo "^^^ +++" echo "+++ :warning: Failed to run command, exited with $exitcode, run params:" @@ -206,4 +197,4 @@ if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then fi fi -return "$exitcode" \ No newline at end of file +exit "$exitcode" \ No newline at end of file diff --git a/commands/run_cmd_generator.sh b/commands/run_cmd_generator.sh index 284dfd30..b121614f 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_cmd_generator.sh @@ -4,9 +4,9 @@ set -uo pipefail # We set a predictable container name so we can find it and inspect it later on function generate_run_args() { local -n params="$1" - pulled_services=$2 + service_was_pulled=$2 - if [[ $pulled_services -eq 0 ]] ; then + if [[ $service_was_pulled -eq 0 ]] ; then echo "~~~ :docker: Creating docker-compose override file for prebuilt services" params+=(-f "$override_file") up_params+=(-f "$override_file") diff --git a/hooks/command b/hooks/command index 35e619f2..9d783dd5 100755 --- a/hooks/command +++ b/hooks/command @@ -6,8 +6,19 @@ DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" expand_headers_on_error() { echo "trapped an error" echo "^^^ +++" + exit $1 } -trap expand_headers_on_error ERR +trap 'expand_headers_on_error "$?"' ERR + +ensure_stopped() { + echo '+++ :warning: Trapped fired, signal received, stopping container gracefully' + # docker stop "${container_name}" || true + compose_cleanup ${run_service} + echo '~~~ Last log lines that may be missing above (if container was not already removed)' + docker logs "${container_name}" || true + exit $1 +} +trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT # shellcheck source=lib/shared.bash . "$DIR/../lib/shared.bash" From d208f69ac8bacc061b536ed6551f58c2863a1107 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 12:28:57 -0700 Subject: [PATCH 42/52] run compose in subshell, move run command generation to it's own file --- commands/cmd_to_run_generator.sh | 84 +++++++++++++++++ commands/run.sh | 92 ++----------------- ...d_generator.sh => run_params_generator.sh} | 1 - hooks/command | 4 +- 4 files changed, 95 insertions(+), 86 deletions(-) create mode 100644 commands/cmd_to_run_generator.sh rename commands/{run_cmd_generator.sh => run_params_generator.sh} (99%) diff --git a/commands/cmd_to_run_generator.sh b/commands/cmd_to_run_generator.sh new file mode 100644 index 00000000..1b9b2c6e --- /dev/null +++ b/commands/cmd_to_run_generator.sh @@ -0,0 +1,84 @@ +#!/bin/bash +set -uo pipefail + +function generate_cmd() { + local -n cmds="$1" + local -n display="$2" + + shell_disabled=1 + result=() + + if [[ -n "${BUILDKITE_COMMAND}" ]]; then + shell_disabled='' + fi + + # Handle shell being disabled + if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" =~ ^(false|off|0)$ ]] ; then + shell_disabled=1 + + # Show a helpful error message if a string version of shell is used + elif [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" ]] ; then + echo -n "🚨 The Docker Compose Plugin’s shell configuration option must be specified as an array. " + echo -n "Please update your pipeline.yml to use an array, " + echo "for example: [\"/bin/sh\", \"-e\", \"-u\"]." + echo + echo -n "Note that a shell will be inferred if one is required, so you might be able to remove" + echo "the option entirely" + exit 1 + + # Handle shell being provided as a string or list + elif plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL ; then + shell_disabled='' + for arg in "${result[@]}" ; do + cmds+=("$arg") + done + fi + + # Set a default shell if one is needed + if [[ -z $shell_disabled ]] && [[ ${#cmds[@]} -eq 0 ]] ; then + if is_windows ; then + cmds=("CMD.EXE" "/c") + # else + # cmds=("/bin/sh" "-e" "-c") + fi + fi + + if [[ ${#cmds[@]} -gt 0 ]] ; then + for shell_arg in "${cmds[@]}" ; do + display+=("$shell_arg") + done + fi + + # Show a helpful error message if string version of command is used + if [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND:-}" ]] ; then + echo "🚨 The Docker Compose Plugin’s command configuration option must be an array." + exit 1 + fi + + # Parse plugin command if provided + if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then + if [[ "${#result[@]}" -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" + exit 1 + elif [[ "${#result[@]}" -gt 0 ]] ; then + echo "compose plugin command: ${result[@]}" + for arg in "${result[@]}" ; do + cmds+=("$arg") + display+=("$arg") + done + fi + fi + if [[ -n "${BUILDKITE_COMMAND}" ]] ; then + echo "buildkite command: ${BUILDKITE_COMMAND}" + if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then + # FIXME: This is easy to fix, just need to do at end + + # An array of commands in the step will be a single string with multiple lines + # This breaks a lot of things here so we will print a warning for user to be aware + echo "⚠️ Warning: The command received has multiple lines." + echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." + fi + cmds+=("${BUILDKITE_COMMAND}") + display+=("'${BUILDKITE_COMMAND}'") + fi +} \ No newline at end of file diff --git a/commands/run.sh b/commands/run.sh index 85cde899..8c1eb02e 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -1,9 +1,9 @@ #!/bin/bash set -uo pipefail -. "$DIR/../lib/run.bash" . "$DIR/../commands/pull.sh" -. "$DIR/../commands/run_cmd_generator.sh" +. "$DIR/../commands/run_params_generator.sh" +. "$DIR/../commands/cmd_to_run_generator.sh" # Can't set both user and propagate-uid-gid if [[ -n "$(plugin_read_config USER)" ]] && [[ -n "$(plugin_read_config PROPAGATE_UID_GID)" ]]; then @@ -41,6 +41,7 @@ run_params+=("run" "--name" "$container_name") generate_run_args "run_params" $pulled_status echo "run_params after func: ${run_params[@]}" +# We set a predictable container name so we can find it and inspect it later on run_params+=("$run_service") up_params+=("up") # this ensures that the array has elements to avoid issues with bash 4.3 @@ -80,92 +81,14 @@ if [[ $dependency_exitcode -ne 0 ]] ; then upload_container_logs "$run_service" fi - return $dependency_exitcode + exit $dependency_exitcode fi # Assemble the shell and command arguments into the docker arguments display_command=() commands=() - -shell_disabled=1 -result=() - -if [[ -n "${BUILDKITE_COMMAND}" ]]; then - shell_disabled='' -fi - -# Handle shell being disabled -if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" =~ ^(false|off|0)$ ]] ; then - shell_disabled=1 - -# Show a helpful error message if a string version of shell is used -elif [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL:-}" ]] ; then - echo -n "🚨 The Docker Compose Plugin’s shell configuration option must be specified as an array. " - echo -n "Please update your pipeline.yml to use an array, " - echo "for example: [\"/bin/sh\", \"-e\", \"-u\"]." - echo - echo -n "Note that a shell will be inferred if one is required, so you might be able to remove" - echo "the option entirely" - exit 1 - -# Handle shell being provided as a string or list -elif plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_SHELL ; then - shell_disabled='' - for arg in "${result[@]}" ; do - commands+=("$arg") - done -fi - -# Set a default shell if one is needed -if [[ -z $shell_disabled ]] && [[ ${#commands[@]} -eq 0 ]] ; then - if is_windows ; then - commands=("CMD.EXE" "/c") - # else - # commands=("/bin/sh" "-e" "-c") - fi -fi - -if [[ ${#commands[@]} -gt 0 ]] ; then - for shell_arg in "${commands[@]}" ; do - display_command+=("$shell_arg") - done -fi - -# Show a helpful error message if string version of command is used -if [[ -n "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND:-}" ]] ; then - echo "🚨 The Docker Compose Plugin’s command configuration option must be an array." - exit 1 -fi - -# Parse plugin command if provided -if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND ; then - if [[ "${#result[@]}" -gt 0 ]] && [[ -n "${BUILDKITE_COMMAND}" ]] ; then - echo "+++ Error: Can't use both a step level command and the command parameter of the plugin" - exit 1 - elif [[ "${#result[@]}" -gt 0 ]] ; then - echo "compose plugin command: ${result[@]}" - for arg in "${result[@]}" ; do - commands+=("$arg") - display_command+=("$arg") - done - fi -fi -if [[ -n "${BUILDKITE_COMMAND}" ]] ; then - echo "buildkite command: ${BUILDKITE_COMMAND}" - if [[ $(echo "$BUILDKITE_COMMAND" | wc -l) -gt 1 ]]; then - # FIXME: This is easy to fix, just need to do at end - - # An array of commands in the step will be a single string with multiple lines - # This breaks a lot of things here so we will print a warning for user to be aware - echo "⚠️ Warning: The command received has multiple lines." - echo "⚠️ The Docker Compose Plugin does not correctly support step-level array commands." - fi - commands+=("${BUILDKITE_COMMAND}") - display_command+=("'${BUILDKITE_COMMAND}'") -fi - - +generate_cmd "commands" "display_command" if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_LOGS:-false}" = "true" ]]; then group_type="---" @@ -180,7 +103,10 @@ cmd_lit=( "${run_params[@]}" "${commands[@]}" ) echo "PID is: $BASHPID" exitcode=0 -run_docker_compose "${cmd_lit[@]}" || exitcode=$? +( + echo "docker compose being called" + run_docker_compose "${cmd_lit[@]}" || exitcode=$? +) if [[ $exitcode -ne 0 ]] ; then diff --git a/commands/run_cmd_generator.sh b/commands/run_params_generator.sh similarity index 99% rename from commands/run_cmd_generator.sh rename to commands/run_params_generator.sh index b121614f..5dd62225 100644 --- a/commands/run_cmd_generator.sh +++ b/commands/run_params_generator.sh @@ -1,7 +1,6 @@ #!/bin/bash set -uo pipefail -# We set a predictable container name so we can find it and inspect it later on function generate_run_args() { local -n params="$1" service_was_pulled=$2 diff --git a/hooks/command b/hooks/command index 9d783dd5..10820aca 100755 --- a/hooks/command +++ b/hooks/command @@ -24,6 +24,8 @@ trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT . "$DIR/../lib/shared.bash" # shellcheck source=lib/metadata.bash . "$DIR/../lib/metadata.bash" +# shellcheck source=lib/run.bash +. "$DIR/../lib/run.bash" commands=() @@ -48,8 +50,6 @@ if in_array "BUILD" "${commands[@]}" ; then . "$DIR/../commands/build.sh" fi if in_array "RUN" "${commands[@]}" ; then - # shellcheck source=lib/run.bash - . "$DIR/../lib/run.bash" # shellcheck source=commands/run.sh . "$DIR/../commands/run.sh" fi From eebd7a26786d631188af97087692c6e00297d05c Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 12:53:00 -0700 Subject: [PATCH 43/52] try running compose commands directly --- commands/run.sh | 5 +++-- lib/run.bash | 3 ++- lib/shared.bash | 2 -- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 8c1eb02e..d28fbb70 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -104,8 +104,9 @@ echo "PID is: $BASHPID" exitcode=0 ( - echo "docker compose being called" - run_docker_compose "${cmd_lit[@]}" || exitcode=$? + echo "docker compose being called. PID is: $BASHPID" + docker compose "${cmd_lit[@]}" || exitcode=$? + # run_docker_compose "${cmd_lit[@]}" || exitcode=$? ) diff --git a/lib/run.bash b/lib/run.bash index 522ef069..7fbc5afd 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -33,7 +33,8 @@ compose_cleanup() { if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then echo "graceful shutdown was true, stopping ${1}" # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period - run_docker_compose stop "$1" || true + # run_docker_compose stop "$1" || true + docker compose "$1" kill -s SIGTERM fi } diff --git a/lib/shared.bash b/lib/shared.bash index 8ec34f02..26d48a98 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -255,8 +255,6 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") - echo "running: ${command[@]} $@" - plugin_prompt_and_run "${command[@]}" "$@" } From a99183045115f738577d060094fb57c131cd6962 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 13:09:12 -0700 Subject: [PATCH 44/52] revert run cmd --- commands/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index d28fbb70..97a362aa 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -105,8 +105,8 @@ echo "PID is: $BASHPID" exitcode=0 ( echo "docker compose being called. PID is: $BASHPID" - docker compose "${cmd_lit[@]}" || exitcode=$? - # run_docker_compose "${cmd_lit[@]}" || exitcode=$? + # docker compose "${cmd_lit[@]}" || exitcode=$? + run_docker_compose "${cmd_lit[@]}" || exitcode=$? ) From b7690a6d943b3b71fb0e4a967e5a266d232e07f6 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 13:21:44 -0700 Subject: [PATCH 45/52] Don't add name to compose kill --- lib/run.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/run.bash b/lib/run.bash index 7fbc5afd..da1960e9 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -34,7 +34,7 @@ compose_cleanup() { echo "graceful shutdown was true, stopping ${1}" # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period # run_docker_compose stop "$1" || true - docker compose "$1" kill -s SIGTERM + docker compose kill -s SIGTERM fi } From 58ee5e48de80b6b1b2fa5ed1a543b8410a7e3fb7 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 13:36:27 -0700 Subject: [PATCH 46/52] send kill to named service --- lib/run.bash | 2 +- lib/shared.bash | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/run.bash b/lib/run.bash index da1960e9..1b0b33a3 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -34,7 +34,7 @@ compose_cleanup() { echo "graceful shutdown was true, stopping ${1}" # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period # run_docker_compose stop "$1" || true - docker compose kill -s SIGTERM + docker compose kill -s SIGTERM "$1" fi } diff --git a/lib/shared.bash b/lib/shared.bash index 26d48a98..2e24ac84 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -255,6 +255,8 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") + echo "running: ${command[@]}" + plugin_prompt_and_run "${command[@]}" "$@" } From a931a74947cf57ee1869840ccc39554fe1058b66 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 13:48:45 -0700 Subject: [PATCH 47/52] send kill using build compose command --- lib/run.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/run.bash b/lib/run.bash index 1b0b33a3..250f2b5f 100644 --- a/lib/run.bash +++ b/lib/run.bash @@ -34,7 +34,7 @@ compose_cleanup() { echo "graceful shutdown was true, stopping ${1}" # Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period # run_docker_compose stop "$1" || true - docker compose kill -s SIGTERM "$1" + run_docker_compose kill -s SIGTERM "$1" fi } From 115141501f21bb0c4dac5f8abd95942fd0c45a7e Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 14:13:02 -0700 Subject: [PATCH 48/52] try running sleep to see if it restarts --- commands/run.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/run.sh b/commands/run.sh index 97a362aa..d92e965e 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -106,7 +106,8 @@ exitcode=0 ( echo "docker compose being called. PID is: $BASHPID" # docker compose "${cmd_lit[@]}" || exitcode=$? - run_docker_compose "${cmd_lit[@]}" || exitcode=$? + # run_docker_compose "${cmd_lit[@]}" || exitcode=$? + run_docker_compose "echo hello world, I'm starting here; sleep 10000" || exitcode=$? ) From d5e07aa2cd9ea441e65d59b151cd62a7e20feb0e Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 14:20:31 -0700 Subject: [PATCH 49/52] fix to prev --- commands/run.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index d92e965e..652f44a8 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -99,15 +99,15 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" # printf -v cmd_lit ' "%s" ' "${commands[@]}" -cmd_lit=( "${run_params[@]}" "${commands[@]}" ) +# cmd_lit=( "${run_params[@]}" "${commands[@]}" ) +cmd_lit=( "${run_params[@]}" "echo hello world, I'm starting here; sleep 10000" ) echo "PID is: $BASHPID" exitcode=0 ( echo "docker compose being called. PID is: $BASHPID" # docker compose "${cmd_lit[@]}" || exitcode=$? - # run_docker_compose "${cmd_lit[@]}" || exitcode=$? - run_docker_compose "echo hello world, I'm starting here; sleep 10000" || exitcode=$? + run_docker_compose "${cmd_lit[@]}" || exitcode=$? ) From 305ed1d1af6f281495c087af488fdc2159ac7463 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 14:33:44 -0700 Subject: [PATCH 50/52] use shell --- commands/cmd_to_run_generator.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/cmd_to_run_generator.sh b/commands/cmd_to_run_generator.sh index 1b9b2c6e..5b73d1d9 100644 --- a/commands/cmd_to_run_generator.sh +++ b/commands/cmd_to_run_generator.sh @@ -38,8 +38,8 @@ function generate_cmd() { if [[ -z $shell_disabled ]] && [[ ${#cmds[@]} -eq 0 ]] ; then if is_windows ; then cmds=("CMD.EXE" "/c") - # else - # cmds=("/bin/sh" "-e" "-c") + else + cmds=("/bin/sh" "-e" "-c") fi fi From 3e000614fa78bca1efd5a50c973614bafd4cba35 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 14:42:37 -0700 Subject: [PATCH 51/52] run regular cmd in subprocess --- commands/run.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index 652f44a8..708383e5 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -99,14 +99,13 @@ fi echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" echo "commands is: ${commands[@]}" # printf -v cmd_lit ' "%s" ' "${commands[@]}" -# cmd_lit=( "${run_params[@]}" "${commands[@]}" ) -cmd_lit=( "${run_params[@]}" "echo hello world, I'm starting here; sleep 10000" ) +cmd_lit=( "${run_params[@]}" "${commands[@]}" ) +# cmd_lit=( "${run_params[@]}" "echo hello world, I'm starting here; sleep 10000" ) echo "PID is: $BASHPID" exitcode=0 ( echo "docker compose being called. PID is: $BASHPID" - # docker compose "${cmd_lit[@]}" || exitcode=$? run_docker_compose "${cmd_lit[@]}" || exitcode=$? ) From 1435f76e4414f93b554645f23ef98e2abc577099 Mon Sep 17 00:00:00 2001 From: David Kutalek Date: Fri, 3 May 2024 15:35:22 -0700 Subject: [PATCH 52/52] try without -T --- commands/cmd_to_run_generator.sh | 4 ++-- commands/run_params_generator.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commands/cmd_to_run_generator.sh b/commands/cmd_to_run_generator.sh index 5b73d1d9..87b86bda 100644 --- a/commands/cmd_to_run_generator.sh +++ b/commands/cmd_to_run_generator.sh @@ -38,8 +38,8 @@ function generate_cmd() { if [[ -z $shell_disabled ]] && [[ ${#cmds[@]} -eq 0 ]] ; then if is_windows ; then cmds=("CMD.EXE" "/c") - else - cmds=("/bin/sh" "-e" "-c") + # else + # cmds=("/bin/sh" "-e" "-c") fi fi diff --git a/commands/run_params_generator.sh b/commands/run_params_generator.sh index 5dd62225..fd390613 100644 --- a/commands/run_params_generator.sh +++ b/commands/run_params_generator.sh @@ -119,7 +119,7 @@ function generate_run_args() { fi # Disable allocating a TTY - tty_default='false' + tty_default='true' if [[ "$(plugin_read_config TTY "$tty_default")" == "false" ]] ; then params+=(-T) fi