- fix an intermittent startup problem: the only way we can be really
[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.2 2006/04/25 21:18:19 mlhuang Exp $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Default locations
18 PGDATA=/var/lib/pgsql/data
19 postgresql_conf=$PGDATA/postgresql.conf
20 pghba_conf=$PGDATA/pg_hba.conf
21
22 # Export so that we do not have to specify -p to psql invocations
23 export PGPORT=$PLC_DB_PORT
24
25 # /etc/init.d/postgresql always returns 0, even on failure
26 postgresql_start ()
27 {
28     # start() always returns 0
29     service postgresql start
30
31     # status() will still return 0 even while still initializing
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             psql -U postgres -c "" template1 && return 0
36             sleep 1
37         done
38     fi
39
40     return 1
41 }
42
43 case "$1" in
44     start)
45         if [ "$PLC_DB_ENABLED" != "1" ] ; then
46             exit 0
47         fi
48
49         MESSAGE=$"Starting database server"
50         dialog "$MESSAGE"
51
52         # Set data directory and redirect startup output to /var/log/pgsql
53         mkdir -p /etc/sysconfig/pgsql
54         (
55             echo "PGDATA=$PGDATA"
56             echo "PGLOG=/var/log/pgsql"
57             echo "PGPORT=$PLC_DB_PORT"
58         ) >>/etc/sysconfig/pgsql/postgresql
59
60         # Fix ownership (rpm installation may have changed it)
61         chown -R -H postgres:postgres $(dirname $PGDATA)
62
63         # PostgreSQL must be started at least once to bootstrap
64         # /var/lib/pgsql/data
65         if [ ! -f $postgresql_conf ] ; then
66             postgresql_start
67             check
68             service postgresql stop
69             check
70         fi
71
72         # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
73         # PostgreSQL 7.x uses tcpip_socket.
74         if grep -q listen_addresses $postgresql_conf ; then
75             sed -i -e '/^listen_addresses/d' $postgresql_conf
76             echo "listen_addresses = '*'" >>$postgresql_conf
77         elif grep -q tcpip_socket $postgresql_conf ; then
78             sed -i -e '/^tcpip_socket/d' $postgresql_conf
79             echo "tcpip_socket = true" >>$postgresql_conf
80         fi
81
82         # Disable access to all DBs from all hosts
83         sed -i -e '/^\(host\|local\)/d' $pghba_conf
84
85         # Enable passwordless localhost access
86         echo "local all all trust" >>$pghba_conf
87
88         # Enable access from the API and web servers
89         PLC_API_IP=$(gethostbyname $PLC_API_HOST)
90         PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
91         (
92             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
93             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
94         ) >>$pghba_conf
95
96         # Fix ownership (sed -i changes it)
97         chown postgres:postgres $postgresql_conf $pghba_conf
98
99         # Start up the server
100         postgresql_start
101         check
102
103         # Create/update the unprivileged database user and password
104         if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
105             psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
106         else
107             psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
108         fi
109
110         # Create the database if necessary
111         if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
112             createdb -U postgres $PLC_DB_NAME
113             psql -U $PLC_DB_USER -f /usr/share/pl_db/plc_schema_3.sql $PLC_DB_NAME
114         fi
115
116         result "$MESSAGE"
117         ;;
118
119     stop)
120         MESSAGE=$"Stopping database server"
121         dialog "$MESSAGE"
122
123         # Drop the current user in case the username changes
124         psql -U postgres -c "DROP USER $PLC_DB_USER" template1
125
126         # WARNING: If the DB name changes, the old DB will be left
127         # intact and a new one will be created. If it changes
128         # back, the old DB will not be re-created.
129
130         # Shut down the server
131         service postgresql stop
132         check
133
134         result "$MESSAGE"
135         ;;
136 esac
137
138 exit $ERRORS