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();
}
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;
}
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);
}
}
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;
}
}
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();
}
Aggregations