com.spotify.docker.client.messages.swarm.TaskSpec Java Examples

The following examples show how to use com.spotify.docker.client.messages.swarm.TaskSpec. 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
@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 #2
Source File: ZookeeperDockerService.java    From pravega with Apache License 2.0 6 votes vote down vote up
private ServiceSpec setServiceSpec() {

        Map<String, String> labels = new HashMap<>();
        labels.put("com.docker.swarm.service.name", serviceName);

        final TaskSpec taskSpec = TaskSpec
                .builder()
                .containerSpec(ContainerSpec.builder().image(IMAGE_PATH + IMAGE_PREFIX + ZOOKEEPER_IMAGE_NAME + ZOOKEEPER_IMAGE_VERSION)
                        .hostname(serviceName)
                        .labels(labels)
                        .healthcheck(ContainerConfig.Healthcheck.builder().test(defaultHealthCheck(ZKSERVICE_ZKPORT)).build()).build())
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .resources(ResourceRequirements.builder()
                        .limits(Resources.builder().memoryBytes(mem).nanoCpus((long) cpu).build())
                        .build())
                .build();
        ServiceSpec spec = ServiceSpec.builder().name(serviceName).mode(ServiceMode.withReplicas(instances))
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(ZKSERVICE_ZKPORT).targetPort(ZKSERVICE_ZKPORT)
                        .publishMode(PortConfig.PortConfigPublishMode.HOST).build()).build())
                .taskTemplate(taskSpec)
                .build();
        return spec;
    }
 
Example #3
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServiceWithWarnings() throws Exception {
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

  // build() calls /version to check what format of header to send
  enqueueServerApiVersion("1.25");
  enqueueServerApiResponse(201, "fixtures/1.25/createServiceResponse.json");

  final TaskSpec taskSpec = TaskSpec.builder()
      .containerSpec(ContainerSpec.builder()
          .image("this_image_is_not_found_in_the_registry")
          .build())
      .build();

  final ServiceSpec spec = ServiceSpec.builder()
      .name("test")
      .taskTemplate(taskSpec)
      .build();

  final ServiceCreateResponse response = dockerClient.createService(spec);
  assertThat(response.id(), is(notNullValue()));
  assertThat(response.warnings(), is(hasSize(1)));
  assertThat(response.warnings(),
      contains("unable to pin image this_image_is_not_found_in_the_registry to digest"));
}
 
Example #4
Source File: DockerServiceElasticAgent.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 5 votes vote down vote up
public static DockerServiceElasticAgent fromService(Service service, DockerClient client) throws DockerException, InterruptedException {
    DockerServiceElasticAgent agent = new DockerServiceElasticAgent();

    agent.id = service.id();
    agent.name = service.spec().name();
    agent.createdAt = service.createdAt();
    agent.jobIdentifier = JobIdentifier.fromJson(service.spec().labels().get(JOB_IDENTIFIER_LABEL_KEY));

    LogStream logStream = client.serviceLogs(service.id(), DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr());
    agent.logs = logStream.readFully();
    logStream.close();

    TaskSpec taskSpec = service.spec().taskTemplate();

    agent.image = taskSpec.containerSpec().image();
    agent.hostname = taskSpec.containerSpec().hostname();
    agent.limits = resourceToString(taskSpec.resources().limits());
    agent.reservations = resourceToString(taskSpec.resources().reservations());
    agent.command = listToString(taskSpec.containerSpec().command());
    agent.args = listToString(taskSpec.containerSpec().args());
    agent.placementConstraints = listToString(taskSpec.placement().constraints());
    agent.environments = toMap(taskSpec);
    agent.hosts = listToString(taskSpec.containerSpec().hosts());

    final List<Task> tasks = client.listTasks(Task.Criteria.builder().serviceName(service.id()).build());
    if (!tasks.isEmpty()) {
        for (Task task : tasks) {
            agent.tasksStatus.add(new TaskStatus(task));
        }
    }

    return agent;
}
 
