libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

libeufin-ebisync-dbconfig (3862B)


      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 # Error checking on
     17 set -eu
     18 
     19 # 1 is true, 0 is false
     20 RESET_DB=0
     21 FORCE_PERMS=0
     22 SKIP_INIT=0
     23 DBUSER="libeufin-ebisync"
     24 CFGFILE="/etc/libeufin-ebisync/libeufin-ebisync.conf"
     25 
     26 function exit_fail() {
     27   echo "$@" >&2
     28   exit 1
     29 }
     30 
     31 
     32 # Parse command-line options
     33 while getopts 'c:g:hprsu:' OPTION; do
     34   case "$OPTION" in
     35   c)
     36     CFGFILE="$OPTARG"
     37     ;;
     38   g)
     39     DBGROUP="$OPTARG"
     40     ;;
     41   h)
     42     echo 'Supported options:'
     43     echo "  -c FILENAME  -- use configuration FILENAME (default: $CFGFILE)"
     44     echo "  -h           -- print this help text"
     45     echo "  -r           -- reset database (dangerous)"
     46     echo "  -p           -- force permission setup even without database initialization"
     47     echo "  -s           -- skip database initialization"
     48     echo "  -u USER      -- libeufin-ebisync to be run by USER (default: $DBUSER)"
     49     exit 0
     50     ;;
     51   p)
     52     FORCE_PERMS="1"
     53     ;;
     54   r)
     55     RESET_DB="1"
     56     ;;
     57   s)
     58     SKIP_INIT="1"
     59     ;;
     60   u)
     61     DBUSER="$OPTARG"
     62     ;;
     63   ?)
     64     echo "Unrecognized command line option '$OPTION'" 1 &>2
     65     exit 1
     66     ;;
     67   esac
     68 done
     69 
     70 if ! id postgres >/dev/null; then
     71   exit_fail "Could not find 'postgres' user. Please install Postgresql first"
     72 fi
     73 
     74 if ! libeufin-ebisync --version 2>/dev/null; then
     75   exit_fail "Required 'libeufin-ebisync' not found. Please fix your installation."
     76 fi
     77 
     78 if [ "$(id -u)" -ne 0 ]; then
     79   exit_fail "This script must be run as root"
     80 fi
     81 
     82 # Check OS users exist
     83 if ! id "$DBUSER" >/dev/null; then
     84   exit_fail "Could not find '$DBUSER' user. Please set it up first"
     85 fi
     86 
     87 # Create DB user matching OS user name
     88 echo "Setting up database user '$DBUSER'." 1>&2
     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 # Check database name
     94 DBPATH=$(libeufin-ebisync config get -c "$CFGFILE" ebisyncdb-postgres CONFIG)
     95 if ! echo "$DBPATH" | grep "\(postgres\|postgresql\)://" >/dev/null; then
     96   exit_fail "Invalid database configuration value '$DBPATH'."
     97 fi
     98 DBNAME=$(echo "$DBPATH" | sed -e 's|.*://.*/||' -e 's|?.*||')
     99 
    100 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null; then
    101   if [ 1 = "$RESET_DB" ]; then
    102     echo "Deleting existing database '$DBNAME'." 1>&2
    103     if ! sudo -i -u postgres dropdb "$DBNAME"; then
    104       exit_fail "Failed to delete existing database '$DBNAME'"
    105     fi
    106     DO_CREATE=1
    107   else
    108     echo "Database '$DBNAME' already exists, continuing anyway."
    109     DO_CREATE=0
    110   fi
    111 else
    112   DO_CREATE=1
    113 fi
    114 
    115 # Create database
    116 if [ 1 = "$DO_CREATE" ]; then
    117   echo "Creating database '$DBNAME'." 1>&2
    118   if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then
    119     exit_fail "Failed to create database '$DBNAME'"
    120   fi
    121 else
    122   if ! echo "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO \"$DBUSER\"" |
    123     sudo -i -u postgres psql "$DBNAME"; then
    124     exit_fail "Failed to grant access to database '$DBNAME' to '$DBUSER'."
    125   fi
    126 fi
    127 
    128 # Run dbinit
    129 if [ 0 = "$SKIP_INIT" ]; then
    130   echo "Initialize database schema"
    131   if ! sudo -u "$DBUSER" libeufin-ebisync dbinit -c "$CFGFILE"; then
    132     exit_fail "Failed to initialize database schema"
    133   fi
    134 fi
    135 
    136 echo "Database configuration finished." 1>&2