diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2023-12-05 22:26:59 +0900 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2023-12-05 22:26:59 +0900 |
commit | 2f8ea95eb33a1b9451005b4e4108dd7263d06a77 (patch) | |
tree | b99150df23825d709474f836b5f8c5281309991e /contrib | |
parent | ebf5ac6222112869008614f95c0096d3272600a7 (diff) |
improve taler-merchant-dbconfig: no more config rewriting, more error handling
Diffstat (limited to 'contrib')
-rwxr-xr-x | contrib/taler-merchant-dbconfig | 78 |
1 files changed, 46 insertions, 32 deletions
diff --git a/contrib/taler-merchant-dbconfig b/contrib/taler-merchant-dbconfig index ab17ff8a..372b7e57 100755 --- a/contrib/taler-merchant-dbconfig +++ b/contrib/taler-merchant-dbconfig @@ -22,24 +22,22 @@ set -eu 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 +while getopts 'c:hrsu:' OPTION; do case "$OPTION" in + c) + CFGFILE="$OPTARG" + ;; h) echo 'Supported options:' - echo " -c FILENAME -- write configuration to FILENAME (default: $CFGFILE)" - echo " -n NAME -- user NAME for database name (default: $DBNAME)" + echo " -c FILENAME -- use configuration FILENAME (default: $CFGFILE)" 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" ;; @@ -81,19 +79,6 @@ then 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 @@ -101,30 +86,59 @@ then echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 fi -echo "Creating database $DBNAME." 1>&2 +DBPATH=$(taler-config \ + -c "$CFGFILE" \ + -s exchangedb-postgres \ + -o CONFIG) -if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME" +if ! echo "$DBPATH" | grep "postgres://" > /dev/null then - echo "Failed to create database '$DBNAME'" + echo "Invalid database configuration value '$DBPATH'." 1>&2 exit 1 fi -if [ -f "$CFGFILE" ] +DBNAME=$(echo "$DBPATH" \ + | sed \ + -e "s/postgres:\/\/.*\///" \ + -e "s/?.*//") + +if sudo -i -u postgres psql "$DBNAME" < /dev/null 2> /dev/null then - echo "Adding database configuration to $CFGFILE." 1>&2 - echo -e "[merchantdb-postgres]\nCONFIG=postgres:///$DBNAME\n" >> "$CFGFILE" + if [ 1 = "$RESET_DB" ] + then + echo "Deleting existing database $DBNAME." 1>&2 + if ! sudo -i -u postgres dropdb "$DBNAME" + then + echo "Failed to delete existing database '$DBNAME'" + exit 1 + fi + DO_CREATE=1 + else + echo "Database '$DBNAME' already exists, continuing anyway." + DO_CREATE=0 + fi 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" + DO_CREATE=1 +fi + +if [ 1 = "$DO_CREATE" ] +then + 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 fi if [ 0 = "$SKIP_DBINIT" ] then echo "Initializing database $DBNAME." 1>&2 - sudo -u "$DBUSER" taler-merchant-dbinit -c "$CFGFILE" + if ! sudo -u "$DBUSER" taler-merchant-dbinit -c "$CFGFILE" + then + echo "Failed to initialize database schema" + exit 1 + fi fi echo "Database configuration finished." 1>&2 |