Example #5
Source File: BookkeeperDockerService.java    From pravega with Apache License 2.0 5 votes vote down vote up
private ServiceSpec setServiceSpec() {

        Map<String, String> labels = new HashMap<>();
        labels.put("com.docker.swarm.service.name", serviceName);

        String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;
        List<String> stringList = new ArrayList<>();
        String env1 = "ZK_URL=" + zk;
        String env2 = "ZK=" + zk;
        String env3 = "bookiePort=" + String.valueOf(BK_PORT);
        String env4 = "DLOG_EXTRA_OPTS=-Xms512m";
        String env5 = "BK_useHostNameAsBookieID=false";
        stringList.add(env1);
        stringList.add(env2);
        stringList.add(env3);
        stringList.add(env4);
        stringList.add(env5);

        final TaskSpec taskSpec = TaskSpec
                .builder().restartPolicy(RestartPolicy.builder().maxAttempts(3).condition(RESTART_POLICY_ANY).build())
                .containerSpec(ContainerSpec.builder()
                        .hostname(serviceName)
                        .labels(labels)
                        .image(IMAGE_PATH + IMAGE_PREFIX + BOOKKEEPER_IMAGE_NAME + PRAVEGA_VERSION)
                        .healthcheck(ContainerConfig.Healthcheck.builder().test(defaultHealthCheck(BK_PORT)).build())
                        .env(stringList).build())
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .resources(ResourceRequirements.builder()
                        .reservations(Resources.builder()
                                .memoryBytes(setMemInBytes(mem)).nanoCpus(setNanoCpus(cpu)).build())
                        .build())
                .build();
        ServiceSpec spec = ServiceSpec.builder().name(serviceName).mode(ServiceMode.withReplicas(instances))
                .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
                .endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(BK_PORT).build()).build())
                .taskTemplate(taskSpec)
                .build();
        return spec;
    }
 
Example #6
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateServiceWithPlacementPreference()
    throws IOException, DockerException, InterruptedException {
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

  final ImmutableList<Preference> prefs = ImmutableList.of(
      Preference.create(
          Spread.create(
              "test"
          )
      )
  );

  final TaskSpec taskSpec = TaskSpec.builder()
      .placement(Placement.create(null, prefs))
      .containerSpec(ContainerSpec.builder()
          .image("this_image_is_found_in_the_registry")
          .build())
      .build();

  final ServiceSpec spec = ServiceSpec.builder()
      .name("test")
      .taskTemplate(taskSpec)
      .build();


  enqueueServerApiVersion("1.30");
  enqueueServerApiResponse(201, "fixtures/1.30/createServiceResponse.json");

  final ServiceCreateResponse response = dockerClient.createService(spec);
  assertThat(response.id(), equalTo("ak7w3gjqoa3kuz8xcpnyy0pvl"));

  enqueueServerApiVersion("1.30");
  enqueueServerApiResponse(200, "fixtures/1.30/inspectCreateResponseWithPlacementPrefs.json");

  final Service service = dockerClient.inspectService("ak7w3gjqoa3kuz8xcpnyy0pvl");
  assertThat(service.spec().taskTemplate().placement(), equalTo(taskSpec.placement()));
}
 
Example #7
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testInspectTask() throws Exception {
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

  enqueueServerApiVersion("1.24");
  enqueueServerApiResponse(200, "fixtures/1.24/task.json");

  final Task task = dockerClient.inspectTask("0kzzo1i0y4jz6027t0k7aezc7");

  assertThat(task, is(pojo(Task.class)
      .where("id", is("0kzzo1i0y4jz6027t0k7aezc7"))
      .where("version", is(pojo(Version.class)
          .where("index", is(71L))
      ))
      .where("createdAt", is(Date.from(Instant.parse("2016-06-07T21:07:31.171892745Z"))))
      .where("updatedAt", is(Date.from(Instant.parse("2016-06-07T21:07:31.376370513Z"))))
      .where("spec", is(pojo(TaskSpec.class)
          .where("containerSpec", is(pojo(ContainerSpec.class)
              .where("image", is("redis"))
          ))
          .where("resources", is(pojo(ResourceRequirements.class)
              .where("limits",
                  is(pojo(com.spotify.docker.client.messages.swarm.Resources.class)))
              .where("reservations",
                  is(pojo(com.spotify.docker.client.messages.swarm.Resources.class)))
          ))
      ))
  ));
}
 
