From 4205c9b506d7b495586314cd6db09ca57c0c074e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 11 Aug 2023 01:03:51 +0200 Subject: add anastasis-dbconfig --- contrib/Makefile.am | 3 + contrib/anastasis-dbconfig | 132 +++++++++++++++++++++ .../etc/anastasis/secrets/anastasis-db.secret.conf | 2 +- 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100755 contrib/anastasis-dbconfig diff --git a/contrib/Makefile.am b/contrib/Makefile.am index b175a23..c51edfa 100644 --- a/contrib/Makefile.am +++ b/contrib/Makefile.am @@ -3,6 +3,9 @@ SUBDIRS = . +bin_SCRIPTS = \ + anastasis-dbconfig + # English (en) tosendir=$(datadir)/anastasis/tos/en diff --git a/contrib/anastasis-dbconfig b/contrib/anastasis-dbconfig new file mode 100755 index 0000000..1e682d8 --- /dev/null +++ b/contrib/anastasis-dbconfig @@ -0,0 +1,132 @@ +#!/bin/bash +# This file is part of GNU ANASTASIS. +# Copyright (C) 2023 Anastasis Systems SA +# +# ANASTASIS is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 2.1, or (at your option) any later version. +# +# ANASTASIS 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along with +# ANASTASIS; see the file COPYING. If not, see +# +# @author Christian Grothoff +# +# +# Error checking on +set -eu + +if ! id postgres > /dev/null +then + echo "Could not find 'postgres' user. Please install Postgresql first" + exit 1 +fi + +if [ "$(id -u)" -ne 0 ] +then + echo "This script must be run as root" + exit 1 +fi + +RESET_DB=0 +SKIP_DBINIT=0 +DBUSER="anastasis-httpd" +DBNAME="anastasis" +CFGFILE="/etc/anastasis/secrets/anastasis-db.secret.conf" + +# Parse command-line options +while getopts ':hn:rsu:' OPTION; do + case "$OPTION" in + h) + echo 'Supported options:' + echo " -c FILENAME -- write configuration to FILENAME (default: $CFGFILE)" + echo " -n NAME -- user NAME for database name (default: $DBNAME)" + echo " -r -- reset database (dangerous)" + echo " -s -- skip database initialization" + echo " -u USER -- anastasis-httpd to be run by USER (default: $DBUSER)" + exit 0 + ;; + n) + DBNAME="$OPTARG" + ;; + r) + RESET_DB="1" + ;; + s) + SKIP_DBINIT="1" + ;; + u) + DBUSER="$OPTARG" + ;; + ?) + exit_fail "Unrecognized command line option" + ;; + esac +done + +if [ 0 = "$SKIP_DBINIT" ] +then + if ! anastasis-dbinit -v 2> /dev/null + then + echo "Required 'anastasis-dbinit' not found. Please fix your installation." + fi +fi + +if ! id "$DBUSER" > /dev/null +then + echo "Could not find '$DBUSER' user. Please set it up first" + exit 1 +fi + +if sudo -i -u postgres psql "$DBNAME" < /dev/null 2> /dev/null +then + if [ 1 = "$RESET_DB" ] + then + echo "Deleting existing database $DBNAME." 1>&2 + sudo -i -u postgres dropdb "$DBNAME" + else + echo "Database '$DBNAME' already exists, refusing to setup again." + echo "Use -r to delete the existing database first (dangerous!)." + exit 77 + fi +fi + +echo "Setting up database user $DBUSER." 1>&2 + +if ! sudo -i -u postgres createuser "$DBUSER" 2> /dev/null +then + echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 +fi + +echo "Creating database $DBNAME." 1>&2 + +if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME" +then + echo "Failed to create database '$DBNAME'" + exit 1 +fi + +if [ -f "$CFGFILE" ] +then + echo "Adding database configuration to $CFGFILE." 1>&2 + echo -e "[stasis-postgres]\nCONFIG=postgres:///$DBNAME\n" >> "$CFGFILE" +else + echo "Configuration $CFGFILE does not yet exist, creating it." 1>&2 + mkdir -p "$(dirname "$CFGFILE")" + echo -e "[stasis-postgres]\nCONFIG=postgres:///$DBNAME\n" >> "$CFGFILE" + chown "$DBUSER":root "$CFGFILE" + chmod 460 "$CFGFILE" +fi + +if [ 0 = "$SKIP_DBINIT" ] +then + echo "Initializing database $DBNAME." 1>&2 + sudo -i -u "$DBUSER" anastasis-dbinit +fi + +echo "Database configuration finished." 1>&2 + +exit 0 diff --git a/debian/etc/anastasis/secrets/anastasis-db.secret.conf b/debian/etc/anastasis/secrets/anastasis-db.secret.conf index 8f9fb54..512366d 100644 --- a/debian/etc/anastasis/secrets/anastasis-db.secret.conf +++ b/debian/etc/anastasis/secrets/anastasis-db.secret.conf @@ -1,3 +1,3 @@ [stasis-postgres] #The connection string the plugin has to use for connecting to the database -CONFIG = postgres:///anastasis +# CONFIG = postgres:///anastasis -- cgit v1.2.3