From 5483b2af3413e9d191e989bf11ccabf5ad206e20 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 30 Apr 2022 23:23:16 +0200 Subject: postgresql (WIP) Signed-off-by: Daniel Baumann --- postgresql/Makefile | 90 ++++++++++++++ postgresql/bin/pg_hba.conf | 169 +++++++++++++++++++++++++++ postgresql/bin/postgresql-backup | 47 ++++++++ postgresql/share/bash-completion/pg_hba.conf | 48 ++++++++ postgresql/share/man/Makefile | 59 ++++++++++ postgresql/share/man/container.1.rst | 145 +++++++++++++++++++++++ postgresql/share/man/man.in | 19 +++ 7 files changed, 577 insertions(+) create mode 100644 postgresql/Makefile create mode 100755 postgresql/bin/pg_hba.conf create mode 100755 postgresql/bin/postgresql-backup create mode 100644 postgresql/share/bash-completion/pg_hba.conf create mode 100644 postgresql/share/man/Makefile create mode 100644 postgresql/share/man/container.1.rst create mode 100644 postgresql/share/man/man.in diff --git a/postgresql/Makefile b/postgresql/Makefile new file mode 100644 index 0000000..9bc75b3 --- /dev/null +++ b/postgresql/Makefile @@ -0,0 +1,90 @@ +# Open Infrastructure: service-tools + +# Copyright (C) 2014-2022 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program 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. +# +# This program 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 this program. If not, see . + +SHELL := sh -e + +SCRIPTS = bin/* + +all: build + +test: + @echo -n "Checking for syntax errors with sh... " + @for SCRIPT in $(SCRIPTS); \ + do \ + sh -n $${SCRIPT}; \ + echo -n "."; \ + done + @echo " done." + + @echo -n "Checking for bashisms... " + @if [ -x /usr/bin/checkbashisms ]; \ + then \ + for SCRIPT in $(SCRIPTS); \ + do \ + checkbashisms -f -x $${SCRIPT}; \ + echo -n "."; \ + done; \ + else \ + echo "Note: devscripts not installed, skipping checkbashisms."; \ + fi + @echo " done." + + @echo -n "Checking with shellcheck... " + @if [ -x /usr/bin/shellcheck ]; \ + then \ + for SCRIPT in $(SCRIPTS); \ + do \ + shellcheck -e SC2039 $${SCRIPT}; \ + echo -n "."; \ + done; \ + else \ + echo "Note: shellcheck not installed, skipping shellcheck."; \ + fi + @echo " done." + +build: + +install: build + mkdir -p $(DESTDIR)/usr/bin + cp -r bin/* $(DESTDIR)/usr/bin + ln -sf pg_hba.conf $(DESTDIR)/usr/bin/update-pg_hba.conf + + mkdir -p $(DESTDIR)/usr/share/bash-completion/completions + cp -r share/bash-completion/* $(DESTDIR)/usr/share/bash-completion/completions + +uninstall: + for FILE in share/bash-completion/*; \ + do \ + rm -f $(DESTDIR)/usr/share/bash-completion/completions/$$(basename $${FILE}); \ + done + + rm -f $(DESTDIR)/usr/bin/update-pg_hba.conf + for FILE in bin/*; \ + do \ + rm -f $(DESTDIR)/usr/bin/$$(basename $${FILE}); \ + done + rmdir --ignore-fail-on-non-empty --parents $(DESTDIR)/usr/bin || true + + rmdir --ignore-fail-on-non-empty --parents $(DESTDIR) || true + +clean: + +distclean: + +reinstall: uninstall install diff --git a/postgresql/bin/pg_hba.conf b/postgresql/bin/pg_hba.conf new file mode 100755 index 0000000..2b6c7cb --- /dev/null +++ b/postgresql/bin/pg_hba.conf @@ -0,0 +1,169 @@ +#!/bin/sh + +# Open Infrastructure: service-tools + +# Copyright (C) 2014-2022 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program 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. +# +# This program 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 this program. If not, see . + +set -e + +PROGRAM="$(basename "${0}")" + +Parameters () +{ + GETOPT_LONGOPTIONS="add,init,remove,update" + GETOPT_OPTIONS="a,i,r,u," + + PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" + + if [ "${?}" != "0" ] + then + echo "'${COMMAND}': getopt exit" >&2 + exit 1 + fi + + eval set -- "${PARAMETERS}" + + while true + do + case "${1}" in + -a|--add) + ACTION="add" + shift 1 + ;; + + -i|--init) + ACTION="init" + shift 1 + ;; + + -r|--remove) + ACTION="remove" + shift 1 + ;; + + -u|--update) + ACTION="update" + shift 1 + ;; + + --) + shift 1 + break + ;; + + *) + echo "'${COMMAND}': getopt error" >&2 + exit 1 + ;; + esac + done +} + +Usage () +{ + echo "Usage: ${PROGRAM} [-i|--init] [-a|--add LINE] [-r|--remove LINE] [-u|--update]" >&2 + echo + echo "See ${PROGRAM}(1) for more information." + + exit 1 +} + +Parameters "${@}" + +Run_add() +{ + echo "'add' not implemented yet" + exit 0 +} + +Run_init() +{ + echo "'init' not implemented yet" + exit 0 +} + +Run_remove() +{ + echo "'remove' not implemented yet" + exit 0 +} + +Run_update() +{ + # Check for pg_hba.conf + if ! ls /etc/postgresql/*/*/pg_hba.conf > /dev/null 2>&1 + then + echo "W: no pg_hba.conf in /etc/postgresql found." >&2 + exit 1 + fi + + # Check for managed pg_hba.conf + if ! ls /etc/postgresql/*/*/pg_hba.conf.g > /dev/null 2>&1 + then + echo "W: no managed pg_hba.conf in /etc/postgresql found, maybe use '${PROGRAM} --init' first." >&2 + exit 1 + fi + + # Update pg_hba.conf from pg_hba.conf.g directories + for DIRECTORY in /etc/postgresql/*/*/pg_hba.conf.g + do + if ls "${DIRECTORY}"/*.conf > /dev/null 2>&1 + then + CONFIG="$(basename "${DIRECTORY}" .g)" + + echo -n "Updating ${CONFIG}..." + + rm -f "${CONFIG}" + + for FILE in "${DIRECTORY}"/*.conf + do + cat "${FILE}" >> "${CONFIG}" + done + + echo " done." + fi + done +} + +case "${PROGRAM}" in + update-pg_hba.conf) + ACTION="update" + ;; +esac + +case "${ACTION}" in + add) + Run_add + ;; + + init) + Run_init + ;; + + remove) + Run_remove + ;; + + update) + Run_update + ;; + + *) + Usage + ;; +esac diff --git a/postgresql/bin/postgresql-backup b/postgresql/bin/postgresql-backup new file mode 100755 index 0000000..beaf2d8 --- /dev/null +++ b/postgresql/bin/postgresql-backup @@ -0,0 +1,47 @@ +#!/bin/sh + +# Open Infrastructure: service-tools + +# Copyright (C) 2014-2022 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program 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. +# +# This program 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 this program. If not, see . + +set -e + + +#!/bin/sh + +set -e + +DIRECTORY="/srv/$(cat /etc/hostname)/postgresql-backup/$(date +%Y-%m-%d)" +DATABASES="fbm_test" + +mkdir -p "${DIRECTORY}" +chown -R postgres:postgres "${DIRECTORY}" + +for DATABASE in ${DATABASES} +do + su - postgres -c "pg_dump -f ${DIRECTORY} -F d -j $(nproc) -C -d ${DATABASE}" +done + +chown -R root:root "${DIRECTORY}" + + + + +# FIXME +#postgres pg_dump ${DATABASE} | plzip > postgresql-backup/${DATABASE}-"$(date +\%Y\%m\%d)".sql.lz +#find postgresql-backup/ -mtime +90 -name "${DATABASE}-*lz" | xargs -r rm diff --git a/postgresql/share/bash-completion/pg_hba.conf b/postgresql/share/bash-completion/pg_hba.conf new file mode 100644 index 0000000..c69b07b --- /dev/null +++ b/postgresql/share/bash-completion/pg_hba.conf @@ -0,0 +1,48 @@ +# Open Infrastructure: service-tools + +# Copyright (C) 2014-2022 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program 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. +# +# This program 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 this program. If not, see . + +_pg_hba_conf() +{ + local cur prev pg_type + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + if [ ${COMP_CWORD} -gt 0 ] + then + pg_type="${COMP_WORDS[1]}" + fi + + if [ "${prev}" = "--" ] + then + compopt -o bashdefault + COMPREPLY=( $(compgen -c -- $cur) ) + return 0 + fi + + case "${pg_type}" in + *) + local pg_types="local host hostssl hostnossl hostgssenc hostnogssenc" + COMPREPLY=( $(compgen -W "${pg_types}" -- ${cur}) ) + return 0 + ;; + esac +} + +complete -F _pg_hba_conf pg_hba.conf diff --git a/postgresql/share/man/Makefile b/postgresql/share/man/Makefile new file mode 100644 index 0000000..a8af58d --- /dev/null +++ b/postgresql/share/man/Makefile @@ -0,0 +1,59 @@ +# Open Infrastructure: compute-tools + +# Copyright (C) 2014-2021 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program 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. +# +# This program 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 this program. If not, see . + +# Depends: python3-docutils + +RST2MAN = rst2man \ + --no-datestamp \ + --no-generator \ + --strict \ + --strip-comments \ + --tab-width=4 \ + --verbose + +VERSION := $(shell cat ../../VERSION.txt) + +SHELL := sh -e + +all: build + +build: man + +rebuild: clean build + +man: man.in *.rst + @echo -n "Creating manpages... " + + @for FILE in *.rst; \ + do \ + cp man.in $$(basename $${FILE} .rst); \ + $(RST2MAN) $${FILE} | \ + sed -e '/^.\\" Man page generated/d' \ + -e '/^.\\" Generated by/d' \ + -e "s|^\(.TH .*\) \(\"\" \"\"\) |\1 $${VERSION} compute-tools |" \ + >> $$(basename $${FILE} .rst); \ + echo -n "."; \ + done + + @echo " done." + +clean: + rm -f *.[0-9] + +.PHONY: all clean build rebuild man diff --git a/postgresql/share/man/container.1.rst b/postgresql/share/man/container.1.rst new file mode 100644 index 0000000..6cd516f --- /dev/null +++ b/postgresql/share/man/container.1.rst @@ -0,0 +1,145 @@ +.. Open Infrastructure: compute-tools + +.. Copyright (C) 2014-2021 Daniel Baumann +.. +.. SPDX-License-Identifier: GPL-3.0+ +.. +.. This program 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. +.. +.. This program 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 this program. If not, see . + +============== +container-list +============== + +---------------------------- +List container on the system +---------------------------- + +:manual section: 1 +:manual group: Open Infrastructure + +Synopsis +======== + +| **container list** ['OPTIONS'] +| **cnt ls** ['OPTIONS'] + +Description +=========== + +The **container list** command lists container on the system. + +Options +======= + +The following **container list** options are available, defaults to '--started +--stopped': + +-a, --all: + List all available container (started, stopped, and other). + +--csv-separator='SEPARATOR': + Specify custom CSV separator, defaults to ','. + +-f, --format='FORMAT': + Use format to list container. Currently available formats are 'cli' (default), + 'csv', 'json', 'nwdiag', 'shell', 'sh', 'yaml', or 'xml'. + +-h, --host='HOSTNAME': + List only container that are enabled for automatic start on the specified + hostname. Defaults to list containers of the local system only. Using 'all' + shows all container regardless of any automatic start configuration. + +--nwdiag-color='COLOR': + Specify custom nwdiag color for the host box, defaults to '#3465a4'. + +--nwdiag-label='LABEL': + Specify custom nwdiag label for the diagram, defaults to empty. + +-o, --other: + List only container that are not enable for automatic start on the current + system. + +-s, --started: + List only started container. + +-t, --stopped: + List only stopped container. + +Examples +======== + +List all started and stopped containers of the local system: + + sudo container list + +List all started and other containers: + + sudo container list -s -o + +Create a CSV export of all started and stopped containers: + + sudo container list -f csv + +Create a JSON export of all started and stopped containers: + + sudo container list -f json + +Create a nwdiag export of all started and stopped containers: + + sudo container list -f nwdiag + +Create a SVG image via nwdiag of all started and stopped containers: + + sudo container list -f nwdiag | nwdiag -T svg -o cnt-list.svg - + +Create a shell export of all started and stopped containers: + + sudo container list -f shell + sudo container list -f sh + +Create a YAML export of all started and stopped containers: + + sudo container list -f yaml + +Create a XML export of all started and stopped containers: + + sudo container list -f xml + +See also +======== + +| compute-tools(7), +| container(1). + +Homepage +======== + +More information about compute-tools and the Open Infrastructure project can be +found on the homepage (https://open-infrastructure.net). + +Contact +======= + +Bug reports, feature requests, help, patches, support and everything else are +welcome on the Open Infrastructure Software Mailing List +. + +Debian specific bugs can also be reported in the Debian Bug Tracking System +(https://bugs.debian.org). + +Authors +======= + +compute-tools were written by Daniel Baumann + and others. diff --git a/postgresql/share/man/man.in b/postgresql/share/man/man.in new file mode 100644 index 0000000..f95ca67 --- /dev/null +++ b/postgresql/share/man/man.in @@ -0,0 +1,19 @@ +.\" Open Infrastructure: service-tools +.\" +.\" Copyright (C) 2014-2022 Daniel Baumann +.\" +.\" SPDX-License-Identifier: GPL-3.0+ +.\" +.\" This program 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. +.\" +.\" This program 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 this program. If not, see . +.\" -- cgit v1.2.3