use of com.spotify.docker.client.messages.swarm.ContainerSpec in project docker-client by spotify.
the class DefaultDockerClientTest method testCreateServiceWithDefaults.
@Test
public void testCreateServiceWithDefaults() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final String serviceName = randomName();
final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).mounts(Mount.builder().volumeOptions(VolumeOptions.builder().driverConfig(com.spotify.docker.client.messages.mount.Driver.builder().build()).build()).bindOptions(BindOptions.builder().build()).tmpfsOptions(TmpfsOptions.builder().build()).build()).build()).resources(ResourceRequirements.builder().build()).restartPolicy(RestartPolicy.builder().build()).placement(Placement.create(null)).networks(NetworkAttachmentConfig.builder().build()).logDriver(Driver.builder().build()).build();
final ServiceMode serviceMode = ServiceMode.builder().replicated(ReplicatedService.builder().build()).build();
final ServiceSpec serviceSpec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(serviceMode).updateConfig(UpdateConfig.create(null, null, null)).networks(Collections.<NetworkAttachmentConfig>emptyList()).endpointSpec(EndpointSpec.builder().addPort(PortConfig.builder().build()).build()).build();
final ServiceCreateResponse response = sut.createService(serviceSpec);
assertThat(response.id(), is(notNullValue()));
sut.listTasks();
final Service service = sut.inspectService(serviceName);
final ServiceSpec actualServiceSpec = service.spec();
assertThat(actualServiceSpec.mode().replicated().replicas(), equalTo(1L));
assertThat(actualServiceSpec.taskTemplate().logDriver().options(), equalTo(Collections.<String, String>emptyMap()));
assertThat(actualServiceSpec.endpointSpec().mode(), equalTo(EndpointSpec.Mode.RESOLUTION_MODE_VIP));
assertThat(actualServiceSpec.updateConfig().failureAction(), equalTo("pause"));
final PortConfig.Builder portConfigBuilder = PortConfig.builder().protocol(PROTOCOL_TCP);
if (dockerApiVersionAtLeast("1.25")) {
// Ingress publish mode is the default for ports in API versions >= 1.25
portConfigBuilder.publishMode(PortConfigPublishMode.INGRESS);
}
final PortConfig expectedPortConfig = portConfigBuilder.build();
assertThat(actualServiceSpec.endpointSpec().ports(), contains(expectedPortConfig));
assertThat(service.endpoint().spec().ports(), contains(expectedPortConfig));
final ContainerSpec containerSpec = actualServiceSpec.taskTemplate().containerSpec();
assertThat(containerSpec.labels(), equalTo(Collections.<String, String>emptyMap()));
assertThat(containerSpec.mounts().size(), equalTo(1));
final Mount mount = containerSpec.mounts().get(0);
assertThat(mount.type(), equalTo("bind"));
final VolumeOptions volumeOptions = mount.volumeOptions();
assertThat(volumeOptions.noCopy(), nullValue());
assertThat(volumeOptions.labels(), nullValue());
final com.spotify.docker.client.messages.mount.Driver driver = volumeOptions.driverConfig();
assertThat(driver.name(), nullValue());
assertThat(driver.options(), nullValue());
final RestartPolicy restartPolicy = actualServiceSpec.taskTemplate().restartPolicy();
assertThat(restartPolicy.condition(), equalTo(RESTART_POLICY_ANY));
assertThat(restartPolicy.maxAttempts(), equalTo(0));
}
use of com.spotify.docker.client.messages.swarm.ContainerSpec in project docker-client by spotify.
the class DefaultDockerClientTest method testInspectTask.
@Test
public void testInspectTask() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final Date start = new Date();
final ServiceSpec serviceSpec = createServiceSpec(randomName());
assertThat(sut.listTasks().size(), is(0));
final ServiceCreateResponse serviceCreateResponse = sut.createService(serviceSpec);
await().until(numberOfTasks(sut), is(greaterThan(0)));
final Task someTask = sut.listTasks().get(0);
final Task inspectedTask = sut.inspectTask(someTask.id());
final Date now = new Date();
assertThat(inspectedTask.id(), notNullValue());
assertThat(inspectedTask.version().index(), allOf(notNullValue(), greaterThan(0L)));
assertThat(inspectedTask.createdAt(), allOf(notNullValue(), greaterThanOrEqualTo(start), lessThanOrEqualTo(now)));
assertThat(inspectedTask.updatedAt(), allOf(notNullValue(), greaterThanOrEqualTo(start), lessThanOrEqualTo(now)));
assertThat(inspectedTask.slot(), allOf(notNullValue(), greaterThan(0)));
assertThat(inspectedTask.status(), notNullValue());
assertThat(inspectedTask.name(), nullValue());
assertEquals(serviceCreateResponse.id(), inspectedTask.serviceId());
if (serviceSpec.labels() == null || serviceSpec.labels().isEmpty()) {
// Hamcrest has generally bad support for "is null or empty",
// and no support at all for empty maps
assertTrue(inspectedTask.labels() == null || inspectedTask.labels().isEmpty());
} else {
assertEquals(serviceSpec.labels(), inspectedTask.labels());
}
assertThat(inspectedTask.desiredState(), is(anything()));
assertThat(inspectedTask.networkAttachments(), is(anything()));
final TaskSpec taskSpecTemplate = serviceSpec.taskTemplate();
final TaskSpec taskSpecActual = inspectedTask.spec();
assertEquals(taskSpecTemplate.resources(), taskSpecActual.resources());
assertEquals(taskSpecTemplate.restartPolicy(), taskSpecActual.restartPolicy());
assertEquals(taskSpecTemplate.placement(), taskSpecActual.placement());
assertEquals(taskSpecTemplate.networks(), taskSpecActual.networks());
assertEquals(taskSpecTemplate.logDriver(), taskSpecActual.logDriver());
final ContainerSpec containerSpecTemplate = taskSpecTemplate.containerSpec();
final ContainerSpec containerSpecActual = taskSpecActual.containerSpec();
assertThat(containerSpecActual.image(), latestImageNameMatcher(containerSpecTemplate.image()));
assertEquals(containerSpecTemplate.labels(), containerSpecActual.labels());
assertEquals(containerSpecTemplate.command(), containerSpecActual.command());
assertEquals(containerSpecTemplate.args(), containerSpecActual.args());
assertEquals(containerSpecTemplate.env(), containerSpecActual.env());
assertEquals(containerSpecTemplate.dir(), containerSpecActual.dir());
assertEquals(containerSpecTemplate.user(), containerSpecActual.user());
assertEquals(containerSpecTemplate.groups(), containerSpecActual.groups());
assertEquals(containerSpecTemplate.tty(), containerSpecActual.tty());
assertEquals(containerSpecTemplate.mounts(), containerSpecActual.mounts());
assertEquals(containerSpecTemplate.stopGracePeriod(), containerSpecActual.stopGracePeriod());
}
use of com.spotify.docker.client.messages.swarm.ContainerSpec in project shinyproxy by openanalytics.
the class DockerSwarmBackend method doStartProxy.
@Override
protected void doStartProxy(DockerContainerProxy proxy) throws Exception {
Mount[] mounts = getBindVolumes(proxy.getApp()).stream().map(b -> b.split(":")).map(fromTo -> Mount.builder().source(fromTo[0]).target(fromTo[1]).type("bind").build()).toArray(i -> new Mount[i]);
ContainerSpec containerSpec = ContainerSpec.builder().image(proxy.getApp().getDockerImage()).command(proxy.getApp().getDockerCmd()).env(buildEnv(proxy.getUserId(), proxy.getApp())).dnsConfig(DnsConfig.builder().nameServers(proxy.getApp().getDockerDns()).build()).mounts(mounts).build();
NetworkAttachmentConfig[] networks = Arrays.stream(Optional.ofNullable(proxy.getApp().getDockerNetworkConnections()).orElse(new String[0])).map(n -> NetworkAttachmentConfig.builder().target(n).build()).toArray(i -> new NetworkAttachmentConfig[i]);
ServiceSpec.Builder serviceSpecBuilder = ServiceSpec.builder().networks(networks).name(proxy.getName()).taskTemplate(TaskSpec.builder().containerSpec(containerSpec).build());
if (proxy.getPort() > 0) {
serviceSpecBuilder.endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(proxy.getPort()).targetPort(getAppPort(proxy)).build()).build());
}
proxy.setServiceId(dockerClient.createService(serviceSpecBuilder.build()).id());
boolean containerFound = Utils.retry(i -> {
try {
Task serviceTask = dockerClient.listTasks(Task.Criteria.builder().serviceName(proxy.getName()).build()).stream().findAny().orElseThrow(() -> new IllegalStateException("Swarm service has no tasks"));
proxy.setContainerId(serviceTask.status().containerStatus().containerId());
} catch (Exception e) {
throw new RuntimeException("Failed to inspect swarm service tasks", e);
}
return (proxy.getContainerId() != null);
}, 10, 2000, true);
if (!containerFound) {
dockerClient.removeService(proxy.getServiceId());
throw new IllegalStateException("Swarm container did not start in time");
}
}
Aggregations