challenger-dbconfig (3478B)
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 '$OPTARG'" 1>&2 55 exit 1 56 ;; 57 esac 58 done 59 60 if ! id postgres >/dev/null; then 61 echo "Could not find 'postgres' user. Please install Postgresql first" 62 exit 1 63 fi 64 65 if [ "$(id -u)" -ne 0 ]; then 66 echo "This script must be run as root" 67 exit 1 68 fi 69 70 if [ 0 = "$SKIP_DBINIT" ]; then 71 if ! challenger-dbinit -v 2>/dev/null; then 72 echo "Required 'challenger-dbinit' not found. Please fix your installation." 73 exit 1 74 fi 75 DBINIT=$(which challenger-dbinit) 76 fi 77 78 if ! id "$DBUSER" >/dev/null; then 79 echo "Could not find '$DBUSER' user. Please create it up first" 80 exit 1 81 fi 82 83 echo "Setting up database user $DBUSER." 1>&2 84 85 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null; then 86 echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 87 fi 88 89 DBPATH=$(challenger-config \ 90 -c "$CFGFILE" \ 91 -s challengerdb-postgres \ 92 -o CONFIG) 93 94 if ! echo "$DBPATH" | grep "postgres://" >/dev/null; then 95 echo "Invalid database configuration value '$DBPATH'." 1>&2 96 exit 1 97 fi 98 99 DBNAME=$(echo "$DBPATH" | 100 sed \ 101 -e "s/postgres:\/\/.*\///" \ 102 -e "s/?.*//") 103 104 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null; then 105 if [ 1 = "$RESET_DB" ]; then 106 echo "Deleting existing database '$DBNAME'." 1>&2 107 if ! sudo -i -u postgres dropdb "$DBNAME"; then 108 echo "Failed to delete existing database '$DBNAME'" 109 exit 1 110 fi 111 DO_CREATE=1 112 else 113 echo "Database '$DBNAME' already exists, continuing anyway." 114 DO_CREATE=0 115 fi 116 else 117 DO_CREATE=1 118 fi 119 120 if [ 1 = "$DO_CREATE" ]; then 121 echo "Creating database '$DBNAME'." 1>&2 122 123 if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then 124 echo "Failed to create database '$DBNAME'" 125 exit 1 126 fi 127 fi 128 129 if [ 0 = "$SKIP_DBINIT" ]; then 130 echo "Initializing database '$DBNAME'." 1>&2 131 if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE"; then 132 echo "Failed to initialize database schema" 133 exit 1 134 fi 135 fi 136 137 echo "Database configuration finished." 1>&2 138 139 exit 0