forked from ungleich-public/cdist
[type/__postgres_extension] Add state explorer
This commit is contained in:
parent
3cf93249c3
commit
8296051653
3 changed files with 104 additions and 36 deletions
41
cdist/conf/type/__postgres_extension/explorer/state
Normal file
41
cdist/conf/type/__postgres_extension/explorer/state
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#!/bin/sh -e
|
||||||
|
# -*- mode: sh; indent-tabs-mode: t -*-
|
||||||
|
#
|
||||||
|
# 2021 Dennis Camera (dennis.camera 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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
# Prints "present" if the extension is currently installed.
|
||||||
|
# "absent" otherwise.
|
||||||
|
|
||||||
|
quote() { printf '%s\n' "$*" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"; }
|
||||||
|
|
||||||
|
postgres_user=$("${__type_explorer:?}/postgres_user")
|
||||||
|
|
||||||
|
IFS=: read -r dbname extname <<EOF
|
||||||
|
${__object_id:?}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
psql_exec() {
|
||||||
|
su - "${postgres_user}" -c "psql $(quote "$1") -twAc $(quote "$2")"
|
||||||
|
}
|
||||||
|
|
||||||
|
if psql_exec "${dbname}" 'SELECT extname FROM pg_extension' | grep -qFx "${extname}"
|
||||||
|
then
|
||||||
|
echo present
|
||||||
|
else
|
||||||
|
echo absent
|
||||||
|
fi
|
|
@ -2,9 +2,10 @@
|
||||||
#
|
#
|
||||||
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
||||||
# 2013 Tomas Pospisek (tpo_deb at sourcepole.ch)
|
# 2013 Tomas Pospisek (tpo_deb at sourcepole.ch)
|
||||||
|
# 2021 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
|
||||||
#
|
#
|
||||||
# This type was created by Tomas Pospisek based on the
|
# This type was created by Tomas Pospisek based on the
|
||||||
#__postgres_role type by Steven Armstrong
|
# __postgres_role type by Steven Armstrong.
|
||||||
#
|
#
|
||||||
# This file is part of cdist.
|
# This file is part of cdist.
|
||||||
#
|
#
|
||||||
|
@ -24,19 +25,36 @@
|
||||||
|
|
||||||
postgres_user=$(cat "${__object:?}/explorer/postgres_user")
|
postgres_user=$(cat "${__object:?}/explorer/postgres_user")
|
||||||
|
|
||||||
|
quote() { printf '%s\n' "$*" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"; }
|
||||||
|
psql_cmd() {
|
||||||
|
printf 'su - %s -c %s\n' \
|
||||||
|
"$(quote "${postgres_user}")" \
|
||||||
|
"$(quote psql "$(quote "$1")" -c "$(quote "$2")")"
|
||||||
|
}
|
||||||
|
|
||||||
dbname=$( echo "$__object_id" | cut -d":" -f1 )
|
|
||||||
extension=$( echo "$__object_id" | cut -d":" -f2 )
|
|
||||||
|
|
||||||
state_should=$( cat "$__object/parameter/state" )
|
IFS=: read -r dbname extname <<EOF
|
||||||
|
${__object_id:?}
|
||||||
|
EOF
|
||||||
|
|
||||||
case "$state_should" in
|
state_is=$(cat "${__object:?}/explorer/state")
|
||||||
present)
|
state_should=$(cat "${__object:?}/parameter/state")
|
||||||
cmd="CREATE EXTENSION IF NOT EXISTS $extension"
|
|
||||||
echo "su - '$postgres_user' -c 'psql -c \"$cmd\" \"$dbname\"'"
|
if test "${state_is}" = "${state_should}"
|
||||||
|
then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
case ${state_should}
|
||||||
|
in
|
||||||
|
(present)
|
||||||
|
psql_cmd "${dbname}" "CREATE EXTENSION ${extname}"
|
||||||
;;
|
;;
|
||||||
absent)
|
(absent)
|
||||||
cmd="DROP EXTENSION IF EXISTS $extension"
|
psql_cmd "${dbname}" "DROP EXTENSION ${extname}"
|
||||||
echo "su - '$postgres_user' -c 'psql -c \"$cmd\" \"$dbname\"'"
|
;;
|
||||||
|
(*)
|
||||||
|
printf 'Invalid --state: %s\n' "${state_should}" >&2
|
||||||
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
|
@ -3,32 +3,36 @@ cdist-type__postgres_extension(7)
|
||||||
|
|
||||||
NAME
|
NAME
|
||||||
----
|
----
|
||||||
cdist-type__postgres_extension - manage postgres extensions
|
cdist-type__postgres_extension - Manage PostgreSQL extensions
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
This cdist type allows you to create or drop postgres extensions.
|
This cdist type allows you to manage PostgreSQL extensions.
|
||||||
|
|
||||||
The object you need to pass to __postgres_extension consists of
|
The ``__object_id`` to pass to ``__postgres_extension`` is of the form
|
||||||
the database name and the extension name joined by a colon in the
|
``dbname:extension``, e.g.:
|
||||||
following form:
|
|
||||||
|
|
||||||
.. code-block:: sh
|
|
||||||
|
|
||||||
dbname:extension
|
|
||||||
|
|
||||||
f.ex.
|
|
||||||
|
|
||||||
.. code-block:: sh
|
.. code-block:: sh
|
||||||
|
|
||||||
rails_test:unaccent
|
rails_test:unaccent
|
||||||
|
|
||||||
|
|
||||||
|
**CAUTION!** Be careful when installing extensions from (untrusted) third-party
|
||||||
|
sources:
|
||||||
|
|
||||||
|
| Installing an extension as superuser requires trusting that the extension's
|
||||||
|
author wrote the extension installation script in a secure fashion. It is
|
||||||
|
not terribly difficult for a malicious user to create trojan-horse objects
|
||||||
|
that will compromise later execution of a carelessly-written extension
|
||||||
|
script, allowing that user to acquire superuser privileges.
|
||||||
|
| – `<https://www.postgresql.org/docs/13/sql-createextension.html#id-1.9.3.64.7>`_
|
||||||
|
|
||||||
|
|
||||||
OPTIONAL PARAMETERS
|
OPTIONAL PARAMETERS
|
||||||
-------------------
|
-------------------
|
||||||
state
|
state
|
||||||
either "present" or "absent", defaults to "present"
|
either ``present`` or ``absent``, defaults to ``present``.
|
||||||
|
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
|
@ -36,24 +40,29 @@ EXAMPLES
|
||||||
|
|
||||||
.. code-block:: sh
|
.. code-block:: sh
|
||||||
|
|
||||||
|
# Install extension unaccent into database rails_test
|
||||||
__postgres_extension rails_test:unaccent
|
__postgres_extension rails_test:unaccent
|
||||||
__postgres_extension --present rails_test:unaccent
|
|
||||||
__postgres_extension --absent rails_test:unaccent
|
# Drop extension unaccent from database fails_test
|
||||||
|
__postgres_extension rails_test:unaccent --state absent
|
||||||
|
|
||||||
|
|
||||||
SEE ALSO
|
SEE ALSO
|
||||||
--------
|
--------
|
||||||
:strong:`cdist-type__postgre_database`\ (7)
|
- :strong:`cdist-type__postgres_database`\ (7)
|
||||||
|
- PostgreSQL "CREATE EXTENSION" documentation at:
|
||||||
|
`<http://www.postgresql.org/docs/current/static/sql-createextension.html>`_.
|
||||||
|
|
||||||
Postgres "Create Extension" documentation at: <http://www.postgresql.org/docs/current/static/sql-createextension.html>.
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHORS
|
||||||
-------
|
-------
|
||||||
Tomas Pospisek <tpo_deb--@--sourcepole.ch>
|
| Tomas Pospisek <tpo_deb--@--sourcepole.ch>
|
||||||
|
| Dennis Camera <dennis.camera--@--ssrq-sds-fds.ch>
|
||||||
|
|
||||||
|
|
||||||
COPYING
|
COPYING
|
||||||
-------
|
-------
|
||||||
Copyright \(C) 2014 Tomas Pospisek. You can redistribute it
|
Copyright \(C) 2014 Tomas Pospisek, 2021 Dennis Camera.
|
||||||
and/or modify it under the terms of the GNU General Public License as
|
You can redistribute it and/or modify it under the terms of the GNU General
|
||||||
published by the Free Software Foundation, either version 3 of the
|
Public License as published by the Free Software Foundation, either version 3 of
|
||||||
License, or (at your option) any later version.
|
the License, or (at your option) any later version.
|
||||||
|
|
Loading…
Reference in a new issue