Example #8
Source File: HDFSDockerService.java    From pravega with Apache License 2.0 4 votes vote down vote up
private ServiceSpec setServiceSpec() {
    Map<String, String> labels = new HashMap<>();
    labels.put("com.docker.swarm.task.name", serviceName);
    Mount mount = Mount.builder().type("volume").source("hadoop-logs").target("/opt/hadoop/logs").build();
    String env1 = "SSH_PORT=2222";
    String env2 = "HDFS_HOST=" + serviceName;

    final TaskSpec taskSpec = TaskSpec
            .builder()
            .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
            .containerSpec(ContainerSpec.builder().image(hdfsimage).env(Arrays.asList(env1, env2))
                    .healthcheck(ContainerConfig.Healthcheck.builder().test(customHealthCheck("ss -l | grep " + HDFS_PORT + " || exit 1")).build())
                    .mounts(mount)
                    .labels(labels)
                    .hostname(serviceName)
                    .build())
            .resources(ResourceRequirements.builder()
                    .reservations(Resources.builder()
                            .memoryBytes(setMemInBytes(mem)).nanoCpus(setNanoCpus(cpu)).build())
                    .build())
            .build();
    List<PortConfig> portConfigs = new ArrayList<>();
    PortConfig port1 = PortConfig.builder().publishedPort(8020).targetPort(8020).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs").build();
    PortConfig port2 = PortConfig.builder().publishedPort(50090).targetPort(50090).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-secondary").build();
    PortConfig port3 = PortConfig.builder().publishedPort(50010).targetPort(50010).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-datanode").build();
    PortConfig port4 = PortConfig.builder().publishedPort(50020).targetPort(50020).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-datanode-ipc").build();
    PortConfig port5 = PortConfig.builder().publishedPort(50075).targetPort(50075).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-datanode-http").build();
    PortConfig port6 = PortConfig.builder().publishedPort(50070).targetPort(50070).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-web").build();
    PortConfig port7 = PortConfig.builder().publishedPort(2222).targetPort(2222).publishMode(PortConfig.PortConfigPublishMode.HOST).name("hdfs-ssh").build();
    portConfigs.add(port1);
    portConfigs.add(port2);
    portConfigs.add(port3);
    portConfigs.add(port4);
    portConfigs.add(port5);
    portConfigs.add(port6);
    portConfigs.add(port7);

    ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(instances))
            .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
            .endpointSpec(EndpointSpec.builder().ports(portConfigs)
                    .build()).build();
    return spec;
}
 
Example #9
Source File: PravegaControllerDockerService.java    From pravega with Apache License 2.0 4 votes vote down vote up
private ServiceSpec setServiceSpec() {
    Mount mount = Mount.builder().type("Volume").source("controller-logs").target("/opt/pravega/logs").build();
    String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;
    Map<String, String> stringBuilderMap = new HashMap<>();
    stringBuilderMap.put("controller.zk.connect.uri", zk);
    stringBuilderMap.put("controller.service.rpc.published.host.nameOrIp", serviceName);
    stringBuilderMap.put("controller.service.rpc.published.port", String.valueOf(controllerPort));
    stringBuilderMap.put("controller.service.rpc.listener.port", String.valueOf(controllerPort));
    stringBuilderMap.put("controller.service.rest.listener.port", String.valueOf(restPort));
    stringBuilderMap.put("log.level", "DEBUG");
    stringBuilderMap.put("curator-default-session-timeout", String.valueOf(10 * 1000));
    stringBuilderMap.put("controller.zk.connect.session.timeout.milliseconds", String.valueOf(30 * 1000));
    stringBuilderMap.put("controller.transaction.lease.count.max", String.valueOf(120 * 1000));
    stringBuilderMap.put("controller.retention.frequency.minutes", String.valueOf(2));
    StringBuilder systemPropertyBuilder = new StringBuilder();
    for (Map.Entry<String, String> entry : stringBuilderMap.entrySet()) {
        systemPropertyBuilder.append("-D").append(entry.getKey()).append("=").append(entry.getValue()).append(" ");
    }

    String controllerSystemProperties = systemPropertyBuilder.toString();
    String env1 = "PRAVEGA_CONTROLLER_OPTS=" + controllerSystemProperties;
    String env2 = "JAVA_OPTS=-Xmx512m";
    Map<String, String> labels = new HashMap<>();
    labels.put("com.docker.swarm.task.name", serviceName);

    final TaskSpec taskSpec = TaskSpec
            .builder()
            .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).aliases(serviceName).build())
            .containerSpec(ContainerSpec.builder().image(IMAGE_PATH + IMAGE_PREFIX + PRAVEGA_IMAGE_NAME + PRAVEGA_VERSION)
                    .healthcheck(ContainerConfig.Healthcheck.builder().test(defaultHealthCheck(controllerPort)).build())
                    .mounts(Arrays.asList(mount))
                    .hostname(serviceName)
                    .labels(labels)
                    .env(Arrays.asList(env1, env2)).args("controller").build())
            .resources(ResourceRequirements.builder()
                    .reservations(Resources.builder()
                            .memoryBytes(setMemInBytes(mem)).nanoCpus(setNanoCpus(cpu)).build())
                    .build())
            .build();
    ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(instances))
            .endpointSpec(EndpointSpec.builder()
                    .ports(Arrays.asList(PortConfig.builder()
                                    .publishedPort(controllerPort).targetPort(controllerPort).publishMode(PortConfig.PortConfigPublishMode.HOST).build(),
                            PortConfig.builder().publishedPort(restPort).targetPort(restPort).publishMode(PortConfig.PortConfigPublishMode.HOST).build())).
                            build())
            .build();
    return spec;
}
 
