merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-dbconfig (3801B)


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