Search in sources :

Example 61 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project flink by apache.

the class GCloudEmulatorManager method launchDocker.

public static void launchDocker() throws DockerException, InterruptedException, DockerCertificateException {
    // Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
    docker = DefaultDockerClient.fromEnv().build();
    terminateAndDiscardAnyExistingContainers(true);
    LOG.info("");
    LOG.info("/===========================================");
    LOG.info("| GCloud Emulator");
    ContainerInfo containerInfo;
    String id;
    try {
        docker.inspectImage(DOCKER_IMAGE_NAME);
    } catch (ImageNotFoundException e) {
        // No such image so we must download it first.
        LOG.info("| - Getting docker image \"{}\"", DOCKER_IMAGE_NAME);
        docker.pull(DOCKER_IMAGE_NAME, message -> {
            if (message.id() != null && message.progress() != null) {
                LOG.info("| - Downloading > {} : {}", message.id(), message.progress());
            }
        });
    }
    // No such container. Good, we create one!
    LOG.info("| - Creating new container");
    // Bind container ports to host ports
    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    portBindings.put(INTERNAL_PUBSUB_PORT, Collections.singletonList(PortBinding.randomPort("0.0.0.0")));
    final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
    // Create new container with exposed ports
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).exposedPorts(INTERNAL_PUBSUB_PORT).image(DOCKER_IMAGE_NAME).cmd("sh", "-c", "mkdir -p /opt/data/pubsub ; gcloud beta emulators pubsub start --data-dir=/opt/data/pubsub --host-port=0.0.0.0:" + INTERNAL_PUBSUB_PORT).build();
    LOG.debug("Launching container with configuration {}", containerConfig);
    final ContainerCreation creation = docker.createContainer(containerConfig, CONTAINER_NAME_JUNIT);
    id = creation.id();
    containerInfo = docker.inspectContainer(id);
    if (!containerInfo.state().running()) {
        LOG.warn("| - Starting it up ....");
        docker.startContainer(id);
        Thread.sleep(1000);
    }
    containerInfo = docker.inspectContainer(id);
    dockerIpAddress = "127.0.0.1";
    Map<String, List<PortBinding>> ports = containerInfo.networkSettings().ports();
    assertNotNull("Unable to retrieve the ports where to connect to the emulators", ports);
    assertEquals("We expect 1 port to be mapped", 1, ports.size());
    pubsubPort = getPort(ports, INTERNAL_PUBSUB_PORT, "PubSub");
    LOG.info("| Waiting for the emulators to be running");
    // PubSub exposes an "Ok" at the root url when running.
    if (!waitForOkStatus("PubSub", pubsubPort)) {
        // Oops, we did not get an "Ok" within 10 seconds
        startHasFailedKillEverything();
    }
    LOG.info("\\===========================================");
    LOG.info("");
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) 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) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Assert.assertNotNull(org.junit.Assert.assertNotNull) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PortBinding(com.spotify.docker.client.messages.PortBinding) HostConfig(com.spotify.docker.client.messages.HostConfig) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HashMap(java.util.HashMap) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) HostConfig(com.spotify.docker.client.messages.HostConfig) List(java.util.List) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Example 62 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project flink by apache.

the class GCloudEmulatorManager method terminateAndDiscardAnyExistingContainers.

private static void terminateAndDiscardAnyExistingContainers(boolean warnAboutExisting) throws DockerException, InterruptedException {
    ContainerInfo containerInfo;
    try {
        containerInfo = docker.inspectContainer(CONTAINER_NAME_JUNIT);
        // Already have this container running.
        assertNotNull("We should either get a containerInfo or we get an exception", containerInfo);
        LOG.info("");
        LOG.info("/===========================================");
        if (warnAboutExisting) {
            LOG.warn("|    >>> FOUND OLD EMULATOR INSTANCE RUNNING <<< ");
            LOG.warn("| Destroying that one to keep tests running smoothly.");
        }
        LOG.info("| Cleanup of GCloud Emulator. Log output of container: ");
        if (LOG.isInfoEnabled()) {
            try (LogStream stream = docker.logs(containerInfo.id(), DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr())) {
                LOG.info("| > {}", stream.readFully());
            }
        }
        // We REQUIRE 100% accurate side effect free unit tests
        // So we completely discard this one.
        String id = containerInfo.id();
        // Kill container
        if (containerInfo.state().running()) {
            docker.killContainer(id);
            LOG.info("| - Killed");
        }
        // Remove container
        docker.removeContainer(id);
        LOG.info("| - Removed");
        LOG.info("\\===========================================");
        LOG.info("");
    } catch (ContainerNotFoundException cnfe) {
    // No such container. Good !
    }
}
Also used : ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) LogStream(com.spotify.docker.client.LogStream) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException)

