Search in sources :

Example 1 with ContainerCreation

use of com.spotify.docker.client.messages.ContainerCreation in project helios by spotify.

the class SyslogRedirectionTest method test.

@Test
public void test() throws Exception {
    final String syslogOutput = "should-be-redirected";
    try (final DockerClient docker = getNewDockerClient()) {
        // Start a container that will be our "syslog" endpoint (just run netcat and print whatever
        // we receive).
        final String port = "4711";
        final String expose = port + "/udp";
        docker.pull(ALPINE);
        final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
        final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
        ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
        final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
        final String syslogContainerId = creation.id();
        docker.startContainer(syslogContainerId);
        final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
        assertThat(containerInfo.state().running(), equalTo(true));
        final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
        // Run a Helios job that logs to syslog.
        startDefaultMaster();
        startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        final List<String> command = Lists.newArrayList();
        final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        {
            // Verify the log for the task container
            LogStream logs = null;
            try {
                logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
                final String log = logs.readFully();
                // for old docker versions should be nothing in the docker output log, either error text
                // or our message
                assertEquals("", log);
            } catch (DockerRequestException e) {
                // for new docker versions, trying to read logs should throw an error but the syslog
                // option should be set
                final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
                assertEquals("syslog", logType);
            } finally {
                if (logs != null) {
                    logs.close();
                }
            }
        }
        // Verify the log for the syslog container
        {
            final String log;
            try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
                log = logs.readFully();
            }
            // the output message from the command should appear in the syslog container
            assertThat(log, containsString(syslogOutput));
        }
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 2 with ContainerCreation

use of com.spotify.docker.client.messages.ContainerCreation in project helios by spotify.

the class SyslogRedirectionTest method setup.

@Before
public void setup() throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Build an image with an ENTRYPOINT and CMD prespecified
        final String dockerDirectory = Resources.getResource("syslog-test-image").getPath();
        docker.build(Paths.get(dockerDirectory), testImage);
        // Figure out the host IP from the container's point of view (needed for syslog)
        final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd(asList("ip", "route", "show")).build();
        final ContainerCreation creation = docker.createContainer(config);
        final String containerId = creation.id();
        docker.startContainer(containerId);
        // Wait for the container to exit.
        // If we don't wait, docker.logs() might return an epmty string because the container
        // cmd hasn't run yet.
        docker.waitContainer(containerId);
        final String log;
        try (LogStream logs = docker.logs(containerId, stdout(), stderr())) {
            log = logs.readFully();
        }
        final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log);
        if (m.find()) {
            syslogHost = m.group("gateway");
        } else {
            fail("couldn't determine the host address from '" + log + "'");
        }
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) Matcher(java.util.regex.Matcher) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) Before(org.junit.Before)

Example 3 with ContainerCreation

use of com.spotify.docker.client.messages.ContainerCreation in project helios by spotify.

the class SupervisorTest method verifySupervisorRestartsExitedContainer.

@Test
public void verifySupervisorRestartsExitedContainer() throws Exception {
    final String containerId1 = "deadbeef1";
    final String containerId2 = "deadbeef2";
    final ContainerCreation createResponse1 = ContainerCreation.builder().id(containerId1).build();
    final ContainerCreation createResponse2 = ContainerCreation.builder().id(containerId2).build();
    when(docker.createContainer(any(ContainerConfig.class), any(String.class))).thenReturn(createResponse1);
    final ImageInfo imageInfo = mock(ImageInfo.class);
    when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);
    when(docker.inspectContainer(eq(containerId1))).thenReturn(runningResponse);
    final SettableFuture<ContainerExit> waitFuture1 = SettableFuture.create();
    final SettableFuture<ContainerExit> waitFuture2 = SettableFuture.create();
    when(docker.waitContainer(containerId1)).thenAnswer(futureAnswer(waitFuture1));
    when(docker.waitContainer(containerId2)).thenAnswer(futureAnswer(waitFuture2));
    // Start the job
    sut.setGoal(START);
    verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
    verify(docker, timeout(30000)).startContainer(eq(containerId1));
    verify(docker, timeout(30000)).waitContainer(containerId1);
    // Indicate that the container exited
    when(docker.inspectContainer(eq(containerId1))).thenReturn(stoppedResponse);
    when(docker.createContainer(any(ContainerConfig.class), any(String.class))).thenReturn(createResponse2);
    when(docker.inspectContainer(eq(containerId2))).thenReturn(runningResponse);
    waitFuture1.set(ContainerExit.create(1));
    // Verify that the container was restarted
    verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
    verify(docker, timeout(30000)).startContainer(eq(containerId2));
    verify(docker, timeout(30000)).waitContainer(containerId2);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerExit(com.spotify.docker.client.messages.ContainerExit) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Example 4 with ContainerCreation

use of com.spotify.docker.client.messages.ContainerCreation in project helios by spotify.

