[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[dak/master] Allow getting db config out easily



Signed-off-by: Mark Hymers <mhy@debian.org>
---
 dak/admin.py |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/dak/admin.py b/dak/admin.py
index dbf1d45..2be3470 100755
--- a/dak/admin.py
+++ b/dak/admin.py
@@ -55,6 +55,10 @@ Perform administrative work on the dak database.
 
   Commands can use a long or abbreviated form:
 
+  config / c:
+     c db                   show db config
+     c db-shell             show db config in a usable form for psql
+
   architecture / a:
      a list                 show a list of architectures
      a rm ARCH              remove an architecture (will only work if
@@ -323,6 +327,47 @@ dispatch['s-a'] = suite_architecture
 
 ################################################################################
 
+def show_config(command):
+    args = [str(x) for x in command]
+    cnf = utils.get_conf()
+    d = DBConn()
+
+    die_arglen(args, 2, "E: config needs at least a command")
+
+    mode = args[1].lower()
+
+    if mode == 'db':
+        connstr = ""
+        if cnf["DB::Host"]:
+            # TCP/IP
+            connstr = "postgres://%s" % cnf["DB::Host"]
+            if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
+                connstr += ":%s" % cnf["DB::Port"]
+            connstr += "/%s" % cnf["DB::Name"]
+        else:
+            # Unix Socket
+            connstr = "postgres:///%s" % cnf["DB::Name"]
+            if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
+                connstr += "?port=%s" % cnf["DB::Port"]
+        print connstr
+    elif mode == 'db-shell':
+        e = ['PGDATABASE']
+        print "PGDATABASE=%s" % cnf["DB::Name"]
+        if cnf["DB::Host"]:
+            print "PGHOST=%s" % cnf["DB::Host"]
+            e.append('PGHOST')
+        if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
+            print "PGPORT=%s" % cnf["DB::Port"]
+            e.append('PGPORT')
+        print "export " + " ".join(e)
+    else:
+        die("E: config command unknown")
+
+dispatch['config'] = show_config
+dispatch['c'] = show_config
+
+################################################################################
+
 def main():
     """Perform administrative work on the dak database"""
     global dryrun
-- 
1.5.6.5



Reply to: