- append site-specific access rules pg_hba.conf.d/*.conf to pg_hba.conf
[myplc.git] / plc.d / postgresql
1 #!/bin/bash
2 #
3 # priority: 300
4 #
5 # Manage the PostgreSQL database server
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: postgresql,v 1.11 2007/02/05 19:11:06 mlhuang Exp $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Default locations
21 PGDATA=/var/lib/pgsql/data
22 postgresql_conf=$PGDATA/postgresql.conf
23 pghba_conf=$PGDATA/pg_hba.conf
24
25 # Export so that we do not have to specify -p to psql invocations
26 export PGPORT=$PLC_DB_PORT
27
28 # /etc/init.d/postgresql always returns 0, even on failure
29 postgresql_start ()
30 {
31     # start() always returns 0
32     service postgresql start
33
34     # status() will still return 0 even while still initializing
35     if status postmaster && [ -f /var/lock/subsys/postgresql ] ; then
36         # The only way we can be sure is if we can access it
37         for i in $(seq 1 10) ; do
38             # Must do this as the postgres user initially (before we
39             # fix pg_hba.conf to passwordless localhost access).
40             su -c 'psql -U postgres -c "" template1' postgres && return 0
41             sleep 1
42         done
43     fi
44
45     return 1
46 }
47
48 case "$1" in
49     start)
50         if [ "$PLC_DB_ENABLED" != "1" ] ; then
51             exit 0
52         fi
53
54         MESSAGE=$"Starting database server"
55         dialog "$MESSAGE"
56
57         # Set data directory and redirect startup output to /var/log/pgsql
58         mkdir -p /etc/sysconfig/pgsql
59         (
60             echo "PGDATA=$PGDATA"
61             echo "PGLOG=/var/log/pgsql"
62             echo "PGPORT=$PLC_DB_PORT"
63         ) >>/etc/sysconfig/pgsql/postgresql
64
65         # Fix ownership (rpm installation may have changed it)
66         chown -R -H postgres:postgres $(dirname $PGDATA)
67
68         # PostgreSQL must be started at least once to bootstrap
69         # /var/lib/pgsql/data
70         if [ ! -f $postgresql_conf ] ; then
71             postgresql_start
72             check
73             service postgresql stop
74             check
75         fi
76
77         # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
78         # PostgreSQL 7.x uses tcpip_socket.
79         if grep -q listen_addresses $postgresql_conf ; then
80             sed -i -e '/^listen_addresses/d' $postgresql_conf
81             echo "listen_addresses = '*'" >>$postgresql_conf
82         elif grep -q tcpip_socket $postgresql_conf ; then
83             sed -i -e '/^tcpip_socket/d' $postgresql_conf
84             echo "tcpip_socket = true" >>$postgresql_conf
85         fi
86
87         # Disable access to all DBs from all hosts
88         sed -i -e '/^\(host\|local\)/d' $pghba_conf
89
90         # Enable passwordless localhost access
91         echo "local all all trust" >>$pghba_conf
92
93         # Enable access from the API, boot, and web servers
94         PLC_API_IP=$(gethostbyname $PLC_API_HOST)
95         PLC_BOOT_IP=$(gethostbyname $PLC_BOOT_HOST)
96         PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
97         (
98             echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
99             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
100             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_BOOT_IP/32 password"
101             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
102             # Drupal also uses PostgreSQL
103             echo "host drupal $PLC_DB_USER 127.0.0.1/32 password"
104             echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
105         ) >>$pghba_conf
106
107         # Append site-specific access rules
108         for file in $pghba_conf.d/*.conf ; do
109             cat "$file" >>$pghba_conf
110         done
111
112         # Fix ownership (sed -i changes it)
113         chown postgres:postgres $postgresql_conf $pghba_conf
114
115         # Start up the server
116         postgresql_start
117         check
118
119         # Create/update the unprivileged database user and password
120         if [ -z "$PLC_DB_PASSWORD" ] ; then
121             PLC_DB_PASSWORD=$(uuidgen)
122             plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save
123         fi
124         if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
125             psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
126         else
127             psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
128         fi
129         check
130
131         # Create the databases if necessary
132         if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
133             createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
134             psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
135         fi
136         check
137         if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
138             createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER drupal
139             psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal
140         fi
141         check
142
143         result "$MESSAGE"
144         ;;
145
146     stop)
147         MESSAGE=$"Stopping database server"
148         dialog "$MESSAGE"
149
150         # Drop the current user in case the username changes
151         psql -U postgres -c "DROP USER $PLC_DB_USER" template1
152
153         # WARNING: If the DB name changes, the old DB will be left
154         # intact and a new one will be created. If it changes
155         # back, the old DB will not be re-created.
156
157         # Shut down the server
158         service postgresql stop
159
160         # /etc/init.d/postgresql fails if it is not running
161         [ "$PLC_DB_ENABLED" = 1 ] && check
162
163         result "$MESSAGE"
164         ;;
165 esac
166
167 exit $ERRORS