]> git.decadent.org.uk Git - dak.git/blob - dak/admin.py
Merge branch 'contents'
[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
29 ################################################################################
30
31 dispatch = {}
32 dryrun = False
33
34 ################################################################################
35 def warn(msg):
36     print >> sys.stderr, msg
37
38 def die(msg, exit_code=1):
39     print >> sys.stderr, msg
40     sys.exit(exit_code)
41
42 def die_arglen(args, args_needed, msg):
43     if len(args) < args_needed:
44         die(msg)
45
46 def usage(exit_code=0):
47     """Perform administrative work on the dak database."""
48
49     print """Usage: dak admin COMMAND
50 Perform administrative work on the dak database.
51
52   -h, --help          show this help and exit.
53   -n, --dry-run       don't do anything, just show what would have been done
54                       (only applies to add or rm operations).
55
56   Commands can use a long or abbreviated form:
57
58   config / c:
59      c db                   show db config
60      c db-shell             show db config in a usable form for psql
61
62   architecture / a:
63      a list                 show a list of architectures
64      a rm ARCH              remove an architecture (will only work if
65                             no longer linked to any suites)
66      a add ARCH DESCRIPTION [SUITELIST]
67                             add architecture ARCH with DESCRIPTION.
68                             If SUITELIST is given, add to each of the
69                             suites at the same time
70
71   suite / s:
72      s list                 show a list of suites
73      s show SUITE           show config details for a suite
74      s add SUITE VERSION [ label=LABEL ] [ description=DESCRIPTION ]
75                          [ origin=ORIGIN ] [ codename=CODENAME ]
76                             add suite SUITE, version VERSION. label,
77                             description, origin and codename are optional.
78
79   suite-architecture / s-a:
80      s-a list               show the architectures for all suites
81      s-a list-suite ARCH    show the suites an ARCH is in
82      s-a list-arch SUITE    show the architectures in a SUITE
83      s-a add SUITE ARCH     add ARCH to suite
84      s-a rm SUITE ARCH      remove ARCH from suite (will only work if
85                             no packages remain for the arch in the suite)
86 """
87     sys.exit(exit_code)
88
89 ################################################################################
90
91 def __architecture_list(d, args):
92     q = d.session().query(Architecture).order_by('arch_string')
93     for j in q.all():
94         # HACK: We should get rid of source from the arch table
95         if j.arch_string == 'source': continue
96         print j.arch_string
97     sys.exit(0)
98
99 def __architecture_add(d, args):
100     die_arglen(args, 4, "E: adding an architecture requires a name and a description")
101     print "Adding architecture %s" % args[2]
102     suites = [str(x) for x in args[4:]]
103     if len(suites) > 0:
104         print "Adding to suites %s" % ", ".join(suites)
105     if not dryrun:
106         try:
107             s = d.session()
108             a = Architecture()
109             a.arch_string = str(args[2]).lower()
110             a.description = str(args[3])
111             s.add(a)
112             for sn in suites:
113                 su = get_suite(sn, s)
114                 if su is not None:
115                     a.suites.append(su)
116                 else:
117                     warn("W: Cannot find suite %s" % su)
118             s.commit()
119         except IntegrityError, e:
120             die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
121         except SQLAlchemyError, e:
122             die("E: Error adding architecture %s (%s)" % (args[2], e))
123     print "Architecture %s added" % (args[2])
124
125 def __architecture_rm(d, args):
126     die_arglen(args, 3, "E: removing an architecture requires at least a name")
127     print "Removing architecture %s" % args[2]
128     if not dryrun:
129         try:
130             s = d.session()
131             a = get_architecture(args[2].lower(), s)
132             if a is None:
133                 die("E: Cannot find architecture %s" % args[2])
134             s.delete(a)
135             s.commit()
136         except IntegrityError, e:
137             die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
138         except SQLAlchemyError, e:
139             die("E: Error removing architecture %s (%s)" % (args[2], e))
140     print "Architecture %s removed" % args[2]
141
142 def architecture(command):
143     args = [str(x) for x in command]
144     Cnf = utils.get_conf()
145     d = DBConn()
146
147     die_arglen(args, 2, "E: architecture needs at least a command")
148
149     mode = args[1].lower()
150     if mode == 'list':
151         __architecture_list(d, args)
152     elif mode == 'add':
153         __architecture_add(d, args)
154     elif mode == 'rm':
155         __architecture_rm(d, args)
156     else:
157         die("E: architecture command unknown")
158
159 dispatch['architecture'] = architecture
160 dispatch['a'] = architecture
161
162 ################################################################################
163
164 def __suite_list(d, args):
165     s = d.session()
166     for j in s.query(Suite).order_by('suite_name').all():
167         print j.suite_name
168
169 def __suite_show(d, args):
170     if len(args) < 2:
171         die("E: showing an suite entry requires a suite")
172
173     s = d.session()
174     su = get_suite(args[2].lower())
175     if su is None:
176         die("E: can't find suite entry for %s" % (args[2].lower()))
177
178     print su.details()
179
180 def __suite_add(d, args):
181     die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
182     suite_name = args[2].lower()
183     version = args[3]
184     rest = args[3:]
185
186     def get_field(field):
187         for varval in args:
188             if varval.startswith(field + '='):
189                 return varval.split('=')[1]
190         return None
191
192     print "Adding suite %s" % suite_name
193     if not dryrun:
194         try:
195             s = d.session()
196             suite = Suite()
197             suite.suite_name = suite_name
198             suite.version = version
199             suite.label = get_field('label')
200             suite.description = get_field('description')
201             suite.origin = get_field('origin')
202             suite.codename = get_field('codename')
203             s.add(suite)
204             s.commit()
205         except IntegrityError, e:
206             die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
207         except SQLAlchemyError, e:
208             die("E: Error adding suite %s (%s)" % (suite_name, e))
209     print "Suite %s added" % (suite_name)
210
211 def suite(command):
212     args = [str(x) for x in command]
213     Cnf = utils.get_conf()
214     d = DBConn()
215
216     die_arglen(args, 2, "E: suite needs at least a command")
217
218     mode = args[1].lower()
219
220     if mode == 'list':
221         __suite_list(d, args)
222     elif mode == 'show':
223         __suite_show(d, args)
224     elif mode == 'add':
225         __suite_add(d, args)
226     else:
227         die("E: suite command unknown")
228
229 dispatch['suite'] = suite
230 dispatch['s'] = suite
231
232 ################################################################################
233
234 def __suite_architecture_list(d, args):
235     s = d.session()
236     for j in s.query(Suite).order_by('suite_name'):
237         architectures = j.get_architectures(skipsrc = True, skipall = True)
238         print j.suite_name + ': ' + \
239               ', '.join([a.arch_string for a in architectures])
240
241 def __suite_architecture_listarch(d, args):
242     die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
243     suite = get_suite(args[2].lower(), d.session())
244     if suite is None:
245         die('E: suite %s is invalid' % args[2].lower())
246     a = suite.get_architectures(skipsrc = True, skipall = True)
247     for j in a:
248         print j.arch_string
249
250
251 def __suite_architecture_listsuite(d, args):
252     die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
253     architecture = get_architecture(args[2].lower(), d.session())
254     if architecture is None:
255         die("E: architecture %s is invalid" % args[2].lower())
256     for j in architecture.suites:
257         print j.suite_name
258
259
260 def __suite_architecture_add(d, args):
261     if len(args) < 3:
262         die("E: adding a suite-architecture entry requires a suite and arch")
263
264     s = d.session()
265
266     suite = get_suite(args[2].lower(), s)
267     if suite is None: die("E: Can't find suite %s" % args[2].lower())
268
269     arch = get_architecture(args[3].lower(), s)
270     if arch is None: die("E: Can't find architecture %s" % args[3].lower())
271
272     if not dryrun:
273         try:
274             suite.architectures.append(arch)
275             s.commit()
276         except IntegrityError, e:
277             die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
278         except SQLAlchemyError, e:
279             die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
280
281     print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
282
283
284 def __suite_architecture_rm(d, args):
285     if len(args) < 3:
286         die("E: removing an suite-architecture entry requires a suite and arch")
287
288     s = d.session()
289     if not dryrun:
290         try:
291             suite_name = args[2].lower()
292             suite = get_suite(suite_name, s)
293             if suite is None:
294                 die('E: no such suite %s' % suite_name)
295             arch_string = args[3].lower()
296             architecture = get_architecture(arch_string, s)
297             if architecture not in suite.architectures:
298                 die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
299             suite.architectures.remove(architecture)
300             s.commit()
301         except IntegrityError, e:
302             die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
303         except SQLAlchemyError, e:
304             die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
305
306     print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
307
308
309 def suite_architecture(command):
310     args = [str(x) for x in command]
311     Cnf = utils.get_conf()
312     d = DBConn()
313
314     die_arglen(args, 2, "E: suite-architecture needs at least a command")
315
316     mode = args[1].lower()
317
318     if mode == 'list':
319         __suite_architecture_list(d, args)
320     elif mode == 'list-arch':
321         __suite_architecture_listarch(d, args)
322     elif mode == 'list-suite':
323         __suite_architecture_listsuite(d, args)
324     elif mode == 'add':
325         __suite_architecture_add(d, args)
326     elif mode == 'rm':
327         __suite_architecture_rm(d, args)
328     else:
329         die("E: suite-architecture command unknown")
330
331 dispatch['suite-architecture'] = suite_architecture
332 dispatch['s-a'] = suite_architecture
333
334 ################################################################################
335
336 def show_config(command):
337     args = [str(x) for x in command]
338     cnf = utils.get_conf()
339
340     die_arglen(args, 2, "E: config needs at least a command")
341
342     mode = args[1].lower()
343
344     if mode == 'db':
345         connstr = ""
346         if cnf.has_key("DB::Service"):
347             # Service mode
348             connstr = "postgresql://service=%s" % cnf["DB::Service"]
349         elif cnf.has_key("DB::Host"):
350             # TCP/IP
351             connstr = "postgres://%s" % cnf["DB::Host"]
352             if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
353                 connstr += ":%s" % cnf["DB::Port"]
354             connstr += "/%s" % cnf["DB::Name"]
355         else:
356             # Unix Socket
357             connstr = "postgres:///%s" % cnf["DB::Name"]
358             if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
359                 connstr += "?port=%s" % cnf["DB::Port"]
360         print connstr
361     elif mode == 'db-shell':
362         e = []
363         if cnf.has_key("DB::Service"):
364             e.append('PGSERVICE')
365             print "PGSERVICE=%s" % cnf["DB::Service"]
366         if cnf.has_key("DB::Name"):
367             e.append('PGDATABASE')
368             print "PGDATABASE=%s" % cnf["DB::Name"]
369         if cnf.has_key("DB::Host"):
370             print "PGHOST=%s" % cnf["DB::Host"]
371             e.append('PGHOST')
372         if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
373             print "PGPORT=%s" % cnf["DB::Port"]
374             e.append('PGPORT')
375         print "export " + " ".join(e)
376     else:
377         die("E: config command unknown")
378
379 dispatch['config'] = show_config
380 dispatch['c'] = show_config
381
382 ################################################################################
383
384 def main():
385     """Perform administrative work on the dak database"""
386     global dryrun
387     Cnf = utils.get_conf()
388     arguments = [('h', "help", "Admin::Options::Help"),
389                  ('n', "dry-run", "Admin::Options::Dry-Run")]
390     for i in [ "help", "dry-run" ]:
391         if not Cnf.has_key("Admin::Options::%s" % (i)):
392             Cnf["Admin::Options::%s" % (i)] = ""
393
394     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
395
396     options = Cnf.SubTree("Admin::Options")
397     if options["Help"] or len(arguments) < 1:
398         usage()
399     if options["Dry-Run"]:
400         dryrun = True
401
402     subcommand = str(arguments[0])
403
404     if subcommand in dispatch.keys():
405         dispatch[subcommand](arguments)
406     else:
407         die("E: Unknown command")
408
409 ################################################################################
410
411 if __name__ == '__main__':
412     main()