oops
[plcapi.git] / plc.d / postgresql
1 #!/bin/bash
2 #
3 # priority: 700
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
11 # Source function library and configuration
12 . /etc/plc.d/functions
13 . /etc/planetlab/plc_config
14 local_config=/etc/planetlab/configs/site.xml
15
16 # Be verbose
17 set -x
18
19 # Default locations
20 PGDATA=/var/lib/pgsql/data
21 postgresql_conf=$PGDATA/postgresql.conf
22 pghba_conf=$PGDATA/pg_hba.conf
23 postgresql_sysconfig=/etc/sysconfig/pgsql/postgresql
24
25 # Export so that we do not have to specify -p to psql invocations
26 export PGPORT=$PLC_DB_PORT
27
28 # can't trust the return of service postgresql start / nor status
29 function postgresql_check () {
30
31     # wait until postmaster is up and running - or 10s max
32     if status postmaster && [ -f /var/lock/subsys/postgresql ] ; then
33         # The only way we can be sure is if we can access it
34         for i in $(seq 1 10) ; do
35             # Must do this as the postgres user initially (before we
36             # fix pg_hba.conf to passwordless localhost access).
37             su -c 'psql -U postgres -c "" template1' postgres && return 0
38             sleep 1
39         done
40     fi
41
42     return 1
43 }
44
45 case "$1" in
46     start)
47         if [ "$PLC_DB_ENABLED" != "1" ] ; then
48             exit 0
49         fi
50
51         MESSAGE=$"Starting PostgreSQL server"
52         dialog "$MESSAGE"
53         
54         ######## sysconfig 
55         # Set data directory and redirect startup output to /var/log/pgsql
56         mkdir -p $(dirname $postgresql_sysconfig)
57         touch $postgresql_sysconfig
58         tmp=${postgresql_sysconfig}.new
59         # remove any previous definitions and write ours
60         ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig 
61             echo "PGDATA=$PGDATA"
62             echo "PGLOG=/var/log/pgsql"
63             echo "PGPORT=$PLC_DB_PORT"
64         ) > $tmp ; mv -f $tmp $postgresql_sysconfig
65
66         ######## /var/lib/pgsql/data 
67         # Fix ownership of /var/lib/pgsql (rpm installation may have changed it)
68         chown -R -H postgres:postgres $(dirname $PGDATA)
69
70         # PostgreSQL must be started at least once to bootstrap
71         # /var/lib/pgsql/data
72         if [ ! -f $postgresql_conf ] ; then
73             service postgresql initdb &> /dev/null || :
74             check
75         fi
76
77         ######## /var/lib/pgsql/data/postgresql.conf
78         # Enable DB server. drop Postgresql<=7.x
79         # PostgreSQL >=8.0 defines listen_addresses
80         # listen on a specific IP + localhost, more robust when run within a vserver
81         sed -i -e '/^listen_addresses/d' $postgresql_conf
82         echo "listen_addresses = '${PLC_DB_HOST},localhost'" >> $postgresql_conf
83         # tweak timezone to be 'UTC'
84         sed -i -e '/^timezone=/d' $postgresql_conf
85         echo "timezone='UTC'" >> $postgresql_conf
86
87         ######## /var/lib/pgsql/data/pg_hba.conf
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         ip_failure=0
99         if [ -z "$PLC_API_IP" ] ; then
100             MESSAGE=$"PLC_API_IP is not set"
101             dialog "$MESSAGE"
102             ip_failure=1
103         fi
104         if [ -z "$PLC_BOOT_IP" ] ; then
105             MESSAGE=$"PLC_BOOT_IP is not set"
106             dialog "$MESSAGE"
107             ip_failure=1
108         fi
109         if [ -z "$PLC_WWW_IP" ] ; then
110             MESSAGE=$"PLC_WWW_IP is not set"
111             dialog "$MESSAGE"
112             ip_failure=1
113         fi
114         if [ $ip_failure -eq 1 ] ; then
115             /bin/false
116             check
117         fi
118
119         (
120             echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
121             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
122             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_BOOT_IP/32 password"
123             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
124             # Drupal also uses PostgreSQL
125             echo "host drupal $PLC_DB_USER 127.0.0.1/32 password"
126             echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
127         ) >>$pghba_conf
128
129         # Append site-specific access rules
130         for file in $pghba_conf.d/*.conf ; do
131             cat "$file" >>$pghba_conf
132         done
133
134         # Fix ownership (sed -i changes it)
135         chown postgres:postgres $postgresql_conf $pghba_conf
136
137         ######## Start up the server - ignore retcod and check this our way
138         (exec 3>&- 4>&- ; service postgresql start)
139         postgresql_check
140         check
141
142         ######## Create/update the unprivileged database user and password
143         if [ -z "$PLC_DB_PASSWORD" ] ; then
144             PLC_DB_PASSWORD=$(uuidgen)
145             plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save=$local_config $local_config
146             service plc reload
147         fi
148         if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
149             psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
150         else
151             psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
152         fi
153         check
154
155         ######## Create the databases if necessary
156         if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
157             createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
158             psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
159         fi
160         check
161         if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
162             createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER drupal
163             psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal 
164         fi
165         check
166
167         result "$MESSAGE"
168         ;;
169
170     stop)
171         MESSAGE=$"Stopping PostgreSQL server"
172         dialog "$MESSAGE"
173
174         # Drop the current user in case the username changes
175         psql -U postgres -c "DROP USER $PLC_DB_USER" template1
176
177         # WARNING: If the DB name changes, the old DB will be left
178         # intact and a new one will be created. If it changes
179         # back, the old DB will not be re-created.
180
181         # Shut down the server
182         service postgresql stop
183
184         # /etc/init.d/postgresql fails if it is not running
185         [ "$PLC_DB_ENABLED" = 1 ] && check
186
187         result "$MESSAGE"
188         ;;
189 esac
190
191 exit $ERRORS