1 #!/bin/sh 2 3 DIRNAME=`dirname $0` 4 CONFIG="$DIRNAME/config.sh" 5 6 if [ -e "$CONFIG" ]; then 7 . "$CONFIG" 8 else 9 CONFIG=/etc/imip-agent/config.sh 10 . "$CONFIG" 11 fi 12 13 SCHEMA="$DIRNAME/../conf/postgresql/schema.sql" 14 15 if [ ! -e "$SCHEMA" ]; then 16 SCHEMA=/etc/imip-agent/postgresql/schema.sql 17 fi 18 19 PROGNAME=`basename $0` 20 21 if [ "$1" = "--help" ]; then 22 cat 1>&2 <<EOF 23 Usage: $PROGNAME 24 25 Initialise stored and published data directories at... 26 27 * $INSTALL_DIR 28 * $WEB_INSTALL_DIR 29 30 ...respectively. 31 32 Set permissions to the user and group respectively given as $IMIP_AGENT_USER 33 and $IMIP_AGENT_GROUP. 34 35 Within the stored data directory, the following directories will be created 36 (with STORE_TYPE currently set as "$STORE_TYPE"): 37 38 * $INSTALL_DIR/preferences 39 EOF 40 41 if [ "$STORE_TYPE" = "file" ]; then 42 cat 1>&2 <<EOF 43 * $INSTALL_DIR/journal (if STORE_TYPE is "file") 44 * $INSTALL_DIR/store (if STORE_TYPE is "file") 45 EOF 46 fi 47 48 cat 1>&2 <<EOF 49 50 Within the published data directory the following directory will be created: 51 52 * $WEB_INSTALL_DIR/static 53 EOF 54 55 if [ "$STORE_TYPE" = "postgresql" ]; then 56 cat 1>&2 <<EOF 57 58 With STORE_TYPE set as "database", a database schema will be initialised for the 59 following database: 60 61 * $POSTGRESQL_DB 62 EOF 63 fi 64 65 cat 1>&2 <<EOF 66 67 See $CONFIG for the settings used as described above. 68 EOF 69 exit 1 70 fi 71 72 for DIR in "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do 73 mkdir -p "$DIR" 74 chown "$IMIP_AGENT_USER" "$DIR" 75 chgrp "$IMIP_AGENT_GROUP" "$DIR" 76 chmod g+ws "$DIR" 77 done 78 79 # Initialise a file store. 80 81 if [ "$STORE_TYPE" = "file" ]; then 82 for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do 83 mkdir -p "$DIR" 84 chown "$IMIP_AGENT_USER" "$DIR" 85 chgrp "$IMIP_AGENT_GROUP" "$DIR" 86 chmod g+ws "$DIR" 87 done 88 89 # Initialise a PostgreSQL store. 90 91 elif [ "$STORE_TYPE" = "postgresql" ]; then 92 93 # Check for the database. 94 95 if $AS_POSTGRES psql -tA -c 'select datname from pg_database' postgres | grep -q ^"$POSTGRESQL_DB"$ ; then 96 cat 1>&2 <<EOF 97 Database $POSTGRESQL_DB already exists. 98 EOF 99 exit 1 100 fi 101 102 # Attempt to create the database. 103 104 if ! $AS_POSTGRES createdb "$POSTGRESQL_DB" ; then 105 cat 1>&2 <<EOF 106 Could not create database $POSTGRESQL_DB using createdb. 107 EOF 108 exit 1 109 fi 110 111 # Attempt to initialise the schema. 112 113 if ! $AS_POSTGRES psql -f "$SCHEMA" "$POSTGRESQL_DB" ; then 114 cat 1>&2 <<EOF 115 Could not initialise schema in database $POSTGRESQL_DB using psql. 116 EOF 117 exit 1 118 fi 119 120 # For each user needing to connect, attempt to create a role and grant it 121 # privileges on the tables. 122 123 for USER in $POSTGRESQL_USERS ; do 124 if ! $AS_POSTGRES createuser -D -R -S "$USER" ; then 125 cat 1>&2 <<EOF 126 Could not create database user $USER using createuser. 127 EOF 128 fi 129 if ! $AS_POSTGRES psql -Atc '\dt' "$POSTGRESQL_DB" \ 130 | cut -d '|' -f 2 \ 131 | xargs -I{} psql -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then 132 133 cat 1>&2 <<EOF 134 Could not grant permissions for schema in database $POSTGRESQL_DB to $USER 135 using psql. 136 EOF 137 fi 138 done 139 fi