check_yum_update.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. '''
  4. Ansible module to test whether a yum update or install will succeed,
  5. without actually performing it or running yum.
  6. parameters:
  7. packages: (optional) A list of package names to install or update.
  8. If omitted, all installed RPMs are considered for updates.
  9. '''
  10. # import os
  11. import sys
  12. import yum # pylint: disable=import-error
  13. from ansible.module_utils.basic import AnsibleModule
  14. def main(): # pylint: disable=missing-docstring,too-many-branches
  15. module = AnsibleModule(
  16. argument_spec=dict(
  17. packages=dict(type='list', default=[])
  18. ),
  19. supports_check_mode=True
  20. )
  21. # NOTE(rhcarvalho): sosiouxme added _unmute, but I couldn't find a case yet
  22. # for when it is actually necessary. Leaving it commented out for now,
  23. # though this comment and the commented out code related to _unmute should
  24. # be deleted later if not proven necessary.
  25. # sys.stdout = os.devnull # mute yum so it doesn't break our output
  26. # def _unmute(): # pylint: disable=missing-docstring
  27. # sys.stdout = sys.__stdout__
  28. def bail(error): # pylint: disable=missing-docstring
  29. # _unmute()
  30. module.fail_json(msg=error)
  31. yb = yum.YumBase() # pylint: disable=invalid-name
  32. # determine if the existing yum configuration is valid
  33. try:
  34. yb.repos.populateSack(mdtype='metadata', cacheonly=1)
  35. # for error of type:
  36. # 1. can't reach the repo URL(s)
  37. except yum.Errors.NoMoreMirrorsRepoError as e: # pylint: disable=invalid-name
  38. bail('Error getting data from at least one yum repository: %s' % e)
  39. # 2. invalid repo definition
  40. except yum.Errors.RepoError as e: # pylint: disable=invalid-name
  41. bail('Error with yum repository configuration: %s' % e)
  42. # 3. other/unknown
  43. # * just report the problem verbatim
  44. except: # pylint: disable=bare-except; # noqa
  45. bail('Unexpected error with yum repository: %s' % sys.exc_info()[1])
  46. packages = module.params['packages']
  47. no_such_pkg = []
  48. for pkg in packages:
  49. try:
  50. yb.install(name=pkg)
  51. except yum.Errors.InstallError as e: # pylint: disable=invalid-name
  52. no_such_pkg.append(pkg)
  53. except: # pylint: disable=bare-except; # noqa
  54. bail('Unexpected error with yum install/update: %s' %
  55. sys.exc_info()[1])
  56. if not packages:
  57. # no packages requested means test a yum update of everything
  58. yb.update()
  59. elif no_such_pkg:
  60. # wanted specific packages to install but some aren't available
  61. user_msg = 'Cannot install all of the necessary packages. Unavailable:\n'
  62. for pkg in no_such_pkg:
  63. user_msg += ' %s\n' % pkg
  64. user_msg += 'You may need to enable one or more yum repositories to make this content available.'
  65. bail(user_msg)
  66. try:
  67. txn_result, txn_msgs = yb.buildTransaction()
  68. except: # pylint: disable=bare-except; # noqa
  69. bail('Unexpected error during dependency resolution for yum update: \n %s' %
  70. sys.exc_info()[1])
  71. # find out if there are any errors with the update/install
  72. if txn_result == 0: # 'normal exit' meaning there's nothing to install/update
  73. pass
  74. elif txn_result == 1: # error with transaction
  75. user_msg = 'Could not perform a yum update.\n'
  76. if len(txn_msgs) > 0:
  77. user_msg += 'Errors from dependency resolution:\n'
  78. for msg in txn_msgs:
  79. user_msg += ' %s\n' % msg
  80. user_msg += 'You should resolve these issues before proceeding with an install.\n'
  81. user_msg += 'You may need to remove or downgrade packages or enable/disable yum repositories.'
  82. bail(user_msg)
  83. # TODO: it would be nice depending on the problem:
  84. # 1. dependency for update not found
  85. # * construct the dependency tree
  86. # * find the installed package(s) that required the missing dep
  87. # * determine if any of these packages matter to openshift
  88. # * build helpful error output
  89. # 2. conflicts among packages in available content
  90. # * analyze dependency tree and build helpful error output
  91. # 3. other/unknown
  92. # * report the problem verbatim
  93. # * add to this list as we come across problems we can clearly diagnose
  94. elif txn_result == 2: # everything resolved fine
  95. pass
  96. else:
  97. bail('Unknown error(s) from dependency resolution. Exit Code: %d:\n%s' %
  98. (txn_result, txn_msgs))
  99. # _unmute()
  100. module.exit_json(changed=False)
  101. if __name__ == '__main__':
  102. main()