challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

challenger-dbconfig (3598B)


      1 #!/bin/bash
      2 # This file is part of GNU TALER.
      3 # Copyright (C) 2023 Taler Systems SA
      4 #
      5 # TALER is free software; you can redistribute it and/or modify it under the
      6 # terms of the GNU Lesser General Public License as published by the Free Software
      7 # Foundation; either version 2.1, or (at your option) any later version.
      8 #
      9 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU Lesser General Public License along with
     14 # TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # @author Christian Grothoff
     17 #
     18 #
     19 # Error checking on
     20 set -eu
     21 
     22 RESET_DB=0
     23 SKIP_DBINIT=0
     24 DBUSER="challenger-httpd"
     25 DBNAME="challenger"
     26 CFGFILE="/etc/challenger/challenger.conf"
     27 
     28 
     29 
     30 # Parse command-line options
     31 while getopts 'c:hn:rsu:' OPTION; do
     32   case "$OPTION" in
     33   c)
     34     CFGFILE="$OPTARG"
     35     ;;
     36   h)
     37     echo 'Supported options:'
     38     echo "  -c FILENAME  -- write configuration to FILENAME (default: $CFGFILE)"
     39     echo "  -h           -- print this help text"
     40     echo "  -n NAME      -- user NAME for database name (default: $DBNAME)"
     41     echo "  -r           -- reset database (dangerous)"
     42     echo "  -s           -- skip database initialization"
     43     echo "  -u USER      -- challenger-httpd to be run by USER (default: $DBUSER)"
     44     exit 0
     45     ;;
     46   n)
     47     DBNAME="$OPTARG"
     48     ;;
     49   r)
     50     RESET_DB="1"
     51     ;;
     52   s)
     53     SKIP_DBINIT="1"
     54     ;;
     55   u)
     56     DBUSER="$OPTARG"
     57     ;;
     58   ?)
     59     echo "Unrecognized command line option '$OPTION'" 1>&2
     60     exit 1
     61     ;;
     62   esac
     63 done
     64 
     65 if ! id postgres >/dev/null; then
     66   echo "Could not find 'postgres' user. Please install Postgresql first"
     67   exit 1
     68 fi
     69 
     70 if [ "$(id -u)" -ne 0 ]; then
     71   echo "This script must be run as root"
     72   exit 1
     73 fi
     74 
     75 if [ 0 = "$SKIP_DBINIT" ]; then
     76   if ! challenger-dbinit -v 2>/dev/null; then
     77     echo "Required 'challenger-dbinit' not found. Please fix your installation."
     78   fi
     79   DBINIT=$(which challenger-dbinit)
     80 fi
     81 
     82 if ! id "$DBUSER" >/dev/null; then
     83   echo "Could not find '$DBUSER' user. Please create it up first"
     84   exit 1
     85 fi
     86 
     87 echo "Setting up database user $DBUSER." 1>&2
     88 
     89 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null; then
     90   echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2
     91 fi
     92 
     93 DBPATH=$(challenger-config \
     94   -c "$CFGFILE" \
     95   -s challengerdb-postgres \
     96   -o CONFIG)
     97 
     98 if ! echo "$DBPATH" | grep "postgres://" >/dev/null; then
     99   echo "Invalid database configuration value '$DBPATH'." 1>&2
    100   exit 1
    101 fi
    102 
    103 DBNAME=$(echo "$DBPATH" |
    104   sed \
    105     -e "s/postgres:\/\/.*\///" \
    106     -e "s/?.*//")
    107 
    108 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null; then
    109   if [ 1 = "$RESET_DB" ]; then
    110     echo "Deleting existing database '$DBNAME'." 1>&2
    111     if ! sudo -i -u postgres dropdb "$DBNAME"; then
    112       echo "Failed to delete existing database '$DBNAME'"
    113       exit 1
    114     fi
    115     DO_CREATE=1
    116   else
    117     echo "Database '$DBNAME' already exists, continuing anyway."
    118     DO_CREATE=0
    119   fi
    120 else
    121   DO_CREATE=1
    122 fi
    123 
    124 if [ 1 = "$DO_CREATE" ]; then
    125   echo "Creating database '$DBNAME'." 1>&2
    126 
    127   if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then
    128     echo "Failed to create database '$DBNAME'"
    129     exit 1
    130   fi
    131 fi
    132 
    133 if [ 0 = "$SKIP_DBINIT" ]; then
    134   echo "Initializing database '$DBNAME'." 1>&2
    135   if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE"; then
    136     echo "Failed to initialize database schema"
    137     exit 1
    138   fi
    139 fi
    140 
    141 echo "Database configuration finished." 1>&2
    142 
    143 exit 0