openshift_version.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Custom version comparison filters for use in openshift-ansible
  5. """
  6. # pylint can't locate distutils.version within virtualenv
  7. # https://github.com/PyCQA/pylint/issues/73
  8. # pylint: disable=no-name-in-module, import-error
  9. from distutils.version import LooseVersion
  10. def legacy_gte_function_builder(name, versions):
  11. """
  12. Build and return a version comparison function.
  13. Ex: name = 'oo_version_gte_3_1_or_1_1'
  14. versions = {'enterprise': '3.1', 'origin': '1.1'}
  15. returns oo_version_gte_3_1_or_1_1, a function which based on the
  16. version and deployment type will return true if the provided
  17. version is greater than or equal to the function's version
  18. """
  19. enterprise_version = versions['enterprise']
  20. origin_version = versions['origin']
  21. def _gte_function(version, deployment_type):
  22. """
  23. Dynamic function created by gte_function_builder.
  24. Ex: version = '3.1'
  25. deployment_type = 'openshift-enterprise'
  26. returns True/False
  27. """
  28. version_gte = False
  29. if 'enterprise' in deployment_type:
  30. if str(version) >= LooseVersion(enterprise_version):
  31. version_gte = True
  32. elif 'origin' in deployment_type:
  33. if str(version) >= LooseVersion(origin_version):
  34. version_gte = True
  35. return version_gte
  36. _gte_function.__name__ = name
  37. return _gte_function
  38. def gte_function_builder(name, gte_version):
  39. """
  40. Build and return a version comparison function.
  41. Ex: name = 'oo_version_gte_3_6'
  42. version = '3.6'
  43. returns oo_version_gte_3_6, a function which based on the
  44. version will return true if the provided version is greater
  45. than or equal to the function's version
  46. """
  47. def _gte_function(version):
  48. """
  49. Dynamic function created by gte_function_builder.
  50. Ex: version = '3.1'
  51. returns True/False
  52. """
  53. version_gte = False
  54. if str(version) >= LooseVersion(gte_version):
  55. version_gte = True
  56. return version_gte
  57. _gte_function.__name__ = name
  58. return _gte_function
  59. # pylint: disable=too-few-public-methods
  60. class FilterModule(object):
  61. """
  62. Filters for version checking.
  63. """
  64. # Each element of versions is composed of (major, minor_start, minor_end)
  65. # Origin began versioning 3.x with 3.6, so begin 3.x with 3.6.
  66. versions = [(3, 6, 10)]
  67. def __init__(self):
  68. """
  69. Creates a new FilterModule for ose version checking.
  70. """
  71. self._filters = {}
  72. # For each set of (major, minor, minor_iterations)
  73. for major, minor_start, minor_end in self.versions:
  74. # For each minor version in the range
  75. for minor in range(minor_start, minor_end):
  76. # Create the function name
  77. func_name = 'oo_version_gte_{}_{}'.format(major, minor)
  78. # Create the function with the builder
  79. func = gte_function_builder(func_name, "{}.{}.0".format(major, minor))
  80. # Add the function to the mapping
  81. self._filters[func_name] = func
  82. # Create filters with special versioning requirements.
  83. # Treat all Origin 1.x as special case.
  84. legacy_filters = [{'name': 'oo_version_gte_3_1_or_1_1',
  85. 'versions': {'enterprise': '3.0.2.905',
  86. 'origin': '1.1.0'}},
  87. {'name': 'oo_version_gte_3_1_1_or_1_1_1',
  88. 'versions': {'enterprise': '3.1.1',
  89. 'origin': '1.1.1'}},
  90. {'name': 'oo_version_gte_3_2_or_1_2',
  91. 'versions': {'enterprise': '3.1.1.901',
  92. 'origin': '1.2.0'}},
  93. {'name': 'oo_version_gte_3_3_or_1_3',
  94. 'versions': {'enterprise': '3.3.0',
  95. 'origin': '1.3.0'}},
  96. {'name': 'oo_version_gte_3_4_or_1_4',
  97. 'versions': {'enterprise': '3.4.0',
  98. 'origin': '1.4.0'}},
  99. {'name': 'oo_version_gte_3_5_or_1_5',
  100. 'versions': {'enterprise': '3.5.0',
  101. 'origin': '1.5.0'}}]
  102. for legacy_filter in legacy_filters:
  103. self._filters[legacy_filter['name']] = legacy_gte_function_builder(legacy_filter['name'],
  104. legacy_filter['versions'])
  105. def filters(self):
  106. """
  107. Return the filters mapping.
  108. """
  109. return self._filters