sanity_checks.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. Ansible action plugin to ensure inventory variables are set
  3. appropriately and no conflicting options have been provided.
  4. """
  5. from ansible.plugins.action import ActionBase
  6. from ansible import errors
  7. # Tuple of variable names and default values if undefined.
  8. NET_PLUGIN_LIST = (('openshift_use_openshift_sdn', True),
  9. ('openshift_use_flannel', False),
  10. ('openshift_use_nuage', False),
  11. ('openshift_use_contiv', False),
  12. ('openshift_use_calico', False))
  13. def to_bool(var_to_check):
  14. """Determine a boolean value given the multiple
  15. ways bools can be specified in ansible."""
  16. yes_list = (True, 1, "True", "1", "true", "Yes", "yes")
  17. return var_to_check in yes_list
  18. class ActionModule(ActionBase):
  19. """Action plugin to execute sanity checks."""
  20. def template_var(self, hostvars, host, varname):
  21. """Retrieve a variable from hostvars and template it.
  22. If undefined, return None type."""
  23. res = hostvars[host].get(varname)
  24. if res is None:
  25. return None
  26. return self._templar.template(res)
  27. def network_plugin_check(self, hostvars, host):
  28. """Ensure only one type of network plugin is enabled"""
  29. res = []
  30. # Loop through each possible network plugin boolean, determine the
  31. # actual boolean value, and append results into a list.
  32. for plugin, default_val in NET_PLUGIN_LIST:
  33. res_temp = self.template_var(hostvars, host, plugin)
  34. if res_temp is None:
  35. res_temp = default_val
  36. res.append(to_bool(res_temp))
  37. if sum(res) != 1:
  38. plugin_str = list(zip([x[0] for x in NET_PLUGIN_LIST], res))
  39. msg = "Host Checked: {} Only one of must be true. Found: {}".format(host, plugin_str)
  40. raise errors.AnsibleModuleError(msg)
  41. def check_hostname_vars(self, hostvars, host):
  42. """Checks to ensure openshift_hostname
  43. and openshift_public_hostname
  44. conform to the proper length of 63 characters or less"""
  45. for varname in ('openshift_public_hostname', 'openshift_hostname'):
  46. var_value = self.template_var(hostvars, host, varname)
  47. if var_value and len(var_value) > 63:
  48. msg = '{} must be 63 characters or less'.format(varname)
  49. raise errors.AnsibleModuleError(msg)
  50. def run_checks(self, hostvars, host):
  51. """Execute the hostvars validations against host"""
  52. # msg = hostvars[host]['ansible_default_ipv4']
  53. self.network_plugin_check(hostvars, host)
  54. self.check_hostname_vars(hostvars, host)
  55. def run(self, tmp=None, task_vars=None):
  56. result = super(ActionModule, self).run(tmp, task_vars)
  57. # self.task_vars holds all in-scope variables.
  58. # Ignore settting self.task_vars outside of init.
  59. # pylint: disable=W0201
  60. self.task_vars = task_vars or {}
  61. # self._task.args holds task parameters.
  62. # check_hosts is a parameter to this plugin, and should provide
  63. # a list of hosts.
  64. check_hosts = self._task.args.get('check_hosts')
  65. if not check_hosts:
  66. msg = "check_hosts is required"
  67. raise errors.AnsibleModuleError(msg)
  68. # We need to access each host's variables
  69. hostvars = self.task_vars.get('hostvars')
  70. if not hostvars:
  71. msg = hostvars
  72. raise errors.AnsibleModuleError(msg)
  73. # We loop through each host in the provided list check_hosts
  74. for host in check_hosts:
  75. self.run_checks(hostvars, host)
  76. result["changed"] = False
  77. result["failed"] = False
  78. result["msg"] = "Sanity Checks passed"
  79. return result