/** * Determine if the image is already pulled. * * @param image the image name * @param imageTag the image tag * @return true if the image is already pulled, false otherwise */ boolean isImagePulled(String image, String imageTag) { boolean imagePulled = false; //determine if the image has already been pulled try { client.inspectImage(image); //if the tag is not the latest then that means we can look to see if //the image has been pulled. if it is then that means we always go and //pull the latest image by setting leaving imagePulled as false if (!DEFAULT_VERSION.equals(imageTag)) { imagePulled = true; } } catch (InterruptedException | DockerException e) { LoggingUtil.INSTANCE.info("Image '{}' not found", image); } return imagePulled; }
@Override public void onComplete() { LoggingUtil.INSTANCE.info("Image '{}:{}' pulled", virtualResource.value(), virtualResource.version()); latch.countDown(); }
boolean isImagePulled(String image, String imageTag) { boolean imagePulled = false; //determine if the image has already been pulled try { client.inspectImageCmd(image).exec(); //if the tag is not the latest then that means we can look to see if //the image has been pulled. if it is then that means we always go and //pull the latest image by setting leaving imagePulled as false if (!DEFAULT_VERSION.equals(imageTag)) { imagePulled = true; } } catch (NotFoundException e) { LoggingUtil.INSTANCE.info("Image '{}' not found", image); } return imagePulled; }
@Override public void onStart(Closeable closeable) { LoggingUtil.INSTANCE.info("Pulling '{}:{}' image", virtualResource.value(), virtualResource.version()); }
void waitForContainerToStart(VirtualResource virtualResource, Map<Integer, Integer> mappedPorts, InetAddress host) { RetryPolicy retryPolicy = new RetryPolicy() .retryOn(IOException.class) .withBackoff(virtualResource.delay(), virtualResource.maxDelay(), virtualResource.unit()) .withMaxRetries(virtualResource.maxRetries()) .withMaxDuration(virtualResource.maxDuration(), virtualResource.unit()); mappedPorts.entrySet().forEach(entry -> Failsafe.with(retryPolicy).run(() -> { LoggingUtil.INSTANCE.info("Waiting for '{}:{}' to be reachable", host.getHostAddress(), entry.getKey()); new Socket(host, entry.getKey()).close(); })); }
/** * Remove the given container using the given retry policy. * * @param retryPolicy the retry policy in the event of failure * @param containerId the container id */ void removeContainer(String containerId, RetryPolicy retryPolicy) { Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.info( "Trying to remove Docker Container '{}'", containerId)) .onSuccess(result -> LoggingUtil.INSTANCE.info( "Docker Container '{}' removed", containerId)) .onFailure(throwable -> LoggingUtil.INSTANCE.error( "Docker Container '{}' could not be removed", containerId, throwable)) .run(() -> client.removeContainer(containerId)); }
for (int port : ports) { Failsafe.with(retryPolicy).run(() -> { LoggingUtil.INSTANCE.info("Waiting for '{}:{}' to be reachable", host .getHostAddress(), port); new Socket(host, port).close(); mappedPorts.entrySet().forEach(entry -> Failsafe.with(retryPolicy).run(() -> { LoggingUtil.INSTANCE.info( "Waiting for '{}:{}' to be reachable", host.getHostAddress(), entry.getKey());
/** * Stop the given container using the given retry policy. * * @param retryPolicy the retry policy in the event of failure * @param containerId the container id */ void stopContainer(String containerId, RetryPolicy retryPolicy) { Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.info( "Trying to stop Docker Container '{}'", containerId)) .onSuccess(result -> { LoggingUtil.INSTANCE .info("Docker Container '{}' stopped", containerId); removeContainer(containerId, retryPolicy); }) .onFailure(throwable -> LoggingUtil.INSTANCE.error( "Docker Container '{}' could not be stopped", containerId, throwable)) .run(() -> client.stopContainer(containerId, 8)); }
@Override public void stop(TestContext testContext, VirtualResource virtualResource, VirtualResourceInstance instance) { try { if (started.compareAndSet(true, false)) { testContext.<ContainerInfo>findCollection(DOCKER_CONTAINERS).stream().map( p -> p.id()) .forEachOrdered(containerId -> { LoggingUtil.INSTANCE.info( "Stopping and Removing Docker Container {}", containerId); RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withBackoff(virtualResource.delay(), virtualResource .maxDelay(), virtualResource.unit()); stopContainer(containerId, retryPolicy); }); } } finally { if (client != null) { client.close(); } } }
DockerClientConfigBuilder configBuilder) { DockerClientConfig clientConfig = configBuilder.build(); LoggingUtil.INSTANCE.info("Connecting to {}", clientConfig.getDockerHost()); client = DockerClientBuilder.getInstance(clientConfig).build();
LoggingUtil.INSTANCE.info("Connecting to {}", clientBuilder.uri()); client = clientBuilder.build();
void removeContainer(String containerId, VirtualResource virtualResource) { LoggingUtil.INSTANCE.info("Stopping and Removing Docker Container {}", containerId); if (client.inspectContainerCmd(containerId).exec().getState().getRunning()) { client.stopContainerCmd(containerId).exec(); try { client.waitContainerCmd(containerId) .exec(new WaitCallback(containerId)) .awaitCompletion(); } catch (InterruptedException e) { LoggingUtil.INSTANCE.warn("wating for container interrupted", e); Thread.currentThread().interrupt(); } } RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withBackoff(virtualResource.delay(), virtualResource.maxDelay(), virtualResource.unit()) .withMaxRetries(virtualResource.maxRetries()) .withMaxDuration(8000, TimeUnit.MILLISECONDS); Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.debug("Trying to remove Docker Container {}", containerId, throwable)) .onSuccess(result -> { LoggingUtil.INSTANCE.info("Docker Container '{}' Removed", containerId); client.removeVolumeCmd(containerId).exec(); }) .onFailure(throwable -> LoggingUtil.INSTANCE.error("Docker Container '{}' could not be removed", containerId, throwable)) .run(() -> client.removeContainerCmd(containerId).exec()); }