openshift_ansible.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # TODO: Temporarily disabled due to importing old code into openshift-ansible
  2. # repo. We will work on these over time.
  3. # pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name,global-statement,global-variable-not-assigned
  4. import socket
  5. import subprocess
  6. import sys
  7. import os
  8. import yaml
  9. from ooinstall.variants import find_variant
  10. CFG = None
  11. def set_config(cfg):
  12. global CFG
  13. CFG = cfg
  14. def generate_inventory(hosts):
  15. global CFG
  16. masters = [host for host in hosts if host.master]
  17. nodes = [host for host in hosts if host.node]
  18. new_nodes = [host for host in hosts if host.node and host.new_host]
  19. proxy = determine_proxy_configuration(hosts)
  20. multiple_masters = len(masters) > 1
  21. scaleup = len(new_nodes) > 0
  22. base_inventory_path = CFG.settings['ansible_inventory_path']
  23. base_inventory = open(base_inventory_path, 'w')
  24. write_inventory_children(base_inventory, multiple_masters, proxy, scaleup)
  25. write_inventory_vars(base_inventory, multiple_masters, proxy)
  26. # Find the correct deployment type for ansible:
  27. ver = find_variant(CFG.settings['variant'],
  28. version=CFG.settings.get('variant_version', None))[1]
  29. base_inventory.write('deployment_type={}\n'.format(ver.ansible_key))
  30. if 'OO_INSTALL_ADDITIONAL_REGISTRIES' in os.environ:
  31. base_inventory.write('cli_docker_additional_registries={}\n'
  32. .format(os.environ['OO_INSTALL_ADDITIONAL_REGISTRIES']))
  33. if 'OO_INSTALL_INSECURE_REGISTRIES' in os.environ:
  34. base_inventory.write('cli_docker_insecure_registries={}\n'
  35. .format(os.environ['OO_INSTALL_INSECURE_REGISTRIES']))
  36. if 'OO_INSTALL_PUDDLE_REPO' in os.environ:
  37. # We have to double the '{' here for literals
  38. base_inventory.write("openshift_additional_repos=[{{'id': 'ose-devel', "
  39. "'name': 'ose-devel', "
  40. "'baseurl': '{}', "
  41. "'enabled': 1, 'gpgcheck': 0}}]\n".format(os.environ['OO_INSTALL_PUDDLE_REPO']))
  42. base_inventory.write('\n[masters]\n')
  43. for master in masters:
  44. write_host(master, base_inventory)
  45. if len(masters) > 1:
  46. base_inventory.write('\n[etcd]\n')
  47. for master in masters:
  48. write_host(master, base_inventory)
  49. base_inventory.write('\n[nodes]\n')
  50. for node in nodes:
  51. # Let the fact defaults decide if we're not a master:
  52. schedulable = None
  53. # If the node is also a master, we must explicitly set schedulablity:
  54. if node.master:
  55. schedulable = node.is_schedulable_node(hosts)
  56. write_host(node, base_inventory, schedulable)
  57. if not getattr(proxy, 'preconfigured', True):
  58. base_inventory.write('\n[lb]\n')
  59. write_host(proxy, base_inventory)
  60. if scaleup:
  61. base_inventory.write('\n[new_nodes]\n')
  62. for node in new_nodes:
  63. write_host(node, base_inventory)
  64. base_inventory.close()
  65. return base_inventory_path
  66. def determine_proxy_configuration(hosts):
  67. proxy = next((host for host in hosts if host.master_lb), None)
  68. if proxy:
  69. if proxy.hostname == None:
  70. proxy.hostname = proxy.connect_to
  71. proxy.public_hostname = proxy.connect_to
  72. return proxy
  73. return None
  74. def write_inventory_children(base_inventory, multiple_masters, proxy, scaleup):
  75. global CFG
  76. base_inventory.write('\n[OSEv3:children]\n')
  77. base_inventory.write('masters\n')
  78. base_inventory.write('nodes\n')
  79. if scaleup:
  80. base_inventory.write('new_nodes\n')
  81. if multiple_masters:
  82. base_inventory.write('etcd\n')
  83. if not getattr(proxy, 'preconfigured', True):
  84. base_inventory.write('lb\n')
  85. def write_inventory_vars(base_inventory, multiple_masters, proxy):
  86. global CFG
  87. base_inventory.write('\n[OSEv3:vars]\n')
  88. base_inventory.write('ansible_ssh_user={}\n'.format(CFG.settings['ansible_ssh_user']))
  89. if CFG.settings['ansible_ssh_user'] != 'root':
  90. base_inventory.write('ansible_become=true\n')
  91. if multiple_masters and proxy is not None:
  92. base_inventory.write('openshift_master_cluster_method=native\n')
  93. base_inventory.write("openshift_master_cluster_hostname={}\n".format(proxy.hostname))
  94. base_inventory.write("openshift_master_cluster_public_hostname={}\n".format(proxy.public_hostname))
  95. if CFG.settings.get('master_routingconfig_subdomain', False):
  96. base_inventory.write("osm_default_subdomain={}\n".format(CFG.settings['master_routingconfig_subdomain']))
  97. def write_host(host, inventory, schedulable=None):
  98. global CFG
  99. facts = ''
  100. if host.ip:
  101. facts += ' openshift_ip={}'.format(host.ip)
  102. if host.public_ip:
  103. facts += ' openshift_public_ip={}'.format(host.public_ip)
  104. if host.hostname:
  105. facts += ' openshift_hostname={}'.format(host.hostname)
  106. if host.public_hostname:
  107. facts += ' openshift_public_hostname={}'.format(host.public_hostname)
  108. if host.containerized:
  109. facts += ' containerized={}'.format(host.containerized)
  110. # TODO: For not write_host is handles both master and nodes.
  111. # Technically only nodes will ever need this.
  112. # Distinguish between three states, no schedulability specified (use default),
  113. # explicitly set to True, or explicitly set to False:
  114. if schedulable is None:
  115. pass
  116. elif schedulable:
  117. facts += ' openshift_schedulable=True'
  118. elif not schedulable:
  119. facts += ' openshift_schedulable=False'
  120. installer_host = socket.gethostname()
  121. if installer_host in [host.connect_to, host.hostname, host.public_hostname]:
  122. facts += ' ansible_connection=local'
  123. if os.geteuid() != 0:
  124. no_pwd_sudo = subprocess.call(['sudo', '-n', 'echo', 'openshift'])
  125. if no_pwd_sudo == 1:
  126. print 'The atomic-openshift-installer requires sudo access without a password.'
  127. sys.exit(1)
  128. facts += ' ansible_become=true'
  129. inventory.write('{} {}\n'.format(host.connect_to, facts))
  130. def load_system_facts(inventory_file, os_facts_path, env_vars, verbose=False):
  131. """
  132. Retrieves system facts from the remote systems.
  133. """
  134. FNULL = open(os.devnull, 'w')
  135. args = ['ansible-playbook', '-v'] if verbose \
  136. else ['ansible-playbook']
  137. args.extend([
  138. '--inventory-file={}'.format(inventory_file),
  139. os_facts_path])
  140. status = subprocess.call(args, env=env_vars, stdout=FNULL)
  141. if not status == 0:
  142. return [], 1
  143. with open(CFG.settings['ansible_callback_facts_yaml'], 'r') as callback_facts_file:
  144. try:
  145. callback_facts = yaml.safe_load(callback_facts_file)
  146. except yaml.YAMLError, exc:
  147. print "Error in {}".format(CFG.settings['ansible_callback_facts_yaml']), exc
  148. print "Try deleting and rerunning the atomic-openshift-installer"
  149. sys.exit(1)
  150. return callback_facts, 0
  151. def default_facts(hosts, verbose=False):
  152. global CFG
  153. inventory_file = generate_inventory(hosts)
  154. os_facts_path = '{}/playbooks/byo/openshift_facts.yml'.format(CFG.ansible_playbook_directory)
  155. facts_env = os.environ.copy()
  156. facts_env["OO_INSTALL_CALLBACK_FACTS_YAML"] = CFG.settings['ansible_callback_facts_yaml']
  157. facts_env["ANSIBLE_CALLBACK_PLUGINS"] = CFG.settings['ansible_plugins_directory']
  158. facts_env["OPENSHIFT_MASTER_CLUSTER_METHOD"] = 'native'
  159. if 'ansible_log_path' in CFG.settings:
  160. facts_env["ANSIBLE_LOG_PATH"] = CFG.settings['ansible_log_path']
  161. if 'ansible_config' in CFG.settings:
  162. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  163. return load_system_facts(inventory_file, os_facts_path, facts_env, verbose)
  164. def run_main_playbook(hosts, hosts_to_run_on, verbose=False):
  165. global CFG
  166. inventory_file = generate_inventory(hosts_to_run_on)
  167. if len(hosts_to_run_on) != len(hosts):
  168. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  169. 'playbooks/byo/openshift-node/scaleup.yml')
  170. else:
  171. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  172. 'playbooks/byo/openshift-cluster/config.yml')
  173. facts_env = os.environ.copy()
  174. if 'ansible_log_path' in CFG.settings:
  175. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  176. if 'ansible_config' in CFG.settings:
  177. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  178. return run_ansible(main_playbook_path, inventory_file, facts_env, verbose)
  179. def run_ansible(playbook, inventory, env_vars, verbose=False):
  180. args = ['ansible-playbook', '-v'] if verbose \
  181. else ['ansible-playbook']
  182. args.extend([
  183. '--inventory-file={}'.format(inventory),
  184. playbook])
  185. return subprocess.call(args, env=env_vars)
  186. def run_uninstall_playbook(verbose=False):
  187. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  188. 'playbooks/adhoc/uninstall.yml')
  189. inventory_file = generate_inventory(CFG.hosts)
  190. facts_env = os.environ.copy()
  191. if 'ansible_log_path' in CFG.settings:
  192. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  193. if 'ansible_config' in CFG.settings:
  194. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  195. return run_ansible(playbook, inventory_file, facts_env, verbose)
  196. def run_upgrade_playbook(old_version, new_version, verbose=False):
  197. # TODO: do not hardcode the upgrade playbook, add ability to select the
  198. # right playbook depending on the type of upgrade.
  199. old_version = old_version.replace('.', '_')
  200. new_version = old_version.replace('.', '_')
  201. if old_version == new_version:
  202. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  203. 'playbooks/byo/openshift-cluster/upgrades/v{}_minor/upgrade.yml'.format(new_version))
  204. else:
  205. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  206. 'playbooks/byo/openshift-cluster/upgrades/v{}_to_v{}/upgrade.yml'.format(old_version, new_version))
  207. # TODO: Upgrade inventory for upgrade?
  208. inventory_file = generate_inventory(CFG.hosts)
  209. facts_env = os.environ.copy()
  210. if 'ansible_log_path' in CFG.settings:
  211. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  212. if 'ansible_config' in CFG.settings:
  213. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  214. return run_ansible(playbook, inventory_file, facts_env, verbose)