Search in sources :

Example 11 with MachineConfigImpl

use of org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl in project che by eclipse.

the class CheEnvironmentEngineTest method machineStartShouldThrowExceptionIfMachineWithTheSameNameAlreadyExistsInEnvironment.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Machine with name '.*' already exists in environment of workspace '.*'")
public void machineStartShouldThrowExceptionIfMachineWithTheSameNameAlreadyExistsInEnvironment() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Instance instance = instances.get(0);
    MachineConfigImpl config = createConfig(false);
    config.setName(instance.getConfig().getName());
    // when
    engine.startMachine(instance.getWorkspaceId(), config, emptyList());
}
Also used : MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) Test(org.testng.annotations.Test)

Example 12 with MachineConfigImpl

use of org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl in project che by eclipse.

the class WorkspaceManagerTest method shouldThrowExceptionOnStartMachineInNonRunningWs.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Workspace .* is not running, new machine can't be started")
public void shouldThrowExceptionOnStartMachineInNonRunningWs() throws Exception {
    // given
    WorkspaceImpl workspace = createAndMockWorkspace();
    MachineConfigImpl machineConfig = createMachine(workspace.getId(), "env1", false).getConfig();
    // when
    workspaceManager.startMachine(machineConfig, workspace.getId());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Test(org.testng.annotations.Test)

Example 13 with MachineConfigImpl

use of org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl 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 14 with MachineConfigImpl

use of org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl in project che by eclipse.

the class WorkspaceRuntimes method startMachine.

/**
     * Starts machine in running workspace.
     *
     * @param workspaceId
     *         ID of workspace that owns machine
     * @param machineConfig
     *         config of machine that should be started
     * @return running machine
     * @throws ConflictException
     *         if environment is not running or conflicting machine already exists in the environment
     * @throws ConflictException
     *         if environment was stopped during start of machine
     * @throws ServerException
     *         if any other error occurs
     */
public Instance startMachine(String workspaceId, MachineConfig machineConfig) throws ServerException, ConflictException, NotFoundException, EnvironmentException {
    try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspaceId)) {
        getRunningState(workspaceId);
    }
    // Copy constructor makes deep copy of objects graph
    // which means that original values won't affect the values in used further in this class
    MachineConfigImpl machineConfigCopy = new MachineConfigImpl(machineConfig);
    List<String> agents = Collections.singletonList("org.eclipse.che.terminal");
    Instance instance = envEngine.startMachine(workspaceId, machineConfigCopy, agents);
    launchAgents(instance, agents);
    try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
        checkIsNotTerminated("start the machine");
        RuntimeState workspaceState = states.get(workspaceId);
        if (workspaceState == null || workspaceState.status != RUNNING) {
            try {
                envEngine.stopMachine(workspaceId, instance.getId());
            } catch (NotFoundException | ServerException | ConflictException e) {
                LOG.error(e.getLocalizedMessage(), e);
            }
            throw new ConflictException(format("Environment of workspace '%s' was stopped during start of  machine", workspaceId));
        }
    }
    return instance;
}
Also used : MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) ServerException(org.eclipse.che.api.core.ServerException) Instance(org.eclipse.che.api.machine.server.spi.Instance) ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Example 15 with MachineConfigImpl

use of org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl in project che by eclipse.

the class WorkspaceRuntimeIntegrationTest method environmentEngineShouldDestroyAllMachinesBeforeRemovalOfEnvironmentRecord.

// Check for https://github.com/codenvy/codenvy/issues/593
@Test(expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "Workspace with id '" + WORKSPACE_ID + "' is not running")
public void environmentEngineShouldDestroyAllMachinesBeforeRemovalOfEnvironmentRecord() throws Exception {
    // given
    EnvironmentDto environment = newDto(EnvironmentDto.class);
    environment.withMachines(singletonMap("service1", newDto(ExtendedMachineDto.class).withAgents(singletonList("org.eclipse.che.ws-agent"))));
    WorkspaceConfigDto config = newDto(WorkspaceConfigDto.class).withDefaultEnv(ENV_NAME).withName("ws1").withEnvironments(singletonMap(ENV_NAME, environment));
    WorkspaceDto workspace = newDto(WorkspaceDto.class).withId(WORKSPACE_ID).withNamespace("namespace").withConfig(config);
    Instance instance = mock(Instance.class);
    MachineConfigImpl machineConfig = new MachineConfigImpl();
    machineConfig.setDev(true);
    machineConfig.setName("service1");
    when(instance.getWorkspaceId()).thenReturn(WORKSPACE_ID);
    when(instance.getId()).thenReturn("machineId");
    when(instance.getConfig()).thenReturn(machineConfig);
    CheServicesEnvironmentImpl internalEnv = new CheServicesEnvironmentImpl();
    internalEnv.getServices().put("service1", new CheServiceImpl().withId("machineId"));
    when(environmentParser.parse(any(Environment.class))).thenReturn(internalEnv);
    when(instanceProvider.startService(anyString(), anyString(), anyString(), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenReturn(instance);
    runtimes.startAsync(workspace, ENV_NAME, false);
    verify(sharedPool).submit(taskCaptor.capture());
    taskCaptor.getValue().call();
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(instance).destroy();
    // when
    executor.execute(() -> {
        try {
            runtimes.stop(WORKSPACE_ID);
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    });
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // then
    // no exception - environment and workspace are still running
    runtimes.getRuntime(WORKSPACE_ID);
    // let instance removal proceed
    waitingAnswer.completeAnswer();
    // verify destroying was called
    verify(instance, timeout(1000)).destroy();
    verify(instanceProvider, timeout(1000)).destroyNetwork(anyString());
    // wait to ensure that removal of runtime is finished
    Thread.sleep(500);
    // runtime is removed - now getting of it should throw an exception
    runtimes.getRuntime(WORKSPACE_ID);
}
Also used : WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) EnvironmentDto(org.eclipse.che.api.workspace.shared.dto.EnvironmentDto) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) WorkspaceDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceDto) NotFoundException(org.eclipse.che.api.core.NotFoundException) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Environment(org.eclipse.che.api.core.model.workspace.Environment) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) ExtendedMachineDto(org.eclipse.che.api.workspace.shared.dto.ExtendedMachineDto) Test(org.testng.annotations.Test)

Aggregations

MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)16 Test (org.testng.annotations.Test)14 Instance (org.eclipse.che.api.machine.server.spi.Instance)12 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)8 CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)8 Matchers.anyString (org.mockito.Matchers.anyString)8 NotFoundException (org.eclipse.che.api.core.NotFoundException)3 Machine (org.eclipse.che.api.core.model.machine.Machine)3 ExtendedMachine (org.eclipse.che.api.core.model.workspace.ExtendedMachine)3 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)3 ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)3 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)3 ConflictException (org.eclipse.che.api.core.ConflictException)2 MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)2 MachineLimitsImpl (org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl)2 WorkspaceRuntimeImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)2 WorkspaceDto (org.eclipse.che.api.workspace.shared.dto.WorkspaceDto)2 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)2 Response (com.jayway.restassured.response.Response)1 HashSet (java.util.HashSet)1