version_builder.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. import optparse
  17. import os
  18. import subprocess
  19. import sys
  20. import xml.etree.ElementTree as ET
  21. class VersionBuilder:
  22. """
  23. Used to build a version definition file
  24. """
  25. def __init__(self, filename):
  26. self._check_xmllint()
  27. self.filename = filename
  28. if os.path.exists(filename):
  29. tree = ET.ElementTree()
  30. tree.parse(filename)
  31. root = tree.getroot()
  32. else:
  33. attribs = {}
  34. attribs['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
  35. attribs['xsi:noNamespaceSchemaLocation'] = "version_definition.xsd"
  36. root = ET.Element("repository-version", attribs)
  37. ET.SubElement(root, "release")
  38. ET.SubElement(root, "manifest")
  39. ET.SubElement(root, "available-services")
  40. ET.SubElement(root, "repository-info")
  41. self.root_element = root
  42. def persist(self):
  43. """
  44. Saves the XML file
  45. """
  46. p = subprocess.Popen(['xmllint', '--format', '--output', self.filename, '-'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
  47. (stdout, stderr) = p.communicate(input=ET.tostring(self.root_element))
  48. def finalize(self, xsd_file):
  49. """
  50. Validates the XML file against the XSD
  51. """
  52. args = ['xmllint', '--noout', '--load-trace', '--schema', xsd_file, self.filename]
  53. p = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  54. (stdout, stderr) = p.communicate()
  55. if p.returncode != 0:
  56. raise Exception(stderr)
  57. if len(stdout) > 0:
  58. print(stdout.decode("UTF-8"))
  59. if len(stderr) > 0:
  60. print(stderr.decode("UTF-8"))
  61. def set_release(self, type=None, stack=None, version=None, build=None, notes=None, display=None,
  62. compatible=None):
  63. """
  64. Create elements of the 'release' parent
  65. """
  66. release_element = self.root_element.find("./release")
  67. if release_element is None:
  68. raise Exception("Element 'release' is not found")
  69. if type:
  70. update_simple(release_element, "type", type)
  71. if stack:
  72. update_simple(release_element, "stack-id", stack)
  73. if version:
  74. update_simple(release_element, "version", version)
  75. if build:
  76. update_simple(release_element, "build", build)
  77. if compatible:
  78. update_simple(release_element, "compatible-with", compatible)
  79. if notes:
  80. update_simple(release_element, "release-notes", notes)
  81. if display:
  82. update_simple(release_element, "display", display)
  83. def set_os(self, os_family, package_version=None):
  84. repo_parent = self.root_element.find("./repository-info")
  85. if repo_parent is None:
  86. raise Exception("'repository-info' element is not found")
  87. os_element = self.findByAttributeValue(repo_parent, "./os", "family", os_family)
  88. if os_element is None:
  89. os_element = ET.SubElement(repo_parent, 'os')
  90. os_element.set('family', os_family)
  91. if package_version:
  92. pv_element = os_element.find("package-version")
  93. if pv_element is None:
  94. pv_element = ET.SubElement(os_element, "package-version")
  95. pv_element.text = package_version
  96. def add_manifest(self, id, service_name, version, version_id = None):
  97. """
  98. Add a manifest service. A manifest lists all services in a repo, whether they are to be
  99. upgraded or not.
  100. """
  101. manifest_element = self.root_element.find("./manifest")
  102. if manifest_element is None:
  103. raise Exception("Element 'manifest' is not found")
  104. service_element = self.findByAttributeValue(manifest_element, "./service", "id", id)
  105. if service_element is None:
  106. service_element = ET.SubElement(manifest_element, "service")
  107. service_element.set('id', id)
  108. service_element.set('name', service_name)
  109. service_element.set('version', version)
  110. if version_id:
  111. service_element.set('version-id', version_id)
  112. def add_available(self, manifest_id, available_components=None):
  113. """
  114. Adds services available to upgrade for patches
  115. """
  116. manifest_element = self.root_element.find("./manifest")
  117. if manifest_element is None:
  118. raise Exception("'manifest' element is not found")
  119. service_element = self.findByAttributeValue(manifest_element, "./service", "id", manifest_id)
  120. if service_element is None:
  121. raise Exception("Cannot add an available service for {0}; it's not on the manifest".format(manifest_id))
  122. available_element = self.root_element.find("./available-services")
  123. if available_element is None:
  124. raise Exception("'available-services' is not found")
  125. service_element = self.findByAttributeValue(available_element, "./service", "idref", manifest_id)
  126. if service_element is not None:
  127. available_element.remove(service_element)
  128. service_element = ET.SubElement(available_element, "service")
  129. service_element.set('idref', manifest_id)
  130. if available_components:
  131. components = available_components.split(',')
  132. for component in components:
  133. e = ET.SubElement(service_element, 'component')
  134. e.text = component
  135. def add_repo(self, os_family, repo_id, repo_name, base_url, unique):
  136. """
  137. Adds a repository
  138. """
  139. repo_parent = self.root_element.find("./repository-info")
  140. if repo_parent is None:
  141. raise Exception("'repository-info' element is not found")
  142. os_element = self.findByAttributeValue(repo_parent, "./os", "family", os_family)
  143. if os_element is None:
  144. os_element = ET.SubElement(repo_parent, 'os')
  145. os_element.set('family', os_family)
  146. if self.useNewSyntax():
  147. repo_element = os_element.find("./repo/[reponame='{0}']".format(repo_name))
  148. else:
  149. repo_element = self.findByValue(os_element, "./repo/reponame", repo_name)
  150. if repo_element is not None:
  151. os_element.remove(repo_element)
  152. repo_element = ET.SubElement(os_element, 'repo')
  153. e = ET.SubElement(repo_element, 'baseurl')
  154. e.text = base_url
  155. e = ET.SubElement(repo_element, 'repoid')
  156. e.text = repo_id
  157. e = ET.SubElement(repo_element, 'reponame')
  158. e.text = repo_name
  159. if unique is not None:
  160. e = ET.SubElement(repo_element, 'unique')
  161. e.text = unique
  162. def _check_xmllint(self):
  163. """
  164. Verifies utility xmllint is available
  165. """
  166. try:
  167. p = subprocess.Popen(['xmllint', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
  168. (stdout, stderr) = p.communicate()
  169. if p.returncode != 0:
  170. raise Exception("xmllint command does not appear to be available")
  171. except:
  172. raise Exception("xmllint command does not appear to be available")
  173. def findByAttributeValue(self, root, element, attribute, value):
  174. if self.useNewSyntax():
  175. return root.find("./{0}[@{1}='{2}']".format(element, attribute, value))
  176. else:
  177. for node in root.findall("{0}".format(element)):
  178. if node.attrib[attribute] == value:
  179. return node
  180. return None;
  181. def findByValue(self, root, element, value):
  182. for node in root.findall("{0}".format(element)):
  183. if node.text == value:
  184. return node
  185. return None
  186. def useNewSyntax(self):
  187. #Python2.7 and newer shipps with ElementTree that supports a different syntax for XPath queries
  188. major=sys.version_info[0]
  189. minor=sys.version_info[1]
  190. if major > 3 :
  191. return True
  192. elif major == 2:
  193. return (minor > 6)
  194. else:
  195. return False;
  196. def update_simple(parent, name, value):
  197. """
  198. Helper method to either update or create the element
  199. """
  200. element = parent.find('./' + name)
  201. if element is None:
  202. element = ET.SubElement(parent, name)
  203. element.text = value
  204. else:
  205. element.text = value
  206. def process_release(vb, options):
  207. """
  208. Create elements of the 'release' parent
  209. """
  210. if options.release_type:
  211. vb.set_release(type=options.release_type)
  212. if options.release_stack:
  213. vb.set_release(stack=options.release_stack)
  214. if options.release_version:
  215. vb.set_release(version=options.release_version)
  216. if options.release_build:
  217. vb.set_release(build=options.release_build)
  218. if options.release_compatible:
  219. vb.set_release(compatible=options.release_compatible)
  220. if options.release_notes:
  221. vb.set_release(notes=options.release_notes)
  222. if options.release_display:
  223. vb.set_release(display=options.release_display)
  224. if options.release_package_version:
  225. vb.set_release(package_version=options.release_package_version)
  226. def process_manifest(vb, options):
  227. """
  228. Creates the manifest element
  229. """
  230. if not options.manifest:
  231. return
  232. vb.add_manifest(options.manifest_id, options.manifest_service, options.manifest_version, options.manifest_version_id)
  233. def process_available(vb, options):
  234. """
  235. Processes available service elements
  236. """
  237. if not options.available:
  238. return
  239. vb.add_available(options.manifest_id, options.available_components)
  240. def process_os(vb, options):
  241. if not options.os:
  242. return
  243. vb.set_os(options.os_family, options.os_package_version)
  244. def process_repo(vb, options):
  245. """
  246. Processes repository options. This method doesn't update or create individual elements, it
  247. creates the entire repo structure
  248. """
  249. if not options.repo:
  250. return
  251. vb.add_repo(options.repo_os, options.repo_id, options.repo_name, options.repo_url, options.unique)
  252. def validate_manifest(parser, options):
  253. """
  254. Validates manifest options from the command line
  255. """
  256. if not options.manifest:
  257. return
  258. template = "When specifying --manifest, {0} is also required"
  259. if not options.manifest_id:
  260. parser.error(template.format("--manifest-id"))
  261. if not options.manifest_service:
  262. parser.error(template.format("--manifest-service"))
  263. if not options.manifest_version:
  264. parser.error(template.format("--manifest-version"))
  265. def validate_available(parser, options):
  266. """
  267. Validates available service options from the command line
  268. """
  269. if not options.available:
  270. return
  271. if not options.manifest_id:
  272. parser.error("When specifying --available, --manifest-id is also required")
  273. def validate_os(parser, options):
  274. if not options.os:
  275. return
  276. if not options.os_family:
  277. parser.error("When specifying --os, --os-family is also required")
  278. def validate_repo(parser, options):
  279. """
  280. Validates repo options from the command line
  281. """
  282. if not options.repo:
  283. return
  284. template = "When specifying --repo, {0} is also required"
  285. if not options.repo_os:
  286. parser.error(template.format("--repo-os"))
  287. if not options.repo_url:
  288. parser.error(template.format("--repo-url"))
  289. if not options.repo_id:
  290. parser.error(template.format("--repo-id"))
  291. if not options.repo_name:
  292. parser.error(template.format("--repo-name"))
  293. def main(argv):
  294. parser = optparse.OptionParser(
  295. epilog="OS utility 'xmllint' is required for this tool to function. It handles pretty-printing and XSD validation.")
  296. parser.add_option('--file', dest='filename',
  297. help="The output XML file")
  298. parser.add_option('--finalize', action='store_true', dest='finalize',
  299. help="Finalize and validate the XML file")
  300. parser.add_option('--xsd', dest='xsd_file',
  301. help="The XSD location when finalizing")
  302. parser.add_option('--release-type', type='choice', choices=['STANDARD', 'PATCH'], dest='release_type' ,
  303. help="Indicate the release type: i.e. STANDARD or PATCH")
  304. parser.add_option('--release-stack', dest='release_stack',
  305. help="The stack id: e.g. HDP-2.4")
  306. parser.add_option('--release-version', dest='release_version',
  307. help="The release version without build number: e.g. 2.4.0.1")
  308. parser.add_option('--release-build', dest='release_build',
  309. help="The release build number: e.g. 1234")
  310. parser.add_option('--release-compatible', dest='release_compatible',
  311. help="Regular Expression string to identify version compatibility for patches: e.g. 2.4.1.[0-9]")
  312. parser.add_option('--release-notes', dest='release_notes',
  313. help="A http link to the documentation notes")
  314. parser.add_option('--release-display', dest='release_display',
  315. help="The display name for this release")
  316. parser.add_option('--release-package-version', dest='release_package_version',
  317. help="Identifier to use when installing packages, generally a part of the package name")
  318. parser.add_option('--manifest', action='store_true', dest='manifest',
  319. help="Add a manifest service with other options: --manifest-id, --manifest-service, --manifest-version, --manifest-version-id")
  320. parser.add_option('--manifest-id', dest='manifest_id',
  321. help="Unique ID for a service in a manifest. Required when specifying --manifest and --available")
  322. parser.add_option('--manifest-service', dest='manifest_service')
  323. parser.add_option('--manifest-version', dest='manifest_version')
  324. parser.add_option('--manifest-version-id', dest='manifest_version_id')
  325. parser.add_option('--available', action='store_true', dest='available',
  326. help="Add an available service with other options: --manifest-id, --available-components")
  327. parser.add_option('--available-components', dest='available_components',
  328. help="A CSV of service components that are intended to be upgraded via patch. \
  329. Omitting this implies the entire service should be upgraded")
  330. parser.add_option('--os', action='store_true', dest='os', help="Add OS data with options --os-family, --os-package-version")
  331. parser.add_option('--os-family', dest='os_family', help="The operating system: i.e redhat7, debian7, ubuntu12, ubuntu14, suse11, suse12")
  332. parser.add_option('--os-package-version', dest='os_package_version',
  333. help="The package version to use for the OS")
  334. parser.add_option('--repo', action='store_true', dest='repo',
  335. help="Add repository data with options: --repo-os, --repo-url, --repo-id, --repo-name, --repo-unique")
  336. parser.add_option('--repo-os', dest='repo_os',
  337. help="The operating system type: i.e. redhat6, redhat7, debian7, ubuntu12, ubuntu14, ubuntu16, suse11, suse12")
  338. parser.add_option('--repo-url', dest='repo_url',
  339. help="The base url for the repository data")
  340. parser.add_option('--repo-unique', dest='unique', type='choice', choices=['true', 'false'],
  341. help="Indicates base url should be unique")
  342. parser.add_option('--repo-id', dest='repo_id', help="The ID of the repo")
  343. parser.add_option('--repo-name', dest='repo_name', help="The name of the repo")
  344. (options, args) = parser.parse_args()
  345. # validate_filename
  346. if not options.filename:
  347. parser.error("--file option is required")
  348. # validate_finalize
  349. if options.finalize and not options.xsd_file:
  350. parser.error("Must supply XSD (--xsd) when finalizing")
  351. validate_manifest(parser, options)
  352. validate_available(parser, options)
  353. validate_os(parser, options)
  354. validate_repo(parser, options)
  355. vb = VersionBuilder(options.filename)
  356. process_release(vb, options)
  357. process_manifest(vb, options)
  358. process_available(vb, options)
  359. process_os(vb, options)
  360. process_repo(vb, options)
  361. # save file
  362. vb.persist()
  363. if options.finalize:
  364. vb.finalize(options.xsd_file)
  365. if __name__ == "__main__":
  366. main(sys.argv)