1.1 --- a/tools/init.sh Sun Apr 03 21:41:44 2016 +0200
1.2 +++ b/tools/init.sh Sun Apr 03 21:43:35 2016 +0200
1.3 @@ -1,61 +1,139 @@
1.4 #!/bin/sh
1.5
1.6 DIRNAME=`dirname $0`
1.7 +CONFIG="$DIRNAME/config.sh"
1.8
1.9 -if [ -e "$DIRNAME/config.sh" ]; then
1.10 - CONFIG="$DIRNAME/config.sh"
1.11 +if [ -e "$CONFIG" ]; then
1.12 . "$CONFIG"
1.13 else
1.14 CONFIG=/etc/imip-agent/config.sh
1.15 . "$CONFIG"
1.16 fi
1.17
1.18 +SCHEMA="$DIRNAME/../conf/postgresql/schema.sql"
1.19 +
1.20 +if [ ! -e "$SCHEMA" ]; then
1.21 + SCHEMA=/etc/imip-agent/postgresql/schema.sql
1.22 +fi
1.23 +
1.24 PROGNAME=`basename $0`
1.25
1.26 if [ "$1" = "--help" ]; then
1.27 cat 1>&2 <<EOF
1.28 -Usage: $PROGNAME [ <stored data directory> [ <published data directory> [ <user> [ <group> ] ] ] ]
1.29 +Usage: $PROGNAME
1.30 +
1.31 +Initialise stored and published data directories at...
1.32
1.33 -Initialise stored and published data directories either at any specified
1.34 -locations or, respectively, at $INSTALL_DIR and $WEB_INSTALL_DIR.
1.35 + * $INSTALL_DIR
1.36 + * $WEB_INSTALL_DIR
1.37
1.38 -Set permissions to the given user and group or, respectively, to $IMIP_AGENT_USER
1.39 +...respectively.
1.40 +
1.41 +Set permissions to the user and group respectively given as $IMIP_AGENT_USER
1.42 and $IMIP_AGENT_GROUP.
1.43
1.44 -Within the stored data directory (using $INSTALL_DIR as an example), the
1.45 -following directories are created:
1.46 +Within the stored data directory, the following directories will be created
1.47 +(with STORE_TYPE currently set as "$STORE_TYPE"):
1.48
1.49 - * $INSTALL_DIR/journal (if STORE_TYPE is "file")
1.50 * $INSTALL_DIR/preferences
1.51 - * $INSTALL_DIR/store (if STORE_TYPE is "file")
1.52 +EOF
1.53
1.54 -Within the published data directory (using $WEB_INSTALL_DIR as an example), the
1.55 -following directory is created:
1.56 + if [ "$STORE_TYPE" = "file" ]; then
1.57 + cat 1>&2 <<EOF
1.58 + * $INSTALL_DIR/journal (if STORE_TYPE is "file")
1.59 + * $INSTALL_DIR/store (if STORE_TYPE is "file")
1.60 +EOF
1.61 + fi
1.62 +
1.63 + cat 1>&2 <<EOF
1.64 +
1.65 +Within the published data directory the following directory will be created:
1.66
1.67 * $WEB_INSTALL_DIR/static
1.68 +EOF
1.69
1.70 -See $CONFIG for the STORE_TYPE setting.
1.71 + if [ "$STORE_TYPE" = "postgresql" ]; then
1.72 + cat 1>&2 <<EOF
1.73 +
1.74 +With STORE_TYPE set as "database", a database schema will be initialised for the
1.75 +following database:
1.76 +
1.77 + * $POSTGRESQL_DB
1.78 +EOF
1.79 + fi
1.80 +
1.81 + cat 1>&2 <<EOF
1.82 +
1.83 +See $CONFIG for the settings used as described above.
1.84 EOF
1.85 exit 1
1.86 fi
1.87
1.88 -INSTALL_DIR=${1:-$INSTALL_DIR}
1.89 -WEB_INSTALL_DIR=${2:-$WEB_INSTALL_DIR}
1.90 -USER=${3:-$IMIP_AGENT_USER}
1.91 -GROUP=${4:-$IMIP_AGENT_GROUP}
1.92 -
1.93 for DIR in "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do
1.94 mkdir -p "$DIR"
1.95 - chown "$USER" "$DIR"
1.96 - chgrp "$GROUP" "$DIR"
1.97 + chown "$IMIP_AGENT_USER" "$DIR"
1.98 + chgrp "$IMIP_AGENT_GROUP" "$DIR"
1.99 chmod g+ws "$DIR"
1.100 done
1.101
1.102 +# Initialise a file store.
1.103 +
1.104 if [ "$STORE_TYPE" = "file" ]; then
1.105 for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do
1.106 mkdir -p "$DIR"
1.107 - chown "$USER" "$DIR"
1.108 - chgrp "$GROUP" "$DIR"
1.109 + chown "$IMIP_AGENT_USER" "$DIR"
1.110 + chgrp "$IMIP_AGENT_GROUP" "$DIR"
1.111 chmod g+ws "$DIR"
1.112 done
1.113 +
1.114 +# Initialise a PostgreSQL store.
1.115 +
1.116 +elif [ "$STORE_TYPE" = "postgresql" ]; then
1.117 +
1.118 + # Check for the database.
1.119 +
1.120 + if psql -tA -c 'select datname from pg_database' postgres | grep -q ^"$POSTGRESQL_DB"$ ; then
1.121 + cat 1>&2 <<EOF
1.122 +Database $POSTGRESQL_DB already exists.
1.123 +EOF
1.124 + exit 1
1.125 + fi
1.126 +
1.127 + # Attempt to create the database.
1.128 +
1.129 + if ! createdb "$POSTGRESQL_DB" ; then
1.130 + cat 1>&2 <<EOF
1.131 +Could not create database $POSTGRESQL_DB using createdb.
1.132 +EOF
1.133 + exit 1
1.134 + fi
1.135 +
1.136 + # Attempt to initialise the schema.
1.137 +
1.138 + if ! psql -f "$SCHEMA" "$POSTGRESQL_DB" ; then
1.139 + cat 1>&2 <<EOF
1.140 +Could not initialise schema in database $POSTGRESQL_DB using psql.
1.141 +EOF
1.142 + exit 1
1.143 + fi
1.144 +
1.145 + # For each user needing to connect, attempt to create a role and grant it
1.146 + # privileges on the tables.
1.147 +
1.148 + for USER in $POSTGRESQL_USERS ; do
1.149 + if ! createuser -D -R -S "$USER" ; then
1.150 + cat 1>&2 <<EOF
1.151 +Could not create database user $USER using createuser.
1.152 +EOF
1.153 + fi
1.154 + if ! psql -Atc '\dt' "$POSTGRESQL_DB" \
1.155 + | cut -d '|' -f 2 \
1.156 + | xargs -I{} psql -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then
1.157 +
1.158 + cat 1>&2 <<EOF
1.159 +Could not grant permissions for schema in database $POSTGRESQL_DB to $USER
1.160 +using psql.
1.161 +EOF
1.162 + fi
1.163 + done
1.164 fi