1.1 --- a/tools/init.sh Tue Apr 19 23:44:33 2016 +0200
1.2 +++ b/tools/init.sh Wed Apr 20 16:13:29 2016 +0200
1.3 @@ -69,6 +69,21 @@
1.4 exit 1
1.5 fi
1.6
1.7 +# Test for a privileged user.
1.8 +
1.9 +if [ `whoami` != 'root' ]; then
1.10 + cat 1>&2 <<EOF
1.11 +You will need to become a privileged user using su or sudo to run this program
1.12 +because it changes file ownership and may also switch users to run database
1.13 +administration commands.
1.14 +EOF
1.15 + exit 1
1.16 +fi
1.17 +
1.18 +# Create necessary directories regardless of store type.
1.19 +
1.20 +echo "Creating preferences and static Web directories..." 1>&2
1.21 +
1.22 for DIR in "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do
1.23 mkdir -p "$DIR"
1.24 chown "$IMIP_AGENT_USER" "$DIR"
1.25 @@ -79,6 +94,9 @@
1.26 # Initialise a file store.
1.27
1.28 if [ "$STORE_TYPE" = "file" ]; then
1.29 +
1.30 + echo "Creating store and journal directories..." 1>&2
1.31 +
1.32 for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do
1.33 mkdir -p "$DIR"
1.34 chown "$IMIP_AGENT_USER" "$DIR"
1.35 @@ -92,6 +110,8 @@
1.36
1.37 # Check for the database.
1.38
1.39 + echo "Checking for the database ${POSTGRESQL_DB}..." 1>&2
1.40 +
1.41 if $AS_POSTGRES psql -tA -c 'select datname from pg_database' postgres | grep -q ^"$POSTGRESQL_DB"$ ; then
1.42 cat 1>&2 <<EOF
1.43 Database $POSTGRESQL_DB already exists.
1.44 @@ -101,6 +121,8 @@
1.45
1.46 # Attempt to create the database.
1.47
1.48 + echo "Creating database ${POSTGRESQL_DB}..." 1>&2
1.49 +
1.50 if ! $AS_POSTGRES createdb "$POSTGRESQL_DB" ; then
1.51 cat 1>&2 <<EOF
1.52 Could not create database $POSTGRESQL_DB using createdb.
1.53 @@ -110,7 +132,9 @@
1.54
1.55 # Attempt to initialise the schema.
1.56
1.57 - if ! $AS_POSTGRES psql -f "$SCHEMA" "$POSTGRESQL_DB" ; then
1.58 + echo "Initialising the schema for database ${POSTGRESQL_DB}..." 1>&2
1.59 +
1.60 + if ! $AS_POSTGRES psql -q -f "$SCHEMA" "$POSTGRESQL_DB" ; then
1.61 cat 1>&2 <<EOF
1.62 Could not initialise schema in database $POSTGRESQL_DB using psql.
1.63 EOF
1.64 @@ -121,14 +145,20 @@
1.65 # privileges on the tables.
1.66
1.67 for USER in $POSTGRESQL_USERS ; do
1.68 +
1.69 + echo "Creating a database user for ${USER}..." 1>&2
1.70 +
1.71 if ! $AS_POSTGRES createuser -D -R -S "$USER" ; then
1.72 cat 1>&2 <<EOF
1.73 Could not create database user $USER using createuser.
1.74 EOF
1.75 fi
1.76 +
1.77 + echo "Granting privileges to database user for ${USER}..." 1>&2
1.78 +
1.79 if ! $AS_POSTGRES psql -Atc '\dt' "$POSTGRESQL_DB" \
1.80 | cut -d '|' -f 2 \
1.81 - | xargs -I{} psql -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then
1.82 + | xargs -I{} $AS_POSTGRES psql -q -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then
1.83
1.84 cat 1>&2 <<EOF
1.85 Could not grant permissions for schema in database $POSTGRESQL_DB to $USER