Example #10
Source File: PravegaSegmentStoreDockerService.java    From pravega with Apache License 2.0 4 votes vote down vote up
private ServiceSpec setServiceSpec() {
    Map<String, String> labels = new HashMap<>();
    labels.put("com.docker.swarm.task.name", serviceName);

    Mount mount = Mount.builder().type("volume").source("segmentstore-logs").target("/opt/pravega/logs").build();
    String zk = zkUri.getHost() + ":" + ZKSERVICE_ZKPORT;
    //System properties to configure SS service.
    Map<String, String> stringBuilderMap = new HashMap<>();
    StringBuilder systemPropertyBuilder = new StringBuilder();
    stringBuilderMap.put("autoScale.muteInSeconds", "120");
    stringBuilderMap.put("autoScale.cooldownInSeconds", "120");
    stringBuilderMap.put("autoScale.cacheExpiryInSeconds", "120");
    stringBuilderMap.put("autoScale.cacheCleanUpInSeconds", "120");
    stringBuilderMap.put("log.level", "DEBUG");
    stringBuilderMap.put("curator-default-session-timeout", String.valueOf(30 * 1000));
    stringBuilderMap.put("hdfs.replaceDataNodesOnFailure", "false");
    stringBuilderMap.put("bookkeeper.bkAckQuorumSize", "3");
    for (Map.Entry<String, String> entry : stringBuilderMap.entrySet()) {
        systemPropertyBuilder.append("-D").append(entry.getKey()).append("=").append(entry.getValue()).append(" ");
    }

    String hostSystemProperties = systemPropertyBuilder.toString();

    //set env
    String env1 = "PRAVEGA_SEGMENTSTORE_OPTS=" + hostSystemProperties;
    String env2 = "JAVA_OPTS=-Xmx2000m";
    List<String> envList = new ArrayList<>();
    envList.add(env1);
    envList.add(env2);
    getCustomEnvVars(envList, SEGMENTSTORE_EXTRA_ENV, hdfsUri);
    String env3 = "ZK_URL=" + zk;
    String env4 = "BK_ZK_URL=" + zk;
    String env5 = "CONTROLLER_URL=" + conUri.toString();
    envList.add(env3);
    envList.add(env4);
    envList.add(env5);

    final TaskSpec taskSpec = TaskSpec
            .builder()
            .networks(NetworkAttachmentConfig.builder().target(DOCKER_NETWORK).build())
            .containerSpec(ContainerSpec.builder().image(IMAGE_PATH + IMAGE_PREFIX + PRAVEGA_IMAGE_NAME + PRAVEGA_VERSION)
                    .hostname(serviceName)
                    .labels(labels)
                    .healthcheck(ContainerConfig.Healthcheck.builder().test(defaultHealthCheck(SEGMENTSTORE_PORT)).build())
                    .mounts(Arrays.asList(mount))
                    .env(envList).args("segmentstore").build())
            .resources(ResourceRequirements.builder()
                    .reservations(Resources.builder()
                            .memoryBytes(setMemInBytes(mem)).nanoCpus(setNanoCpus(cpu)).build())
                    .build())
            .build();
    ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(instances))
            .endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().
                    publishedPort(SEGMENTSTORE_PORT).targetPort(SEGMENTSTORE_PORT).publishMode(PortConfig.PortConfigPublishMode.HOST).build())
                    .build()).build();
    return spec;
}
 
Example #11
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateServiceWithConfig() throws Exception {
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

  // build() calls /version to check what format of header to send
  enqueueServerApiVersion("1.30");
  enqueueServerApiResponse(201, "fixtures/1.30/configCreateResponse.json");

  final ConfigSpec configSpec = ConfigSpec
      .builder()
      .data(Base64.encodeAsString("foobar"))
      .name("foo.yaml")
      .build();

  final ConfigCreateResponse configCreateResponse = dockerClient.createConfig(configSpec);
  assertThat(configCreateResponse.id(), equalTo("ktnbjxoalbkvbvedmg1urrz8h"));

  final ConfigBind configBind = ConfigBind.builder()
      .configName(configSpec.name())
      .configId(configCreateResponse.id())
      .file(ConfigFile.builder()
        .gid("1000")
        .uid("1000")
        .mode(600L)
        .name(configSpec.name())
        .build()
      ).build();

  final TaskSpec taskSpec = TaskSpec.builder()
      .containerSpec(ContainerSpec.builder()
          .image("this_image_is_found_in_the_registry")
          .configs(ImmutableList.of(configBind))
          .build())
      .build();

  final ServiceSpec spec = ServiceSpec.builder()
      .name("test")
      .taskTemplate(taskSpec)
      .build();

  enqueueServerApiVersion("1.30");
  enqueueServerApiResponse(201, "fixtures/1.30/createServiceResponse.json");

  final ServiceCreateResponse response = dockerClient.createService(spec);
  assertThat(response.id(), equalTo("ak7w3gjqoa3kuz8xcpnyy0pvl"));
}