the class HeliosSoloDeployment method deploySolo.

/**
   * @param heliosHost The address at which the Helios agent should expect to find the Helios
   *                   master.
   * @return The container ID of the Helios Solo container.
   * @throws HeliosDeploymentException if Helios Solo could not be deployed.
   */
private String deploySolo(final String heliosHost) throws HeliosDeploymentException {
    //TODO(negz): Don't make this.env immutable so early?
    final List<String> env = new ArrayList<>();
    env.addAll(this.env);
    env.add("HELIOS_NAME=" + agentName);
    env.add("HELIOS_ID=" + this.namespace + HELIOS_ID_SUFFIX);
    env.add("HOST_ADDRESS=" + heliosHost);
    final String heliosPort = String.format("%d/tcp", HELIOS_MASTER_PORT);
    final Map<String, List<PortBinding>> portBindings = ImmutableMap.of(heliosPort, singletonList(PortBinding.of("0.0.0.0", "")));
    final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).binds(binds).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().env(ImmutableList.copyOf(env)).hostConfig(hostConfig).image(heliosSoloImage).build();
    log.info("starting container for helios-solo with image={}", heliosSoloImage);
    final ContainerCreation creation;
    try {
        if (pullBeforeCreate) {
            dockerClient.pull(heliosSoloImage);
        }
        final String containerName = HELIOS_CONTAINER_PREFIX + this.namespace;
        creation = dockerClient.createContainer(containerConfig, containerName);
    } catch (DockerException | InterruptedException e) {
        throw new HeliosDeploymentException("helios-solo container creation failed", e);
    }
    try {
        dockerClient.startContainer(creation.id());
    } catch (DockerException | InterruptedException e) {
        killContainer(creation.id());
        removeContainer(creation.id());
        throw new HeliosDeploymentException("helios-solo container start failed", e);
    }
    log.info("helios-solo container started, containerId={}", creation.id());
    return creation.id();
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ArrayList(java.util.ArrayList) HostConfig(com.spotify.docker.client.messages.HostConfig) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 5 with ContainerCreation

use of com.spotify.docker.client.messages.ContainerCreation in project helios by spotify.

the class HeliosSoloDeployment method checkDockerAndGetGateway.

/**
   * Checks that the local Docker daemon is reachable from inside a container.
   * This method also gets the gateway IP address for this HeliosSoloDeployment.
   *
   * @return The gateway IP address of the gateway probe container.
   * @throws HeliosDeploymentException if we can't deploy the probe container or can't reach
   *         Docker daemon's API from inside the container.
   */
private String checkDockerAndGetGateway() throws HeliosDeploymentException {
    final String probeName = randomString();
    final HostConfig hostConfig = HostConfig.builder().binds(binds).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().env(env).hostConfig(hostConfig).image(PROBE_IMAGE).cmd(probeCommand(probeName)).build();
    final ContainerCreation creation;
    try {
        pullIfAbsent(PROBE_IMAGE);
        creation = dockerClient.createContainer(containerConfig, probeName);
    } catch (DockerException | InterruptedException e) {
        throw new HeliosDeploymentException("helios-solo probe container creation failed", e);
    }
    final ContainerExit exit;
    final String gateway;
    try {
        dockerClient.startContainer(creation.id());
        gateway = dockerClient.inspectContainer(creation.id()).networkSettings().gateway();
        exit = dockerClient.waitContainer(creation.id());
    } catch (DockerException | InterruptedException e) {
        killContainer(creation.id());
        throw new HeliosDeploymentException("helios-solo probe container failed", e);
    } finally {
        removeContainer(creation.id());
    }
    if (exit.statusCode() != 0) {
        throw new HeliosDeploymentException(String.format("Docker was not reachable (curl exit status %d) using DOCKER_HOST=%s and " + "DOCKER_CERT_PATH=%s from within a container. Please ensure that " + "DOCKER_HOST contains a full hostname or IP address, not localhost, " + "127.0.0.1, etc.", exit.statusCode(), containerDockerHost.bindUri(), containerDockerHost.dockerCertPath()));
    }
    return gateway;
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerExit(com.spotify.docker.client.messages.ContainerExit)

Aggregations

ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)11 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)11 HostConfig (com.spotify.docker.client.messages.HostConfig)6 ImageInfo (com.spotify.docker.client.messages.ImageInfo)5 DockerClient (com.spotify.docker.client.DockerClient)4 DockerException (com.spotify.docker.client.exceptions.DockerException)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)4 Test (org.junit.Test)4 ContainerExit (com.spotify.docker.client.messages.ContainerExit)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ImmutableList (com.google.common.collect.ImmutableList)2 LogStream (com.spotify.docker.client.LogStream)2 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)2 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)2 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)2 Integer.toHexString (java.lang.Integer.toHexString)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Before (org.junit.Before)2 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1