Add script to restart postgres clusters
authorPeter Palfrader <peter@palfrader.org>
Thu, 8 Feb 2018 12:38:53 +0000 (13:38 +0100)
committerPeter Palfrader <peter@palfrader.org>
Thu, 8 Feb 2018 12:38:53 +0000 (13:38 +0100)
modules/postgres/files/dsa-restart-all-idle-postgres [new file with mode: 0755]
modules/postgres/manifests/init.pp

diff --git a/modules/postgres/files/dsa-restart-all-idle-postgres b/modules/postgres/files/dsa-restart-all-idle-postgres
new file mode 100755 (executable)
index 0000000..2f7ee90
--- /dev/null
@@ -0,0 +1,53 @@
+#! /bin/bash
+
+# Written by Peter Palfrader
+# Based on code by Tollef Fog Heen <tfheen@err.no> based on code by Peter Palfrader
+#
+# Restart all postgresql clusters when they do not have connections to them.
+
+set -e
+set -u
+
+# chk_net <port>
+# 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
index a3015db..fb6f055 100644 (file)
@@ -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',
+       }
 }