install_transactions.py 5.5 KB

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