@Override public LogStream attachContainer(final String containerId, final AttachParameter... params) throws DockerException, InterruptedException { checkNotNull(containerId, "containerId"); WebTarget resource = noTimeoutResource().path("containers").path(containerId).path("attach"); for (final AttachParameter param : params) { resource = resource.queryParam(param.name().toLowerCase(Locale.ROOT), String.valueOf(true)); } return getLogStream(POST, resource, containerId); }
@Override public LogStream logs(final String containerId, final LogsParam... params) throws DockerException, InterruptedException { WebTarget resource = noTimeoutResource() .path("containers").path(containerId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getLogStream(GET, resource, containerId); }
@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 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 LogStream serviceLogs(String serviceId, LogsParam... params) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.25"); WebTarget resource = noTimeoutResource() .path("services").path(serviceId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getServiceLogStream(GET, resource, serviceId); }
@Override public EventStream events(EventsParam... params) throws DockerException, InterruptedException { WebTarget resource = noTimeoutResource().path("events"); resource = addParameters(resource, params); try { final CloseableHttpClient client = (CloseableHttpClient) ApacheConnectorProvider .getHttpClient(noTimeoutClient); final CloseableHttpResponse response = client.execute(new HttpGet(resource.getUri())); return new EventStream(response, objectMapper()); } catch (IOException exception) { throw new DockerException(exception); } }
@Override public LogStream execStart(final String execId, final ExecStartParameter... params) throws DockerException, InterruptedException { final WebTarget resource = noTimeoutResource().path("exec").path(execId).path("start");
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") @Override public String build(final Path directory, final String name, final String dockerfile, final ProgressHandler handler, final BuildParam... params) throws DockerException, InterruptedException, IOException { checkNotNull(handler, "handler"); WebTarget resource = noTimeoutResource().path("build"); for (final BuildParam param : params) { resource = resource.queryParam(param.name(), param.value()); } if (name != null) { resource = resource.queryParam("t", name); } if (dockerfile != null) { resource = resource.queryParam("dockerfile", dockerfile); } // Convert auth to X-Registry-Config format final RegistryConfigs registryConfigs = registryAuthSupplier.authForBuild(); final BuildProgressHandler buildHandler = new BuildProgressHandler(handler); try (final CompressedDirectory compressedDirectory = CompressedDirectory.create(directory); final InputStream fileStream = Files.newInputStream(compressedDirectory.file())) { requestAndTail(POST, buildHandler, resource, resource.request(APPLICATION_JSON_TYPE) .header("X-Registry-Config", authRegistryHeader(registryConfigs)), Entity.entity(fileStream, "application/tar")); return buildHandler.getImageId(); } }
@Override public LogStream attachContainer(final String containerId, final AttachParameter... params) throws DockerException, InterruptedException { final ContainerInfo containerInfo = inspectContainer(containerId); if (!containerInfo.state().running()) { throw new IllegalStateException("Container " + containerId + " is not running."); } WebTarget resource = noTimeoutResource().path("containers").path(containerId).path("attach"); for (final AttachParameter param : params) { resource = resource.queryParam(param.name().toLowerCase(Locale.ROOT), String.valueOf(true)); } return getLogStream(POST, resource, containerId); }
@Override public LogStream logs(final String containerId, final LogsParam... params) throws DockerException, InterruptedException { WebTarget resource = noTimeoutResource() .path("containers").path(containerId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getLogStream(GET, resource, containerId); }
@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 LogStream serviceLogs(String serviceId, LogsParam... params) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.25"); WebTarget resource = noTimeoutResource() .path("services").path(serviceId) .path("logs"); for (final LogsParam param : params) { resource = resource.queryParam(param.name(), param.value()); } return getServiceLogStream(GET, resource, serviceId); }
@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 EventStream events(EventsParam... params) throws DockerException, InterruptedException { WebTarget resource = noTimeoutResource().path("events"); resource = addParameters(resource, params); try { final CloseableHttpClient client = (CloseableHttpClient) ApacheConnectorProvider .getHttpClient(noTimeoutClient); final CloseableHttpResponse response = client.execute(new HttpGet(resource.getUri())); return new EventStream(response, objectMapper()); } catch (IOException exception) { throw new DockerException(exception); } }
@Override public LogStream execStart(final String execId, final ExecStartParameter... params) throws DockerException, InterruptedException { final WebTarget resource = noTimeoutResource().path("exec").path(execId).path("start");
checkNotNull(handler, "handler"); WebTarget resource = noTimeoutResource().path("build");