Exemple #1
0
def setup_links(instance, create_config, start_config):
    """
    Sets up a container's config for rancher-managed links by removing the
    docker native link configuration and emulating links through environment
    variables.

    Note that a non-rancher container (one created and started outside the
    rancher API) container will not have its link configuration manipulated.
    This is because on a container restart, we would not be able to properly
    rebuild the link configuration because it depends on manipulating the
    create_config.
    """
    if not _has_service(instance, 'linkService') or is_nonrancher_container(
            instance):
        return

    if 'links' in start_config:
        del start_config['links']

    result = {}
    for link in instance.instanceLinks:
        name = link.linkName

        try:
            for link_port in link.data.fields.ports:
                proto = link_port.protocol
                ip = link_port.ipAddress
                dst = link_port.publicPort
                port = link_port.privatePort

                full_port = '{0}://{1}:{2}'.format(proto, ip, dst)

                data = {
                    'NAME': '/cattle/{0}'.format(name),
                    'PORT': full_port,
                    'PORT_{0}_{1}'.format(port, proto): full_port,
                    'PORT_{0}_{1}_ADDR'.format(port, proto): ip,
                    'PORT_{0}_{1}_PORT'.format(port, proto): dst,
                    'PORT_{0}_{1}_PROTO'.format(port, proto): proto,
                }

                for k, v in data.items():
                    result['{0}_{1}'.format(name, k).upper()] = v
        except AttributeError:
            pass

    if len(result) > 0:
        add_to_env(create_config, **result)
Exemple #2
0
def setup_cattle_config_url(instance, create_config):
    if instance.get('agentId') is None:
        return

    url = Config.config_url()

    if url is not None:
        parsed = urlparse(url)

        if 'localhost' == parsed.hostname:
            port = Config.api_proxy_listen_port()
            add_to_env(create_config,
                       CATTLE_AGENT_INSTANCE='true',
                       CATTLE_CONFIG_URL_SCHEME=parsed.scheme,
                       CATTLE_CONFIG_URL_PATH=parsed.path,
                       CATTLE_CONFIG_URL_PORT=port)
        else:
            add_to_env(create_config, CATTLE_CONFIG_URL=url)
Exemple #3
0
def setup_links(instance, create_config, start_config):
    """
    Sets up a container's config for rancher-managed links by removing the
    docker native link configuration and emulating links through environment
    variables.

    Note that a non-rancher container (one created and started outside the
    rancher API) container will not have its link configuration manipulated.
    This is because on a container restart, we would not be able to properly
    rebuild the link configuration because it depends on manipulating the
    create_config.
    """
    if not _has_service(instance, 'linkService') or is_nonrancher_container(
            instance):
        return

    if 'links' in start_config:
        del start_config['links']

    result = {}
    for link in instance.instanceLinks:
        link_name = link.linkName
        _add_link_env(link_name, link, result)
        _copy_link_env(link_name, link, result)

        try:
            for name in link.data.fields.instanceNames:
                _add_link_env(name, link, result, in_ip=link_name)
                _copy_link_env(name, link, result)

                # This does assume the format {env}_{name}
                parts = name.split('_', 1)
                if len(parts) == 1:
                    continue

                _add_link_env(parts[1], link, result, in_ip=link_name)
                _copy_link_env(parts[1], link, result)
        except AttributeError:
            pass

    if len(result) > 0:
        add_to_env(create_config, **result)
Exemple #4
0
def setup_links(instance, create_config, start_config):
    """
    Sets up a container's config for rancher-managed links by removing the
    docker native link configuration and emulating links through environment
    variables.

    Note that a non-rancher container (one created and started outside the
    rancher API) container will not have its link configuration manipulated.
    This is because on a container restart, we would not be able to properly
    rebuild the link configuration because it depends on manipulating the
    create_config.
    """
    if not _has_service(instance,
                        'linkService') or is_nonrancher_container(instance):
        return

    if 'links' in start_config:
        del start_config['links']

    result = {}
    for link in instance.instanceLinks:
        link_name = link.linkName
        _add_link_env(link_name, link, result)
        _copy_link_env(link_name, link, result)

        try:
            for name in link.data.fields.instanceNames:
                _add_link_env(name, link, result, in_ip=link_name)
                _copy_link_env(name, link, result)

                # This does assume the format {env}_{name}
                parts = name.split('_', 1)
                if len(parts) == 1:
                    continue

                _add_link_env(parts[1], link, result, in_ip=link_name)
                _copy_link_env(parts[1], link, result)
        except AttributeError:
            pass

    if len(result) > 0:
        add_to_env(create_config, **result)
Exemple #5
0
def setup_cattle_config_url(instance, create_config):
    if instance.get('agentId') is None and not _has_label(instance):
        return

    if 'labels' not in create_config:
        create_config['labels'] = {}

    create_config['labels']['io.rancher.container.agent_id'] = \
        str(instance.get('agentId'))

    url = Config.config_url()

    if url is not None:
        parsed = urlparse(url)

        if 'localhost' == parsed.hostname:
            port = Config.api_proxy_listen_port()
            add_to_env(create_config,
                       CATTLE_AGENT_INSTANCE='true',
                       CATTLE_CONFIG_URL_SCHEME=parsed.scheme,
                       CATTLE_CONFIG_URL_PATH=parsed.path,
                       CATTLE_CONFIG_URL_PORT=port)
        else:
            add_to_env(create_config, CATTLE_CONFIG_URL=url)
            add_to_env(create_config, CATTLE_URL=url)