imip-agent

tools/init.sh

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