Search in sources :

Example 1 with NetworkAttachmentConfig

use of com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig 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");
    }
}
Also used : Mount(com.spotify.docker.client.messages.mount.Mount) Arrays(java.util.Arrays) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) NetworkAttachmentConfig(com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig) ContainerProxyRequest(eu.openanalytics.shinyproxy.container.ContainerProxyRequest) Random(java.util.Random) TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) Hex(org.apache.commons.codec.binary.Hex) DnsConfig(com.spotify.docker.client.messages.swarm.DnsConfig) EndpointSpec(com.spotify.docker.client.messages.swarm.EndpointSpec) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) Task(com.spotify.docker.client.messages.swarm.Task) Optional(java.util.Optional) Utils(eu.openanalytics.shinyproxy.util.Utils) PortConfig(com.spotify.docker.client.messages.swarm.PortConfig) NetworkAttachmentConfig(com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig) Task(com.spotify.docker.client.messages.swarm.Task) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) Mount(com.spotify.docker.client.messages.mount.Mount) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException)

Example 2 with NetworkAttachmentConfig

use of com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testCreateServiceWithNetwork.

@Test
public void testCreateServiceWithNetwork() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "swarm support");
    final String networkName = randomName();
    final String serviceName = randomName();
    NetworkConfig.Builder networkConfigBuilder = NetworkConfig.builder().driver("overlay").name(networkName);
    if (dockerApiVersionEquals("1.24")) {
        // workaround for https://github.com/docker/docker/issues/25735
        networkConfigBuilder = networkConfigBuilder.ipam(Ipam.create("default", Collections.<IpamConfig>emptyList()));
    }
    final NetworkCreation networkCreation = sut.createNetwork(networkConfigBuilder.build());
    final String networkId = networkCreation.id();
    assertThat(networkId, is(notNullValue()));
    final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).build()).build();
    final ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(1L)).networks(NetworkAttachmentConfig.builder().target(networkName).build()).build();
    final ServiceCreateResponse response = sut.createService(spec);
    assertThat(response.id(), is(notNullValue()));
    final Service inspectService = sut.inspectService(serviceName);
    assertThat(inspectService.spec().networks().size(), is(1));
    assertThat(Iterables.find(inspectService.spec().networks(), new Predicate<NetworkAttachmentConfig>() {

        @Override
        public boolean apply(NetworkAttachmentConfig config) {
            return networkId.equals(config.target());
        }
    }, null), is(notNullValue()));
    sut.removeService(serviceName);
    sut.removeNetwork(networkName);
}
Also used : NetworkAttachmentConfig(com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig) TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) NetworkConfig(com.spotify.docker.client.messages.NetworkConfig) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ReplicatedService(com.spotify.docker.client.messages.swarm.ReplicatedService) CompletionService(java.util.concurrent.CompletionService) Service(com.spotify.docker.client.messages.swarm.Service) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutorService(java.util.concurrent.ExecutorService) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) NetworkCreation(com.spotify.docker.client.messages.NetworkCreation) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Aggregations

NetworkAttachmentConfig (com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig)2 ServiceSpec (com.spotify.docker.client.messages.swarm.ServiceSpec)2 TaskSpec (com.spotify.docker.client.messages.swarm.TaskSpec)2 Predicate (com.google.common.base.Predicate)1 NetworkConfig (com.spotify.docker.client.messages.NetworkConfig)1 NetworkCreation (com.spotify.docker.client.messages.NetworkCreation)1 ServiceCreateResponse (com.spotify.docker.client.messages.ServiceCreateResponse)1 Mount (com.spotify.docker.client.messages.mount.Mount)1 ContainerSpec (com.spotify.docker.client.messages.swarm.ContainerSpec)1 DnsConfig (com.spotify.docker.client.messages.swarm.DnsConfig)1 EndpointSpec (com.spotify.docker.client.messages.swarm.EndpointSpec)1 PortConfig (com.spotify.docker.client.messages.swarm.PortConfig)1 ReplicatedService (com.spotify.docker.client.messages.swarm.ReplicatedService)1 Service (com.spotify.docker.client.messages.swarm.Service)1 Task (com.spotify.docker.client.messages.swarm.Task)1 ShinyProxyException (eu.openanalytics.shinyproxy.ShinyProxyException)1 ContainerProxyRequest (eu.openanalytics.shinyproxy.container.ContainerProxyRequest)1 Utils (eu.openanalytics.shinyproxy.util.Utils)1 Long.toHexString (java.lang.Long.toHexString)1 Arrays (java.util.Arrays)1