File: init.sh

package info (click to toggle)
imip-agent 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,056 kB
  • sloc: python: 9,888; sh: 4,480; sql: 144; makefile: 8
file content (189 lines) | stat: -rwxr-xr-x 4,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh

# This tool initialises a deployment of imip-agent, creating data stores and
# published data directories, creating and initialising databases, and setting
# filesystem permissions. It is configured using the contents of the config.sh
# script.
#
# Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

DIRNAME=`dirname "$0"`
CONFIG="$DIRNAME/config.sh"

if [ -e "$CONFIG" ]; then
    . "$CONFIG"
else
    CONFIG=/etc/imip-agent/config.sh
    . "$CONFIG"
fi

SCHEMA="$DIRNAME/../conf/postgresql/schema.sql"

if [ ! -e "$SCHEMA" ]; then
    SCHEMA=/etc/imip-agent/postgresql/schema.sql
fi

PROGNAME=`basename "$0"`

if [ "$1" = "--help" ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME

Initialise stored and published data directories at...

  * $INSTALL_DIR
  * $WEB_INSTALL_DIR

...respectively.

Set permissions to the user and group respectively given as $IMIP_AGENT_USER
and $IMIP_AGENT_GROUP.

Within the stored data directory, the following directories will be created
(with STORE_TYPE currently set as "$STORE_TYPE"):

  * $INSTALL_DIR/preferences
EOF

    if [ "$STORE_TYPE" = "file" ]; then
        cat 1>&2 <<EOF
  * $INSTALL_DIR/journal (if STORE_TYPE is "file")
  * $INSTALL_DIR/store (if STORE_TYPE is "file")
EOF
    fi

    cat 1>&2 <<EOF

Within the published data directory the following directory will be created:

  * $WEB_INSTALL_DIR/static
EOF

    if [ "$STORE_TYPE" = "postgresql" ]; then
        cat 1>&2 <<EOF

With STORE_TYPE set as "database", a database schema will be initialised for the
following database:

  * $POSTGRESQL_DB
EOF
    fi

    cat 1>&2 <<EOF

See $CONFIG for the settings used as described above.
EOF
    exit 1
fi

# Test for a privileged user.

if [ `whoami` != 'root' ]; then
    cat 1>&2 <<EOF
You will need to become a privileged user using su or sudo to run this program
because it changes file ownership and may also switch users to run database
administration commands.
EOF
    exit 1
fi

# Create necessary directories regardless of store type.

echo "Creating preferences and static Web directories..." 1>&2

for DIR in "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do
    mkdir -p "$DIR"
    chown "$IMIP_AGENT_USER" "$DIR"
    chgrp "$IMIP_AGENT_GROUP" "$DIR"
    chmod g+ws "$DIR"
done

# Initialise a file store.

if [ "$STORE_TYPE" = "file" ]; then

    echo "Creating store and journal directories..." 1>&2

    for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do
        mkdir -p "$DIR"
        chown "$IMIP_AGENT_USER" "$DIR"
        chgrp "$IMIP_AGENT_GROUP" "$DIR"
        chmod g+ws "$DIR"
    done

# Initialise a PostgreSQL store.

elif [ "$STORE_TYPE" = "postgresql" ]; then

    # Check for the database.

    echo "Checking for the database ${POSTGRESQL_DB}..." 1>&2

    if $AS_POSTGRES psql -tA -c 'select datname from pg_database' postgres | grep -q ^"$POSTGRESQL_DB"$ ; then
        cat 1>&2 <<EOF
Database $POSTGRESQL_DB already exists.
EOF
        exit 1
    fi

    # Attempt to create the database.

    echo "Creating database ${POSTGRESQL_DB}..." 1>&2

    if ! $AS_POSTGRES createdb "$POSTGRESQL_DB" ; then
        cat 1>&2 <<EOF
Could not create database $POSTGRESQL_DB using createdb.
EOF
        exit 1
    fi

    # Attempt to initialise the schema.

    echo "Initialising the schema for database ${POSTGRESQL_DB}..." 1>&2

    if ! $AS_POSTGRES psql -q -f "$SCHEMA" "$POSTGRESQL_DB" ; then
        cat 1>&2 <<EOF
Could not initialise schema in database $POSTGRESQL_DB using psql.
EOF
        exit 1
    fi

    # For each user needing to connect, attempt to create a role and grant it
    # privileges on the tables.

    for USER in $POSTGRESQL_USERS ; do

        echo "Creating a database user for ${USER}..." 1>&2

        if ! $AS_POSTGRES createuser -D -R -S "$USER" ; then
            cat 1>&2 <<EOF
Could not create database user $USER using createuser.
EOF
        fi

        echo "Granting privileges to database user for ${USER}..." 1>&2

        if ! $AS_POSTGRES psql -Atc '\dt' "$POSTGRESQL_DB" \
           | cut -d '|' -f 2 \
           | xargs -I{} $AS_POSTGRES psql -q -c "grant all privileges on table {} to \"$USER\"" "$POSTGRESQL_DB" ; then

            cat 1>&2 <<EOF
Could not grant permissions for schema in database $POSTGRESQL_DB to $USER
using psql.
EOF
        fi
    done
fi