From 8f677ac773fa78109164938375b38c691402fff0 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 5 Dec 2023 22:57:04 +0900 Subject: use improved dbconfig logic with better error handling and no editing of the configuration --- contrib/sync-dbconfig | 81 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 32 deletions(-) (limited to 'contrib/sync-dbconfig') diff --git a/contrib/sync-dbconfig b/contrib/sync-dbconfig index dc7339a..d0d3a4b 100755 --- a/contrib/sync-dbconfig +++ b/contrib/sync-dbconfig @@ -22,24 +22,22 @@ set -eu RESET_DB=0 SKIP_DBINIT=0 DBUSER="sync-httpd" -DBNAME="sync" -CFGFILE="/etc/taler/secrets/sync-db.secret.conf" +CFGFILE="/etc/sync/sync.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 -- sync-httpd to be run by USER (default: $DBUSER)" exit 0 ;; - n) - DBNAME="$OPTARG" - ;; r) RESET_DB="1" ;; @@ -72,7 +70,9 @@ then if ! sync-dbinit -v 2> /dev/null then echo "Required 'sync-dbinit' not found. Please fix your installation." + exit 1 fi + DBINIT=$(which sync-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,60 @@ then echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 fi -echo "Creating database $DBNAME." 1>&2 +DBPATH=$(sync-config \ + -c "$CFGFILE" \ + -s syncdb-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 "[syncdb-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 "[syncdb-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" sync-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 -- cgit v1.2.3