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 [ signingkey=SIGNINGKEY ]
84 add suite SUITE, version VERSION.
85 label, description, origin, codename
86 and signingkey are optional.
88 s add-all-arches SUITE VERSION... as "s add" but adds suite-architecture
89 relationships for all architectures
91 suite-architecture / s-a:
92 s-a list show the architectures for all suites
93 s-a list-suite ARCH show the suites an ARCH is in
94 s-a list-arch SUITE show the architectures in a SUITE
95 s-a add SUITE ARCH add ARCH to suite
96 s-a rm SUITE ARCH remove ARCH from suite (will only work if
97 no packages remain for the arch in the suite)
100 v-c list show version checks for all suites
101 v-c list-suite SUITE show version checks for suite SUITE
102 v-c add SUITE CHECK REFERENCE add a version check for suite SUITE
103 v-c rm SUITE CHECK REFERENCE remove a version check
105 CHECK is one of Enhances, MustBeNewerThan, MustBeOlderThan
106 REFERENCE is another suite name
110 ################################################################################
112 def __architecture_list(d, args):
113 q = d.session().query(Architecture).order_by('arch_string')
115 # HACK: We should get rid of source from the arch table
116 if j.arch_string == 'source': continue
120 def __architecture_add(d, args):
121 die_arglen(args, 4, "E: adding an architecture requires a name and a description")
122 print "Adding architecture %s" % args[2]
123 suites = [str(x) for x in args[4:]]
125 print "Adding to suites %s" % ", ".join(suites)
130 a.arch_string = str(args[2]).lower()
131 a.description = str(args[3])
134 su = get_suite(sn, s)
138 warn("W: Cannot find suite %s" % su)
140 except IntegrityError as e:
141 die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
142 except SQLAlchemyError as e:
143 die("E: Error adding architecture %s (%s)" % (args[2], e))
144 print "Architecture %s added" % (args[2])
146 def __architecture_rm(d, args):
147 die_arglen(args, 3, "E: removing an architecture requires at least a name")
148 print "Removing architecture %s" % args[2]
152 a = get_architecture(args[2].lower(), s)
154 die("E: Cannot find architecture %s" % args[2])
157 except IntegrityError as e:
158 die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
159 except SQLAlchemyError as e:
160 die("E: Error removing architecture %s (%s)" % (args[2], e))
161 print "Architecture %s removed" % args[2]
163 def architecture(command):
164 args = [str(x) for x in command]
165 Cnf = utils.get_conf()
168 die_arglen(args, 2, "E: architecture needs at least a command")
170 mode = args[1].lower()
172 __architecture_list(d, args)
174 __architecture_add(d, args)
176 __architecture_rm(d, args)
178 die("E: architecture command unknown")
180 dispatch['architecture'] = architecture
181 dispatch['a'] = architecture
183 ################################################################################
185 def __suite_list(d, args):
187 for j in s.query(Suite).order_by('suite_name').all():
190 def __suite_show(d, args):
192 die("E: showing an suite entry requires a suite")
195 su = get_suite(args[2].lower())
197 die("E: can't find suite entry for %s" % (args[2].lower()))
201 def __suite_add(d, args, addallarches=False):
202 die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
203 suite_name = args[2].lower()
207 def get_field(field):
209 if varval.startswith(field + '='):
210 return varval.split('=')[1]
213 print "Adding suite %s" % suite_name
218 suite.suite_name = suite_name
219 suite.overridecodename = suite_name
220 suite.version = version
221 suite.label = get_field('label')
222 suite.description = get_field('description')
223 suite.origin = get_field('origin')
224 suite.codename = get_field('codename')
225 signingkey = get_field('signingkey')
226 if signingkey is not None:
227 suite.signingkeys = [signingkey.upper()]
228 suite.srcformats = s.query(SrcFormat).all()
231 except IntegrityError as e:
232 die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
233 except SQLAlchemyError as e:
234 die("E: Error adding suite %s (%s)" % (suite_name, e))
235 print "Suite %s added" % (suite_name)
239 q = s.query(Architecture).order_by('arch_string')
241 suite.architectures.append(arch)
242 arches.append(arch.arch_string)
244 print "Architectures %s added to %s" % (','.join(arches), suite_name)
250 args = [str(x) for x in command]
251 Cnf = utils.get_conf()
254 die_arglen(args, 2, "E: suite needs at least a command")
256 mode = args[1].lower()
259 __suite_list(d, args)
261 __suite_show(d, args)
263 __suite_add(d, args, False)
264 elif mode == 'add-all-arches':
265 __suite_add(d, args, True)
267 die("E: suite command unknown")
269 dispatch['suite'] = suite
270 dispatch['s'] = suite
272 ################################################################################
274 def __suite_architecture_list(d, args):
276 for j in s.query(Suite).order_by('suite_name'):
277 architectures = j.get_architectures(skipsrc = True, skipall = True)
278 print j.suite_name + ': ' + \
279 ', '.join([a.arch_string for a in architectures])
281 def __suite_architecture_listarch(d, args):
282 die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
283 suite = get_suite(args[2].lower(), d.session())
285 die('E: suite %s is invalid' % args[2].lower())
286 a = suite.get_architectures(skipsrc = True, skipall = True)
291 def __suite_architecture_listsuite(d, args):
292 die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
293 architecture = get_architecture(args[2].lower(), d.session())
294 if architecture is None:
295 die("E: architecture %s is invalid" % args[2].lower())
296 for j in architecture.suites:
300 def __suite_architecture_add(d, args):
302 die("E: adding a suite-architecture entry requires a suite and arch")
306 suite = get_suite(args[2].lower(), s)
307 if suite is None: die("E: Can't find suite %s" % args[2].lower())
309 arch = get_architecture(args[3].lower(), s)
310 if arch is None: die("E: Can't find architecture %s" % args[3].lower())
314 suite.architectures.append(arch)
316 except IntegrityError as e:
317 die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
318 except SQLAlchemyError as e:
319 die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
321 print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
324 def __suite_architecture_rm(d, args):
326 die("E: removing an suite-architecture entry requires a suite and arch")
331 suite_name = args[2].lower()
332 suite = get_suite(suite_name, s)
334 die('E: no such suite %s' % suite_name)
335 arch_string = args[3].lower()
336 architecture = get_architecture(arch_string, s)
337 if architecture not in suite.architectures:
338 die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
339 suite.architectures.remove(architecture)
341 except IntegrityError as e:
342 die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
343 except SQLAlchemyError as e:
344 die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
346 print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
349 def suite_architecture(command):
350 args = [str(x) for x in command]
351 Cnf = utils.get_conf()
354 die_arglen(args, 2, "E: suite-architecture needs at least a command")
356 mode = args[1].lower()
359 __suite_architecture_list(d, args)
360 elif mode == 'list-arch':
361 __suite_architecture_listarch(d, args)
362 elif mode == 'list-suite':
363 __suite_architecture_listsuite(d, args)
365 __suite_architecture_add(d, args)
367 __suite_architecture_rm(d, args)
369 die("E: suite-architecture command unknown")
371 dispatch['suite-architecture'] = suite_architecture
372 dispatch['s-a'] = suite_architecture
374 ################################################################################
376 def __version_check_list(d):
377 session = d.session()
378 for s in session.query(Suite).order_by('suite_name'):
379 __version_check_list_suite(d, s.suite_name)
381 def __version_check_list_suite(d, suite_name):
382 vcs = get_version_checks(suite_name)
384 print "%s %s %s" % (suite_name, vc.check, vc.reference.suite_name)
386 def __version_check_add(d, suite_name, check, reference_name):
387 suite = get_suite(suite_name)
389 die("E: Could not find suite %s." % (suite_name))
390 reference = get_suite(reference_name)
392 die("E: Could not find reference suite %s." % (reference_name))
394 session = d.session()
398 vc.reference = reference
402 def __version_check_rm(d, suite_name, check, reference_name):
403 suite = get_suite(suite_name)
405 die("E: Could not find suite %s." % (suite_name))
406 reference = get_suite(reference_name)
408 die("E: Could not find reference suite %s." % (reference_name))
410 session = d.session()
412 vc = session.query(VersionCheck).filter_by(suite=suite, check=check, reference=reference).one()
415 except NoResultFound:
416 print "W: version-check not found."
418 def version_check(command):
419 args = [str(x) for x in command]
420 Cnf = utils.get_conf()
423 die_arglen(args, 2, "E: version-check needs at least a command")
424 mode = args[1].lower()
427 __version_check_list(d)
428 elif mode == 'list-suite':
430 die("E: version-check list-suite needs a single parameter")
431 __version_check_list_suite(d, args[2])
434 die("E: version-check add needs three parameters")
435 __version_check_add(d, args[2], args[3], args[4])
438 die("E: version-check rm needs three parameters")
439 __version_check_rm(d, args[2], args[3], args[4])
441 die("E: version-check command unknown")
443 dispatch['version-check'] = version_check
444 dispatch['v-c'] = version_check
446 ################################################################################
448 def show_config(command):
449 args = [str(x) for x in command]
450 cnf = utils.get_conf()
452 die_arglen(args, 2, "E: config needs at least a command")
454 mode = args[1].lower()
458 if cnf.has_key("DB::Service"):
460 connstr = "postgresql://service=%s" % cnf["DB::Service"]
461 elif cnf.has_key("DB::Host"):
463 connstr = "postgres://%s" % cnf["DB::Host"]
464 if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
465 connstr += ":%s" % cnf["DB::Port"]
466 connstr += "/%s" % cnf["DB::Name"]
469 connstr = "postgres:///%s" % cnf["DB::Name"]
470 if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
471 connstr += "?port=%s" % cnf["DB::Port"]
473 elif mode == 'db-shell':
475 if cnf.has_key("DB::Service"):
476 e.append('PGSERVICE')
477 print "PGSERVICE=%s" % cnf["DB::Service"]
478 if cnf.has_key("DB::Name"):
479 e.append('PGDATABASE')
480 print "PGDATABASE=%s" % cnf["DB::Name"]
481 if cnf.has_key("DB::Host"):
482 print "PGHOST=%s" % cnf["DB::Host"]
484 if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
485 print "PGPORT=%s" % cnf["DB::Port"]
487 print "export " + " ".join(e)
489 session = DBConn().session()
491 o = session.query(DBConfig).filter_by(name = mode).one()
493 except NoResultFound:
494 print "W: option '%s' not set" % mode
496 dispatch['config'] = show_config
497 dispatch['c'] = show_config
499 ################################################################################
501 def show_keyring(command):
502 args = [str(x) for x in command]
503 cnf = utils.get_conf()
505 die_arglen(args, 2, "E: keyring needs at least a command")
507 mode = args[1].lower()
511 q = d.session().query(Keyring).filter(Keyring.active == True)
513 if mode == 'list-all':
515 elif mode == 'list-binary':
516 q = q.filter(Keyring.default_source_acl_id == None)
517 elif mode == 'list-source':
518 q = q.filter(Keyring.default_source_acl_id != None)
520 die("E: keyring command unknown")
525 dispatch['keyring'] = show_keyring
526 dispatch['k'] = show_keyring
528 ################################################################################
531 """Perform administrative work on the dak database"""
533 Cnf = utils.get_conf()
534 arguments = [('h', "help", "Admin::Options::Help"),
535 ('n', "dry-run", "Admin::Options::Dry-Run")]
536 for i in [ "help", "dry-run" ]:
537 if not Cnf.has_key("Admin::Options::%s" % (i)):
538 Cnf["Admin::Options::%s" % (i)] = ""
540 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
542 options = Cnf.SubTree("Admin::Options")
543 if options["Help"] or len(arguments) < 1:
545 if options["Dry-Run"]:
548 subcommand = str(arguments[0])
550 if subcommand in dispatch.keys():
551 dispatch[subcommand](arguments)
553 die("E: Unknown command")
555 ################################################################################
557 if __name__ == '__main__':