libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 99b5c6c428026af8ac8c5fbf17e3cbc10d677a6f
parent 26f253ee8aadc1cca56588878cd93230b156f733
Author: Christian Grothoff <grothoff@gnunet.org>
Date:   Thu, 30 Nov 2023 14:57:17 +0900

adjust dbconfig scripts to better support database sharing

Diffstat:
Mcontrib/libeufin-bank-dbconfig | 42+++++++++++++++++++++++++-----------------
Mcontrib/libeufin-bank-dbinit | 3++-
Mcontrib/libeufin-nexus-dbconfig | 41++++++++++++++++++++++++-----------------
Mcontrib/libeufin-nexus-dbinit | 3++-
4 files changed, 53 insertions(+), 36 deletions(-)

diff --git a/contrib/libeufin-bank-dbconfig b/contrib/libeufin-bank-dbconfig @@ -72,7 +72,9 @@ then if ! libeufin-bank-dbinit --help 1> /dev/null # -v not provided then echo "Required 'libeufin-bank-dbinit' not found. Please fix your installation." + exit 1 fi + BANK_DBINIT=$(which libeufin-bank-dbinit) fi if ! id "$DBUSER" > /dev/null @@ -81,6 +83,14 @@ then exit 1 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 + + if sudo -i -u postgres psql "$DBNAME" < /dev/null 2> /dev/null then if [ 1 = "$RESET_DB" ] @@ -88,26 +98,24 @@ 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 + echo "Database '$DBNAME' already exists." + if ! echo "GRANT ALL PRIVILEGES ON $DBNAME TO '$DBUSER'" \ + | sudo -i -u postgres psql "$DBNAME" + then + echo "Failed to grant access to database '$DBNAME' to '$DBUSER'." 1>&2 + exit 1 + fi fi -fi - -echo "Setting up database user $DBUSER." 1>&2 +else + echo "Creating database $DBNAME." 1>&2 -if ! sudo -i -u postgres createuser "$DBUSER" 2> /dev/null -then - echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 + if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME" + then + echo "Failed to create database '$DBNAME'." 1>&2 + exit 1 + fi 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 @@ -124,7 +132,7 @@ fi if [ 0 = "$SKIP_DBINIT" ] then echo "Initializing database $DBNAME." 1>&2 - sudo -u "$DBUSER" libeufin-bank-dbinit + sudo -u "$DBUSER" "$BANK_DBINIT" -c "$CFGFILE" fi echo "Database configuration finished." 1>&2 diff --git a/contrib/libeufin-bank-dbinit b/contrib/libeufin-bank-dbinit @@ -1,3 +1,4 @@ #!/bin/sh -exec libeufin-bank dbinit "$@" +DIR=$(dirname $0) +exec ${DIR}/libeufin-bank dbinit "$@" diff --git a/contrib/libeufin-nexus-dbconfig b/contrib/libeufin-nexus-dbconfig @@ -72,7 +72,9 @@ then if ! libeufin-nexus-dbinit --help 1> /dev/null # -v not provided then echo "Required 'libeufin-nexus-dbinit' not found. Please fix your installation." + exit 1 fi + NEXUS_DBINIT=$(which libeufin-nexus-dbinit) fi if ! id "$DBUSER" > /dev/null @@ -81,6 +83,13 @@ then exit 1 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 + if sudo -i -u postgres psql "$DBNAME" < /dev/null 2> /dev/null then if [ 1 = "$RESET_DB" ] @@ -88,26 +97,24 @@ 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 + echo "Database '$DBNAME' already exists." + if ! echo "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO \"$DBUSER\"" \ + | sudo -i -u postgres psql "$DBNAME" + then + echo "Failed to grant access to database '$DBNAME' to '$DBUSER'." 1>&2 + exit 1 + fi fi -fi - -echo "Setting up database user $DBUSER." 1>&2 +else + echo "Creating database $DBNAME." 1>&2 -if ! sudo -i -u postgres createuser "$DBUSER" 2> /dev/null -then - echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 + if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME" + then + echo "Failed to create database '$DBNAME'." 1>&2 + exit 1 + fi 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 @@ -124,7 +131,7 @@ fi if [ 0 = "$SKIP_DBINIT" ] then echo "Initializing database $DBNAME." 1>&2 - sudo -u "$DBUSER" libeufin-nexus-dbinit + sudo -u "$DBUSER" "$NEXUS_DBINIT" -c "$CFGFILE" fi echo "Database configuration finished." 1>&2 diff --git a/contrib/libeufin-nexus-dbinit b/contrib/libeufin-nexus-dbinit @@ -1,2 +1,3 @@ #!/bin/sh -exec libeufin-nexus dbinit "$@" +DIR=$(dirname $0) +exec ${DIR}/libeufin-nexus dbinit "$@"