private void requestAndTail(final String method, final ProgressHandler handler, final WebTarget resource, final Invocation.Builder request) throws DockerException, InterruptedException { Response response = request(method, Response.class, resource, request); if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { throw new DockerRequestException(method, resource.getUri(), response.getStatus(), message(response), null); } tailResponse(method, response, handler, resource); }
@Override public Object call() throws Exception { try { dockerClient.killContainer(containerId); } catch (DockerRequestException e) { if (e.getResponseBody().contains("is not running")) { // Container already isn't running. So we continue. } else { throw e; } } try { dockerClient.removeContainer(containerId); return true; } catch (ContainerNotFoundException e) { // We're done here return true; } catch (DockerException e) { if ((e instanceof DockerRequestException) && ((DockerRequestException) e).getResponseBody().contains( "Driver btrfs failed to remove root filesystem")) { // Workaround btrfs issue where removing containers throws an exception, // but succeeds anyway. return true; } else { return null; } } } });
@Override public ImageInfo inspectImage(final String image) throws DockerException, InterruptedException { try { final WebTarget resource = resource().path("images").path(image).path("json"); return request(GET, ImageInfo.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ImageNotFoundException(image, e); default: throw e; } } }
@Override public ExecState execInspect(final String execId) throws DockerException, InterruptedException { final WebTarget resource = resource().path("exec").path(execId).path("json"); try { return request(GET, ExecState.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ExecNotFoundException(execId, e); default: throw e; } } }
throw new DockerRequestException(method, resource.getUri(), response.getStatus(), message(response), cause); } else if ((cause instanceof SocketTimeoutException)
@Override public ContainerStats stats(final String containerId) throws DockerException, InterruptedException { final WebTarget resource = resource().path("containers").path(containerId).path("stats") .queryParam("stream", "0"); try { return request(GET, ContainerStats.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public TopResults topContainer(final String containerId, final String psArgs) throws DockerException, InterruptedException { try { WebTarget resource = resource().path("containers").path(containerId).path("top"); if (!Strings.isNullOrEmpty(psArgs)) { resource = resource.queryParam("ps_args", psArgs); } return request(GET, TopResults.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public Network inspectNetwork(String networkId) throws DockerException, InterruptedException { final WebTarget resource = resource().path("networks").path(networkId); try { return request(GET, Network.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new NetworkNotFoundException(networkId, e); default: throw e; } } }
@Override public Volume inspectVolume(final String volumeName) throws DockerException, InterruptedException { final WebTarget resource = resource().path("volumes").path(volumeName); try { return request(GET, Volume.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new VolumeNotFoundException(volumeName, e); default: throw e; } } }
@Override public ContainerInfo inspectContainer(final String containerId) throws DockerException, InterruptedException { try { final WebTarget resource = resource().path("containers").path(containerId).path("json"); return request(GET, ContainerInfo.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public List<ContainerChange> inspectContainerChanges(final String containerId) throws DockerException, InterruptedException { try { final WebTarget resource = resource().path("containers").path(containerId).path("changes"); return request(GET, CONTAINER_CHANGE_LIST, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public InputStream exportContainer(String containerId) throws DockerException, InterruptedException { final WebTarget resource = resource() .path("containers").path(containerId).path("export"); try { return request(GET, InputStream.class, resource, resource.request(APPLICATION_OCTET_STREAM_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public void stopContainer(final String containerId, final int secondsToWaitBeforeKilling) throws DockerException, InterruptedException { try { final WebTarget resource = noTimeoutResource() .path("containers").path(containerId).path("stop") .queryParam("t", String.valueOf(secondsToWaitBeforeKilling)); request(POST, resource, resource.request()); } catch (DockerRequestException e) { switch (e.status()) { case 304: // already stopped, so we're cool return; case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public void removeNetwork(String networkId) throws DockerException, InterruptedException { try { final WebTarget resource = resource().path("networks").path(networkId); request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new NetworkNotFoundException(networkId, e); default: throw e; } } }
@Override public ContainerExit waitContainer(final String containerId) throws DockerException, InterruptedException { try { final WebTarget resource = noTimeoutResource() .path("containers").path(containerId).path("wait"); // Wait forever return request(POST, ContainerExit.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); default: throw e; } } }
@Override public List<ImageHistory> history(final String image) throws DockerException, InterruptedException { final WebTarget resource = resource() .path("images") .path(image) .path("history"); try { return request(GET, IMAGE_HISTORY_LIST, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ImageNotFoundException(image, e); default: throw e; } } }
@Override public List<RemovedImage> removeImage(String image, boolean force, boolean noPrune) throws DockerException, InterruptedException { try { final WebTarget resource = resource().path("images").path(image) .queryParam("force", String.valueOf(force)) .queryParam("noprune", String.valueOf(noPrune)); return request(DELETE, REMOVED_IMAGE_LIST, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ImageNotFoundException(image, e); case 409: throw new ConflictException(e); default: throw e; } } }
@Override public void renameContainer(final String containerId, final String name) throws DockerException, InterruptedException { WebTarget resource = resource() .path("containers").path(containerId).path("rename"); if (name == null) { throw new IllegalArgumentException("Cannot rename container to null"); } checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(), "Invalid container name: \"%s\"", name); resource = resource.queryParam("name", name); log.info("Renaming container with id {}. New name {}.", containerId, name); try { request(POST, resource, resource.request()); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ContainerNotFoundException(containerId, e); case 409: throw new ContainerRenameConflictException(containerId, name, e); default: throw e; } } }
@Override public void disconnectFromNetwork(String containerId, String networkId, boolean force) throws DockerException, InterruptedException { final WebTarget resource = resource().path("networks").path(networkId).path("disconnect"); final Map<String, Object> request = new HashMap<>(); request.put("Container", containerId); request.put("Force", force); try { request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(request)); } catch (DockerRequestException e) { switch (e.status()) { case 404: final String message = String.format("Container %s or network %s not found.", containerId, networkId); throw new NotFoundException(message, e); default: throw e; } } }
@Override public void removeService(final String serviceId) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); try { final WebTarget resource = resource().path("services").path(serviceId); request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ServiceNotFoundException(serviceId); default: throw e; } } }