Search in sources :

Example 46 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class MachineProviderImplTest method shouldAddBindMountAndRegularVolumesOnInstanceCreationFromSnapshot.

@Test
public void shouldAddBindMountAndRegularVolumesOnInstanceCreationFromSnapshot() throws Exception {
    String[] bindMountVolumesFromMachine = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
    String[] volumesFromMachine = new String[] { "/projects", "/something", "/something/else" };
    String[] expectedBindMountVolumes = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
    Map<String, Volume> expectedVolumes = Stream.of("/projects", "/something", "/something/else").collect(toMap(Function.identity(), v -> new Volume()));
    provider = new MachineProviderBuilder().setDevMachineVolumes(emptySet()).setAllMachineVolumes(emptySet()).build();
    CheServiceImpl service = createService();
    service.setVolumes(Stream.concat(Stream.of(bindMountVolumesFromMachine), Stream.of(volumesFromMachine)).collect(Collectors.toList()));
    createInstanceFromSnapshot(service, true);
    ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
    verify(dockerConnector).createContainer(argumentCaptor.capture());
    String[] actualBindMountVolumes = argumentCaptor.getValue().getContainerConfig().getHostConfig().getBinds();
    Map<String, Volume> actualVolumes = argumentCaptor.getValue().getContainerConfig().getVolumes();
    assertEquals(actualVolumes, expectedVolumes);
    assertEqualsNoOrder(actualBindMountVolumes, expectedBindMountVolumes);
}
Also used : RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) InspectContainerParams(org.eclipse.che.plugin.docker.client.params.InspectContainerParams) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) DOCKER_FILE_TYPE(org.eclipse.che.plugin.docker.machine.DockerInstanceProvider.DOCKER_FILE_TYPE) Mockito.doThrow(org.mockito.Mockito.doThrow) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Assert.assertFalse(org.testng.Assert.assertFalse) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) BeforeMethod(org.testng.annotations.BeforeMethod) RecipeRetriever(org.eclipse.che.api.machine.server.util.RecipeRetriever) Set(java.util.Set) Collectors(java.util.stream.Collectors) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) MACHINE_SNAPSHOT_PREFIX(org.eclipse.che.plugin.docker.machine.DockerInstanceProvider.MACHINE_SNAPSHOT_PREFIX) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) 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) DockerConnectorConfiguration(org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) HashSet(java.util.HashSet) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) ArgumentCaptor(org.mockito.ArgumentCaptor) ContainerState(org.eclipse.che.plugin.docker.client.json.ContainerState) Collections.singletonMap(java.util.Collections.singletonMap) ContainerInfo(org.eclipse.che.plugin.docker.client.json.ContainerInfo) Collections.emptySet(java.util.Collections.emptySet) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Machine(org.eclipse.che.api.core.model.machine.Machine) Mockito.never(org.mockito.Mockito.never) ServerException(org.eclipse.che.api.core.ServerException) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) Assert.assertEqualsNoOrder(org.testng.Assert.assertEqualsNoOrder) Volume(org.eclipse.che.plugin.docker.client.json.Volume) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 47 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class MachineProviderImpl method startService.

