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