aos_version.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. '''
  4. Ansible module for determining if multiple versions of an OpenShift package are
  5. available, and if the version requested is available down to the given
  6. precision.
  7. Multiple versions available suggest that multiple repos are enabled for the
  8. different versions, which may cause installation problems.
  9. '''
  10. import yum # pylint: disable=import-error
  11. from ansible.module_utils.basic import AnsibleModule
  12. def main(): # pylint: disable=missing-docstring,too-many-branches
  13. module = AnsibleModule(
  14. argument_spec=dict(
  15. prefix=dict(required=True), # atomic-openshift, origin, ...
  16. version=dict(required=True),
  17. ),
  18. supports_check_mode=True
  19. )
  20. def bail(error): # pylint: disable=missing-docstring
  21. module.fail_json(msg=error)
  22. rpm_prefix = module.params['prefix']
  23. if not rpm_prefix:
  24. bail("prefix must not be empty")
  25. yb = yum.YumBase() # pylint: disable=invalid-name
  26. # search for package versions available for aos pkgs
  27. expected_pkgs = [
  28. rpm_prefix,
  29. rpm_prefix + '-master',
  30. rpm_prefix + '-node',
  31. ]
  32. try:
  33. pkgs = yb.pkgSack.returnPackages(patterns=expected_pkgs)
  34. except yum.Errors.PackageSackError as e: # pylint: disable=invalid-name
  35. # you only hit this if *none* of the packages are available
  36. bail('Unable to find any OpenShift packages.\nCheck your subscription and repo settings.\n%s' % e)
  37. # determine what level of precision we're expecting for the version
  38. expected_version = module.params['version']
  39. if expected_version.startswith('v'): # v3.3 => 3.3
  40. expected_version = expected_version[1:]
  41. num_dots = expected_version.count('.')
  42. pkgs_by_name_version = {}
  43. pkgs_precise_version_found = {}
  44. for pkg in pkgs:
  45. # get expected version precision
  46. match_version = '.'.join(pkg.version.split('.')[:num_dots + 1])
  47. if match_version == expected_version:
  48. pkgs_precise_version_found[pkg.name] = True
  49. # get x.y version precision
  50. minor_version = '.'.join(pkg.version.split('.')[:2])
  51. if pkg.name not in pkgs_by_name_version:
  52. pkgs_by_name_version[pkg.name] = {}
  53. pkgs_by_name_version[pkg.name][minor_version] = True
  54. # see if any packages couldn't be found at requested version
  55. # see if any packages are available in more than one minor version
  56. not_found = []
  57. multi_found = []
  58. for name in expected_pkgs:
  59. if name not in pkgs_precise_version_found:
  60. not_found.append(name)
  61. if name in pkgs_by_name_version and len(pkgs_by_name_version[name]) > 1:
  62. multi_found.append(name)
  63. if not_found:
  64. msg = 'Not all of the required packages are available at requested version %s:\n' % expected_version
  65. for name in not_found:
  66. msg += ' %s\n' % name
  67. bail(msg + 'Please check your subscriptions and enabled repositories.')
  68. if multi_found:
  69. msg = 'Multiple minor versions of these packages are available\n'
  70. for name in multi_found:
  71. msg += ' %s\n' % name
  72. bail(msg + "There should only be one OpenShift version's repository enabled at a time.")
  73. module.exit_json(changed=False)
  74. if __name__ == '__main__':
  75. main()