paul@634 | 1 | #!/bin/sh |
paul@634 | 2 | |
paul@1201 | 3 | # This tool initialises a deployment of imip-agent, creating data stores and |
paul@1201 | 4 | # published data directories, creating and initialising databases, and setting |
paul@1201 | 5 | # filesystem permissions. It is configured using the contents of the config.sh |
paul@1201 | 6 | # script. |
paul@1201 | 7 | # |
paul@1201 | 8 | # Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1201 | 9 | # |
paul@1201 | 10 | # This program is free software; you can redistribute it and/or modify it under |
paul@1201 | 11 | # the terms of the GNU General Public License as published by the Free Software |
paul@1201 | 12 | # Foundation; either version 3 of the License, or (at your option) any later |
paul@1201 | 13 | # version. |
paul@1201 | 14 | # |
paul@1201 | 15 | # This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1201 | 16 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1201 | 17 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1201 | 18 | # details. |
paul@1201 | 19 | # |
paul@1201 | 20 | # You should have received a copy of the GNU General Public License along with |
paul@1201 | 21 | # this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1201 | 22 | |
paul@1108 | 23 | DIRNAME=`dirname "$0"` |
paul@1096 | 24 | CONFIG="$DIRNAME/config.sh" |
paul@891 | 25 | |
paul@1096 | 26 | if [ -e "$CONFIG" ]; then |
paul@1089 | 27 | . "$CONFIG" |
paul@891 | 28 | else |
paul@1089 | 29 | CONFIG=/etc/imip-agent/config.sh |
paul@1089 | 30 | . "$CONFIG" |
paul@891 | 31 | fi |
paul@891 | 32 | |
paul@1096 | 33 | SCHEMA="$DIRNAME/../conf/postgresql/schema.sql" |
paul@1096 | 34 | |
paul@1096 | 35 | if [ ! -e "$SCHEMA" ]; then |
paul@1096 | 36 | SCHEMA=/etc/imip-agent/postgresql/schema.sql |
paul@891 | 37 | fi |
paul@891 | 38 | |
paul@1108 | 39 | PROGNAME=`basename "$0"` |
paul@731 | 40 | |
paul@731 | 41 | if [ "$1" = "--help" ]; then |
paul@731 | 42 | cat 1>&2 <<EOF |
paul@1096 | 43 | Usage: $PROGNAME |
paul@1096 | 44 | |
paul@1096 | 45 | Initialise stored and published data directories at... |
paul@731 | 46 | |
paul@1096 | 47 | * $INSTALL_DIR |
paul@1096 | 48 | * $WEB_INSTALL_DIR |
paul@891 | 49 | |
paul@1096 | 50 | ...respectively. |
paul@1096 | 51 | |
paul@1096 | 52 | Set permissions to the user and group respectively given as $IMIP_AGENT_USER |
paul@891 | 53 | and $IMIP_AGENT_GROUP. |
paul@731 | 54 | |
paul@1096 | 55 | Within the stored data directory, the following directories will be created |
paul@1096 | 56 | (with STORE_TYPE currently set as "$STORE_TYPE"): |
paul@731 | 57 | |
paul@1039 | 58 | * $INSTALL_DIR/preferences |
paul@1096 | 59 | EOF |
paul@731 | 60 | |
paul@1096 | 61 | if [ "$STORE_TYPE" = "file" ]; then |
paul@1096 | 62 | cat 1>&2 <<EOF |
paul@1096 | 63 | * $INSTALL_DIR/journal (if STORE_TYPE is "file") |
paul@1096 | 64 | * $INSTALL_DIR/store (if STORE_TYPE is "file") |
paul@1096 | 65 | EOF |
paul@1096 | 66 | fi |
paul@1096 | 67 | |
paul@1096 | 68 | cat 1>&2 <<EOF |
paul@1096 | 69 | |
paul@1096 | 70 | Within the published data directory the following directory will be created: |
paul@731 | 71 | |
paul@891 | 72 | * $WEB_INSTALL_DIR/static |
paul@731 | 73 | EOF |
paul@1089 | 74 | |
paul@1096 | 75 | if [ "$STORE_TYPE" = "postgresql" ]; then |
paul@1096 | 76 | cat 1>&2 <<EOF |
paul@1096 | 77 | |
paul@1096 | 78 | With STORE_TYPE set as "database", a database schema will be initialised for the |
paul@1096 | 79 | following database: |
paul@1096 | 80 | |
paul@1096 | 81 | * $POSTGRESQL_DB |
paul@1096 | 82 | EOF |
paul@1096 | 83 | fi |
paul@1096 | 84 | |
paul@1096 | 85 | cat 1>&2 <<EOF |
paul@1096 | 86 | |
paul@1096 | 87 | See $CONFIG for the settings used as described above. |
paul@731 | 88 | EOF |
paul@731 | 89 | exit 1 |
paul@731 | 90 | fi |
paul@731 | 91 | |
paul@1135 | 92 | # Test for a privileged user. |
paul@1135 | 93 | |
paul@1135 | 94 | if [ `whoami` != 'root' ]; then |
paul@1135 | 95 | cat 1>&2 <<EOF |
paul@1135 | 96 | You will need to become a privileged user using su or sudo to run this program |
paul@1135 | 97 | because it changes file ownership and may also switch users to run database |
paul@1135 | 98 | administration commands. |
paul@1135 | 99 | EOF |
paul@1135 | 100 | exit 1 |
paul@1135 | 101 | fi |
paul@1135 | 102 | |
paul@1135 | 103 | # Create necessary directories regardless of store type. |
paul@1135 | 104 | |
paul@1135 | 105 | echo "Creating preferences and static Web directories..." 1>&2 |
paul@1135 | 106 | |
paul@1089 | 107 | for DIR in "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do |
paul@634 | 108 | mkdir -p "$DIR" |
paul@1096 | 109 | chown "$IMIP_AGENT_USER" "$DIR" |
paul@1096 | 110 | chgrp "$IMIP_AGENT_GROUP" "$DIR" |
paul@634 | 111 | chmod g+ws "$DIR" |
paul@634 | 112 | done |
paul@1089 | 113 | |
paul@1096 | 114 | # Initialise a file store. |
paul@1096 | 115 | |
paul@1089 | 116 | if [ "$STORE_TYPE" = "file" ]; then |
paul@1135 | 117 | |
paul@1135 | 118 | echo "Creating store and journal directories..." 1>&2 |
paul@1135 | 119 | |
paul@1089 | 120 | for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do |
paul@1089 | 121 | mkdir -p "$DIR" |
paul@1096 | 122 | chown "$IMIP_AGENT_USER" "$DIR" |
paul@1096 | 123 | chgrp "$IMIP_AGENT_GROUP" "$DIR" |
paul@1089 | 124 | chmod g+ws "$DIR" |
paul@1089 | 125 | done |
paul@1096 | 126 | |
paul@1096 | 127 | # Initialise a PostgreSQL store. |
paul@1096 | 128 | |
paul@1096 | 129 | elif [ "$STORE_TYPE" = "postgresql" ]; then |
paul@1096 | 130 | |
paul@1096 | 131 | # Check for the database. |
paul@1096 | 132 | |
paul@1135 | 133 | echo "Checking for the database ${POSTGRESQL_DB}..." 1>&2 |
paul@1135 | 134 | |
paul@1101 | 135 | if $AS_POSTGRES psql -tA -c 'select datname from pg_database' postgres | grep -q ^"$POSTGRESQL_DB"$ ; then |
paul@1096 | 136 | cat 1>&2 <<EOF |
paul@1096 | 137 | Database $POSTGRESQL_DB already exists. |
paul@1096 | 138 | EOF |
paul@1096 | 139 | exit 1 |
paul@1096 | 140 | fi |
paul@1096 | 141 | |
paul@1096 | 142 | # Attempt to create the database. |
paul@1096 | 143 | |
paul@1135 | 144 | echo "Creating database ${POSTGRESQL_DB}..." 1>&2 |
paul@1135 | 145 | |
paul@1101 | 146 | if ! $AS_POSTGRES createdb "$POSTGRESQL_DB" ; then |
paul@1096 | 147 | cat 1>&2 <<EOF |
paul@1096 | 148 | Could not create database $POSTGRESQL_DB using createdb. |
paul@1096 | 149 | EOF |
paul@1096 | 150 | exit 1 |
paul@1096 | 151 | fi |
paul@1096 | 152 | |
paul@1096 | 153 | # Attempt to initialise the schema. |
paul@1096 | 154 | |
paul@1135 | 155 | echo "Initialising the schema for database ${POSTGRESQL_DB}..." 1>&2 |
paul@1135 | 156 | |
paul@1135 | 157 | if ! $AS_POSTGRES psql -q -f "$SCHEMA" "$POSTGRESQL_DB" ; then |
paul@1096 | 158 | cat 1>&2 <<EOF |
paul@1096 | 159 | Could not initialise schema in database $POSTGRESQL_DB using psql. |
paul@1096 | 160 | EOF |
paul@1096 | 161 | exit 1 |
paul@1096 | 162 | fi |
paul@1096 | 163 | |
paul@1096 | 164 | # For each user needing to connect, attempt to create a role and grant it |
paul@1096 | 165 | # privileges on the tables. |
paul@1096 | 166 | |
paul@1096 | 167 | for USER in $POSTGRESQL_USERS ; do |
paul@1135 | 168 | |
paul@1135 | 169 | echo "Creating a database user for ${USER}..." 1>&2 |
paul@1135 | 170 | |
paul@1101 | 171 | if ! $AS_POSTGRES createuser -D -R -S "$USER" ; then |
paul@1096 | 172 | cat 1>&2 <<EOF |
paul@1096 | 173 | Could not create database user $USER using createuser. |
paul@1096 | 174 | EOF |
paul@1096 | 175 | fi |
paul@1135 | 176 | |
paul@1135 | 177 | echo "Granting privileges to database user for ${USER}..." 1>&2 |
paul@1135 | 178 | |
paul@1101 | 179 | if ! $AS_POSTGRES psql -Atc '\dt' "$POSTGRESQL_DB" \ |
paul@1096 | 180 | | cut -d '|' -f 2 \ |
paul@1135 | 181 | | xargs -I{} $AS_POSTGRES psql -q -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then |
paul@1096 | 182 | |
paul@1096 | 183 | cat 1>&2 <<EOF |
paul@1096 | 184 | Could not grant permissions for schema in database $POSTGRESQL_DB to $USER |
paul@1096 | 185 | using psql. |
paul@1096 | 186 | EOF |
paul@1096 | 187 | fi |
paul@1096 | 188 | done |
paul@1089 | 189 | fi |