[rhmessaging-commits] rhmessaging commits: r3707 - mgmt/trunk/mint/bin.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Wed Nov 11 17:38:43 EST 2009


Author: justi9
Date: 2009-11-11 17:38:43 -0500 (Wed, 11 Nov 2009)
New Revision: 3707

Modified:
   mgmt/trunk/mint/bin/mint-database
Log:
Revamp mint-database; add a command to perform postgres configuration

Modified: mgmt/trunk/mint/bin/mint-database
===================================================================
--- mgmt/trunk/mint/bin/mint-database	2009-11-09 21:03:12 UTC (rev 3706)
+++ mgmt/trunk/mint/bin/mint-database	2009-11-11 22:38:43 UTC (rev 3707)
@@ -5,58 +5,189 @@
     exit 2
 fi
 
-function check-basics {
+pgdata="/var/lib/pgsql/data"
+pglog="${pgdata}/pg_log"
+pghbaconf="${pgdata}/pg_hba.conf"
+dbname="cumin"
+
+function check-environment {
+    which rpm > /dev/null
+    run "rpm -q postgresql-server"
+}
+
+function check-postgresql {
     # Is it installed?
     # Is it initialized?
     # Is it running?
 
-    which rpm > /dev/null
-    rpm -q postgresql-server
-
     test -d /var/lib/pgsql/data || {
-        echo "The database is not initialized; run '/sbin/service postgresql start'"
+        echo "The database is not initialized.  Run 'mint-database configure'."
         exit 1
     }
 
-    /sbin/service postgresql status || {
-        echo "The database is not running; run '/sbin/service postgresql start'"
+    run "/sbin/service postgresql status" || {
+        echo "The database is not running.  Run '/sbin/service postgresql start'."
         exit 1
     }
 }
 
+function confirmed {
+    while [[ "$confirm" != "yes" ]]; do
+        echo -n "Type 'yes' to proceed or [Ctrl-c] to exit: "
+        read confirm
+    done
+
+    return 0
+}
+
+function format-output {
+    while read line; do
+        echo " | $line"
+    done
+}
+
+function run {
+    echo " | \$ $1"
+
+    if [[ "$2" ]]; then
+        su - postgres -c "$1" | format-output 2>&1
+    else
+        $1 | format-output 2>&1
+    fi
+
+    return ${PIPESTATUS[0]}
+}
+
+function initdb {
+    run "initdb --pgdata='$pgdata' --auth='ident sameuser'" postgres
+    run "mkdir '$pglog'" postgres
+    run "chmod 700 '$pglog'" postgres
+
+    /sbin/restorecon -R "$pgdata"
+}
+
+function modify-postgresql-config {
+    python <<EOF
+import re
+
+comment_or_empty_line_pattern = re.compile('^\w*#|^\w*$')
+record_pattern = re.compile('^\w*(local|host|hostssl|hostnossl)')
+
+database_name = "cumin"
+path = "/var/lib/pgsql/data/pg_hba.conf"
+file = open(path, "r")
+
+lines = list()
+first_record_index = None
+
+for i, line in enumerate(file):
+    lines.append(line)
+
+    if record_pattern.match(line):
+        if first_record_index is None:
+            first_record_index = i
+
+        tokens = line.split()
+
+        if tokens[1] == database_name:
+            raise Exception("This file already contains a " + \
+                                "%s record" % database_name)
+    elif comment_or_empty_line_pattern.match(line):
+        pass
+    else:
+        raise Exception("This doesn't look like a pg_hba.conf file")
+
+file.close()
+
+if first_record_index is None:
+    first_record_index = len(lines)
+
+line = "host  %s  %s  ::1/128  trust\n" % (database_name, database_name)
+lines.insert(first_record_index, line)
+line = "host  %s  %s  127.0.0.1/32  trust\n" % (database_name, database_name)
+lines.insert(first_record_index, line)
+
+file = open(path, "w")
+
+for line in lines:
+    file.write(line)
+
+file.close()
+EOF
+
+    return $?
+}
+
 case "$1" in
-    check)
-        check-basics
+    status)
+        check-environment
+        check-postgresql
 
         # Is it configured to be accessible?
         # Is it accessible?
         # Does it have a schema loaded?
 
-        su - postgres -c "psql -c '\q'" || {
+        run "psql -c '\q'" postgres || {
             echo "The database is not accessible"
             exit 1
         }
 
         echo "The database is ready"
         ;;
+    configure)
+        check-environment
+
+        if test -f $pghbaconf && run "grep ${dbname} ${pghbaconf}"; then
+            echo "The database appears to have been configured already."
+            exit 1
+        fi
+
+        if run "/sbin/service postgresql status"; then
+            echo "The database is running.  To proceed with configuration, I need to stop it."
+
+            if confirmed; then
+                run "/sbin/service postgresql stop"
+            fi
+        fi
+
+        test -d /var/lib/pgsql/data || {
+            echo "The database is not initialized.  To proceed, I need to initialize it."
+
+            if confirmed; then
+                initdb
+            fi
+        }
+
+        modify-postgresql-config
+
+        echo "The database is configured.  You can now run 'mint-database create'."
+
+        # chkconfig stuff ?
+        ;;
     create)
-        check-basics
-        su - postgres -c "createuser --superuser cumin"
-        su - postgres -c "createdb --owner=cumin cumin"
-        mint-admin load-schema
-        echo "The database is created"
+        check-environment
+        check-postgresql
+
+        run "createuser --superuser ${dbname}" postgres
+        run "createdb --owner=${dbname} ${dbname}" postgres
+
+        echo "The database is created.  You can now run 'mint-admin load-schema'."
         ;;
     destroy)
-        check-basics
-        su - postgres -c "dropdb cumin"
-        su - postgres -c "dropuser cumin"
+        check-environment
+        check-postgresql
+
+        run "dropdb ${dbname}" postgres
+        run "dropuser ${dbname}" postgres
+
         echo "The database is destroyed"
         ;;
     *)
         echo "Configure and check the mint database"
         echo "Usage: mint-database COMMAND"
         echo "Commands:"
-        echo "    check       Check the database"
+        echo "    status      Check the database"
+        echo "    configure   Modify the database configuration"
         echo "    create      Create the mint user and database"
         echo "    destroy     Discard the mint user, database, and all data"
         exit 1



More information about the rhmessaging-commits mailing list