com.spotify.docker.client.exceptions.DockerRequestException Java Examples

The following examples show how to use com.spotify.docker.client.exceptions.DockerRequestException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private LogStream getLogStream(final String method, final WebTarget resource,
                               final String containerId)
    throws DockerException, InterruptedException {
  try {
    final Invocation.Builder request = resource.request("application/vnd.docker.raw-stream");
    return request(method, LogStream.class, resource, request);
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new BadParamException(getQueryParamMap(resource), e);
      case 404:
        throw new ContainerNotFoundException(containerId);
      default:
        throw e;
    }
  }
}
 
Example #2
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteNode(final String nodeId, final boolean force)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");

  final WebTarget resource = resource().path("nodes")
      .path(nodeId)
      .queryParam("force", String.valueOf(force));

  try {
    request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new NodeNotFoundException(nodeId);
      case 503:
        throw new NonSwarmNodeException("Node " + nodeId + " is not a swarm node", e);
      default:
        throw e;
    }
  }
}
 
Example #3
Source File: SysImageServiceImpl.java    From paas with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Docker Hub镜像列表
 * @author jitwxs
 * @since 2018/6/28 16:15
 */
@Override
public ResultVO listHubImage(String name, Integer limit) {
    if (StringUtils.isBlank(name)) {
        return ResultVOUtils.error(ResultEnum.PARAM_ERROR);
    }

    try {
        List<ImageSearchResult> results = dockerClient.searchImages(name);
        return ResultVOUtils.success(results);
    } catch (DockerRequestException requestException){
        return ResultVOUtils.error(
                ResultEnum.SERVICE_CREATE_ERROR.getCode(),
                HttpClientUtils.getErrorMessage(requestException.getMessage()));
    } catch (Exception e) {
        log.error("Docker搜索异常,错误位置:SysImageServiceImpl.listHubImage,出错信息:" + HttpClientUtils.getStackTraceAsString(e));
        return ResultVOUtils.error(ResultEnum.DOCKER_EXCEPTION);
    }
}
 
Example #4
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public List<Container> listContainers(final ListContainersParam... params)
    throws DockerException, InterruptedException {
  WebTarget resource = resource()
      .path("containers").path("json");
  resource = addParameters(resource, params);

  try {
    return request(GET, CONTAINER_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new BadParamException(getQueryParamMap(resource), e);
      default:
        throw e;
    }
  }
}
 
Example #5
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteSecret(final String secretId) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.25");
  final WebTarget resource = resource().path("secrets").path(secretId);

  try {
    request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (final DockerRequestException ex) {
    switch (ex.status()) {
      case 404:
        throw new NotFoundException("Secret " + secretId + " not found.", ex);
      default:
        throw ex;
    }
  }
}
 
