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