openshift_upgrade_config.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # vim: expandtab:tabstop=4:shiftwidth=4
  4. """Ansible module for modifying OpenShift configs during an upgrade"""
  5. import os
  6. import yaml
  7. DOCUMENTATION = '''
  8. ---
  9. module: openshift_upgrade_config
  10. short_description: OpenShift Upgrade Config
  11. author: Jason DeTiberus
  12. requirements: [ ]
  13. '''
  14. EXAMPLES = '''
  15. '''
  16. def modify_api_levels(level_list, remove, ensure, msg_prepend='',
  17. msg_append=''):
  18. """ modify_api_levels """
  19. changed = False
  20. changes = []
  21. if not isinstance(remove, list):
  22. remove = []
  23. if not isinstance(ensure, list):
  24. ensure = []
  25. if not isinstance(level_list, list):
  26. new_list = []
  27. changed = True
  28. changes.append("%s created missing %s" % (msg_prepend, msg_append))
  29. else:
  30. new_list = level_list
  31. for level in remove:
  32. if level in new_list:
  33. new_list.remove(level)
  34. changed = True
  35. changes.append("%s removed %s %s" % (msg_prepend, level, msg_append))
  36. for level in ensure:
  37. if level not in new_list:
  38. new_list.append(level)
  39. changed = True
  40. changes.append("%s added %s %s" % (msg_prepend, level, msg_append))
  41. return {'new_list': new_list, 'changed': changed, 'changes': changes}
  42. def upgrade_master_3_0_to_3_1(ansible_module, config_base, backup):
  43. """Main upgrade method for 3.0 to 3.1."""
  44. changes = []
  45. # Facts do not get transferred to the hosts where custom modules run,
  46. # need to make some assumptions here.
  47. master_config = os.path.join(config_base, 'master/master-config.yaml')
  48. master_cfg_file = open(master_config, 'r')
  49. config = yaml.safe_load(master_cfg_file.read())
  50. master_cfg_file.close()
  51. # Remove unsupported api versions and ensure supported api versions from
  52. # master config
  53. unsupported_levels = ['v1beta1', 'v1beta2', 'v1beta3']
  54. supported_levels = ['v1']
  55. result = modify_api_levels(config.get('apiLevels'), unsupported_levels,
  56. supported_levels, 'master-config.yaml:', 'from apiLevels')
  57. if result['changed']:
  58. config['apiLevels'] = result['new_list']
  59. changes.append(result['changes'])
  60. if 'kubernetesMasterConfig' in config and 'apiLevels' in config['kubernetesMasterConfig']:
  61. config['kubernetesMasterConfig'].pop('apiLevels')
  62. changes.append('master-config.yaml: removed kubernetesMasterConfig.apiLevels')
  63. # Add proxyClientInfo to master-config
  64. if 'proxyClientInfo' not in config['kubernetesMasterConfig']:
  65. config['kubernetesMasterConfig']['proxyClientInfo'] = {
  66. 'certFile': 'master.proxy-client.crt',
  67. 'keyFile': 'master.proxy-client.key'
  68. }
  69. changes.append("master-config.yaml: added proxyClientInfo")
  70. if len(changes) > 0:
  71. if backup:
  72. # TODO: Check success:
  73. ansible_module.backup_local(master_config)
  74. # Write the modified config:
  75. out_file = open(master_config, 'w')
  76. out_file.write(yaml.safe_dump(config, default_flow_style=False))
  77. out_file.close()
  78. return changes
  79. def upgrade_master(ansible_module, config_base, from_version, to_version, backup):
  80. """Upgrade entry point."""
  81. if from_version == '3.0':
  82. if to_version == '3.1':
  83. return upgrade_master_3_0_to_3_1(ansible_module, config_base, backup)
  84. def main():
  85. """ main """
  86. # disabling pylint errors for global-variable-undefined and invalid-name
  87. # for 'global module' usage, since it is required to use ansible_facts
  88. # pylint: disable=global-variable-undefined, invalid-name,
  89. # redefined-outer-name
  90. global module
  91. module = AnsibleModule(
  92. argument_spec=dict(
  93. config_base=dict(required=True),
  94. from_version=dict(required=True, choices=['3.0']),
  95. to_version=dict(required=True, choices=['3.1']),
  96. role=dict(required=True, choices=['master']),
  97. backup=dict(required=False, default=True, type='bool')
  98. ),
  99. supports_check_mode=True,
  100. )
  101. from_version = module.params['from_version']
  102. to_version = module.params['to_version']
  103. role = module.params['role']
  104. backup = module.params['backup']
  105. config_base = module.params['config_base']
  106. try:
  107. changes = []
  108. if role == 'master':
  109. changes = upgrade_master(module, config_base, from_version,
  110. to_version, backup)
  111. changed = len(changes) > 0
  112. return module.exit_json(changed=changed, changes=changes)
  113. # ignore broad-except error to avoid stack trace to ansible user
  114. # pylint: disable=broad-except
  115. except Exception, e:
  116. return module.fail_json(msg=str(e))
  117. # ignore pylint errors related to the module_utils import
  118. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import
  119. # import module snippets
  120. from ansible.module_utils.basic import *
  121. if __name__ == '__main__':
  122. main()