Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Davkutalek/refactor #436

Draft
wants to merge 53 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0782f49
Fix for graceful shutdown option. Don't call docker rm --force until …
Apr 24, 2024
853a524
Merge remote-tracking branch 'upstream/master' into davkutalek/premat…
Apr 25, 2024
af54780
working on testing graceful shutdown fix
Apr 25, 2024
bebc54f
fixed broken tests
Apr 25, 2024
1b6df23
try running docker shell arg in [] to use exec mode
Apr 29, 2024
dc72a0f
try graceful compose shutdown
Apr 30, 2024
553a8d6
pass service name arg to stop/wait
Apr 30, 2024
4dbb936
exit upon getting term signal, otherwise service restarts
Apr 30, 2024
846e1bc
Don't run docker command in subshell, obviously signals wont make it\!
Apr 30, 2024
e5fd440
rewrite run.sh to use docker compose exec form. All the tests broke, …
Apr 30, 2024
99c0fc3
revert previous since it isn't working
Apr 30, 2024
722c50e
attempt to run shell script without sh -c which creates new shell
Apr 30, 2024
47d200d
syntax fix
Apr 30, 2024
aa1220c
try subshell with direct command
Apr 30, 2024
8e06129
refactor subshell to make it a little clearer, prevent exitcode -u issue
May 1, 2024
1a5d072
try not using a subshell again
May 1, 2024
812ece2
trying to clean up run file so I can understand the docker calls
May 1, 2024
3031858
Remove e flag from file, just gets in the way
May 2, 2024
570b262
Tabbing fix, add some logging
May 2, 2024
02a77d7
not sure why exit code is causing exit
May 2, 2024
4fa1a59
Move trap
May 2, 2024
df849c9
remove root -e
May 2, 2024
73500d8
declare array
May 2, 2024
8a41127
syntax fix
May 2, 2024
6dad4ad
fix circular name issue
May 2, 2024
fd19fe4
simplify logic, put run args in an array
May 2, 2024
e3df3ba
try different syntax
May 2, 2024
79a565e
try different syntax
May 2, 2024
78c7d41
try different syntax
May 2, 2024
e1d6ba3
try different syntax
May 2, 2024
0c79c5d
add some logging
May 2, 2024
fc598aa
logic fix
May 2, 2024
ae42cf4
syntax fix
May 2, 2024
000655d
syntax fix
May 2, 2024
b2bef76
syntax fix
May 2, 2024
662f6ad
revert to non-array since it isn't workingx
May 2, 2024
857c525
syntax fix
May 2, 2024
83f6a47
syntax fix
May 3, 2024
b9ccdd6
syntax fix
May 3, 2024
b7722ca
syntax fix
May 3, 2024
47e78be
Move err trap, add some logging
May 3, 2024
882e5a3
move signal trap, make sure err triggers in places we want it to and …
May 3, 2024
d208f69
run compose in subshell, move run command generation to it's own file
May 3, 2024
eebd7a2
try running compose commands directly
May 3, 2024
a991830
revert run cmd
May 3, 2024
b7690a6
Don't add name to compose kill
May 3, 2024
58ee5e4
send kill to named service
May 3, 2024
a931a74
send kill using build compose command
May 3, 2024
1151415
try running sleep to see if it restarts
May 3, 2024
d5e07aa
fix to prev
May 3, 2024
305ed1d
use shell
May 3, 2024
3e00061
run regular cmd in subprocess
May 3, 2024
1435f76
try without -T
May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions commands/cmd_to_run_generator.sh
Original file line number Diff line number Diff line change
@@ -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
}
73 changes: 73 additions & 0 deletions commands/pull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
set -uo 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

echo "done pulling. exitcode: $exitcode"
return $exitcode
}
Loading