openshift_ansible.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 os
  7. import yaml
  8. from ooinstall.variants import find_variant
  9. CFG = None
  10. def set_config(cfg):
  11. global CFG
  12. CFG = cfg
  13. def generate_inventory(hosts):
  14. print hosts
  15. global CFG
  16. installer_host = socket.gethostname()
  17. base_inventory_path = CFG.settings['ansible_inventory_path']
  18. base_inventory = open(base_inventory_path, 'w')
  19. base_inventory.write('\n[OSEv3:children]\nmasters\nnodes\n')
  20. base_inventory.write('\n[OSEv3:vars]\n')
  21. base_inventory.write('ansible_ssh_user={}\n'.format(CFG.settings['ansible_ssh_user']))
  22. if CFG.settings['ansible_ssh_user'] != 'root':
  23. base_inventory.write('ansible_sudo=true\n')
  24. # Find the correct deployment type for ansible:
  25. ver = find_variant(CFG.settings['variant'],
  26. version=CFG.settings.get('variant_version', None))[1]
  27. base_inventory.write('deployment_type={}\n'.format(ver.ansible_key))
  28. if 'OO_INSTALL_DEVEL_REGISTRY' in os.environ:
  29. base_inventory.write('oreg_url=rcm-img-docker01.build.eng.bos.redhat.com:'
  30. '5001/openshift3/ose-${component}:${version}\n')
  31. if 'OO_INSTALL_PUDDLE_REPO_ENABLE' in os.environ:
  32. base_inventory.write("openshift_additional_repos=[{'id': 'ose-devel', "
  33. "'name': 'ose-devel', "
  34. "'baseurl': 'http://buildvm-devops.usersys.redhat.com"
  35. "/puddle/build/AtomicOpenShift/3.1/latest/RH7-RHAOS-3.1/$basearch/os', "
  36. "'enabled': 1, 'gpgcheck': 0}]\n")
  37. if 'OO_INSTALL_STAGE_REGISTRY' in os.environ:
  38. base_inventory.write('oreg_url=registry.access.stage.redhat.com/openshift3/ose-${component}:${version}\n')
  39. if any(host.hostname == installer_host or host.public_hostname == installer_host
  40. for host in hosts):
  41. base_inventory.write("ansible_connection=local\n")
  42. base_inventory.write('\n[masters]\n')
  43. masters = (host for host in hosts if host.master)
  44. for master in masters:
  45. write_host(master, base_inventory)
  46. base_inventory.write('\n[nodes]\n')
  47. nodes = (host for host in hosts if host.node)
  48. for node in nodes:
  49. # TODO: Until the Master can run the SDN itself we have to configure the Masters
  50. # as Nodes too.
  51. scheduleable = True
  52. # If there's only one Node and it's also a Master we want it to be scheduleable:
  53. if node in masters and len(masters) != 1:
  54. scheduleable = False
  55. write_host(node, base_inventory, scheduleable)
  56. base_inventory.close()
  57. return base_inventory_path
  58. def write_host(host, inventory, scheduleable=True):
  59. global CFG
  60. facts = ''
  61. if host.ip:
  62. facts += ' openshift_ip={}'.format(host.ip)
  63. if host.public_ip:
  64. facts += ' openshift_public_ip={}'.format(host.public_ip)
  65. if host.hostname:
  66. facts += ' openshift_hostname={}'.format(host.hostname)
  67. if host.public_hostname:
  68. facts += ' openshift_public_hostname={}'.format(host.public_hostname)
  69. # TODO: For not write_host is handles both master and nodes.
  70. # Technically only nodes will ever need this.
  71. if not scheduleable:
  72. facts += ' openshift_scheduleable=False'
  73. inventory.write('{} {}\n'.format(host, facts))
  74. def load_system_facts(inventory_file, os_facts_path, env_vars):
  75. """
  76. Retrieves system facts from the remote systems.
  77. """
  78. FNULL = open(os.devnull, 'w')
  79. status = subprocess.call(['ansible-playbook',
  80. '--inventory-file={}'.format(inventory_file),
  81. os_facts_path],
  82. env=env_vars,
  83. stdout=FNULL)
  84. if not status == 0:
  85. return [], 1
  86. callback_facts_file = open(CFG.settings['ansible_callback_facts_yaml'], 'r')
  87. callback_facts = yaml.load(callback_facts_file)
  88. callback_facts_file.close()
  89. return callback_facts, 0
  90. def default_facts(hosts):
  91. global CFG
  92. inventory_file = generate_inventory(hosts)
  93. os_facts_path = '{}/playbooks/byo/openshift_facts.yml'.format(CFG.ansible_playbook_directory)
  94. facts_env = os.environ.copy()
  95. facts_env["OO_INSTALL_CALLBACK_FACTS_YAML"] = CFG.settings['ansible_callback_facts_yaml']
  96. facts_env["ANSIBLE_CALLBACK_PLUGINS"] = CFG.settings['ansible_plugins_directory']
  97. if 'ansible_log_path' in CFG.settings:
  98. facts_env["ANSIBLE_LOG_PATH"] = CFG.settings['ansible_log_path']
  99. if 'ansible_config' in CFG.settings:
  100. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  101. return load_system_facts(inventory_file, os_facts_path, facts_env)
  102. def run_main_playbook(hosts, hosts_to_run_on):
  103. global CFG
  104. inventory_file = generate_inventory(hosts)
  105. if len(hosts_to_run_on) != len(hosts):
  106. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  107. 'playbooks/common/openshift-cluster/scaleup.yml')
  108. else:
  109. main_playbook_path = os.path.join(CFG.ansible_playbook_directory,
  110. 'playbooks/byo/config.yml')
  111. facts_env = os.environ.copy()
  112. if 'ansible_log_path' in CFG.settings:
  113. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  114. if 'ansible_config' in CFG.settings:
  115. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  116. return run_ansible(main_playbook_path, inventory_file, facts_env)
  117. def run_ansible(playbook, inventory, env_vars):
  118. return subprocess.call(['ansible-playbook',
  119. '--inventory-file={}'.format(inventory),
  120. playbook],
  121. env=env_vars)
  122. def run_uninstall_playbook():
  123. playbook = os.path.join(CFG.settings['ansible_playbook_directory'],
  124. 'playbooks/adhoc/uninstall.yml')
  125. inventory_file = generate_inventory(CFG.hosts)
  126. facts_env = os.environ.copy()
  127. if 'ansible_log_path' in CFG.settings:
  128. facts_env['ANSIBLE_LOG_PATH'] = CFG.settings['ansible_log_path']
  129. if 'ansible_config' in CFG.settings:
  130. facts_env['ANSIBLE_CONFIG'] = CFG.settings['ansible_config']
  131. return run_ansible(playbook, inventory_file, facts_env)