Search in sources :

Example 1 with DockerException

use of com.spotify.docker.client.exceptions.DockerException 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 2 with DockerException

use of com.spotify.docker.client.exceptions.DockerException 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)

Example 3 with DockerException

use of com.spotify.docker.client.exceptions.DockerException in project opennms by OpenNMS.

the class MinionHeartbeatOutageIT method restartContainer.

private void restartContainer(ContainerAlias alias) {
    final DockerClient docker = ((AbstractTestEnvironment) testEnvironment).getDockerClient();
    final String id = testEnvironment.getContainerInfo(alias).id();
    final Logger logger = getLogger();
    try {
        logger.info("Restarting container: {} -> {}", alias, id);
        docker.restartContainer(id);
        logger.info("Container restarted: {} -> {}", alias, id);
    } catch (DockerException | InterruptedException e) {
        logger.warn("Unexpected exception while restarting container {}", id, e);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) AbstractTestEnvironment(org.opennms.test.system.api.AbstractTestEnvironment) Logger(org.slf4j.Logger)

Example 4 with DockerException

use of com.spotify.docker.client.exceptions.DockerException in project helios by spotify.

the class TaskRunner method pullImage.

private void pullImage(final String image) throws DockerException, InterruptedException {
    listener.pulling();
    DockerTimeoutException wasTimeout = null;
    final Stopwatch pullTime = Stopwatch.createStarted();
    // Attempt to pull.  Failure, while less than ideal, is ok.
    try {
        docker.pull(image);
        listener.pulled();
        log.info("Pulled image {} in {}s", image, pullTime.elapsed(SECONDS));
    } catch (DockerTimeoutException e) {
        log.warn("Pulling image {} failed with timeout after {}s", image, pullTime.elapsed(SECONDS), e);
        listener.pullFailed();
        wasTimeout = e;
    } catch (DockerException e) {
        log.warn("Pulling image {} failed after {}s", image, pullTime.elapsed(SECONDS), e);
        listener.pullFailed();
    }
    try {
        // If we don't have the image by now, fail.
        docker.inspectImage(image);
    } catch (ImageNotFoundException e) {
        // to know, as the pull should have fixed the not found-ness.
        if (wasTimeout != null) {
            throw new ImagePullFailedException("Failed pulling image " + image + " because of timeout", wasTimeout);
        }
        throw e;
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ImagePullFailedException(com.spotify.docker.client.exceptions.ImagePullFailedException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) Stopwatch(com.google.common.base.Stopwatch) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Example 5 with DockerException

use of com.spotify.docker.client.exceptions.DockerException in project helios by spotify.

the class SystemTestBase method baseTeardown.

@After
public void baseTeardown() throws Exception {
    for (final HeliosClient client : clients) {
        client.close();
    }
    clients.clear();
    for (final Service service : services) {
        try {
            service.stopAsync();
        } catch (Exception e) {
            log.error("Uncaught exception", e);
        }
    }
    for (final Service service : services) {
        try {
            service.awaitTerminated();
        } catch (Exception e) {
            log.error("Service failed", e);
        }
    }
    services.clear();
    // Clean up docker
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final List<Container> containers = dockerClient.listContainers();
        for (final Container container : containers) {
            for (final String name : container.names()) {
                if (name.contains(testTag)) {
                    try {
                        dockerClient.killContainer(container.id());
                    } catch (DockerException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    } catch (Exception e) {
        log.error("Docker client exception", e);
    }
    if (zk != null) {
        zk.close();
    }
    listThreads();
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Service(com.google.common.util.concurrent.Service) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) HeliosClient(com.spotify.helios.client.HeliosClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) After(org.junit.After)

Aggregations

DockerException (com.spotify.docker.client.exceptions.DockerException)9 DockerClient (com.spotify.docker.client.DockerClient)6 AbstractTestEnvironment (org.opennms.test.system.api.AbstractTestEnvironment)4 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)2 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 HostConfig (com.spotify.docker.client.messages.HostConfig)2 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableList (com.google.common.collect.ImmutableList)1 Service (com.google.common.util.concurrent.Service)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)1 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)1 ImagePullFailedException (com.spotify.docker.client.exceptions.ImagePullFailedException)1 Container (com.spotify.docker.client.messages.Container)1 ContainerExit (com.spotify.docker.client.messages.ContainerExit)1 HeliosClient (com.spotify.helios.client.HeliosClient)1 IOException (java.io.IOException)1