Skip to content

Commit b7ed3dd

Browse files
committed
changes to try graceful shutdown
1 parent 4e23e98 commit b7ed3dd

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

roles/docker_image_build/defaults/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ healthcheck_command: |-
5656
{% endif %}
5757
5858
entrypoint: "{{ splunk_home }}/entrypoint.sh"
59-
tini_entrypoint: "{{ [\"/tini\", \"-g\", \"--\", \"/bin/bash\", \"-c\"] + entrypoint | split }}"
59+
tini_entrypoint: "{{ [\"/tini\", \"--\", \"/bin/bash\", \"-c\"] + entrypoint | split }}"
6060
entrypoint_list: "{{ tini_entrypoint | to_json }}"
6161

6262
# by default run entrypoint as splunk user

roles/docker_image_build/templates/base/entrypoint.sh

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=all
23

3-
set -e
4+
# set -e
45

56
# performs mongod upgrades and engine migrations as configured
67
migrate_kvstore() {
7-
{{ splunk_home }}/bin/splunk migrate migrate-kvstore-36-40 || true
8+
{{ splunk_home }}/bin/splunk migrate migrate-kvstore-36-40
89

910
{% if migrate_kvstore is defined %}
1011
{{ splunk_home }}/bin/splunk start --accept-license --answer-yes --no-prompt $@
@@ -19,22 +20,45 @@ migrate_kvstore() {
1920

2021
# starts Splunk using the CLI
2122
start_splunk() {
22-
# replace the process running this script with the process running Splunk
23-
# note: this "fixes" signal forwarding so that the trap for stop_splunk
24-
# should not be needed, however it has been included here in case it
25-
# occurs before the exec is complete
23+
printf "Performing kvstore migration before starting Splunk\n"
2624
migrate_kvstore
27-
exec {{ splunk_home }}/bin/splunk start --nodaemon --accept-license --answer-yes --no-prompt $@
25+
26+
printf "Starting Splunk with arguments ${*}\n"
27+
{{ splunk_home }}/bin/splunk start --nodaemon --accept-license --answer-yes --no-prompt $@ &
28+
splunk_pid=$!
29+
printf "Splunk is now running with pid ${splunk_pid}\n"
30+
wait_for_splunk_to_stop
2831
}
2932

3033
# stops Splunk using the CLI
3134
stop_splunk() {
32-
{{ splunk_home }}/bin/splunk stop $@ 2>/dev/null || true
35+
printf "Stopping Splunk running with pid ${splunk_pid} for graceful shutdown\n"
36+
{{ splunk_home }}/bin/splunk stop
37+
if [ -f {{ splunk_home }}/var/run/splunk/conf-mutator.pid ]; then
38+
printf "Found conf-mutator.pid file containing pid %d after shutdown, removing...\n" "$(cat {{ splunk_home }}/var/run/splunk/conf-mutator.pid)"
39+
rm -vf {{ splunk_home }}/var/run/splunk/conf-mutator.pid
40+
fi
41+
}
42+
43+
wait_for_splunk_to_stop() {
44+
wait -n ${splunk_pid}
45+
printf "Entrypoint has detected that Splunk is no longer running and is now exiting\n"
46+
}
47+
48+
# log graceful shutdown and call stop_splunk
49+
graceful_shutdown() {
50+
printf "Received SIGTERM interrupt, now stopping Splunk running with PID ${splunk_pid} for graceful shutdown\n"
51+
printf "Stopping Splunk running with pid ${splunk_pid} for graceful shutdown\n"
52+
{{ splunk_home }}/bin/splunk stop
53+
if [ -f {{ splunk_home }}/var/run/splunk/conf-mutator.pid ]; then
54+
printf "Found conf-mutator.pid file containing pid %d after shutdown, removing...\n" "$(cat {{ splunk_home }}/var/run/splunk/conf-mutator.pid)"
55+
rm -vf {{ splunk_home }}/var/run/splunk/conf-mutator.pid
56+
fi
3357
}
3458

3559
# calls stop_splunk function when SIGINT and SIGTERM are received
3660
# to allow for graceful shutdowns in Docker Swarm
37-
trap stop_splunk SIGINT SIGTERM
61+
trap graceful_shutdown SIGTERM
3862

3963
# restarts Splunk using the CLI
4064
restart_splunk() {

0 commit comments

Comments
 (0)