aos_version.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. '''
  4. An ansible module for determining if more than one minor version
  5. of any atomic-openshift package is available, which would indicate
  6. that multiple repos are enabled for different versions of the same
  7. thing which may cause problems.
  8. Also, determine if the version requested is available down to the
  9. precision requested.
  10. '''
  11. # import os
  12. # import sys
  13. import yum # pylint: disable=import-error
  14. from ansible.module_utils.basic import AnsibleModule
  15. def main(): # pylint: disable=missing-docstring
  16. module = AnsibleModule(
  17. argument_spec=dict(
  18. version=dict(required=True)
  19. ),
  20. supports_check_mode=True
  21. )
  22. # NOTE(rhcarvalho): sosiouxme added _unmute, but I couldn't find a case yet
  23. # for when it is actually necessary. Leaving it commented out for now,
  24. # though this comment and the commented out code related to _unmute should
  25. # be deleted later if not proven necessary.
  26. # sys.stdout = os.devnull # mute yum so it doesn't break our output
  27. # sys.stderr = os.devnull # mute yum so it doesn't break our output
  28. # def _unmute(): # pylint: disable=missing-docstring
  29. # sys.stdout = sys.__stdout__
  30. def bail(error): # pylint: disable=missing-docstring
  31. # _unmute()
  32. module.fail_json(msg=error)
  33. yb = yum.YumBase() # pylint: disable=invalid-name
  34. # search for package versions available for aos pkgs
  35. expected_pkgs = [
  36. 'atomic-openshift',
  37. 'atomic-openshift-master',
  38. 'atomic-openshift-node',
  39. ]
  40. try:
  41. pkgs = yb.pkgSack.returnPackages(patterns=expected_pkgs)
  42. except yum.Errors.PackageSackError as e: # pylint: disable=invalid-name
  43. # you only hit this if *none* of the packages are available
  44. bail('Unable to find any atomic-openshift packages. \nCheck your subscription and repo settings. \n%s' % e)
  45. # determine what level of precision we're expecting for the version
  46. expected_version = module.params['version']
  47. if expected_version.startswith('v'): # v3.3 => 3.3
  48. expected_version = expected_version[1:]
  49. num_dots = expected_version.count('.')
  50. pkgs_by_name_version = {}
  51. pkgs_precise_version_found = {}
  52. for pkg in pkgs:
  53. # get expected version precision
  54. match_version = '.'.join(pkg.version.split('.')[:num_dots + 1])
  55. if match_version == expected_version:
  56. pkgs_precise_version_found[pkg.name] = True
  57. # get x.y version precision
  58. minor_version = '.'.join(pkg.version.split('.')[:2])
  59. if pkg.name not in pkgs_by_name_version:
  60. pkgs_by_name_version[pkg.name] = {}
  61. pkgs_by_name_version[pkg.name][minor_version] = True
  62. # see if any packages couldn't be found at requested version
  63. # see if any packages are available in more than one minor version
  64. not_found = []
  65. multi_found = []
  66. for name in expected_pkgs:
  67. if name not in pkgs_precise_version_found:
  68. not_found.append(name)
  69. if name in pkgs_by_name_version and len(pkgs_by_name_version[name]) > 1:
  70. multi_found.append(name)
  71. if not_found:
  72. msg = 'Not all of the required packages are available at requested version %s:\n' % expected_version
  73. for name in not_found:
  74. msg += ' %s\n' % name
  75. bail(msg + 'Please check your subscriptions and enabled repositories.')
  76. if multi_found:
  77. msg = 'Multiple minor versions of these packages are available\n'
  78. for name in multi_found:
  79. msg += ' %s\n' % name
  80. bail(msg + "There should only be one OpenShift version's repository enabled at a time.")
  81. # _unmute()
  82. module.exit_json(changed=False)
  83. if __name__ == '__main__':
  84. main()