Example #6
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private void containerAction(final String containerId, final String action,
                             final MultivaluedMap<String, String> queryParameters)
        throws DockerException, InterruptedException {
  try {
    WebTarget resource = resource()
            .path("containers").path(containerId).path(action);

    for (Map.Entry<String, List<String>> queryParameter : queryParameters.entrySet()) {
      for (String parameterValue : queryParameter.getValue()) {
        resource = resource.queryParam(queryParameter.getKey(), parameterValue);
      }
    }
    request(POST, resource, resource.request());
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #7
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example #8
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example #9
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public NodeInfo inspectNode(final String nodeId) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");

  WebTarget resource = resource().path("nodes")
      .path(nodeId);

  try {
    return request(GET, NodeInfo.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new NodeNotFoundException(nodeId);
      case 503:
        throw new NonSwarmNodeException("Node " + nodeId + " is not in a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #10
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteConfig(final String configId) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.30");
  final WebTarget resource = resource().path("configs").path(configId);

  try {
    request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (final DockerRequestException ex) {
    switch (ex.status()) {
      case 404:
        throw new NotFoundException("Config " + configId + " not found.", ex);
      case 503:
        throw new NonSwarmNodeException("Config not part of a swarm.", ex);
      default:
        throw ex;
    }
  }
}
 
Example #11
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void push(final String image,
                 final ProgressHandler handler,
                 final RegistryAuth registryAuth)
    throws DockerException, InterruptedException {
  final ImageRef imageRef = new ImageRef(image);

  WebTarget resource = resource().path("images").path(imageRef.getImage()).path("push");

  if (imageRef.getTag() != null) {
    resource = resource.queryParam("tag", imageRef.getTag());
  }

  try {
    requestAndTail(POST, handler, resource,
            resource.request(APPLICATION_JSON_TYPE)
                .header("X-Registry-Auth", authHeader(registryAuth)));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ImageNotFoundException(image, e);
      default:
        throw e;
    }
  }
}
 
Example #12
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public NetworkCreation createNetwork(NetworkConfig networkConfig)
    throws DockerException, InterruptedException {
  final WebTarget resource = resource().path("networks").path("create");

  try {
    return request(POST, NetworkCreation.class, resource, resource.request(APPLICATION_JSON_TYPE),
                   Entity.json(networkConfig));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new NotFoundException("Plugin not found", e);
      default:
        throw e;
    }
  }
}
 
Example #13
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public Config inspectConfig(final String configId) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.30");
  final WebTarget resource = resource().path("configs").path(configId);

  try {
    return request(GET, Config.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (final DockerRequestException ex) {
    switch (ex.status()) {
      case 404:
        throw new NotFoundException("Config " + configId + " not found.", ex);
      case 503:
        throw new NonSwarmNodeException("Config not part of swarm.", ex);
      default:
        throw ex;
    }
  }
}
 
Example #14
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example #15
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example #16
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ContainerUpdate updateContainer(final String containerId, final HostConfig config)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.22");
  try {
    WebTarget resource = resource().path("containers").path(containerId).path("update");
    return request(POST, ContainerUpdate.class, resource, resource.request(APPLICATION_JSON_TYPE),
            Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId);
      default:
        throw e;
    }
  }
}
 
Example #17
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private LogStream getServiceLogStream(final String method, final WebTarget resource,
                                      final String serviceId)
    throws DockerException, InterruptedException {
  try {
    final Invocation.Builder request = resource.request("application/vnd.docker.raw-stream");
    return request(method, LogStream.class, resource, request);
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new BadParamException(getQueryParamMap(resource), e);
      case 404:
        throw new ServiceNotFoundException(serviceId);
      default:
        throw e;
    }
  }
}
 
Example #18
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public String initSwarm(final SwarmInit swarmInit) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");

  try {
    final WebTarget resource = resource().path("swarm").path("init");
    return request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE),
        Entity.json(swarmInit));

  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new DockerException("bad parameter", e);
      case 500:
        throw new DockerException("server error", e);
      case 503:
        throw new DockerException("node is already part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #19
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void joinSwarm(final SwarmJoin swarmJoin) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");

  try {
    final WebTarget resource = resource().path("swarm").path("join");
    request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE),
        Entity.json(swarmJoin));

  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 400:
        throw new DockerException("bad parameter", e);
      case 500:
        throw new DockerException("server error", e);
      case 503:
        throw new DockerException("node is already part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #20
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void leaveSwarm(final boolean force) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");

  try {
    final WebTarget resource = resource().path("swarm").path("leave").queryParam("force", force);
    request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE));

  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 500:
        throw new DockerException("server error", e);
      case 503:
        throw new DockerException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #21
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void resizeTty(final String containerId, final Integer height, final Integer width)
    throws DockerException, InterruptedException {
  checkTtyParams(height, width);

  WebTarget resource = resource().path("containers").path(containerId).path("resize");
  if (height != null && height > 0) {
    resource = resource.queryParam("h", height);
  }
  if (width != null && width > 0) {
    resource = resource.queryParam("w", width);
  }

  try {
    request(POST, resource, resource.request(TEXT_PLAIN_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #22
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public UnlockKey unlockKey() throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    final WebTarget resource = resource().path("swarm").path("unlockkey");

    return request(GET, UnlockKey.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 500:
        throw new DockerException("server error", e);
      case 503:
        throw new DockerException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #23
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void unlock(final UnlockKey unlockKey) throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    final WebTarget resource = resource().path("swarm").path("unlock");

    request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE),
        Entity.json(unlockKey));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 500:
        throw new DockerException("server error", e);
      case 503:
        throw new DockerException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #24
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ServiceCreateResponse createService(final ServiceSpec spec,
                                           final RegistryAuth config)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  final WebTarget resource = resource().path("services").path("create");

  try {
    return request(POST, ServiceCreateResponse.class, resource,
                   resource.request(APPLICATION_JSON_TYPE)
                       .header("X-Registry-Auth", authHeader(config)), Entity.json(spec));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 406:
        throw new DockerException("Server error or node is not part of swarm.", e);
      case 409:
        throw new DockerException("Name conflicts with an existing object.", e);
      default:
        throw e;
    }
  }
}
 
Example #25
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public Service inspectService(final String serviceId)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    final WebTarget resource = resource().path("services").path(serviceId);
    return request(GET, Service.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ServiceNotFoundException(serviceId);
      default:
        throw e;
    }
  }
}
 
Example #26
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void updateService(final String serviceId, final Long version, final ServiceSpec spec,
                                           final RegistryAuth config)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    WebTarget resource = resource().path("services").path(serviceId).path("update");
    resource = resource.queryParam("version", version);
    request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE)
            .header("X-Registry-Auth", authHeader(config)),
            Entity.json(spec));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ServiceNotFoundException(serviceId);
      default:
        throw e;
    }
  }
}
 
Example #27
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example #28
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ConfigCreateResponse createConfig(final ConfigSpec config)
    throws DockerException, InterruptedException {

  assertApiVersionIsAbove("1.30");
  final WebTarget resource = resource().path("configs").path("create");

  try {
    return request(POST, ConfigCreateResponse.class, resource,
        resource.request(APPLICATION_JSON_TYPE),
        Entity.json(config));
  } catch (final DockerRequestException ex) {
    switch (ex.status()) {
      case 503:
        throw new NonSwarmNodeException("Server not part of swarm.", ex);
      case 409:
        throw new ConflictException("Name conflicts with an existing object.", ex);
      default:
        throw ex;
    }
  }
}
 
Example #29
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public List<Config> listConfigs() throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.30");

  final WebTarget resource = resource().path("configs");

  try {
    return request(GET, CONFIG_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 503:
        throw new NonSwarmNodeException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example #30
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void execResizeTty(final String execId,
                          final Integer height,
                          final Integer width)
    throws DockerException, InterruptedException {
  checkTtyParams(height, width);

  WebTarget resource = resource().path("exec").path(execId).path("resize");
  if (height != null && height > 0) {
    resource = resource.queryParam("h", height);
  }
  if (width != null && width > 0) {
    resource = resource.queryParam("w", width);
  }

  try {
    request(POST, resource, resource.request(TEXT_PLAIN_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ExecNotFoundException(execId, e);
      default:
        throw e;
    }
  }
}