Search in sources :

Example 16 with ExtendedMachineImpl

use of org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl in project che by eclipse.

the class CheEnvironmentEngine method startMachine.

/**
     * Starts machine in running environment.
     *
     * @param workspaceId
     *         ID of workspace that owns environment in which machine should be started
     * @param machineConfig
     *         configuration of machine that should be started
     * @return running machine
     * @throws EnvironmentNotRunningException
     *         if environment is not running
     * @throws NotFoundException
     *         if provider of machine implementation is not found
     * @throws ConflictException
     *         if machine with the same name already exists in the environment
     * @throws ServerException
     *         if any other error occurs
     */
public Instance startMachine(String workspaceId, MachineConfig machineConfig, List<String> agents) throws ServerException, NotFoundException, ConflictException, EnvironmentException {
    MachineConfig machineConfigCopy = new MachineConfigImpl(machineConfig);
    EnvironmentHolder environmentHolder;
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
        environmentHolder = environments.get(workspaceId);
        if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
            throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
        }
        for (Instance machine : environmentHolder.machines) {
            if (machine.getConfig().getName().equals(machineConfigCopy.getName())) {
                throw new ConflictException(format("Machine with name '%s' already exists in environment of workspace '%s'", machineConfigCopy.getName(), workspaceId));
            }
        }
    }
    final String creator = EnvironmentContext.getCurrent().getSubject().getUserId();
    final String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
    MachineImpl machine = MachineImpl.builder().setConfig(machineConfig).setWorkspaceId(workspaceId).setStatus(MachineStatus.CREATING).setEnvName(environmentHolder.name).setOwner(creator).build();
    MachineStarter machineStarter;
    if ("docker".equals(machineConfig.getType())) {
        // needed to reuse startInstance method and
        // create machine instances by different implementation-specific providers
        CheServiceImpl service = machineConfigToService(machineConfig);
        normalize(namespace, workspaceId, machineConfig.getName(), service);
        machine.setId(service.getId());
        machineStarter = (machineLogger, machineSource) -> {
            CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);
            normalize(namespace, workspaceId, machineConfig.getName(), serviceWithNormalizedSource);
            infrastructureProvisioner.provision(new ExtendedMachineImpl().withAgents(agents), serviceWithNormalizedSource);
            return machineProvider.startService(namespace, workspaceId, environmentHolder.name, machineConfig.getName(), machineConfig.isDev(), environmentHolder.networkId, serviceWithNormalizedSource, machineLogger);
        };
    } else {
        try {
            InstanceProvider provider = machineInstanceProviders.getProvider(machineConfig.getType());
            machine.setId(generateMachineId());
            addAgentsProvidedServers(machine, agents);
            machineStarter = (machineLogger, machineSource) -> {
                Machine machineWithNormalizedSource = normalizeMachineSource(machine, machineSource);
                return provider.createInstance(machineWithNormalizedSource, machineLogger);
            };
        } catch (NotFoundException e) {
            throw new NotFoundException(format("Provider of machine type '%s' not found", machineConfig.getType()));
        }
    }
    return startInstance(false, environmentHolder.logger, machine, machineStarter);
}
Also used : ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) Instance(org.eclipse.che.api.machine.server.spi.Instance) ConflictException(org.eclipse.che.api.core.ConflictException) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) Machine(org.eclipse.che.api.core.model.machine.Machine) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) InstanceProvider(org.eclipse.che.api.machine.server.spi.InstanceProvider)

Example 17 with ExtendedMachineImpl

use of org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl in project che by eclipse.

the class AgentConfigApplierTest method shouldAddEnvVariables.

