openshift_ansible.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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(
  95. "openshift_master_cluster_public_hostname={}\n".format(proxy.public_hostname))
  96. if CFG.settings.get('master_routingconfig_subdomain', False):
  97. base_inventory.write(
  98. "openshift_master_default_subdomain={}\n".format(CFG.settings['master_routingconfig_subdomain']))
  99. def write_host(host, inventory, schedulable=None):
  100. global CFG
  101. facts = ''
  102. if host.ip:
  103. facts += ' openshift_ip={}'.format(host.ip)
  104. if host.public_ip:
  105. facts += ' openshift_public_ip={}'.format(host.public_ip)
  106. if host.hostname:
  107. facts += ' openshift_hostname={}'.format(host.hostname)
  108. if host.public_hostname:
  109. facts += ' openshift_public_hostname={}'.format(host.public_hostname)
  110. if host.containerized:
  111. facts += ' containerized={}'.format(host.containerized)
  112. # TODO: For not write_host is handles both master and nodes.
  113. # Technically only nodes will ever need this.
  114. # Distinguish between three states, no schedulability specified (use default),
  115. # explicitly set to True, or explicitly set to False:
  116. if schedulable is None:
  117. pass
  118. elif schedulable:
  119. facts += ' openshift_schedulable=True'
  120. elif not schedulable:
  121. facts += ' openshift_schedulable=False'
  122. installer_host = socket.gethostname()
  123. if installer_host in [host.connect_to, host.hostname, host.public_hostname]:
  124. facts += ' ansible_connection=local'
  125. if os.geteuid() != 0:
  126. no_pwd_sudo = subprocess.call(['sudo', '-n', 'echo', 'openshift'])
  127. if no_pwd_sudo == 1:
  128. print 'The atomic-openshift-installer requires sudo access without a password.'
  129. sys.exit(1)
  130. facts += ' ansible_become=true'
  131. inventory.write('{} {}\n'.format(host.connect_to, facts))
  132. def load_system_facts(inventory_file, os_facts_path, env_vars, verbose=False):
  133. """
  134. Retrieves system facts from the remote systems.
  135. """
  136. FNULL = open(os.devnull, 'w')
  137. args = ['ansible-playbook', '-v'] if verbose \
  138. else ['ansible-playbook']
  139. args.extend([
  140. '--inventory-file={}'.format(inventory_file),
  141. os_facts_path])
  142. status = subprocess.call(args, env=env_vars, stdout=FNULL)
  143. if not status == 0:
  144. return [], 1
  145. with open(CFG.settings['ansible_callback_facts_yaml'], 'r') as callback_facts_file:
  146. try:
  147. callback_facts = yaml.safe_load(callback_facts_file)
  148. except yaml.YAMLError, exc:
  149. print "Error in {}".format(CFG.settings['ansible_callback_facts_yaml']), exc
  150. print "Try deleting and rerunning the atomic-openshift-installer"
  151. sys.exit(1)
  152. return callback_facts, 0
  153. def default_facts(hosts, verbose=False):
  154. global CFG
  155. inventory_file = generate_inventory(hosts)
  156. os_facts_path = '{}/playbooks/byo/openshift_facts.yml'.format(CFG.ansible_playbook_directory)
  157. facts_env = os.environ.copy()
  158. facts_env["OO_INSTALL_CALLBACK_FACTS_YAML"] = CFG.settings['ansible_callback_facts_yaml']
  159. facts_env["ANSIBLE_CALLBACK_PLUGINS"] = CFG.settings['ansible_plugins_directory']
  160. facts_env["OPENSHIFT_MASTER_CLUSTER_METHOD"] = 'native'
  161. if 'ansible_log_path' in CFG.settings:
  162. facts_env["ANSIBLE_LOG_PATH"] = CFG.settings['ansible_log_path']
  163. if 'ansible_config' in CFG.settings:
  164. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  165. return load_system_facts(inventory_file, os_facts_path, facts_env, verbose)
  166. def run_main_playbook(hosts, hosts_to_run_on, verbose=False):
  167. global CFG
  168. inventory_file = generate_inventory(hosts_to_run_on)
  169. if len(hosts_to_run_on) != len(hosts):
  170. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  171. 'playbooks/byo/openshift-node/scaleup.yml')
  172. else:
  173. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  174. 'playbooks/byo/openshift-cluster/config.yml')
  175. facts_env = os.environ.copy()
  176. if 'ansible_log_path' in CFG.settings:
  177. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  178. if 'ansible_config' in CFG.settings:
  179. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  180. return run_ansible(main_playbook_path, inventory_file, facts_env, verbose)
  181. def run_ansible(playbook, inventory, env_vars, verbose=False):
  182. args = ['ansible-playbook', '-v'] if verbose \
  183. else ['ansible-playbook']
  184. args.extend([
  185. '--inventory-file={}'.format(inventory),
  186. playbook])
  187. return subprocess.call(args, env=env_vars)
  188. def run_uninstall_playbook(verbose=False):
  189. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  190. 'playbooks/adhoc/uninstall.yml')
  191. inventory_file = generate_inventory(CFG.hosts)
  192. facts_env = os.environ.copy()
  193. if 'ansible_log_path' in CFG.settings:
  194. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  195. if 'ansible_config' in CFG.settings:
  196. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  197. return run_ansible(playbook, inventory_file, facts_env, verbose)
  198. def run_upgrade_playbook(old_version, new_version, verbose=False):
  199. # TODO: do not hardcode the upgrade playbook, add ability to select the
  200. # right playbook depending on the type of upgrade.
  201. old_version = old_version.replace('.', '_')
  202. new_version = old_version.replace('.', '_')
  203. if old_version == new_version:
  204. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  205. 'playbooks/byo/openshift-cluster/upgrades/v{}_minor/upgrade.yml'.format(new_version))
  206. else:
  207. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  208. 'playbooks/byo/openshift-cluster/upgrades/v{}_to_v{}/upgrade.yml'.format(old_version,
  209. new_version))
  210. # TODO: Upgrade inventory for upgrade?
  211. inventory_file = generate_inventory(CFG.hosts)
  212. facts_env = os.environ.copy()
  213. if 'ansible_log_path' in CFG.settings:
  214. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  215. if 'ansible_config' in CFG.settings:
  216. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  217. return run_ansible(playbook, inventory_file, facts_env, verbose)