Java Code Examples for com.spotify.docker.client.messages.swarm.Service#Criteria

The following examples show how to use com.spotify.docker.client.messages.swarm.Service#Criteria . 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: DockerBasedService.java    From pravega with Apache License 2.0 6 votes vote down vote up
private String getServiceID() {
    Service.Criteria criteria = Service.Criteria.builder().serviceName(this.serviceName).build();
    String serviceId = null;
    try {
        List<Service> serviceList = Exceptions.handleInterruptedCall(
                () -> dockerClient.listServices(criteria));
        log.info("Service list size {}", serviceList.size());
        if (!serviceList.isEmpty()) {
            serviceId = serviceList.get(0).id();
        }

    } catch (DockerException e) {
        throw new TestFrameworkException(TestFrameworkException.Type.RequestFailed, "Unable to get service id", e);
    }
    return serviceId;
}
 
Example 2
Source File: DockerBasedService.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> scaleService(final int instanceCount) {
    try {
        Preconditions.checkArgument(instanceCount >= 0, "negative value: %s", instanceCount);

        Service.Criteria criteria = Service.Criteria.builder().serviceName(this.serviceName).build();
        TaskSpec taskSpec = Exceptions.handleInterruptedCall(() -> dockerClient.listServices(criteria).get(0).spec().taskTemplate());
        String serviceId = getServiceID();
        EndpointSpec endpointSpec = Exceptions.handleInterruptedCall(() -> dockerClient.inspectService(serviceId).spec().endpointSpec());
        Service service = Exceptions.handleInterruptedCall(() -> dockerClient.inspectService(serviceId));
        Exceptions.handleInterrupted(() -> dockerClient.updateService(serviceId, service.version().index(), ServiceSpec.builder().endpointSpec(endpointSpec).mode(ServiceMode.withReplicas(instanceCount)).taskTemplate(taskSpec).name(serviceName).networks(service.spec().networks()).build()));
        return Exceptions.handleInterruptedCall(() -> waitUntilServiceRunning());
    } catch (DockerException e) {
        throw new TestFrameworkException(TestFrameworkException.Type.RequestFailed, "Test failure: Unable to scale service to given instances=" + instanceCount, e);
    }
}
 
Example 3
Source File: DockerBasedService.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
    try {
        Service.Criteria criteria = Service.Criteria.builder().serviceName(this.serviceName).build();
        List<Service> serviceList = Exceptions.handleInterruptedCall(() -> dockerClient.listServices(criteria));
        for (int i = 0; i < serviceList.size(); i++) {
            String serviceId = serviceList.get(i).id();
            Exceptions.handleInterrupted(() -> dockerClient.removeService(serviceId));
        }
    } catch (DockerException e) {
        throw new TestFrameworkException(TestFrameworkException.Type.RequestFailed, "Unable to remove service.", e);
    }
}
 
Example 4
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public List<Service> listServices(final Service.Criteria criteria)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  final Map<String, List<String>> filters = new HashMap<>();

  if (criteria.serviceId() != null) {
    filters.put("id", Collections.singletonList(criteria.serviceId()));
  }
  if (criteria.serviceName() != null) {
    filters.put("name", Collections.singletonList(criteria.serviceName()));
  }

  final List<String> labels = new ArrayList<>();
  for (Entry<String, String> input: criteria.labels().entrySet()) {
    if ("".equals(input.getValue())) {
      labels.add(input.getKey());
    } else {
      labels.add(String.format("%s=%s", input.getKey(), input.getValue()));
    }
  }

  if (!labels.isEmpty()) {
    filters.put("label", labels);
  }

  WebTarget resource = resource().path("services");
  resource = resource.queryParam("filters", urlEncodeFilters(filters));
  return request(GET, SERVICE_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
}
 
Example 5
Source File: SwarmDiscoveryUtil.java    From hazelcast-docker-swarm-discovery-spi with Apache License 2.0 3 votes vote down vote up
/**
 * Discover containers on the relevant networks that match the given
 * service criteria, using additionally a NullServiceFilter
 *
 * @param docker
 * @param relevantNetIds2Networks
 * @param criteria
 * @return set of DiscoveredContainer instances
 * @throws Exception
 */
private Set<DiscoveredContainer> discoverContainersViaCriteria(DockerClient docker,
                                                               Map<String, Network> relevantNetIds2Networks,
                                                               Service.Criteria criteria) throws Exception {
    // no ServiceFilter provided, so use one with no constraints
    return discoverContainersViaCriteria(docker, relevantNetIds2Networks, criteria, NullServiceFilter.getInstance());
}
 
Example 6
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * List services that match the given criteria. Only available in Docker API &gt;= 1.24.
 *
 * @param criteria Service listing and filtering options.
 * @return A list of {@link Service}s
 * @throws DockerException      if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
List<Service> listServices(Service.Criteria criteria)
        throws DockerException, InterruptedException;