- don't source shell configuration in /etc/plc.d/functions, which is
[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.1 2006/04/06 21:51:59 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     service postgresql start
29     status postmaster && [ -f /var/lock/subsys/postgresql ]
30 }
31
32 case "$1" in
33     start)
34         if [ "$PLC_DB_ENABLED" != "1" ] ; then
35             exit 0
36         fi
37
38         MESSAGE=$"Starting database server"
39         dialog "$MESSAGE"
40
41         # Set data directory and redirect startup output to /var/log/pgsql
42         mkdir -p /etc/sysconfig/pgsql
43         (
44             echo "PGDATA=$PGDATA"
45             echo "PGLOG=/var/log/pgsql"
46             echo "PGPORT=$PLC_DB_PORT"
47         ) >>/etc/sysconfig/pgsql/postgresql
48
49         # Fix ownership (rpm installation may have changed it)
50         chown -R -H postgres:postgres $(dirname $PGDATA)
51
52         # PostgreSQL must be started at least once to bootstrap
53         # /var/lib/pgsql/data
54         if [ ! -f $postgresql_conf ] ; then
55             postgresql_start
56             check
57             service postgresql stop
58             check
59         fi
60
61         # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
62         # PostgreSQL 7.x uses tcpip_socket.
63         if grep -q listen_addresses $postgresql_conf ; then
64             sed -i -e '/^listen_addresses/d' $postgresql_conf
65             echo "listen_addresses = '*'" >>$postgresql_conf
66         elif grep -q tcpip_socket $postgresql_conf ; then
67             sed -i -e '/^tcpip_socket/d' $postgresql_conf
68             echo "tcpip_socket = true" >>$postgresql_conf
69         fi
70
71         # Disable access to all DBs from all hosts
72         sed -i -e '/^\(host\|local\)/d' $pghba_conf
73
74         # Enable passwordless localhost access
75         echo "local all all trust" >>$pghba_conf
76
77         # Enable access from the API and web servers
78         PLC_API_IP=$(gethostbyname $PLC_API_HOST)
79         PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
80         (
81             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
82             echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
83         ) >>$pghba_conf
84
85         # Fix ownership (sed -i changes it)
86         chown postgres:postgres $postgresql_conf $pghba_conf
87
88         # Start up the server
89         postgresql_start
90         check
91
92         # Create/update the unprivileged database user and password
93         if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
94             psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
95         else
96             psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
97         fi
98
99         # Create the database if necessary
100         if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
101             createdb -U postgres $PLC_DB_NAME
102             psql -U $PLC_DB_USER -f /usr/share/pl_db/plc_schema_3.sql $PLC_DB_NAME
103         fi
104
105         result "$MESSAGE"
106         ;;
107
108     stop)
109         MESSAGE=$"Stopping database server"
110         dialog "$MESSAGE"
111
112         # Drop the current user in case the username changes
113         psql -U postgres -c "DROP USER $PLC_DB_USER" template1
114
115         # WARNING: If the DB name changes, the old DB will be left
116         # intact and a new one will be created. If it changes
117         # back, the old DB will not be re-created.
118
119         # Shut down the server
120         service postgresql stop
121         check
122
123         result "$MESSAGE"
124         ;;
125 esac
126
127 exit $ERRORS