Example 63 with ContainerInfo

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

the class DockerContainerMock method getMockedDockerContainerClient.

@SuppressWarnings("ConstantConditions")
public static DockerContainerClient getMockedDockerContainerClient(String networkName) {
    DockerClient dockerClient = mock(DockerClient.class);
    ExecCreation execCreation = mock(ExecCreation.class);
    LogStream logStream = mock(LogStream.class);
    when(logStream.readFully()).thenReturn("ANY_STRING");
    when(execCreation.id()).thenReturn("ANY_ID");
    ContainerCreation containerCreation = mock(ContainerCreation.class);
    when(containerCreation.id()).thenReturn("ANY_CONTAINER_ID");
    AttachedNetwork attachedNetwork = mock(AttachedNetwork.class);
    NetworkSettings networkSettings = mock(NetworkSettings.class);
    HostConfig hostConfig = mock(HostConfig.class);
    ImageInfo imageInfo = mock(ImageInfo.class);
    ContainerConfig containerConfig = mock(ContainerConfig.class);
    ContainerInfo containerInfo = mock(ContainerInfo.class);
    ContainerMount tmpMountedMount = mock(ContainerMount.class);
    when(tmpMountedMount.destination()).thenReturn("/tmp/node/tmp/mounted");
    when(tmpMountedMount.source()).thenReturn("/tmp/mounted");
    ContainerMount homeFolderMount = mock(ContainerMount.class);
    when(homeFolderMount.destination()).thenReturn("/tmp/node/home/seluser/folder");
    when(homeFolderMount.source()).thenReturn("/tmp/folder");
    when(containerInfo.mounts()).thenReturn(ImmutableList.of(tmpMountedMount, homeFolderMount));
    when(attachedNetwork.ipAddress()).thenReturn("127.0.0.1");
    when(networkSettings.networks()).thenReturn(ImmutableMap.of(networkName, attachedNetwork));
    when(networkSettings.ipAddress()).thenReturn("");
    when(containerInfo.networkSettings()).thenReturn(networkSettings);
    when(hostConfig.extraHosts()).thenReturn(null);
    when(containerInfo.hostConfig()).thenReturn(hostConfig);
    String[] httpEnvVars = { "zalenium_http_proxy=http://34.211.100.239:8080", "zalenium_https_proxy=http://34.211.100.239:8080" };
    when(containerConfig.env()).thenReturn(ImmutableList.copyOf(Arrays.asList(httpEnvVars)));
    when(containerInfo.config()).thenReturn(containerConfig);
    String containerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container container_40000 = mock(Container.class);
    when(container_40000.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40000")));
    when(container_40000.id()).thenReturn(containerId);
    when(container_40000.status()).thenReturn("running");
    when(container_40000.image()).thenReturn("elgalu/selenium");
    Container container_40001 = mock(Container.class);
    when(container_40001.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40001")));
    when(container_40001.id()).thenReturn(containerId);
    when(container_40001.status()).thenReturn("running");
    when(container_40001.image()).thenReturn("elgalu/selenium");
    String zaleniumContainerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container zalenium = mock(Container.class);
    when(zalenium.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium")));
    when(zalenium.id()).thenReturn(zaleniumContainerId);
    when(zalenium.status()).thenReturn("running");
    when(zalenium.image()).thenReturn("dosel/zalenium");
    Info dockerInfo = mock(Info.class);
    when(dockerInfo.name()).thenReturn("ubuntu_vm");
    try {
        URL logsLocation = TestUtils.class.getClassLoader().getResource("logs.tar");
        URL videosLocation = TestUtils.class.getClassLoader().getResource("videos.tar");
        File logsFile = new File(logsLocation.getPath());
        File videosFile = new File(videosLocation.getPath());
        when(dockerClient.archiveContainer(containerId, "/var/log/cont/")).thenReturn(new FileInputStream(logsFile));
        when(dockerClient.archiveContainer(containerId, "/videos/")).thenReturn(new FileInputStream(videosFile));
        String[] startVideo = { "bash", "-c", START_RECORDING.getContainerAction() };
        String[] stopVideo = { "bash", "-c", STOP_RECORDING.getContainerAction() };
        String[] transferLogs = { "bash", "-c", TRANSFER_LOGS.getContainerAction() };
        String[] cleanupContainer = { "bash", "-c", CLEANUP_CONTAINER.getContainerAction() };
        String[] sendNotificationCompleted = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(COMPLETED.getTestNotificationMessage()) };
        String[] sendNotificationTimeout = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(TIMEOUT.getTestNotificationMessage()) };
        when(dockerClient.execCreate(containerId, startVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, stopVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, transferLogs, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, cleanupContainer, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationCompleted, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationTimeout, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execStart(anyString())).thenReturn(logStream);
        doNothing().when(dockerClient).stopContainer(anyString(), anyInt());
        when(dockerClient.info()).thenReturn(dockerInfo);
        when(dockerClient.createContainer(any(ContainerConfig.class), anyString())).thenReturn(containerCreation);
        when(dockerClient.listContainers(DockerClient.ListContainersParam.allContainers())).thenReturn(Arrays.asList(container_40000, container_40001, zalenium));
        when(containerConfig.labels()).thenReturn(ImmutableMap.of("selenium_firefox_version", "52", "selenium_chrome_version", "58"));
        when(imageInfo.config()).thenReturn(containerConfig);
        when(dockerClient.inspectContainer(null)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(zaleniumContainerId)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(containerId)).thenReturn(containerInfo);
        when(dockerClient.inspectImage(anyString())).thenReturn(imageInfo);
        when(dockerClient.listImages(DockerClient.ListImagesParam.byName("elgalu/selenium"))).thenReturn(Collections.emptyList());
    } catch (DockerException | InterruptedException | IOException e) {
        e.printStackTrace();
    }
    DockerContainerClient dockerContainerClient = new DockerContainerClient();
    dockerContainerClient.setContainerClient(dockerClient);
    return dockerContainerClient;
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) DockerContainerClient(de.zalando.ep.zalenium.container.DockerContainerClient) LogStream(com.spotify.docker.client.LogStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) Info(com.spotify.docker.client.messages.Info) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) URL(java.net.URL) FileInputStream(java.io.FileInputStream) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) NetworkSettings(com.spotify.docker.client.messages.NetworkSettings) Container(com.spotify.docker.client.messages.Container) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) File(java.io.File) ContainerMount(com.spotify.docker.client.messages.ContainerMount)

