depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

depolymerizer-bitcoin-dbconfig (5041B)


      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="depolymerizer-bitcoin-httpd"
     26 DBGROUP="depolymerizer-bitcoin-db"
     27 CFGFILE="/etc/depolymerizer-bitcoin/depolymerizer-bitcoin.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     -- depolymerizer-bitcoin 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      -- depolymerizer-bitcoin 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 ! depolymerizer-bitcoin --version 2>/dev/null; then
     78   exit_fail "Required 'depolymerizer-bitcoin' 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=$(depolymerizer-bitcoin -c "$CFGFILE" config get depolymerizer-bitcoindb-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   if ! sudo -u "$DBUSER" depolymerizer-bitcoin dbinit -c "$CFGFILE"; then
    130     exit_fail "Failed to initialize database schema"
    131   fi
    132 fi
    133 
    134 # Set permission for group user
    135 if [ 0 = "$SKIP_INIT" ] || [ 1 = "$FORCE_PERMS" ]; then
    136   # Create DB group matching OS group name
    137   echo "Setting up database group '$DBGROUP'." 1>&2
    138   if ! sudo -i -u postgres createuser "$DBGROUP" 2>/dev/null; then
    139     echo "Database group '$DBGROUP' already existed. Continuing anyway." 1>&2
    140   fi
    141   if ! sudo -i -u postgres psql "$DBNAME" <<-EOF
    142     GRANT ALL ON SCHEMA depolymerizer_bitcoin TO "$DBGROUP";
    143     GRANT SELECT ON ALL TABLES IN SCHEMA depolymerizer_bitcoin TO "$DBGROUP";
    144 EOF
    145   then
    146       exit_fail "Failed to grant access to '$DBGROUP'."
    147   fi
    148 
    149   # Update group users rights
    150   DB_GRP="$(getent group "$DBGROUP" | sed -e "s/.*://g" -e "s/,/ /g")"
    151   echo "Initializing permissions for '$DB_GRP' users." 1>&2
    152   for GROUPIE in $DB_GRP; do
    153     if [ "$GROUPIE" != "$DBUSER" ]; then
    154       if ! sudo -i -u postgres createuser "$GROUPIE" 2>/dev/null; then
    155         echo "Database user '$GROUPIE' already existed. Continuing anyway." 1>&2
    156       fi
    157     fi
    158     
    159     if ! echo "GRANT \"$DBGROUP\" TO \"$GROUPIE\"" |
    160       sudo -i -u postgres psql "$DBNAME"; then
    161       exit_fail "Failed to make '$GROUPIE' part of '$DBGROUP' db group."
    162     fi
    163   done
    164 fi
    165 
    166 echo "Database configuration finished." 1>&2