@Override
public Instance startService(String namespace, String workspaceId, String envName, String machineName, boolean isDev, String networkName, CheServiceImpl service, LineConsumer machineLogger) throws ServerException {
    // copy to not affect/be affected by changes in origin
    service = new CheServiceImpl(service);
    ProgressLineFormatterImpl progressLineFormatter = new ProgressLineFormatterImpl();
    ProgressMonitor progressMonitor = currentProgressStatus -> {
        try {
            machineLogger.writeLine(progressLineFormatter.format(currentProgressStatus));
        } catch (IOException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    };
    String container = null;
    try {
        String image = prepareImage(machineName, service, progressMonitor);
        container = createContainer(workspaceId, machineName, isDev, image, networkName, service);
        connectContainerToAdditionalNetworks(container, service);
        docker.startContainer(StartContainerParams.create(container));
        readContainerLogsInSeparateThread(container, workspaceId, service.getId(), machineLogger);
        DockerNode node = dockerMachineFactory.createNode(workspaceId, container);
        dockerInstanceStopDetector.startDetection(container, service.getId(), workspaceId);
        final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
        MachineImpl machine = new MachineImpl(MachineConfigImpl.builder().setDev(isDev).setName(machineName).setType("docker").setLimits(new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"))).setSource(new MachineSourceImpl(service.getBuild() != null ? "context" : "image").setLocation(service.getBuild() != null ? service.getBuild().getContext() : service.getImage())).build(), service.getId(), workspaceId, envName, userId, MachineStatus.RUNNING, null);
        return dockerMachineFactory.createInstance(machine, container, image, node, machineLogger);
    } catch (SourceNotFoundException e) {
        throw e;
    } catch (RuntimeException | ServerException | NotFoundException | IOException e) {
        cleanUpContainer(container);
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
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) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) IOException(java.io.IOException) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor)

Example 48 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl 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)

Example 49 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class LocalCheInfrastructureProvisioner method provision.

@Override
public void provision(EnvironmentImpl envConfig, CheServicesEnvironmentImpl internalEnv) throws EnvironmentException {
    String devMachineName = getDevMachineName(envConfig);
    if (devMachineName == null) {
        throw new EnvironmentException("ws-machine is not found on agents applying");
    }
    CheServiceImpl devMachine = internalEnv.getServices().get(devMachineName);
    for (CheServiceImpl machine : internalEnv.getServices().values()) {
        ArrayList<String> volumes = new ArrayList<>(machine.getVolumes());
        volumes.add(terminalVolumeProvider.get());
        machine.setVolumes(volumes);
    }
    // add bind-mount volume for projects in a workspace
    String projectFolderVolume;
    try {
        projectFolderVolume = format("%s:%s%s", workspaceFolderPathProvider.getPath(internalEnv.getWorkspaceId()), projectFolderPath, projectsVolumeOptions);
    } catch (IOException e) {
        throw new EnvironmentException("Error occurred on resolving path to files of workspace " + internalEnv.getWorkspaceId());
    }
    List<String> devMachineVolumes = devMachine.getVolumes();
    devMachineVolumes.add(SystemInfo.isWindows() ? pathEscaper.escapePath(projectFolderVolume) : projectFolderVolume);
    // add volume with ws-agent archive
    devMachineVolumes.add(wsAgentVolumeProvider.get());
    // add volume and variable to setup ws-agent configuration
    String dockerExtConfVolume = dockerExtConfBindingProvider.get();
    if (dockerExtConfVolume != null) {
        devMachineVolumes.add(dockerExtConfVolume);
    }
    HashMap<String, String> environmentVars = new HashMap<>(devMachine.getEnvironment());
    environmentVars.put(CheBootstrap.CHE_LOCAL_CONF_DIR, DockerExtConfBindingProvider.EXT_CHE_LOCAL_CONF_DIR);
    devMachine.setEnvironment(environmentVars);
    // apply basic infra (e.g. agents)
    super.provision(envConfig, internalEnv);
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException)

Example 50 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class CheEnvironmentEngineTest method shouldSetDefaultRamToMachineWithoutRamOnMachineStart.

@Test
public void shouldSetDefaultRamToMachineWithoutRamOnMachineStart() throws Exception {
    // given
    List<Instance> instances = startEnv();
    String workspaceId = instances.get(0).getWorkspaceId();
    when(engine.generateMachineId()).thenReturn("newMachineId");
    Instance newMachine = mock(Instance.class);
    when(newMachine.getId()).thenReturn("newMachineId");
    when(newMachine.getWorkspaceId()).thenReturn(workspaceId);
    when(machineProvider.startService(anyString(), anyString(), anyString(), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenReturn(newMachine);
    MachineConfigImpl config = createConfig(false);
    String machineName = "extraMachine";
    config.setName(machineName);
    config.setLimits(null);
    // when
    engine.startMachine(workspaceId, config, emptyList());
    // then
    ArgumentCaptor<CheServiceImpl> captor = ArgumentCaptor.forClass(CheServiceImpl.class);
    verify(machineProvider).startService(anyString(), anyString(), anyString(), eq(machineName), eq(false), anyString(), captor.capture(), any(LineConsumer.class));
    CheServiceImpl actualService = captor.getValue();
    assertEquals((long) actualService.getMemLimit(), DEFAULT_MACHINE_MEM_LIMIT_MB * 1024L * 1024L);
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)93 Test (org.testng.annotations.Test)63 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)46 Matchers.anyString (org.mockito.Matchers.anyString)32 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)28 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 Map (java.util.Map)18 Machine (org.eclipse.che.api.core.model.machine.Machine)17 CreateContainerParams (org.eclipse.che.plugin.docker.client.params.CreateContainerParams)17 ServerException (org.eclipse.che.api.core.ServerException)16 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)16 IOException (java.io.IOException)14 List (java.util.List)14 ServerConf (org.eclipse.che.api.core.model.machine.ServerConf)14 Collections.singletonMap (java.util.Collections.singletonMap)13 Set (java.util.Set)13 CheServiceBuildContextImpl (org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl)13 Instance (org.eclipse.che.api.machine.server.spi.Instance)13 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)13