- fix version number, bump release number, added changelog
[myplc.git] / plc.d / postgresql
index 2f46e3c..362cb5f 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.5 2006/07/10 21:09:24 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
@@ -89,6 +106,10 @@ 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