view schema/install-db.sh @ 331:a85f56207d80

db-setup script: Allow to set passwords manually. Especially during testing, when setting up a fresh db it is desirable to set the passwords to well known values instead of getting fresh random ones. This is now possible.
author Sascha Wilde <wilde@intevation.de>
date Fri, 03 Aug 2018 13:52:23 +0200
parents fd04bccae6ca
children 220a893318fa
line wrap: on
line source

#!/bin/bash
# Author(s):
# Sascha Wilde <wilde@intevation.de>

ME=`basename "$0"`
BASEDIR=`dirname "$0"`

usage()
{
  cat <<EOF
$ME [OPTION]...

Options:
  -d, --db=NAME    create the database NAME.  Default: "gemma"
  -p, --port=PORT  connect do the postgresql cluster at PORT.
                   Default is the postgresql standard port 5432
  -D, --demo       also install demo accounts and data
      --adminpw    set the password to use for the "sysadmin" account.
                   Default is a random password.
      --servicepw  set the password to use for the "gemma_service" account.
                   Default is a random password.
      --drop       drop database and all roles
      --help       display this help and exit

EOF
}

fatal()
{
  echo >&2 "$1"
  exit 23
}

genpw()
# $1 - length
{
  dd count=1 if=/dev/urandom 2>/dev/null \
    | tr -cd '[:alnum:],._!?-' | tail -c "$1"
}

# Defaults:

db=gemma
port=5432
demo=0
drop=0
adminpw=`genpw 15`
servicepw=`genpw 15`

# Parse options:

OPTS=`getopt \
      -l help,demo,db:,port:,drop,adminpw:,servicepw: \
      -o Dd:p: -n "$ME" -- "$@"`
[ $? -eq 0 ] || { usage ; exit 1 ; }

eval set -- "$OPTS"

while true ; do
  case "$1" in
    --db|-d)
      db="$2"
      shift 2
      ;;
    --port|-p)
      port="$2"
      shift 2
      ;;
    --adminpw)
      adminpw="$2"
      shift 2
      ;;
    --servicepw)
      servicepw="$2"
      shift 2
      ;;
    --demo|-D)
      demo=1
      shift 1
      ;;
    --drop)
      drop=1
      shift 1
      ;;
    --help)
      { usage ; exit 0 ; }
      ;;
    --)
      shift
      break
      ;;
  esac
done


# Main ------------------------------------------------------------

if [[ drop -eq 0 ]] ; then
  # Default operation: create schema
  psql -q -p "$port" -f "$BASEDIR/roles.sql"
  createdb -p "$port" "$db"
  psql -qt -p "$port" -d "$db" \
       -c "SET client_min_messages TO WARNING;" \
       -f "$BASEDIR/gemma.sql" \
       -f "$BASEDIR/manage_users.sql" \
       -f "$BASEDIR/auth.sql" \
       -f "$BASEDIR/std_login_roles.sql"

  if [[ $demo -eq 1 ]] ; then
    psql -q -p "$port" -f "$BASEDIR/demo-data/responsibility_areas.sql" \
         -d "$db"
    psql -q -p "$port" -f "$BASEDIR/demo-data/roles.sql" \
         -f "$BASEDIR/demo-data/users.sql" -d "$db"
  fi
  # set passwords:
  psql -qt -p "$port" -d "$db" \
       -c "ALTER ROLE sysadmin PASSWORD '$adminpw'"
  psql -qt -p "$port" -d "$db" \
       -c "ALTER ROLE gemma_service PASSWORD '$servicepw'"
  echo "Default admin user 'sysadmin' created with password '$adminpw'."
  echo "Back end user 'gemma_service' created with password '$servicepw'."
else
  # Evil mode: drop everything gemma
  echo "Really drop database '$db' and alle gemma roles? [type 'yes']: "
  read a
  if [[ $a == "yes" ]] ; then
    dropdb -p "$port" "$db"
    for r in `psql -p $port -t -c '\du' | awk -F '|' \
          '$1 "." $3 ~ /waterway_user|waterway_admin|sys_admin|pw_reset/ \
           {print $1}'`
    do
      dropuser -p "$port" "$r"
    done
  else
    echo "No harm done."
  fi
fi