From 060ea8e3e42c4be3eaa7fca6f9d4e5817345bf0f Mon Sep 17 00:00:00 2001 From: Matthias Vach Date: Wed, 8 Sep 2021 10:34:30 +0200 Subject: [PATCH 1/3] Implement a drain script for postgres 10 properly use the fast shutdown mode Co-authored-by: Benjamin Gandon --- jobs/postgres-10/spec | 1 + jobs/postgres-10/templates/drain.erb | 33 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 jobs/postgres-10/templates/drain.erb diff --git a/jobs/postgres-10/spec b/jobs/postgres-10/spec index a9ea92143f3..392f24d1eb6 100644 --- a/jobs/postgres-10/spec +++ b/jobs/postgres-10/spec @@ -7,6 +7,7 @@ templates: create-database.erb: bin/create-database postgres.erb: bin/postgres postgresql.conf.erb: config/postgresql.conf + drain.erb: bin/drain packages: - postgres-10 diff --git a/jobs/postgres-10/templates/drain.erb b/jobs/postgres-10/templates/drain.erb new file mode 100644 index 00000000000..94eacfc02c6 --- /dev/null +++ b/jobs/postgres-10/templates/drain.erb @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# If a command fails, in a pipeline or not, exit immediately +set -e -o pipefail + +function prepend_datetime() { + awk -W interactive '{ system("echo -n [$(date +%FT%T%z)]"); print " " $0 }' +} + +# Before redirecting stdout and stderr, copy the file descriptor for original +# stdout where BOSH is expecting an integer, and only that. +exec 3>&1 +exec \ + 1> >(prepend_datetime >> /var/vcap/sys/log/postgres-10/drain.out.log) \ + 2> >(prepend_datetime >> /var/vcap/sys/log/postgres-10/drain.err.log) + +output_for_bosh() { + exit_code=$? + + if [ $exit_code -eq 0 ]; then + echo "postgres-10 deamon is properly shutdown with Fast Shutdown mode (SIGINT)" + else + echo "drain failed" + fi + + echo $exit_code >&3 +} + +trap output_for_bosh EXIT + + +postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres-10) +kill -s SIGINT "${postgres_pid}" From a388874f7a30bf6365027c448d573396dec83970 Mon Sep 17 00:00:00 2001 From: Benjamin Gandon Date: Wed, 8 Sep 2021 16:02:32 +0200 Subject: [PATCH 2/3] Implement same drain script for all postgres jobs Co-authored-by: Matthias Vach --- jobs/postgres-9.4/spec | 1 + jobs/postgres-9.4/templates/drain.erb | 33 +++++++++++++++++++++++++++ jobs/postgres/spec | 1 + jobs/postgres/templates/drain.erb | 33 +++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 jobs/postgres-9.4/templates/drain.erb create mode 100644 jobs/postgres/templates/drain.erb diff --git a/jobs/postgres-9.4/spec b/jobs/postgres-9.4/spec index d36274eadb8..81618870145 100644 --- a/jobs/postgres-9.4/spec +++ b/jobs/postgres-9.4/spec @@ -7,6 +7,7 @@ templates: create-database.erb: bin/create-database postgres.erb: bin/postgres postgresql.conf.erb: config/postgresql.conf + drain.erb: bin/drain packages: - postgres-9.4 diff --git a/jobs/postgres-9.4/templates/drain.erb b/jobs/postgres-9.4/templates/drain.erb new file mode 100644 index 00000000000..543ab00513d --- /dev/null +++ b/jobs/postgres-9.4/templates/drain.erb @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# If a command fails, in a pipeline or not, exit immediately +set -e -o pipefail + +function prepend_datetime() { + awk -W interactive '{ system("echo -n [$(date +%FT%T%z)]"); print " " $0 }' +} + +# Before redirecting stdout and stderr, copy the file descriptor for original +# stdout where BOSH is expecting an integer, and only that. +exec 3>&1 +exec \ + 1> >(prepend_datetime >> /var/vcap/sys/log/postgres-9.4/drain.out.log) \ + 2> >(prepend_datetime >> /var/vcap/sys/log/postgres-9.4/drain.err.log) + +output_for_bosh() { + exit_code=$? + + if [ $exit_code -eq 0 ]; then + echo "postgres-9.4 deamon is properly shutdown with Fast Shutdown mode (SIGINT)" + else + echo "drain failed" + fi + + echo $exit_code >&3 +} + +trap output_for_bosh EXIT + + +postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres-9.4) +kill -s SIGINT "${postgres_pid}" diff --git a/jobs/postgres/spec b/jobs/postgres/spec index 5819872734a..b7028dfd1a7 100644 --- a/jobs/postgres/spec +++ b/jobs/postgres/spec @@ -7,6 +7,7 @@ templates: create-database.erb: bin/create-database postgres.erb: bin/postgres postgresql.conf.erb: config/postgresql.conf + drain.erb: bin/drain packages: - postgres-13 diff --git a/jobs/postgres/templates/drain.erb b/jobs/postgres/templates/drain.erb new file mode 100644 index 00000000000..4d788b9120c --- /dev/null +++ b/jobs/postgres/templates/drain.erb @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# If a command fails, in a pipeline or not, exit immediately +set -e -o pipefail + +function prepend_datetime() { + awk -W interactive '{ system("echo -n [$(date +%FT%T%z)]"); print " " $0 }' +} + +# Before redirecting stdout and stderr, copy the file descriptor for original +# stdout where BOSH is expecting an integer, and only that. +exec 3>&1 +exec \ + 1> >(prepend_datetime >> /var/vcap/sys/log/postgres/drain.out.log) \ + 2> >(prepend_datetime >> /var/vcap/sys/log/postgres/drain.err.log) + +output_for_bosh() { + exit_code=$? + + if [ $exit_code -eq 0 ]; then + echo "postgres deamon is properly shutdown with Fast Shutdown mode (SIGINT)" + else + echo "drain failed" + fi + + echo $exit_code >&3 +} + +trap output_for_bosh EXIT + + +postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres) +kill -s SIGINT "${postgres_pid}" From bcbaddfbeaa11da3f0abb50ae145e0da309cc15e Mon Sep 17 00:00:00 2001 From: Matthias Vach Date: Mon, 27 Sep 2021 11:52:28 +0200 Subject: [PATCH 3/3] Use custom bpm stop script to shutdown postgres after draining is done Co-authored-by: Benjamin Gandon Co-authored-by: Ramon Makkelie --- jobs/postgres-10/monit | 2 +- jobs/postgres-10/spec | 2 +- jobs/postgres-10/templates/bpm-stop-postgres | 8 +++++ jobs/postgres-10/templates/drain.erb | 33 ------------------- jobs/postgres-9.4/monit | 2 +- jobs/postgres-9.4/spec | 2 +- jobs/postgres-9.4/templates/bpm-stop-postgres | 8 +++++ jobs/postgres-9.4/templates/drain.erb | 33 ------------------- jobs/postgres/monit | 2 +- jobs/postgres/spec | 2 +- jobs/postgres/templates/bpm-stop-postgres | 8 +++++ jobs/postgres/templates/drain.erb | 33 ------------------- 12 files changed, 30 insertions(+), 105 deletions(-) create mode 100755 jobs/postgres-10/templates/bpm-stop-postgres delete mode 100644 jobs/postgres-10/templates/drain.erb create mode 100755 jobs/postgres-9.4/templates/bpm-stop-postgres delete mode 100644 jobs/postgres-9.4/templates/drain.erb create mode 100755 jobs/postgres/templates/bpm-stop-postgres delete mode 100644 jobs/postgres/templates/drain.erb diff --git a/jobs/postgres-10/monit b/jobs/postgres-10/monit index 7ff4169d4cc..ef0e0abf199 100644 --- a/jobs/postgres-10/monit +++ b/jobs/postgres-10/monit @@ -1,5 +1,5 @@ check process postgres with pidfile /var/vcap/sys/run/bpm/postgres-10/postgres-10.pid start program "/var/vcap/jobs/bpm/bin/bpm start postgres-10" with timeout 300 seconds - stop program "/var/vcap/jobs/bpm/bin/bpm stop postgres-10" + stop program "/var/vcap/jobs/postgres-10/bin/bpm-stop-postgres" group vcap diff --git a/jobs/postgres-10/spec b/jobs/postgres-10/spec index 392f24d1eb6..57be1523f5e 100644 --- a/jobs/postgres-10/spec +++ b/jobs/postgres-10/spec @@ -7,7 +7,7 @@ templates: create-database.erb: bin/create-database postgres.erb: bin/postgres postgresql.conf.erb: config/postgresql.conf - drain.erb: bin/drain + bpm-stop-postgres: bin/bpm-stop-postgres packages: - postgres-10 diff --git a/jobs/postgres-10/templates/bpm-stop-postgres b/jobs/postgres-10/templates/bpm-stop-postgres new file mode 100755 index 00000000000..17d5aece59e --- /dev/null +++ b/jobs/postgres-10/templates/bpm-stop-postgres @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e -o pipefail + +postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres-10) +kill -s SIGINT "${postgres_pid}" + +/var/vcap/jobs/bpm/bin/bpm stop postgres-10 diff --git a/jobs/postgres-10/templates/drain.erb b/jobs/postgres-10/templates/drain.erb deleted file mode 100644 index 94eacfc02c6..00000000000 --- a/jobs/postgres-10/templates/drain.erb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -# If a command fails, in a pipeline or not, exit immediately -set -e -o pipefail - -function prepend_datetime() { - awk -W interactive '{ system("echo -n [$(date +%FT%T%z)]"); print " " $0 }' -} - -# Before redirecting stdout and stderr, copy the file descriptor for original -# stdout where BOSH is expecting an integer, and only that. -exec 3>&1 -exec \ - 1> >(prepend_datetime >> /var/vcap/sys/log/postgres-10/drain.out.log) \ - 2> >(prepend_datetime >> /var/vcap/sys/log/postgres-10/drain.err.log) - -output_for_bosh() { - exit_code=$? - - if [ $exit_code -eq 0 ]; then - echo "postgres-10 deamon is properly shutdown with Fast Shutdown mode (SIGINT)" - else - echo "drain failed" - fi - - echo $exit_code >&3 -} - -trap output_for_bosh EXIT - - -postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres-10) -kill -s SIGINT "${postgres_pid}" diff --git a/jobs/postgres-9.4/monit b/jobs/postgres-9.4/monit index 999de4f1c0a..25be4849c68 100644 --- a/jobs/postgres-9.4/monit +++ b/jobs/postgres-9.4/monit @@ -1,5 +1,5 @@ check process postgres with pidfile /var/vcap/sys/run/bpm/postgres-9.4/postgres-9.4.pid start program "/var/vcap/jobs/bpm/bin/bpm start postgres-9.4" with timeout 300 seconds - stop program "/var/vcap/jobs/bpm/bin/bpm stop postgres-9.4" + stop program "/var/vcap/jobs/postgres-9.4/bin/bpm-stop-postgres" group vcap diff --git a/jobs/postgres-9.4/spec b/jobs/postgres-9.4/spec index 81618870145..5abf57427b6 100644 --- a/jobs/postgres-9.4/spec +++ b/jobs/postgres-9.4/spec @@ -7,7 +7,7 @@ templates: create-database.erb: bin/create-database postgres.erb: bin/postgres postgresql.conf.erb: config/postgresql.conf - drain.erb: bin/drain + bpm-stop-postgres: bin/bpm-stop-postgres packages: - postgres-9.4 diff --git a/jobs/postgres-9.4/templates/bpm-stop-postgres b/jobs/postgres-9.4/templates/bpm-stop-postgres new file mode 100755 index 00000000000..98152769330 --- /dev/null +++ b/jobs/postgres-9.4/templates/bpm-stop-postgres @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e -o pipefail + +postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres-9.4) +kill -s SIGINT "${postgres_pid}" + +/var/vcap/jobs/bpm/bin/bpm stop postgres-9.4 diff --git a/jobs/postgres-9.4/templates/drain.erb b/jobs/postgres-9.4/templates/drain.erb deleted file mode 100644 index 543ab00513d..00000000000 --- a/jobs/postgres-9.4/templates/drain.erb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -# If a command fails, in a pipeline or not, exit immediately -set -e -o pipefail - -function prepend_datetime() { - awk -W interactive '{ system("echo -n [$(date +%FT%T%z)]"); print " " $0 }' -} - -# Before redirecting stdout and stderr, copy the file descriptor for original -# stdout where BOSH is expecting an integer, and only that. -exec 3>&1 -exec \ - 1> >(prepend_datetime >> /var/vcap/sys/log/postgres-9.4/drain.out.log) \ - 2> >(prepend_datetime >> /var/vcap/sys/log/postgres-9.4/drain.err.log) - -output_for_bosh() { - exit_code=$? - - if [ $exit_code -eq 0 ]; then - echo "postgres-9.4 deamon is properly shutdown with Fast Shutdown mode (SIGINT)" - else - echo "drain failed" - fi - - echo $exit_code >&3 -} - -trap output_for_bosh EXIT - - -postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres-9.4) -kill -s SIGINT "${postgres_pid}" diff --git a/jobs/postgres/monit b/jobs/postgres/monit index 7c3ef102a37..2c40d5b8f3f 100644 --- a/jobs/postgres/monit +++ b/jobs/postgres/monit @@ -1,5 +1,5 @@ check process postgres with pidfile /var/vcap/sys/run/bpm/postgres/postgres.pid start program "/var/vcap/jobs/bpm/bin/bpm start postgres" with timeout 300 seconds - stop program "/var/vcap/jobs/bpm/bin/bpm stop postgres" + stop program "/var/vcap/jobs/postgres/bin/bpm-stop-postgres" group vcap diff --git a/jobs/postgres/spec b/jobs/postgres/spec index b7028dfd1a7..f072811a886 100644 --- a/jobs/postgres/spec +++ b/jobs/postgres/spec @@ -7,7 +7,7 @@ templates: create-database.erb: bin/create-database postgres.erb: bin/postgres postgresql.conf.erb: config/postgresql.conf - drain.erb: bin/drain + bpm-stop-postgres: bin/bpm-stop-postgres packages: - postgres-13 diff --git a/jobs/postgres/templates/bpm-stop-postgres b/jobs/postgres/templates/bpm-stop-postgres new file mode 100755 index 00000000000..32b7e595485 --- /dev/null +++ b/jobs/postgres/templates/bpm-stop-postgres @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e -o pipefail + +postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres) +kill -s SIGINT "${postgres_pid}" + +/var/vcap/jobs/bpm/bin/bpm stop postgres diff --git a/jobs/postgres/templates/drain.erb b/jobs/postgres/templates/drain.erb deleted file mode 100644 index 4d788b9120c..00000000000 --- a/jobs/postgres/templates/drain.erb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -# If a command fails, in a pipeline or not, exit immediately -set -e -o pipefail - -function prepend_datetime() { - awk -W interactive '{ system("echo -n [$(date +%FT%T%z)]"); print " " $0 }' -} - -# Before redirecting stdout and stderr, copy the file descriptor for original -# stdout where BOSH is expecting an integer, and only that. -exec 3>&1 -exec \ - 1> >(prepend_datetime >> /var/vcap/sys/log/postgres/drain.out.log) \ - 2> >(prepend_datetime >> /var/vcap/sys/log/postgres/drain.err.log) - -output_for_bosh() { - exit_code=$? - - if [ $exit_code -eq 0 ]; then - echo "postgres deamon is properly shutdown with Fast Shutdown mode (SIGINT)" - else - echo "drain failed" - fi - - echo $exit_code >&3 -} - -trap output_for_bosh EXIT - - -postgres_pid=$(/var/vcap/packages/bpm/bin/bpm pid postgres) -kill -s SIGINT "${postgres_pid}"