openshift_ansible.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. base_inventory_path = CFG.settings['ansible_inventory_path']
  17. base_inventory = open(base_inventory_path, 'w')
  18. base_inventory.write('\n[OSEv3:children]\nmasters\nnodes\n')
  19. base_inventory.write('\n[OSEv3:vars]\n')
  20. base_inventory.write('ansible_ssh_user={}\n'.format(CFG.settings['ansible_ssh_user']))
  21. if CFG.settings['ansible_ssh_user'] != 'root':
  22. base_inventory.write('ansible_become=true\n')
  23. # Find the correct deployment type for ansible:
  24. ver = find_variant(CFG.settings['variant'],
  25. version=CFG.settings.get('variant_version', None))[1]
  26. base_inventory.write('deployment_type={}\n'.format(ver.ansible_key))
  27. if 'OO_INSTALL_ADDITIONAL_REGISTRIES' in os.environ:
  28. base_inventory.write('cli_docker_additional_registries={}\n'
  29. .format(os.environ['OO_INSTALL_ADDITIONAL_REGISTRIES']))
  30. if 'OO_INSTALL_INSECURE_REGISTRIES' in os.environ:
  31. base_inventory.write('cli_docker_insecure_registries={}\n'
  32. .format(os.environ['OO_INSTALL_INSECURE_REGISTRIES']))
  33. if 'OO_INSTALL_PUDDLE_REPO' in os.environ:
  34. # We have to double the '{' here for literals
  35. base_inventory.write("openshift_additional_repos=[{{'id': 'ose-devel', "
  36. "'name': 'ose-devel', "
  37. "'baseurl': '{}', "
  38. "'enabled': 1, 'gpgcheck': 0}}]\n".format(os.environ['OO_INSTALL_PUDDLE_REPO']))
  39. base_inventory.write('\n[masters]\n')
  40. masters = (host for host in hosts if host.master)
  41. for master in masters:
  42. write_host(master, base_inventory)
  43. base_inventory.write('\n[nodes]\n')
  44. nodes = (host for host in hosts if host.node)
  45. for node in nodes:
  46. # TODO: Until the Master can run the SDN itself we have to configure the Masters
  47. # as Nodes too.
  48. scheduleable = True
  49. # If there's only one Node and it's also a Master we want it to be scheduleable:
  50. if node in masters and len(masters) != 1:
  51. scheduleable = False
  52. write_host(node, base_inventory, scheduleable)
  53. base_inventory.close()
  54. return base_inventory_path
  55. def write_host(host, inventory, scheduleable=True):
  56. global CFG
  57. facts = ''
  58. if host.ip:
  59. facts += ' openshift_ip={}'.format(host.ip)
  60. if host.public_ip:
  61. facts += ' openshift_public_ip={}'.format(host.public_ip)
  62. if host.hostname:
  63. facts += ' openshift_hostname={}'.format(host.hostname)
  64. if host.public_hostname:
  65. facts += ' openshift_public_hostname={}'.format(host.public_hostname)
  66. # TODO: For not write_host is handles both master and nodes.
  67. # Technically only nodes will ever need this.
  68. if not scheduleable:
  69. facts += ' openshift_scheduleable=False'
  70. installer_host = socket.gethostname()
  71. if host.hostname == installer_host or host.public_hostname == installer_host:
  72. facts += ' ansible_connection=local'
  73. if os.geteuid() != 0:
  74. no_pwd_sudo = subprocess.call(['sudo', '-v', '-n'])
  75. if no_pwd_sudo == 1:
  76. print 'The atomic-openshift-installer requires sudo access without a password.'
  77. sys.exit(1)
  78. facts += ' ansible_become=true'
  79. inventory.write('{} {}\n'.format(host, facts))
  80. def load_system_facts(inventory_file, os_facts_path, env_vars, verbose=False):
  81. """
  82. Retrieves system facts from the remote systems.
  83. """
  84. FNULL = open(os.devnull, 'w')
  85. args = ['ansible-playbook', '-v'] if verbose \
  86. else ['ansible-playbook']
  87. args.extend([
  88. '--inventory-file={}'.format(inventory_file),
  89. os_facts_path])
  90. status = subprocess.call(args, env=env_vars, stdout=FNULL)
  91. if not status == 0:
  92. return [], 1
  93. callback_facts_file = open(CFG.settings['ansible_callback_facts_yaml'], 'r')
  94. callback_facts = yaml.load(callback_facts_file)
  95. callback_facts_file.close()
  96. return callback_facts, 0
  97. def default_facts(hosts, verbose=False):
  98. global CFG
  99. inventory_file = generate_inventory(hosts)
  100. os_facts_path = '{}/playbooks/byo/openshift_facts.yml'.format(CFG.ansible_playbook_directory)
  101. facts_env = os.environ.copy()
  102. facts_env["OO_INSTALL_CALLBACK_FACTS_YAML"] = CFG.settings['ansible_callback_facts_yaml']
  103. facts_env["ANSIBLE_CALLBACK_PLUGINS"] = CFG.settings['ansible_plugins_directory']
  104. if 'ansible_log_path' in CFG.settings:
  105. facts_env["ANSIBLE_LOG_PATH"] = CFG.settings['ansible_log_path']
  106. if 'ansible_config' in CFG.settings:
  107. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  108. return load_system_facts(inventory_file, os_facts_path, facts_env, verbose)
  109. def run_main_playbook(hosts, hosts_to_run_on, verbose=False):
  110. global CFG
  111. inventory_file = generate_inventory(hosts)
  112. if len(hosts_to_run_on) != len(hosts):
  113. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  114. 'playbooks/common/openshift-cluster/scaleup.yml')
  115. else:
  116. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  117. 'playbooks/byo/config.yml')
  118. facts_env = os.environ.copy()
  119. if 'ansible_log_path' in CFG.settings:
  120. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  121. if 'ansible_config' in CFG.settings:
  122. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  123. return run_ansible(main_playbook_path, inventory_file, facts_env, verbose)
  124. def run_ansible(playbook, inventory, env_vars, verbose=False):
  125. args = ['ansible-playbook', '-v'] if verbose \
  126. else ['ansible-playbook']
  127. args.extend([
  128. '--inventory-file={}'.format(inventory),
  129. playbook])
  130. return subprocess.call(args, env=env_vars)
  131. def run_uninstall_playbook(verbose=False):
  132. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  133. 'playbooks/adhoc/uninstall.yml')
  134. inventory_file = generate_inventory(CFG.hosts)
  135. facts_env = os.environ.copy()
  136. if 'ansible_log_path' in CFG.settings:
  137. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  138. if 'ansible_config' in CFG.settings:
  139. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  140. return run_ansible(playbook, inventory_file, facts_env, verbose)
  141. def run_upgrade_playbook(verbose=False):
  142. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  143. 'playbooks/adhoc/upgrades/upgrade.yml')
  144. # TODO: Upgrade inventory for upgrade?
  145. inventory_file = generate_inventory(CFG.hosts)
  146. facts_env = os.environ.copy()
  147. if 'ansible_log_path' in CFG.settings:
  148. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  149. if 'ansible_config' in CFG.settings:
  150. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  151. return run_ansible(playbook, inventory_file, facts_env, verbose)