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