admin.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along
  14. # with this program; if not, write to the Free Software Foundation, Inc.,
  15. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. """module for administrative tasks in a dak install.
  17. This module provides classes to do administrative tasks in a dak install,
  18. eg. creating/modifying archives, suites, architectures, etc..
  19. """
  20. from daklib import utils
  21. from daklib.dbconn import *
  22. ################################################################################
  23. class ArchiveMaint(object):
  24. """Handle low-level archive changes
  25. """
  26. def __init__(self):
  27. pass
  28. def suite_list(self, d, args):
  29. s = d.session()
  30. for j in s.query(Suite).join(Suite.archive).order_by(Archive.archive_name, Suite.suite_name).all():
  31. if len(args) > 2 and args[2] == "--print-archive":
  32. print "{0} {1}".format(j.archive.archive_name, j.suite_name)
  33. else:
  34. print "{0}".format(j.suite_name)
  35. def suite_show(self, d, args):
  36. if len(args) < 2:
  37. die("E: showing an suite entry requires a suite")
  38. s = d.session()
  39. su = get_suite(args[2].lower())
  40. if su is None:
  41. die("E: can't find suite entry for %s" % (args[2].lower()))
  42. print su.details()
  43. def suite_add(self, d, args, addallarches=False):
  44. die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
  45. suite_name = args[2].lower()
  46. version = args[3]
  47. rest = args[3:]
  48. if len(version) == 0:
  49. version = None
  50. def get_field(field):
  51. for varval in args:
  52. if varval.startswith(field + '='):
  53. return varval.split('=')[1]
  54. return None
  55. print "Adding suite %s" % suite_name
  56. if not dryrun:
  57. try:
  58. s = d.session()
  59. suite = Suite()
  60. suite.suite_name = suite_name
  61. suite.overridecodename = suite_name
  62. suite.version = version
  63. suite.label = get_field('label')
  64. suite.description = get_field('description')
  65. suite.origin = get_field('origin')
  66. suite.codename = get_field('codename')
  67. signingkey = get_field('signingkey')
  68. if signingkey is not None:
  69. suite.signingkeys = [signingkey.upper()]
  70. archive_name = get_field('archive')
  71. if archive_name is not None:
  72. suite.archive = get_archive(archive_name, s)
  73. else:
  74. suite.archive = s.query(Archive).filter(~Archive.archive_name.in_(['build-queues', 'new', 'policy'])).one()
  75. suite.srcformats = s.query(SrcFormat).all()
  76. s.add(suite)
  77. s.flush()
  78. except IntegrityError as e:
  79. die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
  80. except SQLAlchemyError as e:
  81. die("E: Error adding suite %s (%s)" % (suite_name, e))
  82. print "Suite %s added" % (suite_name)
  83. if addallarches:
  84. arches = []
  85. q = s.query(Architecture).order_by(Architecture.arch_string)
  86. for arch in q.all():
  87. suite.architectures.append(arch)
  88. arches.append(arch.arch_string)
  89. print "Architectures %s added to %s" % (','.join(arches), suite_name)
  90. s.commit()
  91. def suite_rm(self, d, args):
  92. die_arglen(args, 3, "E: removing a suite requires at least a name")
  93. name = args[2]
  94. print "Removing suite {0}".format(name)
  95. if not dryrun:
  96. try:
  97. s = d.session()
  98. su = get_suite(name.lower())
  99. if su is None:
  100. die("E: Cannot find suite {0}".format(name))
  101. s.delete(su)
  102. s.commit()
  103. except IntegrityError as e:
  104. die("E: Integrity error removing suite {0} (suite-arch entries probably still exist)".format(name))
  105. except SQLAlchemyError as e:
  106. die("E: Error removing suite {0} ({1})".format(name, e))
  107. print "Suite {0} removed".format(name)
  108. def suite_add_build_queue(self, d, args):
  109. session = d.session()
  110. die_arglen(args, 6, "E: Adding a build queue needs four parameters.")
  111. suite_name = args[2]
  112. build_queue_name = args[3]
  113. build_queue_codename = args[4]
  114. build_queue_archive_name = args[5]
  115. try:
  116. suite = session.query(Suite).filter_by(suite_name=suite_name).one()
  117. except NoResultFound:
  118. die("E: Unknown suite '{0}'".format(suite_name))
  119. try:
  120. build_queue_archive = session.query(Archive).filter_by(archive_name=build_queue_archive_name).one()
  121. except NoResultFound:
  122. die("E: Unknown archive '{0}'".format(build_queue_archive_name))
  123. # Create suite
  124. s = Suite()
  125. s.suite_name = build_queue_name
  126. s.origin = suite.origin
  127. s.label = suite.label
  128. s.description = "buildd {0} incoming".format(suite_name)
  129. s.codename = build_queue_codename
  130. s.notautomatic = suite.notautomatic
  131. s.overridesuite = suite.overridesuite or suite.suite_name
  132. s.butautomaticupgrades = suite.butautomaticupgrades
  133. s.signingkeys = suite.signingkeys
  134. s.include_long_description = False
  135. s.archive = build_queue_archive
  136. s.architectures.extend(suite.architectures)
  137. s.components.extend(suite.components)
  138. s.srcformats.extend(suite.srcformats)
  139. session.add(s)
  140. session.flush()
  141. bq = BuildQueue()
  142. bq.queue_name = build_queue_codename
  143. bq.suite = s
  144. session.add(bq)
  145. session.flush()
  146. suite.copy_queues.append(bq)
  147. session.commit()
  148. def suite_architecture_list(self, d, args):
  149. s = d.session()
  150. for j in s.query(Suite).order_by(Suite.suite_name):
  151. architectures = j.get_architectures(skipsrc = True, skipall = True)
  152. print j.suite_name + ': ' + \
  153. ', '.join([a.arch_string for a in architectures])
  154. def suite_architecture_listarch(self, d, args):
  155. die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
  156. suite = get_suite(args[2].lower(), d.session())
  157. if suite is None:
  158. die('E: suite %s is invalid' % args[2].lower())
  159. a = suite.get_architectures(skipsrc = True, skipall = True)
  160. for j in a:
  161. print j.arch_string
  162. def suite_architecture_listsuite(self, d, args):
  163. die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
  164. architecture = get_architecture(args[2].lower(), d.session())
  165. if architecture is None:
  166. die("E: architecture %s is invalid" % args[2].lower())
  167. for j in architecture.suites:
  168. print j.suite_name
  169. def suite_architecture_add(self, d, args):
  170. if len(args) < 3:
  171. die("E: adding a suite-architecture entry requires a suite and arch")
  172. s = d.session()
  173. suite = get_suite(args[2].lower(), s)
  174. if suite is None: die("E: Can't find suite %s" % args[2].lower())
  175. for arch_name in args[3:]:
  176. arch = get_architecture(arch_name.lower(), s)
  177. if arch is None: die("E: Can't find architecture %s" % args[3].lower())
  178. try:
  179. suite.architectures.append(arch)
  180. s.flush()
  181. except IntegrityError as e:
  182. die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), arch_name))
  183. except SQLAlchemyError as e:
  184. die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), arch_name, e))
  185. print "Added suite-architecture entry for %s, %s" % (args[2].lower(), arch_name)
  186. if not dryrun:
  187. s.commit()
  188. s.close()
  189. def suite_architecture_rm(self, d, args):
  190. if len(args) < 3:
  191. die("E: removing an suite-architecture entry requires a suite and arch")
  192. s = d.session()
  193. if not dryrun:
  194. try:
  195. suite_name = args[2].lower()
  196. suite = get_suite(suite_name, s)
  197. if suite is None:
  198. die('E: no such suite %s' % suite_name)
  199. arch_string = args[3].lower()
  200. architecture = get_architecture(arch_string, s)
  201. if architecture not in suite.architectures:
  202. die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
  203. suite.architectures.remove(architecture)
  204. s.commit()
  205. except IntegrityError as e:
  206. die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
  207. except SQLAlchemyError as e:
  208. die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
  209. print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
  210. def suite_component_list(self, d, args):
  211. s = d.session()
  212. for j in s.query(Suite).order_by(Suite.suite_name):
  213. components = j.components
  214. print j.suite_name + ': ' + \
  215. ', '.join([c.component_name for c in components])
  216. def suite_component_listcomponent(self, d, args):
  217. die_arglen(args, 3, "E: suite-component list-component requires a suite")
  218. suite = get_suite(args[2].lower(), d.session())
  219. if suite is None:
  220. die('E: suite %s is invalid' % args[2].lower())
  221. for c in suite.components:
  222. print c.component_name
  223. def suite_component_listsuite(self, d, args):
  224. die_arglen(args, 3, "E: suite-component list-suite requires an component")
  225. component = get_component(args[2].lower(), d.session())
  226. if component is None:
  227. die("E: component %s is invalid" % args[2].lower())
  228. for s in component.suites:
  229. print s.suite_name
  230. def suite_component_add(self, d, args):
  231. if len(args) < 3:
  232. die("E: adding a suite-component entry requires a suite and component")
  233. s = d.session()
  234. suite = get_suite(args[2].lower(), s)
  235. if suite is None: die("E: Can't find suite %s" % args[2].lower())
  236. for component_name in args[3:]:
  237. component = get_component(component_name.lower(), s)
  238. if component is None: die("E: Can't find component %s" % args[3].lower())
  239. try:
  240. suite.components.append(component)
  241. s.flush()
  242. except IntegrityError as e:
  243. die("E: Can't add suite-component entry (%s, %s) - probably already exists" % (args[2].lower(), component_name))
  244. except SQLAlchemyError as e:
  245. die("E: Can't add suite-component entry (%s, %s) - %s" % (args[2].lower(), component_name, e))
  246. print "Added suite-component entry for %s, %s" % (args[2].lower(), component_name)
  247. if not dryrun:
  248. s.commit()
  249. s.close()
  250. def suite_component_rm(self, d, args):
  251. if len(args) < 3:
  252. die("E: removing an suite-component entry requires a suite and component")
  253. s = d.session()
  254. if not dryrun:
  255. try:
  256. suite_name = args[2].lower()
  257. suite = get_suite(suite_name, s)
  258. if suite is None:
  259. die('E: no such suite %s' % suite_name)
  260. component_string = args[3].lower()
  261. component = get_component(arch_string, s)
  262. if component not in suite.components:
  263. die("E: component %s not found in suite %s" % (component_string, suite_name))
  264. suite.components.remove(component)
  265. s.commit()
  266. except IntegrityError as e:
  267. die("E: Can't remove suite-component entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
  268. except SQLAlchemyError as e:
  269. die("E: Can't remove suite-component entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
  270. print "Removed suite-component entry for %s, %s" % (args[2].lower(), args[3].lower())