@Test
public void shouldAddEnvVariables() throws Exception {
    when(sorter.sort(any())).thenReturn(asList(AgentKeyImpl.parse("agent1"), AgentKeyImpl.parse("agent2")));
    when(agent1.getProperties()).thenReturn(singletonMap("environment", "p1=v1,p2=v2"));
    when(agent2.getProperties()).thenReturn(singletonMap("environment", "p3=v3"));
    CheServiceImpl service = new CheServiceImpl();
    agentConfigApplier.apply(new ExtendedMachineImpl(asList("agent1", "agent2"), emptyMap(), emptyMap()), service);
    Map<String, String> env = service.getEnvironment();
    assertEquals(env.size(), 3);
    assertEquals(env.get("p1"), "v1");
    assertEquals(env.get("p2"), "v2");
    assertEquals(env.get("p3"), "v3");
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) Test(org.testng.annotations.Test)

Example 18 with ExtendedMachineImpl

use of org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl in project che by eclipse.

the class AgentConfigApplierTest method shouldIgnoreEnvironmentIfIllegalFormat.

@Test
public void shouldIgnoreEnvironmentIfIllegalFormat() throws Exception {
    when(sorter.sort(any())).thenReturn(singletonList(AgentKeyImpl.parse("agent1")));
    when(agent1.getProperties()).thenReturn(singletonMap("environment", "p1"));
    CheServiceImpl service = new CheServiceImpl();
    agentConfigApplier.apply(new ExtendedMachineImpl(singletonList("agent1"), emptyMap(), emptyMap()), service);
    Map<String, String> env = service.getEnvironment();
    assertEquals(env.size(), 0);
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) Test(org.testng.annotations.Test)

Example 19 with ExtendedMachineImpl

use of org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl in project che by eclipse.

the class AgentConfigApplierTest method shouldAddLabels.

@Test
public void shouldAddLabels() throws Exception {
    final ServerConf2 serverConf1 = mock(ServerConf2.class);
    when(serverConf1.getPort()).thenReturn("1111/udp");
    when(serverConf1.getProtocol()).thenReturn("http");
    when(serverConf1.getProperties()).thenReturn(ImmutableMap.of("path", "b"));
    when(sorter.sort(any())).thenReturn(singletonList(AgentKeyImpl.parse("agent1")));
    when(agent1.getServers()).thenAnswer(invocation -> singletonMap("a", serverConf1));
    CheServiceImpl service = new CheServiceImpl();
    agentConfigApplier.apply(new ExtendedMachineImpl(singletonList("agent1"), emptyMap(), emptyMap()), service);
    Map<String, String> labels = service.getLabels();
    assertEquals(labels.size(), 3);
    assertEquals(labels.get("che:server:1111/udp:ref"), "a");
    assertEquals(labels.get("che:server:1111/udp:protocol"), "http");
    assertEquals(labels.get("che:server:1111/udp:path"), "b");
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) ServerConf2(org.eclipse.che.api.core.model.workspace.ServerConf2) Test(org.testng.annotations.Test)

Example 20 with ExtendedMachineImpl

use of org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl in project che by eclipse.

the class DockerImageEnvironmentParserTest method shouldThrowExceptionOnParseOfDockerimageEnvWithSeveralExtendedMachines.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Environment of type '.*' doesn't support multiple machines, but contains machines: .*")
public void shouldThrowExceptionOnParseOfDockerimageEnvWithSeveralExtendedMachines() throws Exception {
    EnvironmentImpl environment = createDockerimageEnvConfig();
    environment.getMachines().put("anotherMachine", new ExtendedMachineImpl(emptyList(), emptyMap(), emptyMap()));
    // when
    parser.parse(environment);
}
Also used : ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) Test(org.testng.annotations.Test)

Aggregations

ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)21 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)16 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)10 EnvironmentRecipeImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentRecipeImpl)10 CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)9 Test (org.testng.annotations.Test)8 ServerConf2Impl (org.eclipse.che.api.workspace.server.model.impl.ServerConf2Impl)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)3 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)3 ProjectConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)3 SourceStorageImpl (org.eclipse.che.api.workspace.server.model.impl.SourceStorageImpl)3 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)3 ConflictException (org.eclipse.che.api.core.ConflictException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 ServerConf2 (org.eclipse.che.api.core.model.workspace.ServerConf2)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Arrays (java.util.Arrays)1