d1c56d4d4e05596a263521b8ad021b26026ec33f
[plcapi.git] / plc.d / postgresql
1 #!/bin/bash
2 # $Id$
3 # $URL$
4 #
5 # priority: 700
6 #
7 # Manage the PostgreSQL database server
8 #
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2006 The Trustees of Princeton University
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 postgresql_init ()
50 {
51     service postgresql initdb &> /dev/null || :
52     postgresql_start
53 }
54
55 case "$1" in
56     start)
57         if [ "$PLC_DB_ENABLED" != "1" ] ; then
58             exit 0
59         fi
60
61         MESSAGE=$"Starting PostgreSQL server"
62         dialog "$MESSAGE"
63
64         # Set data directory and redirect startup output to /var/log/pgsql
65         mkdir -p /etc/sysconfig/pgsql
66         (
67             echo "PGDATA=$PGDATA"
68             echo "PGLOG=/var/log/pgsql"
69             echo "PGPORT=$PLC_DB_PORT"
70         ) >>/etc/sysconfig/pgsql/postgresql
71
72         # Fix ownership (rpm installation may have changed it)
73         chown -R -H postgres:postgres $(dirname $PGDATA)
74
75         # PostgreSQL must be started at least once to bootstrap
76         # /var/lib/pgsql/data
77         if [ ! -f $postgresql_conf ] ; then
78             postgresql_init
79             check
80             service postgresql stop
81             check
82         fi
83
84         # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
85         # PostgreSQL 7.x uses tcpip_socket.
86         if grep -q listen_addresses $postgresql_conf ; then
87             sed -i -e '/^listen_addresses/d' $postgresql_conf
88             echo "listen_addresses = '*'" >> $postgresql_conf
89             # tweak timezone to be 'UTC'
90             sed -i -e '/^timezone=/d' $postgresql_conf
91             echo "timezone='UTC'" >> $postgresql_conf
92         else
93             dialog "PostgreSQL <= 7.x - not supported"
94             /bin/false
95             check
96         fi
97
98         # Disable access to all DBs from all hosts
99         sed -i -e '/^\(host\|local\)/d' $pghba_conf
100
101         # Enable passwordless localhost access
102         echo "local all all trust" >>$pghba_conf
103
104         # Enable access from the API, boot, and web servers
105         PLC_API_IP=$(gethostbyname $PLC_API_HOST)
106         PLC_BOOT_IP=$(gethostbyname $PLC_BOOT_HOST)
107         PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
108         ip_failure=0
109         if [ -z "$PLC_API_IP" ] ; then
110             MESSAGE=$"PLC_API_IP is not set"
111             dialog "$MESSAGE"
112             ip_failure=1
113         fi
114         if [ -z "$PLC_BOOT_IP" ] ; then
115             MESSAGE=$"PLC_BOOT_IP is not set"
116             dialog "$MESSAGE"
117             ip_failure=1
118         fi
119         if [ -z "$PLC_WWW_IP" ] ; then
120             MESSAGE=$"PLC_WWW_IP is not set"
121             dialog "$MESSAGE"
122             ip_failure=1
123         fi
124         if [ $ip_failure -eq 1 ] ; then
125             /bin/false
126             check
127         fi
128
129         (
130             echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
131             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
132             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_BOOT_IP/32 password"
133             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
134             # Drupal also uses PostgreSQL
135             echo "host drupal $PLC_DB_USER 127.0.0.1/32 password"
136             echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
137         ) >>$pghba_conf
138
139         # Append site-specific access rules
140         for file in $pghba_conf.d/*.conf ; do
141             cat "$file" >>$pghba_conf
142         done
143
144         # Fix ownership (sed -i changes it)
145         chown postgres:postgres $postgresql_conf $pghba_conf
146
147         # Start up the server
148         postgresql_start
149         check
150
151         # Create/update the unprivileged database user and password
152         if [ -z "$PLC_DB_PASSWORD" ] ; then
153             PLC_DB_PASSWORD=$(uuidgen)
154             plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save=$local_config $local_config
155             service plc reload
156         fi
157         if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
158             psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
159         else
160             psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
161         fi
162         check
163
164         # Create the databases if necessary
165         if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
166             createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
167             psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
168         fi
169         check
170         if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
171             createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER drupal
172             psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal 
173         fi
174         check
175
176         result "$MESSAGE"
177         ;;
178
179     stop)
180         MESSAGE=$"Stopping PostgreSQL server"
181         dialog "$MESSAGE"
182
183         # Drop the current user in case the username changes
184         psql -U postgres -c "DROP USER $PLC_DB_USER" template1
185
186         # WARNING: If the DB name changes, the old DB will be left
187         # intact and a new one will be created. If it changes
188         # back, the old DB will not be re-created.
189
190         # Shut down the server
191         service postgresql stop
192
193         # /etc/init.d/postgresql fails if it is not running
194         [ "$PLC_DB_ENABLED" = 1 ] && check
195
196         result "$MESSAGE"
197         ;;
198 esac
199
200 exit $ERRORS