Beispiel #1
0
def get_endpoint_for_network(network: Optional[str] = None) -> str:
    """
    Get the LocalStack endpoint (= IP address) on the given network.
    If a network is given, it will return the IP address/hostname of LocalStack on that network
    If omitted, it will return the IP address/hostname of the main container network
    This is a cached call, clear cache if networks might have changed

    :param network: Network to return the endpoint for
    :return: IP address or hostname of LS on the given network
    """
    container_name = get_main_container_name()
    network = network or get_main_container_network()
    main_container_ip = None
    try:
        if config.is_in_docker:
            main_container_ip = DOCKER_CLIENT.get_container_ipv4_for_network(
                container_name_or_id=container_name,
                container_network=network,
            )
        else:
            # default gateway for the network should be the host
            # (only under Linux - otherwise fall back to DOCKER_HOST_FROM_CONTAINER below)
            if config.is_in_linux:
                main_container_ip = DOCKER_CLIENT.inspect_network(
                    network)["IPAM"]["Config"][0]["Gateway"]
        LOG.info("Determined main container target IP: %s", main_container_ip)
    except Exception as e:
        LOG.info('Unable to get IP address of main Docker container "%s": %s',
                 container_name, e)
    # return (1) predefined endpoint host, or (2) main container IP, or (3) Docker host (e.g., bridge IP)
    return main_container_ip or config.DOCKER_HOST_FROM_CONTAINER
Beispiel #2
0
def get_main_endpoint_from_container():
    global DOCKER_MAIN_CONTAINER_IP
    if not config.HOSTNAME_FROM_LAMBDA and DOCKER_MAIN_CONTAINER_IP is None:
        DOCKER_MAIN_CONTAINER_IP = False
        container_name = bootstrap.get_main_container_name()
        try:
            if config.is_in_docker:
                DOCKER_MAIN_CONTAINER_IP = DOCKER_CLIENT.get_container_ipv4_for_network(
                    container_name_or_id=container_name,
                    container_network=get_container_network_for_lambda(),
                )
            else:
                # default gateway for the network should be the host
                # (only under Linux - otherwise fall back to DOCKER_HOST_FROM_CONTAINER below)
                if config.is_in_linux:
                    DOCKER_MAIN_CONTAINER_IP = DOCKER_CLIENT.inspect_network(
                        get_container_network_for_lambda(
                        ))["IPAM"]["Config"][0]["Gateway"]
            LOG.info("Determined main container target IP: %s",
                     DOCKER_MAIN_CONTAINER_IP)
        except Exception as e:
            LOG.info(
                'Unable to get IP address of main Docker container "%s": %s',
                container_name, e)
    # return (1) predefined endpoint host, or (2) main container IP, or (3) Docker host (e.g., bridge IP)
    return (config.HOSTNAME_FROM_LAMBDA or DOCKER_MAIN_CONTAINER_IP
            or config.DOCKER_HOST_FROM_CONTAINER)
Beispiel #3
0
    def start(self, env_vars: Dict[str, str]) -> None:
        self.executor_endpoint.start()
        network = self.get_network_for_executor()
        container_config = ContainerConfiguration(
            image_name=self.get_image(),
            name=self.id,
            env_vars=env_vars,
            network=network,
            entrypoint=RAPID_ENTRYPOINT,
        )
        CONTAINER_CLIENT.create_container_from_config(container_config)
        if not config.LAMBDA_PREBUILD_IMAGES:
            CONTAINER_CLIENT.copy_into_container(
                self.id, str(get_runtime_client_path()), RAPID_ENTRYPOINT)
            CONTAINER_CLIENT.copy_into_container(
                self.id,
                f"{str(get_code_path_for_function(self.function_version))}/",
                "/var/task/")

        CONTAINER_CLIENT.start_container(self.id)
        self.ip = CONTAINER_CLIENT.get_container_ipv4_for_network(
            container_name_or_id=self.id, container_network=network)
        self.executor_endpoint.container_address = self.ip