<commit_before># TODO: Temporarily disabled due to importing old code into openshift-ansible
# repo. We will work on these over time.
# pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name,global-statement,global-variable-not-assigned

import socket
import subprocess
import sys
import os
import yaml
from ooinstall.variants import find_variant

CFG = None

def set_config(cfg):
    global CFG
    CFG = cfg

def generate_inventory(hosts):
    global CFG
    masters = [host for host in hosts if host.master]
    nodes = [host for host in hosts if host.node]
    new_nodes = [host for host in hosts if host.node and host.new_host]
    proxy = determine_proxy_configuration(hosts)
    multiple_masters = len(masters) > 1
    scaleup = len(new_nodes) > 0

    base_inventory_path = CFG.settings['ansible_inventory_path']
    base_inventory = open(base_inventory_path, 'w')

    write_inventory_children(base_inventory, multiple_masters, proxy, scaleup)

    write_inventory_vars(base_inventory, multiple_masters, proxy)

    # Find the correct deployment type for ansible:
    ver = find_variant(CFG.settings['variant'],
        version=CFG.settings.get('variant_version', None))[1]
    base_inventory.write('deployment_type={}\n'.format(ver.ansible_key))

    if 'OO_INSTALL_ADDITIONAL_REGISTRIES' in os.environ:
        base_inventory.write('cli_docker_additional_registries={}\n'
          .format(os.environ['OO_INSTALL_ADDITIONAL_REGISTRIES']))
    if 'OO_INSTALL_INSECURE_REGISTRIES' in os.environ:
        base_inventory.write('cli_docker_insecure_registries={}\n'
          .format(os.environ['OO_INSTALL_INSECURE_REGISTRIES']))
    if 'OO_INSTALL_PUDDLE_REPO' in os.environ:
        # We have to double the '{' here for literals
        base_inventory.write("openshift_additional_repos=[{{'id': 'ose-devel', "
            "'name': 'ose-devel', "
            "'baseurl': '{}', "
            "'enabled': 1, 'gpgcheck': 0}}]\n".format(os.environ['OO_INSTALL_PUDDLE_REPO']))

    base_inventory.write('\n[masters]\n')
    for master in masters:
        write_host(master, base_inventory)

    if len(masters) > 1:
        base_inventory.write('\n[etcd]\n')
        for master in masters:
            write_host(master, base_inventory)

    base_inventory.write('\n[nodes]\n')

    for node in nodes:
        # Let the fact defaults decide if we're not a master:
        schedulable = None

        # If the node is also a master, we must explicitly set schedulablity:
        if node.master:
            schedulable = node.is_schedulable_node(hosts)
        write_host(node, base_inventory, schedulable)

    if not getattr(proxy, 'preconfigured', True):
        base_inventory.write('\n[lb]\n')
        write_host(proxy, base_inventory)

    if scaleup:
        base_inventory.write('\n[new_nodes]\n')
        for node in new_nodes:
            write_host(node, base_inventory)

    base_inventory.close()
    return base_inventory_path

def determine_proxy_configuration(hosts):
    proxy = next((host for host in hosts if host.master_lb), None)
    if proxy:
        if proxy.hostname == None:
            proxy.hostname = proxy.connect_to
            proxy.public_hostname = proxy.connect_to
        return proxy

    return None

def write_inventory_children(base_inventory, multiple_masters, proxy, scaleup):
    global CFG

    base_inventory.write('\n[OSEv3:children]\n')
    base_inventory.write('masters\n')
    base_inventory.write('nodes\n')
    if scaleup:
        base_inventory.write('new_nodes\n')
    if multiple_masters:
        base_inventory.write('etcd\n')
    if not getattr(proxy, 'preconfigured', True):
        base_inventory.write('lb\n')

def write_inventory_vars(base_inventory, multiple_masters, proxy):
    global CFG
    base_inventory.write('\n[OSEv3:vars]\n')
    base_inventory.write('ansible_ssh_user={}\n'.format(CFG.settings['ansible_ssh_user']))
    if CFG.settings['ansible_ssh_user'] != 'root':
        base_inventory.write('ansible_become=true\n')
    if multiple_masters and proxy is not None:
        base_inventory.write('openshift_master_cluster_method=native\n')
        base_inventory.write("openshift_master_cluster_hostname={}\n".format(proxy.hostname))
        base_inventory.write("openshift_master_cluster_public_hostname={}\n".format(proxy.public_hostname))


def write_host(host, inventory, schedulable=None):
    global CFG

    facts = ''
    if host.ip:
        facts += ' openshift_ip={}'.format(host.ip)
    if host.public_ip:
        facts += ' openshift_public_ip={}'.format(host.public_ip)
    if host.hostname:
        facts += ' openshift_hostname={}'.format(host.hostname)
    if host.public_hostname:
        facts += ' openshift_public_hostname={}'.format(host.public_hostname)
    # TODO: For not write_host is handles both master and nodes.
    # Technically only nodes will ever need this.

    # Distinguish between three states, no schedulability specified (use default),
    # explicitly set to True, or explicitly set to False:
    if schedulable is None:
        pass
    elif schedulable:
        facts += ' openshift_schedulable=True'
    elif not schedulable:
        facts += ' openshift_schedulable=False'
<commit_msg>atomic-openshift-installer: add containerized to inventory<commit_after># TODO: Temporarily disabled due to importing old code into openshift-ansible
# repo. We will work on these over time.
# pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name,global-statement,global-variable-not-assigned

