diff options
author | Christian Grothoff <christian@grothoff.org> | 2023-08-11 01:04:06 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2023-08-11 01:04:13 +0200 |
commit | 298085104a52887dc372ada6d353d02dac5ce933 (patch) | |
tree | c9013d185e708980e8d4e7daf4e864b796948b07 /contrib | |
parent | f0cc7e805cd32edd8cecf7a6ffeb9d001ccbda79 (diff) |
add taler-merchant-dbconfig
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/Makefile.am.in | 5 | ||||
-rwxr-xr-x | contrib/taler-merchant-dbconfig | 132 |
2 files changed, 137 insertions, 0 deletions
diff --git a/contrib/Makefile.am.in b/contrib/Makefile.am.in index b2921bd4..51284f94 100644 --- a/contrib/Makefile.am.in +++ b/contrib/Makefile.am.in @@ -1,7 +1,12 @@ +# This file is in the public domain. + tmplpkgdatadir = $(prefix)/share/taler/merchant/templates/ staticpkgdatadir = $(prefix)/share/taler/merchant/static/ spapkgdatadir = $(prefix)/share/taler/merchant/spa/ +bin_SCRIPTS = \ + taler-merchant-dbconfig + dist_tmplpkgdata_DATA = \ depleted_tip.en.must \ offer_refund.en.must \ diff --git a/contrib/taler-merchant-dbconfig b/contrib/taler-merchant-dbconfig new file mode 100755 index 00000000..18318d85 --- /dev/null +++ b/contrib/taler-merchant-dbconfig @@ -0,0 +1,132 @@ +#!/bin/bash +# This file is part of GNU TALER. +# Copyright (C) 2023 Taler Systems SA +# +# TALER 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. +# +# TALER 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 +# TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +# +# @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="taler-merchant-httpd" +DBNAME="merchant" +CFGFILE="/etc/taler/secrets/merchant-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 -- taler-merchant 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 ! taler-merchant-dbinit -v 2> /dev/null + then + echo "Required 'taler-merchant-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 "[merchantdb-postgres]\nCONFIG=postgres:///$DBNAME\n" >> "$CFGFILE" +else + echo "Configuration $CFGFILE does not yet exist, creating it." 1>&2 + mkdir -p "$(dirname "$CFGFILE")" + echo -e "[merchantdb-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" taler-merchant-dbinit +fi + +echo "Database configuration finished." 1>&2 + +exit 0 |