aos_version.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  13. module = AnsibleModule(
  14. argument_spec=dict(
  15. version=dict(required=True)
  16. ),
  17. supports_check_mode=True
  18. )
  19. def bail(error): # pylint: disable=missing-docstring
  20. module.fail_json(msg=error)
  21. yb = yum.YumBase() # pylint: disable=invalid-name
  22. # search for package versions available for aos pkgs
  23. expected_pkgs = [
  24. 'atomic-openshift',
  25. 'atomic-openshift-master',
  26. 'atomic-openshift-node',
  27. ]
  28. try:
  29. pkgs = yb.pkgSack.returnPackages(patterns=expected_pkgs)
  30. except yum.Errors.PackageSackError as e: # pylint: disable=invalid-name
  31. # you only hit this if *none* of the packages are available
  32. bail('Unable to find any atomic-openshift packages. \nCheck your subscription and repo settings. \n%s' % e)
  33. # determine what level of precision we're expecting for the version
  34. expected_version = module.params['version']
  35. if expected_version.startswith('v'): # v3.3 => 3.3
  36. expected_version = expected_version[1:]
  37. num_dots = expected_version.count('.')
  38. pkgs_by_name_version = {}
  39. pkgs_precise_version_found = {}
  40. for pkg in pkgs:
  41. # get expected version precision
  42. match_version = '.'.join(pkg.version.split('.')[:num_dots + 1])
  43. if match_version == expected_version:
  44. pkgs_precise_version_found[pkg.name] = True
  45. # get x.y version precision
  46. minor_version = '.'.join(pkg.version.split('.')[:2])
  47. if pkg.name not in pkgs_by_name_version:
  48. pkgs_by_name_version[pkg.name] = {}
  49. pkgs_by_name_version[pkg.name][minor_version] = True
  50. # see if any packages couldn't be found at requested version
  51. # see if any packages are available in more than one minor version
  52. not_found = []
  53. multi_found = []
  54. for name in expected_pkgs:
  55. if name not in pkgs_precise_version_found:
  56. not_found.append(name)
  57. if name in pkgs_by_name_version and len(pkgs_by_name_version[name]) > 1:
  58. multi_found.append(name)
  59. if not_found:
  60. msg = 'Not all of the required packages are available at requested version %s:\n' % expected_version
  61. for name in not_found:
  62. msg += ' %s\n' % name
  63. bail(msg + 'Please check your subscriptions and enabled repositories.')
  64. if multi_found:
  65. msg = 'Multiple minor versions of these packages are available\n'
  66. for name in multi_found:
  67. msg += ' %s\n' % name
  68. bail(msg + "There should only be one OpenShift version's repository enabled at a time.")
  69. module.exit_json(changed=False)
  70. if __name__ == '__main__':
  71. main()