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 (3493B)


      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 CFGFILE="/etc/challenger/challenger.conf"
     26 
     27 
     28 
     29 # Parse command-line options
     30 while getopts 'c:hrsu:' OPTION; do
     31   case "$OPTION" in
     32   c)
     33     CFGFILE="$OPTARG"
     34     ;;
     35   h)
     36     echo 'Supported options:'
     37     echo "  -c FILENAME  -- write configuration to FILENAME (default: $CFGFILE)"
     38     echo "  -h           -- print this help text"
     39     echo "  -r           -- reset database (dangerous)"
     40     echo "  -s           -- skip database initialization"
     41     echo "  -u USER      -- challenger-httpd to be run by USER (default: $DBUSER)"
     42     exit 0
     43     ;;
     44   r)
     45     RESET_DB="1"
     46     ;;
     47   s)
     48     SKIP_DBINIT="1"
     49     ;;
     50   u)
     51     DBUSER="$OPTARG"
     52     ;;
     53   ?)
     54     echo "Unrecognized command line option '$OPTION'" 1>&2
     55     exit 1
     56     ;;
     57   esac
     58 done
     59 
     60 if ! id postgres >/dev/null;
     61 then
     62   echo "Could not find 'postgres' user. Please install Postgresql first"
     63   exit 1
     64 fi
     65 
     66 if [ "$(id -u)" -ne 0 ];
     67 then
     68   echo "This script must be run as root"
     69   exit 1
     70 fi
     71 
     72 if [ 0 = "$SKIP_DBINIT" ];
     73 then
     74     if ! challenger-dbinit -v 2>/dev/null;
     75     then
     76 	echo "Required 'challenger-dbinit' not found. Please fix your installation."
     77 	exit 1
     78     fi
     79     DBINIT=$(which challenger-dbinit)
     80 fi
     81 
     82 if ! id "$DBUSER" >/dev/null;
     83 then
     84     echo "Could not find '$DBUSER' user. Please create it up first"
     85     exit 1
     86 fi
     87 
     88 echo "Setting up database user $DBUSER." 1>&2
     89 
     90 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null;
     91 then
     92     echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2
     93 fi
     94 
     95 DBPATH=$(challenger-config \
     96   -c "$CFGFILE" \
     97   -s challengerdb-postgres \
     98   -o CONFIG)
     99 
    100 if ! echo "$DBPATH" | grep "postgres://" >/dev/null;
    101 then
    102     echo "Invalid database configuration value '$DBPATH'." 1>&2
    103     exit 1
    104 fi
    105 
    106 DBNAME=$(echo "$DBPATH" |
    107   sed \
    108     -e "s/postgres:\/\/.*\///" \
    109     -e "s/?.*//")
    110 
    111 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null;
    112 then
    113     if [ 1 = "$RESET_DB" ];
    114     then
    115 	echo "Deleting existing database '$DBNAME'." 1>&2
    116 	if ! sudo -i -u postgres dropdb "$DBNAME";
    117 	then
    118 	    echo "Failed to delete existing database '$DBNAME'"
    119 	    exit 1
    120 	fi
    121 	DO_CREATE=1
    122     else
    123 	echo "Database '$DBNAME' already exists, continuing anyway."
    124 	DO_CREATE=0
    125     fi
    126 else
    127     DO_CREATE=1
    128 fi
    129 
    130 if [ 1 = "$DO_CREATE" ];
    131 then
    132     echo "Creating database '$DBNAME'." 1>&2
    133 
    134     if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME";
    135     then
    136 	echo "Failed to create database '$DBNAME'"
    137 	exit 1
    138     fi
    139 fi
    140 
    141 if [ 0 = "$SKIP_DBINIT" ];
    142 then
    143     echo "Initializing database '$DBNAME'." 1>&2
    144     if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE";
    145     then
    146 	echo "Failed to initialize database schema"
    147 	exit 1
    148     fi
    149 fi
    150 
    151 echo "Database configuration finished." 1>&2
    152 
    153 exit 0