Example 64 with ContainerInfo

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

the class DockerContainerClient method getZaleniumNetwork.

private synchronized String getZaleniumNetwork(String zaleniumContainerName) {
    if (zaleniumNetwork != null) {
        return zaleniumNetwork;
    }
    String zaleniumContainerId = getContainerId(zaleniumContainerName);
    try {
        ContainerInfo containerInfo = dockerClient.inspectContainer(zaleniumContainerId);
        ImmutableMap<String, AttachedNetwork> networks = containerInfo.networkSettings().networks();
        for (Map.Entry<String, AttachedNetwork> networkEntry : networks.entrySet()) {
            if (!DEFAULT_DOCKER_NETWORK_NAME.equalsIgnoreCase(networkEntry.getKey())) {
                zaleniumNetwork = networkEntry.getKey();
                return zaleniumNetwork;
            }
        }
    } catch (DockerException | InterruptedException e) {
        logger.debug(nodeId + " Error while getting Zalenium network.", e);
        ga.trackException(e);
    }
    zaleniumNetwork = DEFAULT_DOCKER_NETWORK_MODE;
    return zaleniumNetwork;
}
Also used : AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 65 with ContainerInfo

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

the class DockerContainerClient method loadStorageOpts.

private void loadStorageOpts(String zaleniumContainerName) {
    if (!this.storageOptsLoaded.getAndSet(true)) {
        String containerId = getContainerId(zaleniumContainerName);
        if (containerId == null) {
            return;
        }
        ContainerInfo containerInfo;
        try {
            containerInfo = dockerClient.inspectContainer(containerId);
            storageOpt = containerInfo.hostConfig().storageOpt();
        } catch (DockerException | InterruptedException e) {
            logger.warn(nodeId + " Error while getting value to use passed storageOpts.", e);
            ga.trackException(e);
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo)

Aggregations

ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)68 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 Test (org.junit.Test)41 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)35 Matchers.containsString (org.hamcrest.Matchers.containsString)33 Long.toHexString (java.lang.Long.toHexString)31 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)31 DockerException (com.spotify.docker.client.exceptions.DockerException)15 HostConfig (com.spotify.docker.client.messages.HostConfig)15 IOException (java.io.IOException)9 DockerClient (com.spotify.docker.client.DockerClient)8 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)7 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)5 Container (com.spotify.docker.client.messages.Container)5 ImageInfo (com.spotify.docker.client.messages.ImageInfo)5 LogStream (com.spotify.docker.client.LogStream)4 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)4 AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)4 Path (java.nio.file.Path)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)3