separate postgresql launching and checking
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 23 Nov 2011 17:59:42 +0000 (18:59 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 23 Nov 2011 17:59:42 +0000 (18:59 +0100)
plc.d/postgresql

index 93cac8c..9191c4d 100755 (executable)
@@ -20,17 +20,15 @@ set -x
 PGDATA=/var/lib/pgsql/data
 postgresql_conf=$PGDATA/postgresql.conf
 pghba_conf=$PGDATA/pg_hba.conf
+postgresql_sysconfig=/etc/sysconfig/pgsql
 
 # Export so that we do not have to specify -p to psql invocations
 export PGPORT=$PLC_DB_PORT
 
-# /etc/init.d/postgresql always returns 0, even on failure
-postgresql_start ()
-{
-    # start() always returns 0
-    (exec 3>&- 4>&- ; service postgresql start)
+# can't trust the return of service postgresql start / nor status
+function postgresql_check () {
 
-    # status() will still return 0 even while still initializing
+    # wait until postmaster is up and running - or 10s max
     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
@@ -44,12 +42,6 @@ postgresql_start ()
     return 1
 }
 
-postgresql_init ()
-{
-    service postgresql initdb &> /dev/null || :
-    postgresql_start
-}
-
 case "$1" in
     start)
        if [ "$PLC_DB_ENABLED" != "1" ] ; then
@@ -58,27 +50,31 @@ case "$1" in
 
        MESSAGE=$"Starting PostgreSQL server"
        dialog "$MESSAGE"
-
+       
+       ######## sysconfig 
        # Set data directory and redirect startup output to /var/log/pgsql
-       mkdir -p /etc/sysconfig/pgsql
-       (
+       mkdir -p $(dirname $postgresql_sysconfig)
+       touch $postgresql_sysconfig
+       tmp=${postgresql_sysconfig}.new
+       # remove any previous definitions and write ours
+       ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig 
            echo "PGDATA=$PGDATA"
            echo "PGLOG=/var/log/pgsql"
            echo "PGPORT=$PLC_DB_PORT"
-       ) >>/etc/sysconfig/pgsql/postgresql
+       ) > $tmp ; mv -f $tmp $postgresql_sysconfig
 
-       # Fix ownership (rpm installation may have changed it)
+       ######## /var/lib/pgsql/data 
+       # Fix ownership of /var/lib/pgsql (rpm installation may have changed it)
        chown -R -H postgres:postgres $(dirname $PGDATA)
 
        # PostgreSQL must be started at least once to bootstrap
        # /var/lib/pgsql/data
        if [ ! -f $postgresql_conf ] ; then
-           postgresql_init
-           check
-           service postgresql stop
+           service postgresql initdb &> /dev/null || :
            check
        fi
 
+       ######## /var/lib/pgsql/data/postgresql.conf
        # Enable DB server. drop Postgresql<=7.x
        # PostgreSQL >=8.0 defines listen_addresses
        # listen on a specific IP + localhost, more robust when run within a vserver
@@ -88,6 +84,7 @@ case "$1" in
        sed -i -e '/^timezone=/d' $postgresql_conf
        echo "timezone='UTC'" >> $postgresql_conf
 
+       ######## /var/lib/pgsql/data/pg_hba.conf
        # Disable access to all DBs from all hosts
        sed -i -e '/^\(host\|local\)/d' $pghba_conf
 
@@ -137,11 +134,12 @@ case "$1" in
        # Fix ownership (sed -i changes it)
        chown postgres:postgres $postgresql_conf $pghba_conf
 
-       # Start up the server
-       postgresql_start
+       ######## Start up the server - ignore retcod and check this our way
+       (exec 3>&- 4>&- ; service postgresql start)
+       postgresql_check
        check
 
-       # Create/update the unprivileged database user and password
+       ######## 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=$local_config $local_config
@@ -154,7 +152,7 @@ case "$1" in
        fi
        check
 
-       # Create the databases 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 --template=template0 --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