]> git.decadent.org.uk Git - dak.git/blobdiff - dak/admin.py
Make admin.py more robust.
[dak.git] / dak / admin.py
index 2bf6161724d93f2b3f743170d0e1a7513886c675..316b62b54d36da67b53a55c142ca3b75872b1f4c 100755 (executable)
@@ -25,7 +25,6 @@ import apt_pkg
 
 from daklib import utils
 from daklib.dbconn import *
-from daklib.config import Config
 
 ################################################################################
 
@@ -56,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
@@ -65,6 +68,14 @@ Perform administrative work on the dak database.
                             If SUITELIST is given, add to each of the
                             suites at the same time
 
+  suite / s:
+     s list                 show a list of suites
+     s show SUITE           show config details for a suite
+     s add SUITE VERSION [ label=LABEL ] [ description=DESCRIPTION ]
+                         [ origin=ORIGIN ] [ codename=CODENAME ]
+                            add suite SUITE, version VERSION. label,
+                            description, origin and codename are optional.
+
   suite-architecture / s-a:
      s-a list-suite ARCH    show the suites an ARCH is in
      s-a list-arch SUITE    show the architectures in a SUITE
@@ -85,25 +96,22 @@ def __architecture_list(d, args):
     sys.exit(0)
 
 def __architecture_add(d, args):
-    die_arglen(args, 3, "E: adding an architecture requires a name and a description")
+    die_arglen(args, 4, "E: adding an architecture requires a name and a description")
     print "Adding architecture %s" % args[2]
     suites = [str(x) for x in args[4:]]
-    print suites
+    if len(suites) > 0:
+        print "Adding to suites %s" % ", ".join(suites)
     if not dryrun:
         try:
             s = d.session()
-            s.begin()
             a = Architecture()
             a.arch_string = str(args[2]).lower()
             a.description = str(args[3])
             s.add(a)
             for sn in suites:
-                su = get_suite(sn ,s)
+                su = get_suite(sns)
                 if su is not None:
-                    archsu = SuiteArchitecture()
-                    archsu.arch_id = a.arch_id
-                    archsu.suite_id = su.suite_id
-                    s.add(archsu)
+                    a.suites.append(su)
                 else:
                     warn("W: Cannot find suite %s" % su)
             s.commit()
@@ -119,7 +127,6 @@ def __architecture_rm(d, args):
     if not dryrun:
         try:
             s = d.session()
-            s.begin()
             a = get_architecture(args[2].lower(), s)
             if a is None:
                 die("E: Cannot find architecture %s" % args[2])
@@ -153,18 +160,98 @@ dispatch['a'] = architecture
 
 ################################################################################
 
+def __suite_list(d, args):
+    s = d.session()
+    for j in s.query(Suite).order_by('suite_name').all():
+        print j.suite_name
+
+def __suite_show(d, args):
+    if len(args) < 2:
+        die("E: showing an suite entry requires a suite")
+
+    s = d.session()
+    su = get_suite(args[2].lower())
+    if su is None:
+        die("E: can't find suite entry for %s" % (args[2].lower()))
+
+    print su.details()
+
+def __suite_add(d, args):
+    die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
+    suite_name = args[2].lower()
+    version = args[3]
+    rest = args[3:]
+
+    def get_field(field):
+        for varval in args:
+            if varval.startswith(field + '='):
+                return varval.split('=')[1]
+        return None
+
+    print "Adding suite %s" % suite_name
+    if not dryrun:
+        try:
+            s = d.session()
+            suite = Suite()
+            suite.suite_name = suite_name
+            suite.version = version
+            suite.label = get_field('label')
+            suite.description = get_field('description')
+            suite.origin = get_field('origin')
+            suite.codename = get_field('codename')
+            s.add(suite)
+            s.commit()
+        except IntegrityError, e:
+            die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
+        except SQLAlchemyError, e:
+            die("E: Error adding suite %s (%s)" % (suite_name, e))
+    print "Suite %s added" % (suite_name)
+
+def suite(command):
+    args = [str(x) for x in command]
+    Cnf = utils.get_conf()
+    d = DBConn()
+
+    die_arglen(args, 2, "E: suite needs at least a command")
+
+    mode = args[1].lower()
+
+    if mode == 'list':
+        __suite_list(d, args)
+    elif mode == 'show':
+        __suite_show(d, args)
+    elif mode == 'add':
+        __suite_add(d, args)
+    else:
+        die("E: suite command unknown")
+
+dispatch['suite'] = suite
+dispatch['s'] = suite
+
+################################################################################
+
+def __suite_architecture_list(d, args):
+    s = d.session()
+    for j in s.query(Suite).order_by('suite_name').all():
+        print j.suite_name + ' ' + \
+              ','.join([a.architecture.arch_string for a in j.suitearchitectures])
+
 def __suite_architecture_listarch(d, args):
     die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
-    a = get_suite_architectures(args[2].lower())
+    suite = get_suite(args[2].lower(), d.session())
+    if suite is None:
+        die('E: suite %s is invalid' % args[2].lower())
+    a = suite.get_architectures(skipsrc = True, skipall = True)
     for j in a:
-        # HACK: We should get rid of source from the arch table
-        if j.arch_string != 'source':
-            print j.arch_string
+        print j.arch_string
 
 
 def __suite_architecture_listsuite(d, args):
     die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
-    for j in get_architecture_suites(args[2].lower()):
+    architecture = get_architecture(args[2].lower(), d.session())
+    if architecture is None:
+        die("E: architecture %s is invalid" % args[2].lower())
+    for j in architecture.suites:
         print j.suite_name
 
 
@@ -182,11 +269,7 @@ def __suite_architecture_add(d, args):
 
     if not dryrun:
         try:
-            s.begin()
-            sa = SuiteArchitecture()
-            sa.arch_id = arch.arch_id
-            sa.suite_id = suite.suite_id
-            s.add(sa)
+            suite.architectures.append(arch)
             s.commit()
         except IntegrityError, e:
             die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
@@ -203,18 +286,22 @@ def __suite_architecture_rm(d, args):
     s = d.session()
     if not dryrun:
         try:
-            s.begin()
-            sa = get_suite_architecture(args[2].lower(), args[3].lower(), s)
-            if sa is None:
-                die("E: can't find suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower()))
-            s.delete(sa)
+            suite_name = args[2].lower()
+            suite = get_suite(suite_name, s)
+            if suite is None:
+                die('E: no such suite %s' % suite_name)
+            arch_string = args[3].lower()
+            architecture = get_architecture(arch_string, s)
+            if architecture not in suite.architectures:
+                die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
+            suite.architectures.remove(architecture)
             s.commit()
         except IntegrityError, e:
             die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
         except SQLAlchemyError, e:
             die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
 
-    print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower()) 
+    print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
 
 
 def suite_architecture(command):
@@ -226,7 +313,9 @@ def suite_architecture(command):
 
     mode = args[1].lower()
 
-    if mode == 'list-arch':
+    if mode == 'list':
+        __suite_architecture_list(d, args)
+    elif mode == 'list-arch':
         __suite_architecture_listarch(d, args)
     elif mode == 'list-suite':
         __suite_architecture_listsuite(d, args)
@@ -242,6 +331,46 @@ dispatch['s-a'] = suite_architecture
 
 ################################################################################
 
+def show_config(command):
+    args = [str(x) for x in command]
+    cnf = utils.get_conf()
+
+    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