]> git.decadent.org.uk Git - dak.git/blob - dak/admin.py
Merge remote-tracking branch 'ansgar/generate-filelist-multiprocessing' into merge
[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   architecture / a:
65      a list                 show a list of architectures
66      a rm ARCH              remove an architecture (will only work if
67                             no longer linked to any suites)
68      a add ARCH DESCRIPTION [SUITELIST]
69                             add architecture ARCH with DESCRIPTION.
70                             If SUITELIST is given, add to each of the
71                             suites at the same time
72
73   suite / s:
74      s list                 show a list of suites
75      s show SUITE           show config details for a suite
76      s add SUITE VERSION [ label=LABEL ] [ description=DESCRIPTION ]
77                          [ origin=ORIGIN ] [ codename=CODENAME ]
78                             add suite SUITE, version VERSION. label,
79                             description, origin and codename are optional.
80
81   suite-architecture / s-a:
82      s-a list               show the architectures for all suites
83      s-a list-suite ARCH    show the suites an ARCH is in
84      s-a list-arch SUITE    show the architectures in a SUITE
85      s-a add SUITE ARCH     add ARCH to suite
86      s-a rm SUITE ARCH      remove ARCH from suite (will only work if
87                             no packages remain for the arch in the suite)
88
89   version-check / v-c:
90      v-c list                        show version checks for all suites
91      v-c list-suite SUITE            show version checks for suite SUITE
92      v-c add SUITE CHECK REFERENCE   add a version check for suite SUITE
93      v-c rm SUITE CHECK REFERENCE    rmove a version check
94        where
95          CHECK     is one of Enhances, MustBeNewerThan, MustBeOlderThan
96          REFERENCE is another suite name
97 """
98     sys.exit(exit_code)
99
100 ################################################################################
101
102 def __architecture_list(d, args):
103     q = d.session().query(Architecture).order_by('arch_string')
104     for j in q.all():
105         # HACK: We should get rid of source from the arch table
106         if j.arch_string == 'source': continue
107         print j.arch_string
108     sys.exit(0)
109
110 def __architecture_add(d, args):
111     die_arglen(args, 4, "E: adding an architecture requires a name and a description")
112     print "Adding architecture %s" % args[2]
113     suites = [str(x) for x in args[4:]]
114     if len(suites) > 0:
115         print "Adding to suites %s" % ", ".join(suites)
116     if not dryrun:
117         try:
118             s = d.session()
119             a = Architecture()
120             a.arch_string = str(args[2]).lower()
121             a.description = str(args[3])
122             s.add(a)
123             for sn in suites:
124                 su = get_suite(sn, s)
125                 if su is not None:
126                     a.suites.append(su)
127                 else:
128                     warn("W: Cannot find suite %s" % su)
129             s.commit()
130         except IntegrityError, e:
131             die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
132         except SQLAlchemyError, e:
133             die("E: Error adding architecture %s (%s)" % (args[2], e))
134     print "Architecture %s added" % (args[2])
135
136 def __architecture_rm(d, args):
137     die_arglen(args, 3, "E: removing an architecture requires at least a name")
138     print "Removing architecture %s" % args[2]
139     if not dryrun:
140         try:
141             s = d.session()
142             a = get_architecture(args[2].lower(), s)
143             if a is None:
144                 die("E: Cannot find architecture %s" % args[2])
145             s.delete(a)
146             s.commit()
147         except IntegrityError, e:
148             die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
149         except SQLAlchemyError, e:
150             die("E: Error removing architecture %s (%s)" % (args[2], e))
151     print "Architecture %s removed" % args[2]
152
153 def architecture(command):
154     args = [str(x) for x in command]
155     Cnf = utils.get_conf()
156     d = DBConn()
157
158     die_arglen(args, 2, "E: architecture needs at least a command")
159
160     mode = args[1].lower()
161     if mode == 'list':
162         __architecture_list(d, args)
163     elif mode == 'add':
164         __architecture_add(d, args)
165     elif mode == 'rm':
166         __architecture_rm(d, args)
167     else:
168         die("E: architecture command unknown")
169
170 dispatch['architecture'] = architecture
171 dispatch['a'] = architecture
172
173 ################################################################################
174
175 def __suite_list(d, args):
176     s = d.session()
177     for j in s.query(Suite).order_by('suite_name').all():
178         print j.suite_name
179
180 def __suite_show(d, args):
181     if len(args) < 2:
182         die("E: showing an suite entry requires a suite")
183
184     s = d.session()
185     su = get_suite(args[2].lower())
186     if su is None:
187         die("E: can't find suite entry for %s" % (args[2].lower()))
188
189     print su.details()
190
191 def __suite_add(d, args):
192     die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
193     suite_name = args[2].lower()
194     version = args[3]
195     rest = args[3:]
196
197     def get_field(field):
198         for varval in args:
199             if varval.startswith(field + '='):
200                 return varval.split('=')[1]
201         return None
202
203     print "Adding suite %s" % suite_name
204     if not dryrun:
205         try:
206             s = d.session()
207             suite = Suite()
208             suite.suite_name = suite_name
209             suite.version = version
210             suite.label = get_field('label')
211             suite.description = get_field('description')
212             suite.origin = get_field('origin')
213             suite.codename = get_field('codename')
214             s.add(suite)
215             s.commit()
216         except IntegrityError, e:
217             die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
218         except SQLAlchemyError, e:
219             die("E: Error adding suite %s (%s)" % (suite_name, e))
220     print "Suite %s added" % (suite_name)
221
222 def suite(command):
223     args = [str(x) for x in command]
224     Cnf = utils.get_conf()
225     d = DBConn()
226
227     die_arglen(args, 2, "E: suite needs at least a command")
228
229     mode = args[1].lower()
230
231     if mode == 'list':
232         __suite_list(d, args)
233     elif mode == 'show':
234         __suite_show(d, args)
235     elif mode == 'add':
236         __suite_add(d, args)
237     else:
238         die("E: suite command unknown")
239
240 dispatch['suite'] = suite
241 dispatch['s'] = suite
242
243 ################################################################################
244
245 def __suite_architecture_list(d, args):
246     s = d.session()
247     for j in s.query(Suite).order_by('suite_name'):
248         architectures = j.get_architectures(skipsrc = True, skipall = True)
249         print j.suite_name + ': ' + \
250               ', '.join([a.arch_string for a in architectures])
251
252 def __suite_architecture_listarch(d, args):
253     die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
254     suite = get_suite(args[2].lower(), d.session())
255     if suite is None:
256         die('E: suite %s is invalid' % args[2].lower())
257     a = suite.get_architectures(skipsrc = True, skipall = True)
258     for j in a:
259         print j.arch_string
260
261
262 def __suite_architecture_listsuite(d, args):
263     die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
264     architecture = get_architecture(args[2].lower(), d.session())
265     if architecture is None:
266         die("E: architecture %s is invalid" % args[2].lower())
267     for j in architecture.suites:
268         print j.suite_name
269
270
271 def __suite_architecture_add(d, args):
272     if len(args) < 3:
273         die("E: adding a suite-architecture entry requires a suite and arch")
274
275     s = d.session()
276
277     suite = get_suite(args[2].lower(), s)
278     if suite is None: die("E: Can't find suite %s" % args[2].lower())
279
280     arch = get_architecture(args[3].lower(), s)
281     if arch is None: die("E: Can't find architecture %s" % args[3].lower())
282
283     if not dryrun:
284         try:
285             suite.architectures.append(arch)
286             s.commit()
287         except IntegrityError, e:
288             die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
289         except SQLAlchemyError, e:
290             die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
291
292     print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
293
294
295 def __suite_architecture_rm(d, args):
296     if len(args) < 3:
297         die("E: removing an suite-architecture entry requires a suite and arch")
298
299     s = d.session()
300     if not dryrun:
301         try:
302             suite_name = args[2].lower()
303             suite = get_suite(suite_name, s)
304             if suite is None:
305                 die('E: no such suite %s' % suite_name)
306             arch_string = args[3].lower()
307             architecture = get_architecture(arch_string, s)
308             if architecture not in suite.architectures:
309                 die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
310             suite.architectures.remove(architecture)
311             s.commit()
312         except IntegrityError, e:
313             die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
314         except SQLAlchemyError, e:
315             die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
316
317     print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
318
319
320 def suite_architecture(command):
321     args = [str(x) for x in command]
322     Cnf = utils.get_conf()
323     d = DBConn()
324
325     die_arglen(args, 2, "E: suite-architecture needs at least a command")
326
327     mode = args[1].lower()
328
329     if mode == 'list':
330         __suite_architecture_list(d, args)
331     elif mode == 'list-arch':
332         __suite_architecture_listarch(d, args)
333     elif mode == 'list-suite':
334         __suite_architecture_listsuite(d, args)
335     elif mode == 'add':
336         __suite_architecture_add(d, args)
337     elif mode == 'rm':
338         __suite_architecture_rm(d, args)
339     else:
340         die("E: suite-architecture command unknown")
341
342 dispatch['suite-architecture'] = suite_architecture
343 dispatch['s-a'] = suite_architecture
344
345 ################################################################################
346
347 def __version_check_list(d):
348     session = d.session()
349     for s in session.query(Suite).order_by('suite_name'):
350         __version_check_list_suite(d, s.suite_name)
351
352 def __version_check_list_suite(d, suite_name):
353     vcs = get_version_checks(suite_name)
354     for vc in vcs:
355         print "%s %s %s" % (suite_name, vc.check, vc.reference.suite_name)
356
357 def __version_check_add(d, suite_name, check, reference_name):
358     suite = get_suite(suite_name)
359     if not suite:
360         die("E: Could not find suite %s." % (suite_name))
361     reference = get_suite(reference_name)
362     if not reference:
363         die("E: Could not find reference suite %s." % (reference_name))
364
365     session = d.session()
366     vc = VersionCheck()
367     vc.suite = suite
368     vc.check = check
369     vc.reference = reference
370     session.add(vc)
371     session.commit()
372
373 def __version_check_rm(d, suite_name, check, reference_name):
374     suite = get_suite(suite_name)
375     if not suite:
376         die("E: Could not find suite %s." % (suite_name))
377     reference = get_suite(reference_name)
378     if not reference:
379         die("E: Could not find reference suite %s." % (reference_name))
380
381     session = d.session()
382     try:
383       vc = session.query(VersionCheck).filter_by(suite=suite, check=check, reference=reference).one()
384       session.delete(vc)
385       session.commit()
386     except NoResultFound:
387       print "W: version-check not found."
388
389 def version_check(command):
390     args = [str(x) for x in command]
391     Cnf = utils.get_conf()
392     d = DBConn()
393
394     die_arglen(args, 2, "E: version-check needs at least a command")
395     mode = args[1].lower()
396
397     if mode == 'list':
398         __version_check_list(d)
399     elif mode == 'list-suite':
400         if len(args) != 3:
401             die("E: version-check list-suite needs a single parameter")
402         __version_check_list_suite(d, args[2])
403     elif mode == 'add':
404         if len(args) != 5:
405             die("E: version-check add needs three parameters")
406         __version_check_add(d, args[2], args[3], args[4])
407     elif mode == 'rm':
408         if len(args) != 5:
409             die("E: version-check rm needs three parameters")
410         __version_check_rm(d, args[2], args[3], args[4])
411     else:
412         die("E: version-check command unknown")
413
414 dispatch['version-check'] = version_check
415 dispatch['v-c'] = version_check
416
417 ################################################################################
418
419 def show_config(command):
420     args = [str(x) for x in command]
421     cnf = utils.get_conf()
422
423     die_arglen(args, 2, "E: config needs at least a command")
424
425     mode = args[1].lower()
426
427     if mode == 'db':
428         connstr = ""
429         if cnf.has_key("DB::Service"):
430             # Service mode
431             connstr = "postgresql://service=%s" % cnf["DB::Service"]
432         elif cnf.has_key("DB::Host"):
433             # TCP/IP
434             connstr = "postgres://%s" % cnf["DB::Host"]
435             if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
436                 connstr += ":%s" % cnf["DB::Port"]
437             connstr += "/%s" % cnf["DB::Name"]
438         else:
439             # Unix Socket
440             connstr = "postgres:///%s" % cnf["DB::Name"]
441             if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
442                 connstr += "?port=%s" % cnf["DB::Port"]
443         print connstr
444     elif mode == 'db-shell':
445         e = []
446         if cnf.has_key("DB::Service"):
447             e.append('PGSERVICE')
448             print "PGSERVICE=%s" % cnf["DB::Service"]
449         if cnf.has_key("DB::Name"):
450             e.append('PGDATABASE')
451             print "PGDATABASE=%s" % cnf["DB::Name"]
452         if cnf.has_key("DB::Host"):
453             print "PGHOST=%s" % cnf["DB::Host"]
454             e.append('PGHOST')
455         if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
456             print "PGPORT=%s" % cnf["DB::Port"]
457             e.append('PGPORT')
458         print "export " + " ".join(e)
459     else:
460         session = DBConn().session()
461         try:
462             o = session.query(DBConfig).filter_by(name = mode).one()
463             print o.value
464         except NoResultFound:
465             print "W: option '%s' not set" % mode
466
467 dispatch['config'] = show_config
468 dispatch['c'] = show_config
469
470 ################################################################################
471
472 def main():
473     """Perform administrative work on the dak database"""
474     global dryrun
475     Cnf = utils.get_conf()
476     arguments = [('h', "help", "Admin::Options::Help"),
477                  ('n', "dry-run", "Admin::Options::Dry-Run")]
478     for i in [ "help", "dry-run" ]:
479         if not Cnf.has_key("Admin::Options::%s" % (i)):
480             Cnf["Admin::Options::%s" % (i)] = ""
481
482     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
483
484     options = Cnf.SubTree("Admin::Options")
485     if options["Help"] or len(arguments) < 1:
486         usage()
487     if options["Dry-Run"]:
488         dryrun = True
489
490     subcommand = str(arguments[0])
491
492     if subcommand in dispatch.keys():
493         dispatch[subcommand](arguments)
494     else:
495         die("E: Unknown command")
496
497 ################################################################################
498
499 if __name__ == '__main__':
500     main()