3 """Configure dak parameters in the database"""
4 # Copyright (C) 2009 Mark Hymers <mhy@debian.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ################################################################################
26 from daklib import utils
27 from daklib.dbconn import *
28 from sqlalchemy.orm.exc import NoResultFound
30 ################################################################################
35 ################################################################################
37 print >> sys.stderr, msg
39 def die(msg, exit_code=1):
40 print >> sys.stderr, msg
43 def die_arglen(args, args_needed, msg):
44 if len(args) < args_needed:
47 def usage(exit_code=0):
48 """Perform administrative work on the dak database."""
50 print """Usage: dak admin COMMAND
51 Perform administrative work on the dak database.
53 -h, --help show this help and exit.
54 -n, --dry-run don't do anything, just show what would have been done
55 (only applies to add or rm operations).
57 Commands can use a long or abbreviated form:
61 c db-shell show db config in a usable form for psql
62 c NAME show option NAME as set in configuration table
65 k list-all list all keyrings
66 k list-binary list all keyrings with a NULL source acl
67 k list-source list all keyrings with a non NULL source acl
70 a list show a list of architectures
71 a rm ARCH remove an architecture (will only work if
72 no longer linked to any suites)
73 a add ARCH DESCRIPTION [SUITELIST]
74 add architecture ARCH with DESCRIPTION.
75 If SUITELIST is given, add to each of the
76 suites at the same time
79 s list show a list of suites
80 s show SUITE show config details for a suite
81 s add SUITE VERSION [ label=LABEL ] [ description=DESCRIPTION ]
82 [ origin=ORIGIN ] [ codename=CODENAME ]
83 add suite SUITE, version VERSION. label,
84 description, origin and codename are optional.
86 suite-architecture / s-a:
87 s-a list show the architectures for all suites
88 s-a list-suite ARCH show the suites an ARCH is in
89 s-a list-arch SUITE show the architectures in a SUITE
90 s-a add SUITE ARCH add ARCH to suite
91 s-a rm SUITE ARCH remove ARCH from suite (will only work if
92 no packages remain for the arch in the suite)
95 v-c list show version checks for all suites
96 v-c list-suite SUITE show version checks for suite SUITE
97 v-c add SUITE CHECK REFERENCE add a version check for suite SUITE
98 v-c rm SUITE CHECK REFERENCE rmove a version check
100 CHECK is one of Enhances, MustBeNewerThan, MustBeOlderThan
101 REFERENCE is another suite name
105 ################################################################################
107 def __architecture_list(d, args):
108 q = d.session().query(Architecture).order_by('arch_string')
110 # HACK: We should get rid of source from the arch table
111 if j.arch_string == 'source': continue
115 def __architecture_add(d, args):
116 die_arglen(args, 4, "E: adding an architecture requires a name and a description")
117 print "Adding architecture %s" % args[2]
118 suites = [str(x) for x in args[4:]]
120 print "Adding to suites %s" % ", ".join(suites)
125 a.arch_string = str(args[2]).lower()
126 a.description = str(args[3])
129 su = get_suite(sn, s)
133 warn("W: Cannot find suite %s" % su)
135 except IntegrityError, e:
136 die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
137 except SQLAlchemyError, e:
138 die("E: Error adding architecture %s (%s)" % (args[2], e))
139 print "Architecture %s added" % (args[2])
141 def __architecture_rm(d, args):
142 die_arglen(args, 3, "E: removing an architecture requires at least a name")
143 print "Removing architecture %s" % args[2]
147 a = get_architecture(args[2].lower(), s)
149 die("E: Cannot find architecture %s" % args[2])
152 except IntegrityError, e:
153 die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
154 except SQLAlchemyError, e:
155 die("E: Error removing architecture %s (%s)" % (args[2], e))
156 print "Architecture %s removed" % args[2]
158 def architecture(command):
159 args = [str(x) for x in command]
160 Cnf = utils.get_conf()
163 die_arglen(args, 2, "E: architecture needs at least a command")
165 mode = args[1].lower()
167 __architecture_list(d, args)
169 __architecture_add(d, args)
171 __architecture_rm(d, args)
173 die("E: architecture command unknown")
175 dispatch['architecture'] = architecture
176 dispatch['a'] = architecture
178 ################################################################################
180 def __suite_list(d, args):
182 for j in s.query(Suite).order_by('suite_name').all():
185 def __suite_show(d, args):
187 die("E: showing an suite entry requires a suite")
190 su = get_suite(args[2].lower())
192 die("E: can't find suite entry for %s" % (args[2].lower()))
196 def __suite_add(d, args):
197 die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
198 suite_name = args[2].lower()
202 def get_field(field):
204 if varval.startswith(field + '='):
205 return varval.split('=')[1]
208 print "Adding suite %s" % suite_name
213 suite.suite_name = suite_name
214 suite.version = version
215 suite.label = get_field('label')
216 suite.description = get_field('description')
217 suite.origin = get_field('origin')
218 suite.codename = get_field('codename')
221 except IntegrityError, e:
222 die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
223 except SQLAlchemyError, e:
224 die("E: Error adding suite %s (%s)" % (suite_name, e))
225 print "Suite %s added" % (suite_name)
228 args = [str(x) for x in command]
229 Cnf = utils.get_conf()
232 die_arglen(args, 2, "E: suite needs at least a command")
234 mode = args[1].lower()
237 __suite_list(d, args)
239 __suite_show(d, args)
243 die("E: suite command unknown")
245 dispatch['suite'] = suite
246 dispatch['s'] = suite
248 ################################################################################
250 def __suite_architecture_list(d, args):
252 for j in s.query(Suite).order_by('suite_name'):
253 architectures = j.get_architectures(skipsrc = True, skipall = True)
254 print j.suite_name + ': ' + \
255 ', '.join([a.arch_string for a in architectures])
257 def __suite_architecture_listarch(d, args):
258 die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
259 suite = get_suite(args[2].lower(), d.session())
261 die('E: suite %s is invalid' % args[2].lower())
262 a = suite.get_architectures(skipsrc = True, skipall = True)
267 def __suite_architecture_listsuite(d, args):
268 die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
269 architecture = get_architecture(args[2].lower(), d.session())
270 if architecture is None:
271 die("E: architecture %s is invalid" % args[2].lower())
272 for j in architecture.suites:
276 def __suite_architecture_add(d, args):
278 die("E: adding a suite-architecture entry requires a suite and arch")
282 suite = get_suite(args[2].lower(), s)
283 if suite is None: die("E: Can't find suite %s" % args[2].lower())
285 arch = get_architecture(args[3].lower(), s)
286 if arch is None: die("E: Can't find architecture %s" % args[3].lower())
290 suite.architectures.append(arch)
292 except IntegrityError, e:
293 die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
294 except SQLAlchemyError, e:
295 die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
297 print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
300 def __suite_architecture_rm(d, args):
302 die("E: removing an suite-architecture entry requires a suite and arch")
307 suite_name = args[2].lower()
308 suite = get_suite(suite_name, s)
310 die('E: no such suite %s' % suite_name)
311 arch_string = args[3].lower()
312 architecture = get_architecture(arch_string, s)
313 if architecture not in suite.architectures:
314 die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
315 suite.architectures.remove(architecture)
317 except IntegrityError, e:
318 die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
319 except SQLAlchemyError, e:
320 die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
322 print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
325 def suite_architecture(command):
326 args = [str(x) for x in command]
327 Cnf = utils.get_conf()
330 die_arglen(args, 2, "E: suite-architecture needs at least a command")
332 mode = args[1].lower()
335 __suite_architecture_list(d, args)
336 elif mode == 'list-arch':
337 __suite_architecture_listarch(d, args)
338 elif mode == 'list-suite':
339 __suite_architecture_listsuite(d, args)
341 __suite_architecture_add(d, args)
343 __suite_architecture_rm(d, args)
345 die("E: suite-architecture command unknown")
347 dispatch['suite-architecture'] = suite_architecture
348 dispatch['s-a'] = suite_architecture
350 ################################################################################
352 def __version_check_list(d):
353 session = d.session()
354 for s in session.query(Suite).order_by('suite_name'):
355 __version_check_list_suite(d, s.suite_name)
357 def __version_check_list_suite(d, suite_name):
358 vcs = get_version_checks(suite_name)
360 print "%s %s %s" % (suite_name, vc.check, vc.reference.suite_name)
362 def __version_check_add(d, suite_name, check, reference_name):
363 suite = get_suite(suite_name)
365 die("E: Could not find suite %s." % (suite_name))
366 reference = get_suite(reference_name)
368 die("E: Could not find reference suite %s." % (reference_name))
370 session = d.session()
374 vc.reference = reference
378 def __version_check_rm(d, suite_name, check, reference_name):
379 suite = get_suite(suite_name)
381 die("E: Could not find suite %s." % (suite_name))
382 reference = get_suite(reference_name)
384 die("E: Could not find reference suite %s." % (reference_name))
386 session = d.session()
388 vc = session.query(VersionCheck).filter_by(suite=suite, check=check, reference=reference).one()
391 except NoResultFound:
392 print "W: version-check not found."
394 def version_check(command):
395 args = [str(x) for x in command]
396 Cnf = utils.get_conf()
399 die_arglen(args, 2, "E: version-check needs at least a command")
400 mode = args[1].lower()
403 __version_check_list(d)
404 elif mode == 'list-suite':
406 die("E: version-check list-suite needs a single parameter")
407 __version_check_list_suite(d, args[2])
410 die("E: version-check add needs three parameters")
411 __version_check_add(d, args[2], args[3], args[4])
414 die("E: version-check rm needs three parameters")
415 __version_check_rm(d, args[2], args[3], args[4])
417 die("E: version-check command unknown")
419 dispatch['version-check'] = version_check
420 dispatch['v-c'] = version_check
422 ################################################################################
424 def show_config(command):
425 args = [str(x) for x in command]
426 cnf = utils.get_conf()
428 die_arglen(args, 2, "E: config needs at least a command")
430 mode = args[1].lower()
434 if cnf.has_key("DB::Service"):
436 connstr = "postgresql://service=%s" % cnf["DB::Service"]
437 elif cnf.has_key("DB::Host"):
439 connstr = "postgres://%s" % cnf["DB::Host"]
440 if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
441 connstr += ":%s" % cnf["DB::Port"]
442 connstr += "/%s" % cnf["DB::Name"]
445 connstr = "postgres:///%s" % cnf["DB::Name"]
446 if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
447 connstr += "?port=%s" % cnf["DB::Port"]
449 elif mode == 'db-shell':
451 if cnf.has_key("DB::Service"):
452 e.append('PGSERVICE')
453 print "PGSERVICE=%s" % cnf["DB::Service"]
454 if cnf.has_key("DB::Name"):
455 e.append('PGDATABASE')
456 print "PGDATABASE=%s" % cnf["DB::Name"]
457 if cnf.has_key("DB::Host"):
458 print "PGHOST=%s" % cnf["DB::Host"]
460 if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
461 print "PGPORT=%s" % cnf["DB::Port"]
463 print "export " + " ".join(e)
465 session = DBConn().session()
467 o = session.query(DBConfig).filter_by(name = mode).one()
469 except NoResultFound:
470 print "W: option '%s' not set" % mode
472 dispatch['config'] = show_config
473 dispatch['c'] = show_config
475 ################################################################################
477 def show_keyring(command):
478 args = [str(x) for x in command]
479 cnf = utils.get_conf()
481 die_arglen(args, 2, "E: keyring needs at least a command")
483 mode = args[1].lower()
487 q = d.session().query(Keyring).filter(Keyring.active == True)
489 if mode == 'list-all':
491 elif mode == 'list-binary':
492 q = q.filter(Keyring.default_source_acl_id == None)
493 elif mode == 'list-source':
494 q = q.filter(Keyring.default_source_acl_id != None)
496 die("E: keyring command unknown")
501 dispatch['keyring'] = show_keyring
502 dispatch['k'] = show_keyring
504 ################################################################################
507 """Perform administrative work on the dak database"""
509 Cnf = utils.get_conf()
510 arguments = [('h', "help", "Admin::Options::Help"),
511 ('n', "dry-run", "Admin::Options::Dry-Run")]
512 for i in [ "help", "dry-run" ]:
513 if not Cnf.has_key("Admin::Options::%s" % (i)):
514 Cnf["Admin::Options::%s" % (i)] = ""
516 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
518 options = Cnf.SubTree("Admin::Options")
519 if options["Help"] or len(arguments) < 1:
521 if options["Dry-Run"]:
524 subcommand = str(arguments[0])
526 if subcommand in dispatch.keys():
527 dispatch[subcommand](arguments)
529 die("E: Unknown command")
531 ################################################################################
533 if __name__ == '__main__':