check_yum_update.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 sys
  11. import yum # pylint: disable=import-error
  12. from ansible.module_utils.basic import AnsibleModule
  13. def main(): # pylint: disable=missing-docstring,too-many-branches
  14. module = AnsibleModule(
  15. argument_spec=dict(
  16. packages=dict(type='list', default=[])
  17. ),
  18. supports_check_mode=True
  19. )
  20. def bail(error): # pylint: disable=missing-docstring
  21. module.fail_json(msg=error)
  22. yb = yum.YumBase() # pylint: disable=invalid-name
  23. yb.conf.disable_excludes = ["all"] # assume the openshift excluder will be managed, ignore current state
  24. # determine if the existing yum configuration is valid
  25. try:
  26. yb.repos.populateSack(mdtype='metadata', cacheonly=1)
  27. # for error of type:
  28. # 1. can't reach the repo URL(s)
  29. except yum.Errors.NoMoreMirrorsRepoError as e: # pylint: disable=invalid-name
  30. bail('Error getting data from at least one yum repository: %s' % e)
  31. # 2. invalid repo definition
  32. except yum.Errors.RepoError as e: # pylint: disable=invalid-name
  33. bail('Error with yum repository configuration: %s' % e)
  34. # 3. other/unknown
  35. # * just report the problem verbatim
  36. except: # pylint: disable=bare-except; # noqa
  37. bail('Unexpected error with yum repository: %s' % sys.exc_info()[1])
  38. packages = module.params['packages']
  39. no_such_pkg = []
  40. for pkg in packages:
  41. try:
  42. yb.install(name=pkg)
  43. except yum.Errors.InstallError as e: # pylint: disable=invalid-name
  44. no_such_pkg.append(pkg)
  45. except: # pylint: disable=bare-except; # noqa
  46. bail('Unexpected error with yum install/update: %s' %
  47. sys.exc_info()[1])
  48. if not packages:
  49. # no packages requested means test a yum update of everything
  50. yb.update()
  51. elif no_such_pkg:
  52. # wanted specific packages to install but some aren't available
  53. user_msg = 'Cannot install all of the necessary packages. Unavailable:\n'
  54. for pkg in no_such_pkg:
  55. user_msg += ' %s\n' % pkg
  56. user_msg += 'You may need to enable one or more yum repositories to make this content available.'
  57. bail(user_msg)
  58. try:
  59. txn_result, txn_msgs = yb.buildTransaction()
  60. except: # pylint: disable=bare-except; # noqa
  61. bail('Unexpected error during dependency resolution for yum update: \n %s' %
  62. sys.exc_info()[1])
  63. # find out if there are any errors with the update/install
  64. if txn_result == 0: # 'normal exit' meaning there's nothing to install/update
  65. pass
  66. elif txn_result == 1: # error with transaction
  67. user_msg = 'Could not perform a yum update.\n'
  68. if len(txn_msgs) > 0:
  69. user_msg += 'Errors from dependency resolution:\n'
  70. for msg in txn_msgs:
  71. user_msg += ' %s\n' % msg
  72. user_msg += 'You should resolve these issues before proceeding with an install.\n'
  73. user_msg += 'You may need to remove or downgrade packages or enable/disable yum repositories.'
  74. bail(user_msg)
  75. # TODO: it would be nice depending on the problem:
  76. # 1. dependency for update not found
  77. # * construct the dependency tree
  78. # * find the installed package(s) that required the missing dep
  79. # * determine if any of these packages matter to openshift
  80. # * build helpful error output
  81. # 2. conflicts among packages in available content
  82. # * analyze dependency tree and build helpful error output
  83. # 3. other/unknown
  84. # * report the problem verbatim
  85. # * add to this list as we come across problems we can clearly diagnose
  86. elif txn_result == 2: # everything resolved fine
  87. pass
  88. else:
  89. bail('Unknown error(s) from dependency resolution. Exit Code: %d:\n%s' %
  90. (txn_result, txn_msgs))
  91. module.exit_json(changed=False)
  92. if __name__ == '__main__':
  93. main()