]> git.decadent.org.uk Git - dak.git/blob - dak/admin.py
Merge remote branch 'mhy/master'
[dak.git] / dak / admin.py
1 #!/usr/bin/env python
2
3 """Configure dak parameters in the database"""
4 # Copyright (C) 2009  Mark Hymers <mhy@debian.org>
5
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.
10
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.
15
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
19
20 ################################################################################
21
22 import sys
23
24 import apt_pkg
25
26 from daklib import utils
27 from daklib.dbconn import *
28 from sqlalchemy.orm.exc import NoResultFound
29
30 ################################################################################
31
32 dispatch = {}
33 dryrun = False
34
35 ################################################################################
36 def warn(msg):
37     print >> sys.stderr, msg
38
39 def die(msg, exit_code=1):
40     print >> sys.stderr, msg
41     sys.exit(exit_code)
42
43 def die_arglen(args, args_needed, msg):
44     if len(args) < args_needed:
45         die(msg)
46
47 def usage(exit_code=0):
48     """Perform administrative work on the dak database."""
49
50     print """Usage: dak admin COMMAND
51 Perform administrative work on the dak database.
52
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).
56
57   Commands can use a long or abbreviated form:
58
59   config / c:
60      c db                   show db config
61      c db-shell             show db config in a usable form for psql
62      c NAME                 show option NAME as set in configuration table
63
64   keyring / k:
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
68
69   architecture / a:
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
77
78   suite / s:
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.
85
86      s add-all-arches SUITE VERSION... as "s add" but adds suite-architecture
87                             relationships for all architectures
88
89   suite-architecture / s-a:
90      s-a list               show the architectures for all suites
91      s-a list-suite ARCH    show the suites an ARCH is in
92      s-a list-arch SUITE    show the architectures in a SUITE
93      s-a add SUITE ARCH     add ARCH to suite
94      s-a rm SUITE ARCH      remove ARCH from suite (will only work if
95                             no packages remain for the arch in the suite)
96
97   version-check / v-c:
98      v-c list                        show version checks for all suites
99      v-c list-suite SUITE            show version checks for suite SUITE
100      v-c add SUITE CHECK REFERENCE   add a version check for suite SUITE
101      v-c rm SUITE CHECK REFERENCE    rmove a version check
102        where
103          CHECK     is one of Enhances, MustBeNewerThan, MustBeOlderThan
104          REFERENCE is another suite name
105 """
106     sys.exit(exit_code)
107
108 ################################################################################
109
110 def __architecture_list(d, args):
111     q = d.session().query(Architecture).order_by('arch_string')
112     for j in q.all():
113         # HACK: We should get rid of source from the arch table
114         if j.arch_string == 'source': continue
115         print j.arch_string
116     sys.exit(0)
117
118 def __architecture_add(d, args):
119     die_arglen(args, 4, "E: adding an architecture requires a name and a description")
120     print "Adding architecture %s" % args[2]
121     suites = [str(x) for x in args[4:]]
122     if len(suites) > 0:
123         print "Adding to suites %s" % ", ".join(suites)
124     if not dryrun:
125         try:
126             s = d.session()
127             a = Architecture()
128             a.arch_string = str(args[2]).lower()
129             a.description = str(args[3])
130             s.add(a)
131             for sn in suites:
132                 su = get_suite(sn, s)
133                 if su is not None:
134                     a.suites.append(su)
135                 else:
136                     warn("W: Cannot find suite %s" % su)
137             s.commit()
138         except IntegrityError, e:
139             die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
140         except SQLAlchemyError, e:
141             die("E: Error adding architecture %s (%s)" % (args[2], e))
142     print "Architecture %s added" % (args[2])
143
144 def __architecture_rm(d, args):
145     die_arglen(args, 3, "E: removing an architecture requires at least a name")
146     print "Removing architecture %s" % args[2]
147     if not dryrun:
148         try:
149             s = d.session()
150             a = get_architecture(args[2].lower(), s)
151             if a is None:
152                 die("E: Cannot find architecture %s" % args[2])
153             s.delete(a)
154             s.commit()
155         except IntegrityError, e:
156             die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
157         except SQLAlchemyError, e:
158             die("E: Error removing architecture %s (%s)" % (args[2], e))
159     print "Architecture %s removed" % args[2]
160
161 def architecture(command):
162     args = [str(x) for x in command]
163     Cnf = utils.get_conf()
164     d = DBConn()
165
166     die_arglen(args, 2, "E: architecture needs at least a command")
167
168     mode = args[1].lower()
169     if mode == 'list':
170         __architecture_list(d, args)
171     elif mode == 'add':
172         __architecture_add(d, args)
173     elif mode == 'rm':
174         __architecture_rm(d, args)
175     else:
176         die("E: architecture command unknown")
177
178 dispatch['architecture'] = architecture
179 dispatch['a'] = architecture
180
181 ################################################################################
182
183 def __suite_list(d, args):
184     s = d.session()
185     for j in s.query(Suite).order_by('suite_name').all():
186         print j.suite_name
187
188 def __suite_show(d, args):
189     if len(args) < 2:
190         die("E: showing an suite entry requires a suite")
191
192     s = d.session()
193     su = get_suite(args[2].lower())
194     if su is None:
195         die("E: can't find suite entry for %s" % (args[2].lower()))
196
197     print su.details()
198
199 def __suite_add(d, args, addallarches=False):
200     die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
201     suite_name = args[2].lower()
202     version = args[3]
203     rest = args[3:]
204
205     def get_field(field):
206         for varval in args:
207             if varval.startswith(field + '='):
208                 return varval.split('=')[1]
209         return None
210
211     print "Adding suite %s" % suite_name
212     if not dryrun:
213         try:
214             s = d.session()
215             suite = Suite()
216             suite.suite_name = suite_name
217             suite.version = version
218             suite.label = get_field('label')
219             suite.description = get_field('description')
220             suite.origin = get_field('origin')
221             suite.codename = get_field('codename')
222             s.add(suite)
223             s.flush()
224         except IntegrityError, e:
225             die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
226         except SQLAlchemyError, e:
227             die("E: Error adding suite %s (%s)" % (suite_name, e))
228     print "Suite %s added" % (suite_name)
229
230     if addallarches:
231         arches = []
232         q = s.query(Architecture).order_by('arch_string')
233         for arch in q.all():
234             suite.architectures.append(arch)
235             arches.append(arch.arch_string)
236
237         print "Architectures %s added to %s" % (','.join(arches), suite_name)
238
239     s.commit()
240
241
242 def suite(command):
243     args = [str(x) for x in command]
244     Cnf = utils.get_conf()
245     d = DBConn()
246
247     die_arglen(args, 2, "E: suite needs at least a command")
248
249     mode = args[1].lower()
250
251     if mode == 'list':
252         __suite_list(d, args)
253     elif mode == 'show':
254         __suite_show(d, args)
255     elif mode == 'add':
256         __suite_add(d, args, False)
257     elif mode == 'add-all-arches':
258         __suite_add(d, args, True)
259     else:
260         die("E: suite command unknown")
261
262 dispatch['suite'] = suite
263 dispatch['s'] = suite
264
265 ################################################################################
266
267 def __suite_architecture_list(d, args):
268     s = d.session()
269     for j in s.query(Suite).order_by('suite_name'):
270         architectures = j.get_architectures(skipsrc = True, skipall = True)
271         print j.suite_name + ': ' + \
272               ', '.join([a.arch_string for a in architectures])
273
274 def __suite_architecture_listarch(d, args):
275     die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
276     suite = get_suite(args[2].lower(), d.session())
277     if suite is None:
278         die('E: suite %s is invalid' % args[2].lower())
279     a = suite.get_architectures(skipsrc = True, skipall = True)
280     for j in a:
281         print j.arch_string
282
283
284 def __suite_architecture_listsuite(d, args):
285     die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
286     architecture = get_architecture(args[2].lower(), d.session())
287     if architecture is None:
288         die("E: architecture %s is invalid" % args[2].lower())
289     for j in architecture.suites:
290         print j.suite_name
291
292
293 def __suite_architecture_add(d, args):
294     if len(args) < 3:
295         die("E: adding a suite-architecture entry requires a suite and arch")
296
297     s = d.session()
298
299     suite = get_suite(args[2].lower(), s)
300     if suite is None: die("E: Can't find suite %s" % args[2].lower())
301
302     arch = get_architecture(args[3].lower(), s)
303     if arch is None: die("E: Can't find architecture %s" % args[3].lower())
304
305     if not dryrun:
306         try:
307             suite.architectures.append(arch)
308             s.commit()
309         except IntegrityError, e:
310             die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
311         except SQLAlchemyError, e:
312             die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
313
314     print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
315
316
317 def __suite_architecture_rm(d, args):
318     if len(args) < 3:
319         die("E: removing an suite-architecture entry requires a suite and arch")
320
321     s = d.session()
322     if not dryrun:
323         try:
324             suite_name = args[2].lower()
325             suite = get_suite(suite_name, s)
326             if suite is None:
327                 die('E: no such suite %s' % suite_name)
328             arch_string = args[3].lower()
329             architecture = get_architecture(arch_string, s)
330             if architecture not in suite.architectures:
331                 die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
332             suite.architectures.remove(architecture)
333             s.commit()
334         except IntegrityError, e:
335             die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
336         except SQLAlchemyError, e:
337             die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
338
339     print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
340
341
342 def suite_architecture(command):
343     args = [str(x) for x in command]
344     Cnf = utils.get_conf()
345     d = DBConn()
346
347     die_arglen(args, 2, "E: suite-architecture needs at least a command")
348
349     mode = args[1].lower()
350
351     if mode == 'list':
352         __suite_architecture_list(d, args)
353     elif mode == 'list-arch':
354         __suite_architecture_listarch(d, args)
355     elif mode == 'list-suite':
356         __suite_architecture_listsuite(d, args)
357     elif mode == 'add':
358         __suite_architecture_add(d, args)
359     elif mode == 'rm':
360         __suite_architecture_rm(d, args)
361     else:
362         die("E: suite-architecture command unknown")
363
364 dispatch['suite-architecture'] = suite_architecture
365 dispatch['s-a'] = suite_architecture
366
367 ################################################################################
368
369 def __version_check_list(d):
370     session = d.session()
371     for s in session.query(Suite).order_by('suite_name'):
372         __version_check_list_suite(d, s.suite_name)
373
374 def __version_check_list_suite(d, suite_name):
375     vcs = get_version_checks(suite_name)
376     for vc in vcs:
377         print "%s %s %s" % (suite_name, vc.check, vc.reference.suite_name)
378
379 def __version_check_add(d, suite_name, check, reference_name):
380     suite = get_suite(suite_name)
381     if not suite:
382         die("E: Could not find suite %s." % (suite_name))
383     reference = get_suite(reference_name)
384     if not reference:
385         die("E: Could not find reference suite %s." % (reference_name))
386
387     session = d.session()
388     vc = VersionCheck()
389     vc.suite = suite
390     vc.check = check
391     vc.reference = reference
392     session.add(vc)
393     session.commit()
394
395 def __version_check_rm(d, suite_name, check, reference_name):
396     suite = get_suite(suite_name)
397     if not suite:
398         die("E: Could not find suite %s." % (suite_name))
399     reference = get_suite(reference_name)
400     if not reference:
401         die("E: Could not find reference suite %s." % (reference_name))
402
403     session = d.session()
404     try:
405       vc = session.query(VersionCheck).filter_by(suite=suite, check=check, reference=reference).one()
406       session.delete(vc)
407       session.commit()
408     except NoResultFound:
409       print "W: version-check not found."
410
411 def version_check(command):
412     args = [str(x) for x in command]
413     Cnf = utils.get_conf()
414     d = DBConn()
415
416     die_arglen(args, 2, "E: version-check needs at least a command")
417     mode = args[1].lower()
418
419     if mode == 'list':
420         __version_check_list(d)
421     elif mode == 'list-suite':
422         if len(args) != 3:
423             die("E: version-check list-suite needs a single parameter")
424         __version_check_list_suite(d, args[2])
425     elif mode == 'add':
426         if len(args) != 5:
427             die("E: version-check add needs three parameters")
428         __version_check_add(d, args[2], args[3], args[4])
429     elif mode == 'rm':
430         if len(args) != 5:
431             die("E: version-check rm needs three parameters")
432         __version_check_rm(d, args[2], args[3], args[4])
433     else:
434         die("E: version-check command unknown")
435
436 dispatch['version-check'] = version_check
437 dispatch['v-c'] = version_check
438
439 ################################################################################
440
441 def show_config(command):
442     args = [str(x) for x in command]
443     cnf = utils.get_conf()
444
445     die_arglen(args, 2, "E: config needs at least a command")
446
447     mode = args[1].lower()
448
449     if mode == 'db':
450         connstr = ""
451         if cnf.has_key("DB::Service"):
452             # Service mode
453             connstr = "postgresql://service=%s" % cnf["DB::Service"]
454         elif cnf.has_key("DB::Host"):
455             # TCP/IP
456             connstr = "postgres://%s" % cnf["DB::Host"]
457             if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
458                 connstr += ":%s" % cnf["DB::Port"]
459             connstr += "/%s" % cnf["DB::Name"]
460         else:
461             # Unix Socket
462             connstr = "postgres:///%s" % cnf["DB::Name"]
463             if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
464                 connstr += "?port=%s" % cnf["DB::Port"]
465         print connstr
466     elif mode == 'db-shell':
467         e = []
468         if cnf.has_key("DB::Service"):
469             e.append('PGSERVICE')
470             print "PGSERVICE=%s" % cnf["DB::Service"]
471         if cnf.has_key("DB::Name"):
472             e.append('PGDATABASE')
473             print "PGDATABASE=%s" % cnf["DB::Name"]
474         if cnf.has_key("DB::Host"):
475             print "PGHOST=%s" % cnf["DB::Host"]
476             e.append('PGHOST')
477         if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
478             print "PGPORT=%s" % cnf["DB::Port"]
479             e.append('PGPORT')
480         print "export " + " ".join(e)
481     else:
482         session = DBConn().session()
483         try:
484             o = session.query(DBConfig).filter_by(name = mode).one()
485             print o.value
486         except NoResultFound:
487             print "W: option '%s' not set" % mode
488
489 dispatch['config'] = show_config
490 dispatch['c'] = show_config
491
492 ################################################################################
493
494 def show_keyring(command):
495     args = [str(x) for x in command]
496     cnf = utils.get_conf()
497
498     die_arglen(args, 2, "E: keyring needs at least a command")
499
500     mode = args[1].lower()
501
502     d = DBConn()
503
504     q = d.session().query(Keyring).filter(Keyring.active == True)
505
506     if mode == 'list-all':
507         pass
508     elif mode == 'list-binary':
509         q = q.filter(Keyring.default_source_acl_id == None)
510     elif mode == 'list-source':
511         q = q.filter(Keyring.default_source_acl_id != None)
512     else:
513         die("E: keyring command unknown")
514
515     for k in q.all():
516         print k.keyring_name
517
518 dispatch['keyring'] = show_keyring
519 dispatch['k'] = show_keyring
520
521 ################################################################################
522
523 def main():
524     """Perform administrative work on the dak database"""
525     global dryrun
526     Cnf = utils.get_conf()
527     arguments = [('h', "help", "Admin::Options::Help"),
528                  ('n', "dry-run", "Admin::Options::Dry-Run")]
529     for i in [ "help", "dry-run" ]:
530         if not Cnf.has_key("Admin::Options::%s" % (i)):
531             Cnf["Admin::Options::%s" % (i)] = ""
532
533     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
534
535     options = Cnf.SubTree("Admin::Options")
536     if options["Help"] or len(arguments) < 1:
537         usage()
538     if options["Dry-Run"]:
539         dryrun = True
540
541     subcommand = str(arguments[0])
542
543     if subcommand in dispatch.keys():
544         dispatch[subcommand](arguments)
545     else:
546         die("E: Unknown command")
547
548 ################################################################################
549
550 if __name__ == '__main__':
551     main()