From c51d68a7375915154a90c22e716e832f635ca878 Mon Sep 17 00:00:00 2001 From: Beni Ruef Date: Thu, 3 Dec 2020 18:48:04 +0100 Subject: [PATCH] [type/__postgres_conf] New type based on ALTER SYSTEM command --- .../conf/type/__postgres_conf/gencode-remote | 92 +++++++++++++++++++ cdist/conf/type/__postgres_conf/man.rst | 55 +++++++++++ .../__postgres_conf/parameter/default/state | 1 + .../type/__postgres_conf/parameter/optional | 2 + 4 files changed, 150 insertions(+) create mode 100755 cdist/conf/type/__postgres_conf/gencode-remote create mode 100644 cdist/conf/type/__postgres_conf/man.rst create mode 100644 cdist/conf/type/__postgres_conf/parameter/default/state create mode 100644 cdist/conf/type/__postgres_conf/parameter/optional diff --git a/cdist/conf/type/__postgres_conf/gencode-remote b/cdist/conf/type/__postgres_conf/gencode-remote new file mode 100755 index 00000000..a09e1873 --- /dev/null +++ b/cdist/conf/type/__postgres_conf/gencode-remote @@ -0,0 +1,92 @@ +#!/bin/sh -e +# -*- mode: sh; indent-tabs-mode: t -*- +# +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) +# 2020 Beni Ruef (bernhard.ruef at ssrq-sds-fds.ch) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +os=$(cat "${__global}/explorer/os") +state_should=$(cat "${__object}/parameter/state") + +if [ "${state_should}" != 'present' ] && [ "${state_should}" != 'absent' ] +then + echo "Invalid state '${state_should}'." \ + 'Only "present" and "absent" are acceptable' >&2 + exit 1 +fi + +if [ "${state_should}" = 'present' ] && [ ! -f "${__object}/parameter/value" ] +then + echo 'Missing required parameter "value"' >&2 + exit 1 +fi + +# Parameters +conf_name="${__object_id}" +if [ -f "${__object}/parameter/value" ] +then + conf_value=$(cat "${__object}/parameter/value") +fi + +if [ "${state_should}" = 'present' ] +then + set_command="ALTER SYSTEM SET ${conf_name} = '${conf_value}'" + check_command="SHOW ${conf_name}" +else + set_command="ALTER SYSTEM SET ${conf_name} = DEFAULT" +fi + +case $os +in + openbsd|devuan) + case $os + in + openbsd) + postgres_user='_postgresql' + restart_command='/etc/rc.d/postgresql restart' + ;; + devuan) + postgres_user='postgres' + restart_command='/etc/init.d/postgresql restart' + ;; + esac + # needs two separate psql commands because ALTER SYSTEM + # cannot run inside a transaction block + cat <<-EOF + su - ${postgres_user} -c "psql postgres -twAc \ + \"${set_command}\"" + su - ${postgres_user} -c "psql postgres -twAc \ + \"SELECT pg_reload_conf()\"" + EOF + # check success (makes only sense if setting to a non-default value) + # and restart server if needed + if [ "${state_should}" = 'present' ] + then + cat <<-EOF + if [ \$(su - ${postgres_user} -c "psql postgres -twAc \"SHOW ${conf_name}\"") != '${conf_value}' ] + then + ${restart_command} + fi + EOF + fi + ;; + *) + echo "Unsupported OS: ${os}" >&2 + exit 1 + ;; +esac diff --git a/cdist/conf/type/__postgres_conf/man.rst b/cdist/conf/type/__postgres_conf/man.rst new file mode 100644 index 00000000..76016c6c --- /dev/null +++ b/cdist/conf/type/__postgres_conf/man.rst @@ -0,0 +1,55 @@ +cdist-type__postgres_conf(7) +============================ + +Configure a PostgreSQL server. + +NOTE: This type might need to be run multiple times to apply all bits of the +configuration due to ordering requirements. + +SSRQ + + +DESCRIPTION +----------- +Configure a PostgreSQL server using ALTER SYSTEM. + + +REQUIRED PARAMETERS +------------------- +value + The value to setup (can be omitted when state is set to "absent"). + + +OPTIONAL PARAMETERS +------------------- +state + "present" or "absent". Defaults to "present". + + +BOOLEAN PARAMETERS +------------------ +None. + + +EXAMPLES +-------- + +.. code-block:: sh + + # set timezone + __postgres_conf timezone --value Europe/Zurich + + # reset maximum number of concurrent connections to default (normally 100) + __postgres_conf max_connections --state absent + + +SEE ALSO +-------- +- `cdist-type(7) `_ + + +COPYING +------- +Copyright \(C) 2020 SSRQ (www.ssrq-sds-fds.ch). +Free use of this software is granted under the terms +of the GNU General Public License version 3 (GPLv3). diff --git a/cdist/conf/type/__postgres_conf/parameter/default/state b/cdist/conf/type/__postgres_conf/parameter/default/state new file mode 100644 index 00000000..e7f6134f --- /dev/null +++ b/cdist/conf/type/__postgres_conf/parameter/default/state @@ -0,0 +1 @@ +present diff --git a/cdist/conf/type/__postgres_conf/parameter/optional b/cdist/conf/type/__postgres_conf/parameter/optional new file mode 100644 index 00000000..d0460d86 --- /dev/null +++ b/cdist/conf/type/__postgres_conf/parameter/optional @@ -0,0 +1,2 @@ +state +value