Search in sources :

Example 1 with NetworkingConfig

use of org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig in project che by eclipse.

the class MachineProviderImpl method createContainer.

private String createContainer(String workspaceId, String machineName, boolean isDev, String image, String networkName, CheServiceImpl service) throws IOException {
    long machineMemorySwap = memorySwapMultiplier == -1 ? -1 : (long) (service.getMemLimit() * memorySwapMultiplier);
    addSystemWideContainerSettings(workspaceId, isDev, service);
    EndpointConfig endpointConfig = new EndpointConfig().withAliases(machineName).withLinks(toArrayIfNotNull(service.getLinks()));
    NetworkingConfig networkingConfig = new NetworkingConfig().withEndpointsConfig(singletonMap(networkName, endpointConfig));
    HostConfig hostConfig = new HostConfig();
    hostConfig.withMemorySwap(machineMemorySwap).withMemory(service.getMemLimit()).withNetworkMode(networkName).withLinks(toArrayIfNotNull(service.getLinks())).withPortBindings(service.getPorts().stream().collect(toMap(Function.identity(), value -> new PortBinding[0]))).withVolumesFrom(toArrayIfNotNull(service.getVolumesFrom()));
    ContainerConfig config = new ContainerConfig();
    config.withImage(image).withExposedPorts(service.getExpose().stream().distinct().collect(toMap(Function.identity(), value -> emptyMap()))).withHostConfig(hostConfig).withCmd(toArrayIfNotNull(service.getCommand())).withEntrypoint(toArrayIfNotNull(service.getEntrypoint())).withLabels(service.getLabels()).withNetworkingConfig(networkingConfig).withEnv(service.getEnvironment().entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).toArray(String[]::new));
    List<String> bindMountVolumes = new ArrayList<>();
    Map<String, Volume> nonBindMountVolumes = new HashMap<>();
    for (String volume : service.getVolumes()) {
        // If volume contains colon then it is bind volume, otherwise - non bind-mount volume.
        if (volume.contains(":")) {
            bindMountVolumes.add(volume);
        } else {
            nonBindMountVolumes.put(volume, new Volume());
        }
    }
    hostConfig.setBinds(bindMountVolumes.toArray(new String[bindMountVolumes.size()]));
    config.setVolumes(nonBindMountVolumes);
    addStaticDockerConfiguration(config);
    return docker.createContainer(CreateContainerParams.create(config).withContainerName(service.getContainerName())).getId();
}
Also used : ConnectContainer(org.eclipse.che.plugin.docker.client.json.network.ConnectContainer) RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) MachineStatus(org.eclipse.che.api.core.model.machine.MachineStatus) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) NetworkingConfig(org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig) LoggingUncaughtExceptionHandler(org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler) FileCleaner(org.eclipse.che.api.core.util.FileCleaner) Collectors.toSet(java.util.stream.Collectors.toSet) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) GetContainerLogsParams(org.eclipse.che.plugin.docker.client.params.GetContainerLogsParams) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) Set(java.util.Set) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) ConnectContainerToNetworkParams(org.eclipse.che.plugin.docker.client.params.network.ConnectContainerToNetworkParams) List(java.util.List) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Pattern(java.util.regex.Pattern) UserSpecificDockerRegistryCredentialsProvider(org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EndpointConfig(org.eclipse.che.plugin.docker.client.json.network.EndpointConfig) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) Size(org.eclipse.che.commons.lang.Size) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) LATEST_TAG(org.eclipse.che.plugin.docker.machine.DockerInstance.LATEST_TAG) CreateNetworkParams(org.eclipse.che.plugin.docker.client.params.network.CreateNetworkParams) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) SocketTimeoutException(java.net.SocketTimeoutException) MachineInstanceProvider(org.eclipse.che.api.environment.server.MachineInstanceProvider) Collections.singletonMap(java.util.Collections.singletonMap) Named(javax.inject.Named) Instance(org.eclipse.che.api.machine.server.spi.Instance) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) Files(java.nio.file.Files) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) FileWriter(java.io.FileWriter) MoreObjects(com.google.common.base.MoreObjects) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) SystemInfo(org.eclipse.che.api.core.util.SystemInfo) RemoveNetworkParams(org.eclipse.che.plugin.docker.client.params.RemoveNetworkParams) NotFoundException(org.eclipse.che.api.core.NotFoundException) File(java.io.File) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) NetworkingConfig(org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) Volume(org.eclipse.che.plugin.docker.client.json.Volume) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) EndpointConfig(org.eclipse.che.plugin.docker.client.json.network.EndpointConfig)

Aggregations

MoreObjects (com.google.common.base.MoreObjects)1 Strings (com.google.common.base.Strings)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Thread.sleep (java.lang.Thread.sleep)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1