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