import socket
import subprocess
import sys
import os
import yaml
from ooinstall.variants import find_variant

CFG = None

def set_config(cfg):
    global CFG
    CFG = cfg

def generate_inventory(hosts):
    global CFG
    masters = [host for host in hosts if host.master]
    nodes = [host for host in hosts if host.node]
    new_nodes = [host for host in hosts if host.node and host.new_host]
    proxy = determine_proxy_configuration(hosts)
    multiple_masters = len(masters) > 1
    scaleup = len(new_nodes) > 0

    base_inventory_path = CFG.settings['ansible_inventory_path']
    base_inventory = open(base_inventory_path, 'w')

    write_inventory_children(base_inventory, multiple_masters, proxy, scaleup)

    write_inventory_vars(base_inventory, multiple_masters, proxy)

    # Find the correct deployment type for ansible:
    ver = find_variant(CFG.settings['variant'],
        version=CFG.settings.get('variant_version', None))[1]
    base_inventory.write('deployment_type={}\n'.format(ver.ansible_key))

    if 'OO_INSTALL_ADDITIONAL_REGISTRIES' in os.environ:
        base_inventory.write('cli_docker_additional_registries={}\n'
          .format(os.environ['OO_INSTALL_ADDITIONAL_REGISTRIES']))
    if 'OO_INSTALL_INSECURE_REGISTRIES' in os.environ:
        base_inventory.write('cli_docker_insecure_registries={}\n'
          .format(os.environ['OO_INSTALL_INSECURE_REGISTRIES']))
    if 'OO_INSTALL_PUDDLE_REPO' in os.environ:
        # We have to double the '{' here for literals
        base_inventory.write("openshift_additional_repos=[{{'id': 'ose-devel', "
            "'name': 'ose-devel', "
            "'baseurl': '{}', "
            "'enabled': 1, 'gpgcheck': 0}}]\n".format(os.environ['OO_INSTALL_PUDDLE_REPO']))

    base_inventory.write('\n[masters]\n')
    for master in masters:
        write_host(master, base_inventory)

    if len(masters) > 1:
        base_inventory.write('\n[etcd]\n')
        for master in masters:
            write_host(master, base_inventory)

    base_inventory.write('\n[nodes]\n')

    for node in nodes:
        # Let the fact defaults decide if we're not a master:
        schedulable = None

        # If the node is also a master, we must explicitly set schedulablity:
        if node.master:
            schedulable = node.is_schedulable_node(hosts)
        write_host(node, base_inventory, schedulable)

    if not getattr(proxy, 'preconfigured', True):
        base_inventory.write('\n[lb]\n')
        write_host(proxy, base_inventory)

    if scaleup:
        base_inventory.write('\n[new_nodes]\n')
        for node in new_nodes:
            write_host(node, base_inventory)

    base_inventory.close()
    return base_inventory_path

def determine_proxy_configuration(hosts):
    proxy = next((host for host in hosts if host.master_lb), None)
    if proxy:
        if proxy.hostname == None:
            proxy.hostname = proxy.connect_to
            proxy.public_hostname = proxy.connect_to
        return proxy

    return None

def write_inventory_children(base_inventory, multiple_masters, proxy, scaleup):
    global CFG

    base_inventory.write('\n[OSEv3:children]\n')
    base_inventory.write('masters\n')
    base_inventory.write('nodes\n')
    if scaleup:
        base_inventory.write('new_nodes\n')
    if multiple_masters:
        base_inventory.write('etcd\n')
    if not getattr(proxy, 'preconfigured', True):
        base_inventory.write('lb\n')

def write_inventory_vars(base_inventory, multiple_masters, proxy):
    global CFG
    base_inventory.write('\n[OSEv3:vars]\n')
    base_inventory.write('ansible_ssh_user={}\n'.format(CFG.settings['ansible_ssh_user']))
    if CFG.settings['ansible_ssh_user'] != 'root':
        base_inventory.write('ansible_become=true\n')
    if multiple_masters and proxy is not None:
        base_inventory.write('openshift_master_cluster_method=native\n')
        base_inventory.write("openshift_master_cluster_hostname={}\n".format(proxy.hostname))
        base_inventory.write("openshift_master_cluster_public_hostname={}\n".format(proxy.public_hostname))


def write_host(host, inventory, schedulable=None):
    global CFG

    facts = ''
    if host.ip:
        facts += ' openshift_ip={}'.format(host.ip)
    if host.public_ip:
        facts += ' openshift_public_ip={}'.format(host.public_ip)
    if host.hostname:
        facts += ' openshift_hostname={}'.format(host.hostname)
    if host.public_hostname:
        facts += ' openshift_public_hostname={}'.format(host.public_hostname)
    if host.containerized:
        facts += ' containerized={}'.format(host.containerized)
    # TODO: For not write_host is handles both master and nodes.
    # Technically only nodes will ever need this.

    # Distinguish between three states, no schedulability specified (use default),
    # explicitly set to True, or explicitly set to False:
    if schedulable is None:
        pass
    elif schedulable:
        facts += ' openshift_schedulable=True'
    elif not schedulable:
        facts += ' openshift_schedulable=False'

{'fraction_non_alphanumeric': 0.10714285714285714, 'fraction_numerical': 0.0015117157974300832, 'mean_word_length': 3.5703799654576858, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6875520', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3351, 'n_words': 710}