Search in sources :

Example 1 with DnsConfig

use of com.spotify.docker.client.messages.swarm.DnsConfig 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)

Aggregations

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 NetworkAttachmentConfig (com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig)1 PortConfig (com.spotify.docker.client.messages.swarm.PortConfig)1 ServiceSpec (com.spotify.docker.client.messages.swarm.ServiceSpec)1 Task (com.spotify.docker.client.messages.swarm.Task)1 TaskSpec (com.spotify.docker.client.messages.swarm.TaskSpec)1 ShinyProxyException (eu.openanalytics.shinyproxy.ShinyProxyException)1 ContainerProxyRequest (eu.openanalytics.shinyproxy.container.ContainerProxyRequest)1 Utils (eu.openanalytics.shinyproxy.util.Utils)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Hex (org.apache.commons.codec.binary.Hex)1