openshift_ansible.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name,global-statement,global-variable-not-assigned
  2. from __future__ import (absolute_import, print_function)
  3. import socket
  4. import subprocess
  5. import sys
  6. import os
  7. import logging
  8. import yaml
  9. from ooinstall.variants import find_variant
  10. from ooinstall.utils import debug_env
  11. installer_log = logging.getLogger('installer')
  12. CFG = None
  13. ROLES_TO_GROUPS_MAP = {
  14. 'master': 'masters',
  15. 'node': 'nodes',
  16. 'etcd': 'etcd',
  17. 'storage': 'nfs',
  18. 'master_lb': 'lb'
  19. }
  20. VARIABLES_MAP = {
  21. 'ansible_ssh_user': 'ansible_ssh_user',
  22. 'deployment_type': 'deployment_type',
  23. 'variant_subtype': 'deployment_subtype',
  24. 'master_routingconfig_subdomain': 'openshift_master_default_subdomain',
  25. 'proxy_http': 'openshift_http_proxy',
  26. 'proxy_https': 'openshift_https_proxy',
  27. 'proxy_exclude_hosts': 'openshift_no_proxy',
  28. }
  29. HOST_VARIABLES_MAP = {
  30. 'ip': 'openshift_ip',
  31. 'public_ip': 'openshift_public_ip',
  32. 'hostname': 'openshift_hostname',
  33. 'public_hostname': 'openshift_public_hostname',
  34. 'containerized': 'containerized',
  35. }
  36. def set_config(cfg):
  37. global CFG
  38. CFG = cfg
  39. def generate_inventory(hosts):
  40. global CFG
  41. new_nodes = [host for host in hosts if host.is_node() and host.new_host]
  42. scaleup = len(new_nodes) > 0
  43. lb = determine_lb_configuration(hosts)
  44. base_inventory_path = CFG.settings['ansible_inventory_path']
  45. base_inventory = open(base_inventory_path, 'w')
  46. write_inventory_children(base_inventory, scaleup)
  47. write_inventory_vars(base_inventory, lb)
  48. # write_inventory_hosts
  49. for role in CFG.deployment.roles:
  50. # write group block
  51. group = ROLES_TO_GROUPS_MAP.get(role, role)
  52. base_inventory.write("\n[{}]\n".format(group))
  53. # write each host
  54. group_hosts = [host for host in hosts if role in host.roles]
  55. for host in group_hosts:
  56. schedulable = host.is_schedulable_node(hosts)
  57. write_host(host, role, base_inventory, schedulable)
  58. if scaleup:
  59. base_inventory.write('\n[new_nodes]\n')
  60. for node in new_nodes:
  61. write_host(node, 'new_nodes', base_inventory)
  62. base_inventory.close()
  63. return base_inventory_path
  64. def determine_lb_configuration(hosts):
  65. lb = next((host for host in hosts if host.is_master_lb()), None)
  66. if lb:
  67. if lb.hostname is None:
  68. lb.hostname = lb.connect_to
  69. lb.public_hostname = lb.connect_to
  70. return lb
  71. def write_inventory_children(base_inventory, scaleup):
  72. global CFG
  73. base_inventory.write('\n[OSEv3:children]\n')
  74. for role in CFG.deployment.roles:
  75. child = ROLES_TO_GROUPS_MAP.get(role, role)
  76. base_inventory.write('{}\n'.format(child))
  77. if scaleup:
  78. base_inventory.write('new_nodes\n')
  79. # pylint: disable=too-many-branches
  80. def write_inventory_vars(base_inventory, lb):
  81. global CFG
  82. base_inventory.write('\n[OSEv3:vars]\n')
  83. for variable, value in CFG.settings.items():
  84. inventory_var = VARIABLES_MAP.get(variable, None)
  85. if inventory_var and value:
  86. base_inventory.write('{}={}\n'.format(inventory_var, value))
  87. for variable, value in CFG.deployment.variables.items():
  88. inventory_var = VARIABLES_MAP.get(variable, variable)
  89. if value:
  90. base_inventory.write('{}={}\n'.format(inventory_var, value))
  91. if CFG.deployment.variables['ansible_ssh_user'] != 'root':
  92. base_inventory.write('ansible_become=yes\n')
  93. base_inventory.write('openshift_hostname_check=false\n')
  94. if lb is not None:
  95. base_inventory.write("openshift_master_cluster_hostname={}\n".format(lb.hostname))
  96. base_inventory.write(
  97. "openshift_master_cluster_public_hostname={}\n".format(lb.public_hostname))
  98. if CFG.settings.get('variant_version', None) == '3.1':
  99. # base_inventory.write('openshift_image_tag=v{}\n'.format(CFG.settings.get('variant_version')))
  100. base_inventory.write('openshift_image_tag=v{}\n'.format('3.1.1.6'))
  101. write_proxy_settings(base_inventory)
  102. # Find the correct deployment type for ansible:
  103. ver = find_variant(CFG.settings['variant'],
  104. version=CFG.settings.get('variant_version', None))[1]
  105. base_inventory.write('deployment_type={}\n'.format(ver.ansible_key))
  106. if getattr(ver, 'variant_subtype', False):
  107. base_inventory.write('deployment_subtype={}\n'.format(ver.deployment_subtype))
  108. if 'OO_INSTALL_ADDITIONAL_REGISTRIES' in os.environ:
  109. base_inventory.write('openshift_docker_additional_registries={}\n'.format(
  110. os.environ['OO_INSTALL_ADDITIONAL_REGISTRIES']))
  111. if 'OO_INSTALL_INSECURE_REGISTRIES' in os.environ:
  112. base_inventory.write('openshift_docker_insecure_registries={}\n'.format(
  113. os.environ['OO_INSTALL_INSECURE_REGISTRIES']))
  114. if 'OO_INSTALL_PUDDLE_REPO' in os.environ:
  115. # We have to double the '{' here for literals
  116. base_inventory.write("openshift_additional_repos=[{{'id': 'ose-devel', "
  117. "'name': 'ose-devel', "
  118. "'baseurl': '{}', "
  119. "'enabled': 1, 'gpgcheck': 0}}]\n".format(os.environ['OO_INSTALL_PUDDLE_REPO']))
  120. for name, role_obj in CFG.deployment.roles.items():
  121. if role_obj.variables:
  122. group_name = ROLES_TO_GROUPS_MAP.get(name, name)
  123. base_inventory.write("\n[{}:vars]\n".format(group_name))
  124. for variable, value in role_obj.variables.items():
  125. inventory_var = VARIABLES_MAP.get(variable, variable)
  126. if value:
  127. base_inventory.write('{}={}\n'.format(inventory_var, value))
  128. base_inventory.write("\n")
  129. def write_proxy_settings(base_inventory):
  130. try:
  131. base_inventory.write("openshift_http_proxy={}\n".format(
  132. CFG.settings['openshift_http_proxy']))
  133. except KeyError:
  134. pass
  135. try:
  136. base_inventory.write("openshift_https_proxy={}\n".format(
  137. CFG.settings['openshift_https_proxy']))
  138. except KeyError:
  139. pass
  140. try:
  141. base_inventory.write("openshift_no_proxy={}\n".format(
  142. CFG.settings['openshift_no_proxy']))
  143. except KeyError:
  144. pass
  145. def write_host(host, role, inventory, schedulable=None):
  146. global CFG
  147. if host.preconfigured:
  148. return
  149. facts = ''
  150. for prop in HOST_VARIABLES_MAP:
  151. if getattr(host, prop):
  152. facts += ' {}={}'.format(HOST_VARIABLES_MAP.get(prop), getattr(host, prop))
  153. if host.other_variables:
  154. for variable, value in host.other_variables.items():
  155. facts += " {}={}".format(variable, value)
  156. if host.node_labels and role == 'node':
  157. facts += ' openshift_node_labels="{}"'.format(host.node_labels)
  158. # Distinguish between three states, no schedulability specified (use default),
  159. # explicitly set to True, or explicitly set to False:
  160. if role != 'node' or schedulable is None:
  161. pass
  162. else:
  163. facts += " openshift_schedulable={}".format(schedulable)
  164. installer_host = socket.gethostname()
  165. if installer_host in [host.connect_to, host.hostname, host.public_hostname]:
  166. facts += ' ansible_connection=local'
  167. if os.geteuid() != 0:
  168. no_pwd_sudo = subprocess.call(['sudo', '-n', 'echo', '-n'])
  169. if no_pwd_sudo == 1:
  170. print('The atomic-openshift-installer requires sudo access without a password.')
  171. sys.exit(1)
  172. facts += ' ansible_become=yes'
  173. inventory.write('{} {}\n'.format(host.connect_to, facts))
  174. def load_system_facts(inventory_file, os_facts_path, env_vars, verbose=False):
  175. """
  176. Retrieves system facts from the remote systems.
  177. """
  178. installer_log.debug("Inside load_system_facts")
  179. installer_log.debug("load_system_facts will run with Ansible/Openshift environment variables:")
  180. debug_env(env_vars)
  181. FNULL = open(os.devnull, 'w')
  182. args = ['ansible-playbook', '-v'] if verbose \
  183. else ['ansible-playbook']
  184. args.extend([
  185. '--inventory-file={}'.format(inventory_file),
  186. os_facts_path])
  187. installer_log.debug("Going to subprocess out to ansible now with these args: %s", ' '.join(args))
  188. installer_log.debug("Subprocess will run with Ansible/Openshift environment variables:")
  189. debug_env(env_vars)
  190. status = subprocess.call(args, env=env_vars, stdout=FNULL)
  191. if status != 0:
  192. installer_log.debug("Exit status from subprocess was not 0")
  193. return [], 1
  194. with open(CFG.settings['ansible_callback_facts_yaml'], 'r') as callback_facts_file:
  195. installer_log.debug("Going to try to read this file: %s", CFG.settings['ansible_callback_facts_yaml'])
  196. try:
  197. callback_facts = yaml.safe_load(callback_facts_file)
  198. except yaml.YAMLError as exc:
  199. print("Error in {}".format(CFG.settings['ansible_callback_facts_yaml']), exc)
  200. print("Try deleting and rerunning the atomic-openshift-installer")
  201. sys.exit(1)
  202. return callback_facts, 0
  203. def default_facts(hosts, verbose=False):
  204. global CFG
  205. installer_log.debug("Current global CFG vars here: %s", CFG)
  206. inventory_file = generate_inventory(hosts)
  207. os_facts_path = '{}/playbooks/byo/openshift_facts.yml'.format(CFG.ansible_playbook_directory)
  208. facts_env = os.environ.copy()
  209. facts_env["OO_INSTALL_CALLBACK_FACTS_YAML"] = CFG.settings['ansible_callback_facts_yaml']
  210. facts_env["ANSIBLE_CALLBACK_PLUGINS"] = CFG.settings['ansible_plugins_directory']
  211. if 'ansible_log_path' in CFG.settings:
  212. facts_env["ANSIBLE_LOG_PATH"] = CFG.settings['ansible_log_path']
  213. if 'ansible_config' in CFG.settings:
  214. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  215. installer_log.debug("facts_env: %s", facts_env)
  216. installer_log.debug("Going to 'load_system_facts' next")
  217. return load_system_facts(inventory_file, os_facts_path, facts_env, verbose)
  218. def run_prerequisites(inventory_file, hosts, hosts_to_run_on, verbose=False):
  219. global CFG
  220. prerequisites_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  221. 'playbooks/prerequisites.yml')
  222. facts_env = os.environ.copy()
  223. if 'ansible_log_path' in CFG.settings:
  224. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  225. # override the ansible config for prerequisites playbook run
  226. if 'ansible_quiet_config' in CFG.settings:
  227. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_quiet_config']
  228. return run_ansible(prerequisites_playbook_path, inventory_file, facts_env, verbose)
  229. def run_main_playbook(inventory_file, hosts, hosts_to_run_on, verbose=False):
  230. global CFG
  231. if len(hosts_to_run_on) != len(hosts):
  232. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  233. 'playbooks/openshift-node/scaleup.yml')
  234. else:
  235. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  236. 'playbooks/deploy_cluster.yml')
  237. facts_env = os.environ.copy()
  238. if 'ansible_log_path' in CFG.settings:
  239. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  240. # override the ansible config for our main playbook run
  241. if 'ansible_quiet_config' in CFG.settings:
  242. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_quiet_config']
  243. return run_ansible(main_playbook_path, inventory_file, facts_env, verbose)
  244. def run_ansible(playbook, inventory, env_vars, verbose=False):
  245. installer_log.debug("run_ansible will run with Ansible/Openshift environment variables:")
  246. debug_env(env_vars)
  247. args = ['ansible-playbook', '-v'] if verbose \
  248. else ['ansible-playbook']
  249. args.extend([
  250. '--inventory-file={}'.format(inventory),
  251. playbook])
  252. installer_log.debug("Going to subprocess out to ansible now with these args: %s", ' '.join(args))
  253. return subprocess.call(args, env=env_vars)
  254. def run_uninstall_playbook(hosts, verbose=False):
  255. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  256. 'playbooks/adhoc/uninstall.yml')
  257. inventory_file = generate_inventory(hosts)
  258. facts_env = os.environ.copy()
  259. if 'ansible_log_path' in CFG.settings:
  260. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  261. if 'ansible_config' in CFG.settings:
  262. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  263. # override the ansible config for our main playbook run
  264. if 'ansible_quiet_config' in CFG.settings:
  265. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_quiet_config']
  266. return run_ansible(playbook, inventory_file, facts_env, verbose)