]> git.decadent.org.uk Git - dak.git/blob - dak/admin.py
Merge branch 'master' of ssh://ftp-master.debian.org/srv/ftp.debian.org/git/dak
[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   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)
93
94   version-check / v-c:
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
99        where
100          CHECK     is one of Enhances, MustBeNewerThan, MustBeOlderThan
101          REFERENCE is another suite name
102 """
103     sys.exit(exit_code)
104
105 ################################################################################
106
107 def __architecture_list(d, args):
108     q = d.session().query(Architecture).order_by('arch_string')
109     for j in q.all():
110         # HACK: We should get rid of source from the arch table
111         if j.arch_string == 'source': continue
112         print j.arch_string
113     sys.exit(0)
114
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:]]
119     if len(suites) > 0:
120         print "Adding to suites %s" % ", ".join(suites)
121     if not dryrun:
122         try:
123             s = d.session()
124             a = Architecture()
125             a.arch_string = str(args[2]).lower()
126             a.description = str(args[3])
127             s.add(a)
128             for sn in suites:
129                 su = get_suite(sn, s)
130                 if su is not None:
131                     a.suites.append(su)
132                 else:
133                     warn("W: Cannot find suite %s" % su)
134             s.commit()
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])
140
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]
144     if not dryrun:
145         try:
146             s = d.session()
147             a = get_architecture(args[2].lower(), s)
148             if a is None:
149                 die("E: Cannot find architecture %s" % args[2])
150             s.delete(a)
151             s.commit()
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]
157
158 def architecture(command):
159     args = [str(x) for x in command]
160     Cnf = utils.get_conf()
161     d = DBConn()
162
163     die_arglen(args, 2, "E: architecture needs at least a command")
164
165     mode = args[1].lower()
166     if mode == 'list':
167         __architecture_list(d, args)
168     elif mode == 'add':
169         __architecture_add(d, args)
170     elif mode == 'rm':
171         __architecture_rm(d, args)
172     else:
173         die("E: architecture command unknown")
174
175 dispatch['architecture'] = architecture
176 dispatch['a'] = architecture
177
178 ################################################################################
179
180 def __suite_list(d, args):
181     s = d.session()
182     for j in s.query(Suite).order_by('suite_name').all():
183         print j.suite_name
184
185 def __suite_show(d, args):
186     if len(args) < 2:
187         die("E: showing an suite entry requires a suite")
188
189     s = d.session()
190     su = get_suite(args[2].lower())
191     if su is None:
192         die("E: can't find suite entry for %s" % (args[2].lower()))
193
194     print su.details()
195
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()
199     version = args[3]
200     rest = args[3:]
201
202     def get_field(field):
203         for varval in args:
204             if varval.startswith(field + '='):
205                 return varval.split('=')[1]
206         return None
207
208     print "Adding suite %s" % suite_name
209     if not dryrun:
210         try:
211             s = d.session()
212             suite = Suite()
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')
219             s.add(suite)
220             s.commit()
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)
226
227 def suite(command):
228     args = [str(x) for x in command]
229     Cnf = utils.get_conf()
230     d = DBConn()
231
232     die_arglen(args, 2, "E: suite needs at least a command")
233
234     mode = args[1].lower()
235
236     if mode == 'list':
237         __suite_list(d, args)
238     elif mode == 'show':
239         __suite_show(d, args)
240     elif mode == 'add':
241         __suite_add(d, args)
242     else:
243         die("E: suite command unknown")
244
245 dispatch['suite'] = suite
246 dispatch['s'] = suite
247
248 ################################################################################
249
250 def __suite_architecture_list(d, args):
251     s = d.session()
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])
256
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())
260     if suite is None:
261         die('E: suite %s is invalid' % args[2].lower())
262     a = suite.get_architectures(skipsrc = True, skipall = True)
263     for j in a:
264         print j.arch_string
265
266
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:
273         print j.suite_name
274
275
276 def __suite_architecture_add(d, args):
277     if len(args) < 3:
278         die("E: adding a suite-architecture entry requires a suite and arch")
279
280     s = d.session()
281
282     suite = get_suite(args[2].lower(), s)
283     if suite is None: die("E: Can't find suite %s" % args[2].lower())
284
285     arch = get_architecture(args[3].lower(), s)
286     if arch is None: die("E: Can't find architecture %s" % args[3].lower())
287
288     if not dryrun:
289         try:
290             suite.architectures.append(arch)
291             s.commit()
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))
296
297     print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
298
299
300 def __suite_architecture_rm(d, args):
301     if len(args) < 3:
302         die("E: removing an suite-architecture entry requires a suite and arch")
303
304     s = d.session()
305     if not dryrun:
306         try:
307             suite_name = args[2].lower()
308             suite = get_suite(suite_name, s)
309             if suite is None:
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)
316             s.commit()
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))
321
322     print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
323
324
325 def suite_architecture(command):
326     args = [str(x) for x in command]
327     Cnf = utils.get_conf()
328     d = DBConn()
329
330     die_arglen(args, 2, "E: suite-architecture needs at least a command")
331
332     mode = args[1].lower()
333
334     if mode == 'list':
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)
340     elif mode == 'add':
341         __suite_architecture_add(d, args)
342     elif mode == 'rm':
343         __suite_architecture_rm(d, args)
344     else:
345         die("E: suite-architecture command unknown")
346
347 dispatch['suite-architecture'] = suite_architecture
348 dispatch['s-a'] = suite_architecture
349
350 ################################################################################
351
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)
356
357 def __version_check_list_suite(d, suite_name):
358     vcs = get_version_checks(suite_name)
359     for vc in vcs:
360         print "%s %s %s" % (suite_name, vc.check, vc.reference.suite_name)
361
362 def __version_check_add(d, suite_name, check, reference_name):
363     suite = get_suite(suite_name)
364     if not suite:
365         die("E: Could not find suite %s." % (suite_name))
366     reference = get_suite(reference_name)
367     if not reference:
368         die("E: Could not find reference suite %s." % (reference_name))
369
370     session = d.session()
371     vc = VersionCheck()
372     vc.suite = suite
373     vc.check = check
374     vc.reference = reference
375     session.add(vc)
376     session.commit()
377
378 def __version_check_rm(d, suite_name, check, reference_name):
379     suite = get_suite(suite_name)
380     if not suite:
381         die("E: Could not find suite %s." % (suite_name))
382     reference = get_suite(reference_name)
383     if not reference:
384         die("E: Could not find reference suite %s." % (reference_name))
385
386     session = d.session()
387     try:
388       vc = session.query(VersionCheck).filter_by(suite=suite, check=check, reference=reference).one()
389       session.delete(vc)
390       session.commit()
391     except NoResultFound:
392       print "W: version-check not found."
393
394 def version_check(command):
395     args = [str(x) for x in command]
396     Cnf = utils.get_conf()
397     d = DBConn()
398
399     die_arglen(args, 2, "E: version-check needs at least a command")
400     mode = args[1].lower()
401
402     if mode == 'list':
403         __version_check_list(d)
404     elif mode == 'list-suite':
405         if len(args) != 3:
406             die("E: version-check list-suite needs a single parameter")
407         __version_check_list_suite(d, args[2])
408     elif mode == 'add':
409         if len(args) != 5:
410             die("E: version-check add needs three parameters")
411         __version_check_add(d, args[2], args[3], args[4])
412     elif mode == 'rm':
413         if len(args) != 5:
414             die("E: version-check rm needs three parameters")
415         __version_check_rm(d, args[2], args[3], args[4])
416     else:
417         die("E: version-check command unknown")
418
419 dispatch['version-check'] = version_check
420 dispatch['v-c'] = version_check
421
422 ################################################################################
423
424 def show_config(command):
425     args = [str(x) for x in command]
426     cnf = utils.get_conf()
427
428     die_arglen(args, 2, "E: config needs at least a command")
429
430     mode = args[1].lower()
431
432     if mode == 'db':
433         connstr = ""
434         if cnf.has_key("DB::Service"):
435             # Service mode
436             connstr = "postgresql://service=%s" % cnf["DB::Service"]
437         elif cnf.has_key("DB::Host"):
438             # TCP/IP
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"]
443         else:
444             # Unix Socket
445             connstr = "postgres:///%s" % cnf["DB::Name"]
446             if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
447                 connstr += "?port=%s" % cnf["DB::Port"]
448         print connstr
449     elif mode == 'db-shell':
450         e = []
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"]
459             e.append('PGHOST')
460         if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
461             print "PGPORT=%s" % cnf["DB::Port"]
462             e.append('PGPORT')
463         print "export " + " ".join(e)
464     else:
465         session = DBConn().session()
466         try:
467             o = session.query(DBConfig).filter_by(name = mode).one()
468             print o.value
469         except NoResultFound:
470             print "W: option '%s' not set" % mode
471
472 dispatch['config'] = show_config
473 dispatch['c'] = show_config
474
475 ################################################################################
476
477 def show_keyring(command):
478     args = [str(x) for x in command]
479     cnf = utils.get_conf()
480
481     die_arglen(args, 2, "E: keyring needs at least a command")
482
483     mode = args[1].lower()
484
485     d = DBConn()
486
487     q = d.session().query(Keyring).filter(Keyring.active == True)
488
489     if mode == 'list-all':
490         pass
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)
495     else:
496         die("E: keyring command unknown")
497
498     for k in q.all():
499         print k.keyring_name
500
501 dispatch['keyring'] = show_keyring
502 dispatch['k'] = show_keyring
503
504 ################################################################################
505
506 def main():
507     """Perform administrative work on the dak database"""
508     global dryrun
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)] = ""
515
516     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
517
518     options = Cnf.SubTree("Admin::Options")
519     if options["Help"] or len(arguments) < 1:
520         usage()
521     if options["Dry-Run"]:
522         dryrun = True
523
524     subcommand = str(arguments[0])
525
526     if subcommand in dispatch.keys():
527         dispatch[subcommand](arguments)
528     else:
529         die("E: Unknown command")
530
531 ################################################################################
532
533 if __name__ == '__main__':
534     main()