Skip to content

Commit f252caa

Browse files
authored
Merge pull request #14 from joinhandshake/old-version-test
Add logging to DD, attempt to make shutdown more graceful
2 parents c5883ed + 3612a72 commit f252caa

6 files changed

Lines changed: 86 additions & 37 deletions

File tree

commands/run.sh

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22
set -ueo pipefail
33

4+
. "$DIR/../lib/log.bash"
5+
46
# Run takes a service name, pulls down any pre-built image for that name
57
# and then runs docker-compose run a generated project name
68

@@ -423,35 +425,30 @@ elif [[ ${#command[@]} -gt 0 ]] ; then
423425
fi
424426

425427
ensure_stopped() {
426-
echo '+++ :warning: Signal received, stopping container'
427-
docker stop "${container_name}" || true
428+
log 143
429+
echo '+++ :warning: Signal received, stopping container gracefully'
430+
# docker stop "${container_name}" || true
431+
compose_cleanup ${run_service}
428432
echo '~~~ Last log lines that may be missing above (if container was not already removed)'
429433
docker logs "${container_name}" || true
430-
exitcode='TRAP'
434+
exit 143
431435
}
432436

433-
trap ensure_stopped SIGINT SIGTERM SIGQUIT
437+
trap 'ensure_stopped "$?"' SIGINT SIGTERM SIGQUIT
434438

435439
if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_COLLAPSE_RUN_LOG_GROUP:-false}" = "true" ]]; then
436440
group_type="---"
437441
else
438442
group_type="+++"
439443
fi
440-
# Disable -e to prevent cancelling step if the command fails for whatever reason
441-
set +e
442-
( # subshell is necessary to trap signals (compose v2 fails to stop otherwise)
444+
445+
exitcode=0
446+
(
443447
echo "${group_type} :docker: Running ${display_command[*]:-} in service $run_service" >&2
444448
run_docker_compose "${run_params[@]}"
445-
)
446-
exitcode=$?
447-
448-
# Restore -e as an option.
449-
set -e
449+
) || exitcode=$?
450450

451-
if [[ $exitcode = "TRAP" ]]; then
452-
# command failed due to cancellation signal, make sure there is an error but no further output
453-
exitcode=-1
454-
elif [[ $exitcode -ne 0 ]] ; then
451+
if [[ $exitcode -ne 0 ]] ; then
455452
echo "^^^ +++"
456453
echo "+++ :warning: Failed to run command, exited with $exitcode, run params:"
457454
echo "${run_params[@]}"
@@ -465,4 +462,4 @@ if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then
465462
fi
466463
fi
467464

468-
return "$exitcode"
465+
return "$exitcode"

hooks/pre-exit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ if [[ -n "$(plugin_read_list RUN)" ]] && [[ "$(plugin_read_config CLEANUP "true"
1515
. "$DIR/../lib/run.bash"
1616

1717
echo "~~~ :docker: Cleaning up after docker-compose" >&2
18-
compose_cleanup
18+
compose_cleanup ""
1919
fi

lib/log.bash

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
log() {
4+
msg="SIG $1 received, process exiting"
5+
echo "${msg}"
6+
buildkite-agent meta-data set "dd_tags.job-signal-${BUILDKITE_STEP_KEY}" "$1"
7+
buildkite-agent meta-data set "dd_tags.step-error-code-${BUILDKITE_STEP_KEY}" "$1"
8+
buildkite-agent meta-data set "dd_tags.job-error-code-${BUILDKITE_JOB_ID}" "$1"
9+
10+
echo "$(pidof buildkite-agent) is the pid of the buildkite agent" || true
11+
12+
send_job_signaled_to_dd "${msg}" "${1}"
13+
}
14+
15+
send_job_signaled_to_dd() {
16+
send_event_to_dd '{ "title": "Job '"${BUILDKITE_STEP_KEY}"' received signal", "text": "'"${1}"'", "alert_type": "error", "tags": [ "ci:job_signal", "exit_status:'"${2}"'", "job_name:'"${BUILDKITE_STEP_KEY}"'", "build_id:'"${BUILDKITE_BUILD_ID}"'", "branch:'"${BUILDKITE_BRANCH}"'", "hs_source:docker_compose_plugin", "env:ci" ] }'
17+
}
18+
19+
send_event_to_dd() {
20+
if command -v curl >/dev/null 2>&1; then
21+
echo "Using curl to send event to Datadog"
22+
curl -X POST "https://api.datadoghq.com/api/v1/events" \
23+
-H "Accept: application/json" \
24+
-H "Content-Type: application/json" \
25+
-H "DD-API-KEY: ${DD_API_KEY}" \
26+
-d "$1"
27+
elif command -v wget >/dev/null 2>&1; then
28+
echo "Using wget to send event to Datadog"
29+
wget \
30+
--header="Accept: application/json" \
31+
--header="Content-Type: application/json" \
32+
--header="DD-API-KEY: ${DD_API_KEY}" \
33+
--post-data="$1" \
34+
--output-document - \
35+
https://api.datadoghq.com/api/v1/events
36+
else
37+
echo "No suitable network tool found to send event to Datadog"
38+
exit 1
39+
fi
40+
}

lib/run.bash

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/bin/bash
22

3-
compose_cleanup() {
4-
if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "false" ]]; then
5-
# Send all containers a SIGKILL
6-
run_docker_compose kill || true
7-
else
8-
# Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period
9-
run_docker_compose stop || true
3+
kill_or_wait_for_stop() {
4+
5+
if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then
6+
# This will block until the container exits
7+
run_docker_compose wait "$1"
8+
container_exit_code=$?
9+
echo "exit code was $container_exit_code"
1010
fi
1111

12+
# This will kill the container if it hasn't exited yet
1213
# `compose down` doesn't support force removing images
1314
if [[ "$(plugin_read_config LEAVE_VOLUMES 'false')" == "false" ]]; then
1415
run_docker_compose rm --force -v || true
@@ -24,6 +25,18 @@ compose_cleanup() {
2425
fi
2526
}
2627

28+
compose_cleanup() {
29+
kill_or_wait_for_stop "$1" &
30+
sleep 1
31+
32+
# No need to call kill directly for GRACEFUL_SHUTDOWN == false since rm --force will send the same kill signal
33+
if [[ "$(plugin_read_config GRACEFUL_SHUTDOWN 'false')" == "true" ]]; then
34+
echo "graceful shutdown was true, stopping ${1}"
35+
# Send all containers a friendly SIGTERM, followed by a SIGKILL after exceeding the stop_grace_period
36+
run_docker_compose stop "$1" || true
37+
fi
38+
}
39+
2740
# Checks for failed containers and writes logs for them the the provided dir
2841
check_linked_containers_and_save_logs() {
2942
local service="$1"

tests/cleanup.bats

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ load '../lib/run'
1717
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=true
1818

1919
stub docker-compose \
20-
"-f docker-compose.yml -p buildkite1111 kill : echo killing containers" \
21-
"-f docker-compose.yml -p buildkite1111 rm --force -v : echo removing stopped containers" \
20+
"-f docker-compose.yml -p buildkite1111 rm --force -v : echo killing and removing stopped containers" \
2221
"-f docker-compose.yml -p buildkite1111 down --remove-orphans --volumes : echo removing everything"
2322

2423
run "$PWD"/hooks/pre-exit
2524

2625
assert_success
2726
assert_output --partial "Cleaning up after docker-compose"
27+
2828
unstub docker-compose
2929
}

tests/docker-compose-cleanup.bats

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,26 @@ setup () {
1515
run compose_cleanup
1616

1717
assert_success
18-
assert_equal "${lines[0]}" "kill"
19-
assert_equal "${lines[1]}" "rm --force -v"
20-
assert_equal "${lines[2]}" "down --remove-orphans --volumes"
18+
assert_equal "${lines[0]}" "rm --force -v"
19+
assert_equal "${lines[1]}" "down --remove-orphans --volumes"
2120
}
2221

2322
@test "Possible to gracefully shutdown containers in docker-compose cleanup" {
24-
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_GRACEFUL_SHUTDOWN=1
23+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_GRACEFUL_SHUTDOWN="true"
2524
run compose_cleanup
2625

2726
assert_success
28-
assert_equal "${lines[0]}" "stop"
29-
assert_equal "${lines[1]}" "rm --force -v"
30-
assert_equal "${lines[2]}" "down --remove-orphans --volumes"
27+
assert_output --partial "wait"
28+
assert_equal "${lines[1]}" "exit code was 0"
29+
assert_equal "${lines[2]}" "rm --force -v"
30+
assert_equal "${lines[3]}" "down --remove-orphans --volumes"
3131
}
3232

3333
@test "Possible to skip volume destruction in docker-compose cleanup" {
3434
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_LEAVE_VOLUMES=1
3535
run compose_cleanup
3636

3737
assert_success
38-
assert_equal "${lines[0]}" "kill"
39-
assert_equal "${lines[1]}" "rm --force"
40-
assert_equal "${lines[2]}" "down --remove-orphans"
38+
assert_equal "${lines[0]}" "rm --force"
39+
assert_equal "${lines[1]}" "down --remove-orphans"
4140
}

0 commit comments

Comments
 (0)