taler-cyclos-dbconfig (4942B)
1 #!/bin/bash 2 # This file is part of GNU TALER. 3 # Copyright (C) 2025 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 Antoine d'Aligny 17 18 # Error checking on 19 set -eu 20 21 # 1 is true, 0 is false 22 RESET_DB=0 23 FORCE_PERMS=0 24 SKIP_INIT=0 25 DBUSER="taler-cyclos-httpd" 26 DBGROUP="taler-cyclos-db" 27 CFGFILE="/etc/taler-cyclos/taler-cyclos.conf" 28 29 # Parse command-line options 30 while getopts 'c:g:hprsu:' OPTION; do 31 case "$OPTION" in 32 c) 33 CFGFILE="$OPTARG" 34 ;; 35 g) 36 DBGROUP="$OPTARG" 37 ;; 38 h) 39 echo 'Supported options:' 40 echo " -c FILENAME -- use configuration FILENAME (default: $CFGFILE)" 41 echo " -g GROUP -- taler-cyclos to be run by GROUP (default: $DBGROUP)" 42 echo " -h -- print this help text" 43 echo " -r -- reset database (dangerous)" 44 echo " -p -- force permission setup even without database initialization" 45 echo " -s -- skip database initialization" 46 echo " -u USER -- taler-cyclos to be run by USER (default: $DBUSER)" 47 exit 0 48 ;; 49 p) 50 FORCE_PERMS="1" 51 ;; 52 r) 53 RESET_DB="1" 54 ;; 55 s) 56 SKIP_INIT="1" 57 ;; 58 u) 59 DBUSER="$OPTARG" 60 ;; 61 ?) 62 echo "Unrecognized command line option '$OPTION'" 1 &>2 63 exit 1 64 ;; 65 esac 66 done 67 68 function exit_fail() { 69 echo "$@" >&2 70 exit 1 71 } 72 73 if ! id postgres >/dev/null; then 74 exit_fail "Could not find 'postgres' user. Please install Postgresql first" 75 fi 76 77 if ! taler-cyclos --version 2>/dev/null; then 78 exit_fail "Required 'taler-cyclos' not found. Please fix your installation." 79 fi 80 81 if [ "$(id -u)" -ne 0 ]; then 82 exit_fail "This script must be run as root" 83 fi 84 85 # Check OS users exist 86 if ! id "$DBUSER" >/dev/null; then 87 exit_fail "Could not find '$DBUSER' user. Please set it up first" 88 fi 89 90 # Create DB user matching OS user name 91 echo "Setting up database user '$DBUSER'." 1>&2 92 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null; then 93 echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 94 fi 95 96 # Check database name 97 DBPATH=$(taler-cyclos -c "$CFGFILE" config get cyclosdb-postgres CONFIG) 98 if ! echo "$DBPATH" | grep "postgres://" >/dev/null; then 99 exit_fail "Invalid database configuration value '$DBPATH'." 1>&2 100 fi 101 DBNAME=$(echo "$DBPATH" | sed -e "s/postgres:\/\/.*\///" -e "s/?.*//") 102 103 # Reset database 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 exit_fail "Failed to delete existing database '$DBNAME'" 109 fi 110 DO_CREATE=1 111 else 112 echo "Database '$DBNAME' already exists, continuing anyway." 113 DO_CREATE=0 114 fi 115 else 116 DO_CREATE=1 117 fi 118 119 # Create database 120 if [ 1 = "$DO_CREATE" ]; then 121 echo "Creating database '$DBNAME'." 1>&2 122 if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then 123 exit_fail "Failed to create database '$DBNAME'" 124 fi 125 fi 126 127 # Run dbinit 128 if [ 0 = "$SKIP_INIT" ]; then 129 echo "Initialize database schema" 130 if ! sudo -u "$DBUSER" taler-cyclos dbinit -c "$CFGFILE"; then 131 exit_fail "Failed to initialize database schema" 132 fi 133 fi 134 135 # Set permission for group user 136 if [ 0 = "$SKIP_INIT" ] || [ 1 = "$FORCE_PERMS" ]; then 137 # Create DB group matching OS group name 138 echo "Setting up database group '$DBGROUP'." 1>&2 139 if ! sudo -i -u postgres createuser "$DBGROUP" 2>/dev/null; then 140 echo "Database group '$DBGROUP' already existed. Continuing anyway." 1>&2 141 fi 142 if ! sudo -i -u postgres psql "$DBNAME" <<-EOF 143 GRANT ALL ON SCHEMA cyclos TO "$DBGROUP"; 144 GRANT SELECT ON ALL TABLES IN SCHEMA cyclos TO "$DBGROUP"; 145 EOF 146 then 147 exit_fail "Failed to grant access to '$DBGROUP'." 148 fi 149 150 # Update group users rights 151 DB_GRP="$(getent group "$DBGROUP" | sed -e "s/.*://g" -e "s/,/ /g")" 152 echo "Initializing permissions for '$DB_GRP' users." 1>&2 153 for GROUPIE in $DB_GRP; do 154 if [ "$GROUPIE" != "$DBUSER" ]; then 155 if ! sudo -i -u postgres createuser "$GROUPIE" 2>/dev/null; then 156 echo "Database user '$GROUPIE' already existed. Continuing anyway." 1>&2 157 fi 158 fi 159 160 if ! echo "GRANT \"$DBGROUP\" TO \"$GROUPIE\"" | 161 sudo -i -u postgres psql "$DBNAME"; then 162 exit_fail "Failed to make '$GROUPIE' part of '$DBGROUP' db group." 163 fi 164 done 165 fi 166 167 echo "Database configuration finished." 1>&2