summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorChristian Grothoff <grothoff@gnunet.org>2023-12-05 22:57:04 +0900
committerChristian Grothoff <grothoff@gnunet.org>2023-12-05 22:57:04 +0900
commit8f677ac773fa78109164938375b38c691402fff0 (patch)
tree955fc18d55cb67ca7c2451b141d2838bdb7c7ca5 /contrib
parentf8890bcac5947f08028480410d7d2574a76064c0 (diff)
downloadsync-8f677ac773fa78109164938375b38c691402fff0.tar.gz
sync-8f677ac773fa78109164938375b38c691402fff0.tar.bz2
sync-8f677ac773fa78109164938375b38c691402fff0.zip
use improved dbconfig logic with better error handling and no editing of the configuration
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/sync-dbconfig81
1 files changed, 49 insertions, 32 deletions
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