- set up cron on a per-server basis
[myplc.git] / plc.d / postgresql
index 2f46e3c..d2dbda3 100755 (executable)
@@ -7,11 +7,15 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: guest.init,v 1.12 2006/04/04 22:09:47 mlhuang Exp $
+# $Id: postgresql,v 1.7 2006/11/16 18:50:32 mlhuang Exp $
 #
 
 # Source function library and configuration
 . /etc/plc.d/functions
+. /etc/planetlab/plc_config
+
+# Be verbose
+set -x
 
 # Default locations
 PGDATA=/var/lib/pgsql/data
@@ -24,8 +28,21 @@ export PGPORT=$PLC_DB_PORT
 # /etc/init.d/postgresql always returns 0, even on failure
 postgresql_start ()
 {
+    # start() always returns 0
     service postgresql start
-    status postmaster && [ -f /var/lock/subsys/postgresql ]
+
+    # status() will still return 0 even while still initializing
+    if status postmaster && [ -f /var/lock/subsys/postgresql ] ; then
+       # The only way we can be sure is if we can access it
+       for i in $(seq 1 10) ; do
+           # Must do this as the postgres user initially (before we
+           # fix pg_hba.conf to passwordless localhost access).
+           su -c 'psql -U postgres -c "" template1' postgres && return 0
+           sleep 1
+       done
+    fi
+
+    return 1
 }
 
 case "$1" in
@@ -79,6 +96,8 @@ case "$1" in
        (
            echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
            echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
+           # Drupal also uses PostgreSQL
+           echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
        ) >>$pghba_conf
 
        # Fix ownership (sed -i changes it)
@@ -89,17 +108,28 @@ case "$1" in
        check
 
        # Create/update the unprivileged database user and password
+       if [ -z "$PLC_DB_PASSWORD" ] ; then
+           PLC_DB_PASSWORD=$(uuidgen)
+           plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save
+       fi
        if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
            psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
        else
            psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
        fi
+       check
 
-       # Create the database if necessary
+       # Create the databases if necessary
        if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
-           createdb -U postgres $PLC_DB_NAME
-           psql -U $PLC_DB_USER -f /usr/share/pl_db/plc_schema_3.sql $PLC_DB_NAME
+           createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
+           psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
        fi
+       check
+       if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
+           createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER drupal
+           psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal
+       fi
+       check
 
        result "$MESSAGE"
        ;;
@@ -117,7 +147,9 @@ case "$1" in
 
        # Shut down the server
        service postgresql stop
-       check
+
+       # /etc/init.d/postgresql fails if it is not running
+       [ "$PLC_DB_ENABLED" = 1 ] && check
 
        result "$MESSAGE"
        ;;