From: Peter Palfrader Date: Thu, 8 Feb 2018 12:38:53 +0000 (+0100) Subject: Add script to restart postgres clusters X-Git-Url: https://git.adam-barratt.org.uk/?a=commitdiff_plain;h=6e66642ee331bbcafad68b2d3606321a9d337a83;p=mirror%2Fdsa-puppet.git Add script to restart postgres clusters --- diff --git a/modules/postgres/files/dsa-restart-all-idle-postgres b/modules/postgres/files/dsa-restart-all-idle-postgres new file mode 100755 index 000000000..2f7ee9025 --- /dev/null +++ b/modules/postgres/files/dsa-restart-all-idle-postgres @@ -0,0 +1,53 @@ +#! /bin/bash + +# Written by Peter Palfrader +# Based on code by Tollef Fog Heen based on code by Peter Palfrader +# +# Restart all postgresql clusters when they do not have connections to them. + +set -e +set -u + +# chk_net +# returns true (0) if there are connections to that port. +chk_net() { + local port="$1"; shift + local con="$(ss -H -nt "sport = :$port" | wc -l)" + if [ "$con" -ge 1 ]; then + return 0 + else + return 1 + fi +} + +get_pidfile_value() { + local version="$1"; shift + local name="$1"; shift + + local fn="/run/postgresql/${version}-${name}.pid" + if [ -e "$fn" ] ; then + cat "$fn" + else + echo "No pidfile at $fn" >&2 + exit 1 + fi +} + +handle_cluster() { + local version="$1"; shift + local name="$1"; shift + local port="$1"; shift + + pid="$(get_pidfile_value "$version" "$name")" + while chk_net "$port" && [ "$pid" = "$(get_pidfile_value "$version" "$name")" ]; do + sleep 10 + done + systemctl restart "postgresql@${version}-${name}.service" +} + +pg_lsclusters -h | while read version name port status; do + echo "$status" | grep -q --word-regexp online || continue + handle_cluster $version $name $port & +done + +wait diff --git a/modules/postgres/manifests/init.pp b/modules/postgres/manifests/init.pp index a3015dbfa..fb6f05566 100644 --- a/modules/postgres/manifests/init.pp +++ b/modules/postgres/manifests/init.pp @@ -28,4 +28,9 @@ class postgres { ensure => $ensure, source => 'puppet:///modules/postgres/plugin.conf', } + file { '/usr/local/sbin/dsa-restart-all-idle-postgres': + ensure => $ensure, + source => 'puppet:///modules/postgres/dsa-restart-idle-postgres', + mode => '0555', + } }