Search in sources :

Example 1 with AnsiProgressHandler

use of com.spotify.docker.client.AnsiProgressHandler in project zalenium by zalando.

the class DockerContainerClient method createContainer.

public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort) {
    String containerName = generateContainerName(zaleniumContainerName, nodePort);
    loadMountedFolders(zaleniumContainerName);
    // In some environments the created containers need to be labeled so the platform can handle them. E.g. Rancher.
    loadSeleniumContainerLabels();
    loadPullSeleniumImageFlag();
    loadIsZaleniumPrivileged(zaleniumContainerName);
    loadStorageOpts(zaleniumContainerName);
    List<String> binds = generateMountedFolderBinds();
    binds.add("/dev/shm:/dev/shm");
    String noVncPort = envVars.get("NOVNC_PORT");
    String networkMode = getZaleniumNetwork(zaleniumContainerName);
    List<String> extraHosts = new ArrayList<>();
    extraHosts.add(String.format("%s:%s", DOCKER_FOR_MAC_LOCALHOST_NAME, DOCKER_FOR_MAC_LOCALHOST_IP));
    // Allows "--net=host" work. Only supported for Linux.
    if (DOCKER_NETWORK_HOST_MODE_NAME.equalsIgnoreCase(networkMode)) {
        envVars.put("SELENIUM_HUB_HOST", "127.0.0.1");
        envVars.put("SELENIUM_NODE_HOST", "127.0.0.1");
        envVars.put("PICK_ALL_RANDOM_PORTS", "true");
        try {
            String hostName = dockerClient.info().name();
            extraHosts.add(String.format("%s:%s", hostName, "127.0.1.0"));
        } catch (DockerException | InterruptedException e) {
            logger.debug(nodeId + " Error while getting host name", e);
        }
    }
    // Reflect extra hosts of the hub container
    final List<String> hubExtraHosts = getContainerExtraHosts(zaleniumContainerName);
    extraHosts.addAll(hubExtraHosts);
    HostConfig hostConfig = HostConfig.builder().appendBinds(binds).networkMode(networkMode).extraHosts(extraHosts).autoRemove(true).storageOpt(storageOpt).privileged(isZaleniumPrivileged).build();
    List<String> flattenedEnvVars = envVars.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList());
    flattenedEnvVars.addAll(zaleniumHttpEnvVars);
    final String[] exposedPorts = { nodePort, noVncPort };
    ContainerConfig.Builder builder = ContainerConfig.builder().image(image).env(flattenedEnvVars).exposedPorts(exposedPorts).hostConfig(hostConfig);
    if (seleniumContainerLabels.size() > 0) {
        builder.labels(seleniumContainerLabels);
    }
    final ContainerConfig containerConfig = builder.build();
    try {
        if (pullSeleniumImage) {
            List<Image> images = dockerClient.listImages(DockerClient.ListImagesParam.byName(image));
            if (images.size() == 0) {
                // If the image has no tag, we add latest, otherwise we end up pulling all the images with that name.
                String imageToPull = image.lastIndexOf(':') > 0 ? image : image.concat(":latest");
                dockerClient.pull(imageToPull, new AnsiProgressHandler());
            }
        }
    } catch (DockerException | InterruptedException e) {
        logger.warn(nodeId + " Error while checking (and pulling) if the image is present", e);
        ga.trackException(e);
    }
    try {
        final ContainerCreation container = dockerClient.createContainer(containerConfig, containerName);
        dockerClient.startContainer(container.id());
        return new ContainerCreationStatus(true, containerName, nodePort);
    } catch (DockerException | InterruptedException e) {
        logger.warn(nodeId + " Error while starting a new container", e);
        ga.trackException(e);
        return new ContainerCreationStatus(false);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Arrays(java.util.Arrays) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Environment(de.zalando.ep.zalenium.util.Environment) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) URL(java.net.URL) AnsiProgressHandler(com.spotify.docker.client.AnsiProgressHandler) LoggerFactory(org.slf4j.LoggerFactory) GoogleAnalyticsApi(de.zalando.ep.zalenium.util.GoogleAnalyticsApi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) ArrayList(java.util.ArrayList) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Map(java.util.Map) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerException(com.spotify.docker.client.exceptions.DockerException) Logger(org.slf4j.Logger) DockerSeleniumStarterRemoteProxy(de.zalando.ep.zalenium.proxy.DockerSeleniumStarterRemoteProxy) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) Container(com.spotify.docker.client.messages.Container) List(java.util.List) ExecCreation(com.spotify.docker.client.messages.ExecCreation) Image(com.spotify.docker.client.messages.Image) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HostConfig(com.spotify.docker.client.messages.HostConfig) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Image(com.spotify.docker.client.messages.Image) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) AnsiProgressHandler(com.spotify.docker.client.AnsiProgressHandler) HostConfig(com.spotify.docker.client.messages.HostConfig)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 AnsiProgressHandler (com.spotify.docker.client.AnsiProgressHandler)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 DockerClient (com.spotify.docker.client.DockerClient)1 LogStream (com.spotify.docker.client.LogStream)1 DockerException (com.spotify.docker.client.exceptions.DockerException)1 AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)1 Container (com.spotify.docker.client.messages.Container)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 ContainerMount (com.spotify.docker.client.messages.ContainerMount)1 ExecCreation (com.spotify.docker.client.messages.ExecCreation)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 Image (com.spotify.docker.client.messages.Image)1 DockerSeleniumStarterRemoteProxy (de.zalando.ep.zalenium.proxy.DockerSeleniumStarterRemoteProxy)1 Environment (de.zalando.ep.zalenium.util.Environment)1 GoogleAnalyticsApi (de.zalando.ep.zalenium.util.GoogleAnalyticsApi)1 InputStream (java.io.InputStream)1