diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2023-12-05 22:57:31 +0900 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2023-12-05 22:57:31 +0900 |
commit | b99f45c9977db68d49c42b80bf42aa8babad266b (patch) | |
tree | 90709481c0f96f63a4fbd9d1ed2f8aaeb3582f3a | |
parent | bb35ddcad2b8136833ad35d79d210ae9ef3a1911 (diff) | |
download | anastasis-b99f45c9977db68d49c42b80bf42aa8babad266b.tar.gz anastasis-b99f45c9977db68d49c42b80bf42aa8babad266b.tar.bz2 anastasis-b99f45c9977db68d49c42b80bf42aa8babad266b.zip |
use improved dbconfig logic with better error handling and no editing of the configuration
-rwxr-xr-x | contrib/anastasis-dbconfig | 80 | ||||
-rw-r--r-- | debian/control | 4 |
2 files changed, 51 insertions, 33 deletions
diff --git a/contrib/anastasis-dbconfig b/contrib/anastasis-dbconfig index cbb2f37..d4af960 100755 --- a/contrib/anastasis-dbconfig +++ b/contrib/anastasis-dbconfig @@ -22,24 +22,22 @@ set -eu 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 +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 " -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" ;; @@ -72,7 +70,9 @@ then if ! anastasis-dbinit -v 2> /dev/null then echo "Required 'anastasis-dbinit' not found. Please fix your installation." + exit 1 fi + DBINIT=$(which anastasis-dbinit) fi if ! id "$DBUSER" > /dev/null @@ -81,19 +81,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 +88,61 @@ then echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 fi -echo "Creating database $DBNAME." 1>&2 +DBPATH=$(anastasis-config \ + -c "$CFGFILE" \ + -s stasis-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 "[stasis-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 "[stasis-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" anastasis-dbinit -c "$CFGFILE" + if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE" + then + echo "Failed to initialize database schema" + exit 1 + fi fi echo "Database configuration finished." 1>&2 diff --git a/debian/control b/debian/control index 5ededeb..5698282 100644 --- a/debian/control +++ b/debian/control @@ -8,7 +8,7 @@ Build-Depends: autopoint, debhelper-compat (= 12), gettext, - libgnunet-dev (>=0.20), + libgnunet-dev (>=0.21), libtalerexchange-dev (>= 0.9.3), libtalermerchant-dev (>= 0.9.3), libpq-dev (>=14), @@ -89,7 +89,7 @@ Architecture: any Depends: libtalermerchant-dev (>= 0.9.3), libtalerexchange-dev (>= 0.9.3), - libgnunet-dev (>=0.20), + libgnunet-dev (>=0.21), ${misc:Depends}, ${shlibs:Depends} Description: libraries to talk to a GNU Anastasis provider (development). |