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 # Test for a privileged user. 73 74 if [ `whoami` != 'root' ]; then 75 cat 1>&2 <<EOF 76 You will need to become a privileged user using su or sudo to run this program 77 because it changes file ownership and may also switch users to run database 78 administration commands. 79 EOF 80 exit 1 81 fi 82 83 # Create necessary directories regardless of store type. 84 85 echo "Creating preferences and static Web directories..." 1>&2 86 87 for DIR in "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do 88 mkdir -p "$DIR" 89 chown "$IMIP_AGENT_USER" "$DIR" 90 chgrp "$IMIP_AGENT_GROUP" "$DIR" 91 chmod g+ws "$DIR" 92 done 93 94 # Initialise a file store. 95 96 if [ "$STORE_TYPE" = "file" ]; then 97 98 echo "Creating store and journal directories..." 1>&2 99 100 for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do 101 mkdir -p "$DIR" 102 chown "$IMIP_AGENT_USER" "$DIR" 103 chgrp "$IMIP_AGENT_GROUP" "$DIR" 104 chmod g+ws "$DIR" 105 done 106 107 # Initialise a PostgreSQL store. 108 109 elif [ "$STORE_TYPE" = "postgresql" ]; then 110 111 # Check for the database. 112 113 echo "Checking for the database ${POSTGRESQL_DB}..." 1>&2 114 115 if $AS_POSTGRES psql -tA -c 'select datname from pg_database' postgres | grep -q ^"$POSTGRESQL_DB"$ ; then 116 cat 1>&2 <<EOF 117 Database $POSTGRESQL_DB already exists. 118 EOF 119 exit 1 120 fi 121 122 # Attempt to create the database. 123 124 echo "Creating database ${POSTGRESQL_DB}..." 1>&2 125 126 if ! $AS_POSTGRES createdb "$POSTGRESQL_DB" ; then 127 cat 1>&2 <<EOF 128 Could not create database $POSTGRESQL_DB using createdb. 129 EOF 130 exit 1 131 fi 132 133 # Attempt to initialise the schema. 134 135 echo "Initialising the schema for database ${POSTGRESQL_DB}..." 1>&2 136 137 if ! $AS_POSTGRES psql -q -f "$SCHEMA" "$POSTGRESQL_DB" ; then 138 cat 1>&2 <<EOF 139 Could not initialise schema in database $POSTGRESQL_DB using psql. 140 EOF 141 exit 1 142 fi 143 144 # For each user needing to connect, attempt to create a role and grant it 145 # privileges on the tables. 146 147 for USER in $POSTGRESQL_USERS ; do 148 149 echo "Creating a database user for ${USER}..." 1>&2 150 151 if ! $AS_POSTGRES createuser -D -R -S "$USER" ; then 152 cat 1>&2 <<EOF 153 Could not create database user $USER using createuser. 154 EOF 155 fi 156 157 echo "Granting privileges to database user for ${USER}..." 1>&2 158 159 if ! $AS_POSTGRES psql -Atc '\dt' "$POSTGRESQL_DB" \ 160 | cut -d '|' -f 2 \ 161 | xargs -I{} $AS_POSTGRES psql -q -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then 162 163 cat 1>&2 <<EOF 164 Could not grant permissions for schema in database $POSTGRESQL_DB to $USER 165 using psql. 166 EOF 167 fi 168 done 169 fi