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