service/utils/pg_create_user.sh

151 lines
3.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env zsh
: ${PGHOST:=localhost}
: ${PGUSER:=postgres}
# TODO: add default postgresql port
2019-10-29 12:32:49 +01:00
: ${pgport:=$(get-port $POSTGRESQL_PROVIDER postgresql)}
: ${PGDATA:=$POSTGRESQL_ROOT}
: ${dbuser:=${SERVICE_ID//\//_}}
: ${dbpassfile:=${SERVICE_ROOT+$SERVICE_ROOT/password_main}}
: ${dbname:=${SERVICE_ID+${SERVICE_ID//\//_}_db}}
: ${command}
: ${cmdparameters}
: ${simulation}
function usage() {
cat <<END
usage: $0 [OPTIONS] command [parameters]
OPTIONS:
--pghost=<...> postgresql hostname
--pgport=<...> postgresql port
--pgdatadir=<...> postgresql data dir
--pguser=<...> postgresql username
--dbuser=<...> database username
--dbpassfile=<...> database password file
--dbname=<...> database name
--simulation print commands
command:
create_user
create_db
create_user_and_db
delete_user_and_db
END
}
function print_create_user() {
cat <<END
# user creation
psql -p ${pgport} -c "CREATE USER ${dbuser};"
psql -p ${pgport} -c "ALTER USER ${dbuser} WITH ENCRYPTED PASSWORD '${dbpass}';"
END
}
function create_user() {
su postgres -c "
psql -p ${pgport} -c \"CREATE USER ${dbuser};\"
psql -p ${pgport} -c \"ALTER USER ${dbuser} WITH ENCRYPTED PASSWORD '${dbpass}';\"
"
}
function print_delete_user() {
cat <<END
# user deletion
psql -p ${pgport} -c "DROP USER ${dbuser};"
END
}
function delete_user() {
su postgres -c "
psql -p ${pgport} -c \"DROP USER ${dbuser};\"
"
}
function print_create_db() {
cat <<END
# database creation
psql -p ${pgport} -c "CREATE DATABASE ${dbname} OWNER ${dbuser};"
END
}
function create_db() {
su postgres -c "psql -p ${pgport} -c \"CREATE DATABASE ${dbname} OWNER ${dbuser};\""
}
function print_delete_db() {
cat <<END
# database deletion
psql -p ${pgport} -c "DROP DATABASE ${dbname};"
END
}
function delete_db() {
su postgres -c "psql -p ${pgport} -c \"DROP DATABASE ${dbname};\""
}
for i
do
case $i in
(--pghost*) export PGHOST=$(echo ${i} | sed "s/--pghost=//") ;;
(--pgport*) export pgport=$(echo ${i} | sed "s/--pgport=//") ;;
(--pgdatadir*) export PGDATA=$(echo ${i} | sed "s/--pgdatadir=//") ;;
(--pguser*) export PGUSER=$(echo ${i} | sed "s/--pguser=//") ;;
(--dbuser*) export dbuser=$(echo ${i} | sed "s/--dbuser=//") ;;
(--dbpassfile*) dbpassfile=$(echo ${i} | sed "s/--dbpassfile=//") ;;
(--dbname*) export dbname=$(echo ${i} | sed "s/--dbname=//") ;;
(--simulation*) simulation="yes" ;;
(-*) echo "option ${i} not recognized" ; usage $0 ; exit 0 ;;
(*) [ -z $command ] && command=${i} || cmdparameters=(${cmdparameters} ${i})
esac
done
if [ -z ${dbpassfile} ]
then
echo "There is no password file for the database"
exit 1
fi
export dbpass="$(<$dbpassfile)"
lacking_information=
[ -z ${PGHOST} ] && lacking_information="yes" && echo "There is no pghost parameter"
[ -z ${pgport} ] && lacking_information="yes" && echo "There is no pgport parameter"
[ -z ${PGDATA} ] && lacking_information="yes" && echo "There is no datadir parameter"
[ -z ${PGUSER} ] && lacking_information="yes" && echo "There is no pguser parameter"
[ -z ${dbuser} ] && lacking_information="yes" && echo "There is no dbuser parameter"
[ -z ${dbpassfile} ] && lacking_information="yes" && echo "There is no passfile parameter"
[ -z ${dbname} ] && lacking_information="yes" && echo "There is no dbname parameter"
if [ ! -z ${lacking_information} ]
then
echo "Some information is missing"
exit 1
fi
export PGDATA PGHOST PGUSER
if [ -z $simulation ]
then
case ${command} in
(create_db) create_db ;;
(create_user) create_user ;;
(create_user_and_db) create_user ; create_db ;;
(delete_user_and_db) delete_user ; delete_db ;;
(*) usage $0 ; exit 1 ;;
esac
else
case ${command} in
(create_db) print_create_db ;;
(create_user) print_create_user ;;
(create_user_and_db) print_create_user ; print_create_db ;;
(delete_user_and_db) print_delete_user ; print_delete_db ;;
(*) usage $0 ; exit 1 ;;
esac
fi