Search in sources :

Example 11 with Environment

use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.

the class WorkspaceRuntimes method validate.

/**
 * Validates the specified workspace configuration.
 *
 * <p>The typical usage of this method is performing validating before asynchronous start of
 * workspace.
 *
 * @param workspace workspace config that contains environment or devfile to start
 * @param envName environment to start. Default will be used if null.
 * @throws NotFoundException if workspace does not have environment with the specified name
 * @throws NotFoundException if workspace has environment with type that is not supported
 * @throws ValidationException if environment is not a valid and can not be run
 * @throws ServerException if any other issue occurred during validation
 */
public void validate(WorkspaceImpl workspace, @Nullable String envName) throws ValidationException, NotFoundException, ServerException {
    WorkspaceConfigImpl config = workspace.getConfig();
    if (workspace.getDevfile() != null) {
        config = devfileConverter.convert(workspace.getDevfile());
    }
    if (envName != null && !config.getEnvironments().containsKey(envName)) {
        throw new NotFoundException(format("Workspace '%s:%s' doesn't contain environment '%s'", workspace.getNamespace(), config.getName(), envName));
    }
    if (envName == null) {
        // use default environment if it is not defined
        envName = config.getDefaultEnv();
    }
    if (envName == null && SidecarToolingWorkspaceUtil.isSidecarBasedWorkspace(config.getAttributes())) {
        // Sidecar-based workspaces are allowed not to have any environments
        return;
    }
    // validate environment in advance
    if (envName == null) {
        throw new NotFoundException(format("Workspace %s:%s can't use null environment", workspace.getNamespace(), config.getName()));
    }
    Environment environment = config.getEnvironments().get(envName);
    if (environment == null) {
        throw new NotFoundException(format("Workspace does not have environment with name %s that specified to be run", envName));
    }
    String type = environment.getRecipe().getType();
    if (!infrastructure.getRecipeTypes().contains(type)) {
        throw new NotFoundException("Infrastructure not found for type: " + type);
    }
    // try to create internal environment to check if the specified environment is valid
    try {
        createInternalEnvironment(environment, emptyMap(), emptyList(), config.getDevfile());
    } catch (InfrastructureException e) {
        throw new ServerException(e.getMessage(), e);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) NotFoundException(org.eclipse.che.api.core.NotFoundException) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)

Example 12 with Environment

use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.

the class WorkspaceValidator method validateEnvironments.

private void validateEnvironments(WorkspaceConfig config) throws ValidationException {
    boolean environmentIsNotSet = (config.getEnvironments() == null || config.getEnvironments().isEmpty()) && isNullOrEmpty(config.getDefaultEnv());
    boolean isSidecarWorkspace = isSidecarBasedWorkspace(config.getAttributes());
    if (environmentIsNotSet && isSidecarWorkspace) {
        // sidecar based workspaces allowed not to have environment
        return;
    }
    check(!isNullOrEmpty(config.getDefaultEnv()), "Workspace default environment name required");
    checkNotNull(config.getEnvironments(), "Workspace must contain at least one environment");
    check(config.getEnvironments().containsKey(config.getDefaultEnv()), "Workspace default environment configuration required");
    for (Environment environment : config.getEnvironments().values()) {
        checkNotNull(environment, "Environment must not be null");
        Recipe recipe = environment.getRecipe();
        checkNotNull(recipe, "Environment recipe must not be null");
        checkNotNull(recipe.getType(), "Environment recipe type must not be null");
        for (Entry<String, ? extends MachineConfig> machineEntry : environment.getMachines().entrySet()) {
            validateMachine(machineEntry.getKey(), machineEntry.getValue());
        }
    }
}
Also used : Recipe(org.eclipse.che.api.core.model.workspace.config.Recipe) Environment(org.eclipse.che.api.core.model.workspace.config.Environment)

Example 13 with Environment

use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.

the class LimitsCheckingWorkspaceManager method checkMaxEnvironmentRam.

@VisibleForTesting
void checkMaxEnvironmentRam(WorkspaceConfig config) throws ServerException {
    if (maxRamPerEnvMB < 0) {
        return;
    }
    if (config.getEnvironments().isEmpty()) {
        return;
    }
    for (Map.Entry<String, ? extends Environment> envEntry : config.getEnvironments().entrySet()) {
        Environment env = envEntry.getValue();
        final long workspaceRam = environmentRamCalculator.calculate(env);
        if (workspaceRam > maxRamPerEnvMB) {
            throw new LimitExceededException(format("You are only allowed to use %d mb. RAM per workspace.", maxRamPerEnvMB), ImmutableMap.of("environment_max_ram", Long.toString(maxRamPerEnvMB), "environment_max_ram_unit", "mb", "environment_ram", Long.toString(workspaceRam), "environment_ram_unit", "mb"));
        }
    }
}
Also used : Environment(org.eclipse.che.api.core.model.workspace.config.Environment) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with Environment

use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.

the class WorkspaceRuntimes method recoverOne.

@VisibleForTesting
InternalRuntime<?> recoverOne(RuntimeInfrastructure infra, RuntimeIdentity identity) throws ServerException, ConflictException {
    if (isStartRefused.get()) {
        throw new ConflictException(format("Recovery of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", identity.getWorkspaceId()));
    }
    WorkspaceImpl workspace;
    try {
        workspace = workspaceDao.get(identity.getWorkspaceId());
    } catch (NotFoundException x) {
        throw new ServerException(format("Workspace configuration is missing for the runtime '%s:%s'. Runtime won't be recovered", identity.getWorkspaceId(), identity.getEnvName()));
    }
    Environment environment = null;
    WorkspaceConfigImpl workspaceConfig = workspace.getConfig();
    if (workspaceConfig == null) {
        workspaceConfig = devfileConverter.convert(workspace.getDevfile());
    }
    if (identity.getEnvName() != null) {
        environment = workspaceConfig.getEnvironments().get(identity.getEnvName());
        if (environment == null) {
            throw new ServerException(format("Environment configuration is missing for the runtime '%s:%s'. Runtime won't be recovered", identity.getWorkspaceId(), identity.getEnvName()));
        }
    }
    InternalRuntime runtime;
    try {
        InternalEnvironment internalEnv = createInternalEnvironment(environment, workspaceConfig.getAttributes(), workspaceConfig.getCommands(), workspaceConfig.getDevfile());
        runtime = infra.prepare(identity, internalEnv).getRuntime();
        WorkspaceStatus runtimeStatus = runtime.getStatus();
        try (Unlocker ignored = lockService.writeLock(workspace.getId())) {
            statuses.replace(identity.getWorkspaceId(), runtimeStatus);
            runtimes.putIfAbsent(identity.getWorkspaceId(), runtime);
        }
        LOG.info("Successfully recovered workspace runtime '{}'. Its status is '{}'", identity.getWorkspaceId(), runtimeStatus);
        return runtime;
    } catch (NotFoundException x) {
        LOG.warn("Not able to create internal environment for  '{}'. Reason: '{}'", identity.getWorkspaceId(), x.getMessage());
        try (Unlocker ignored = lockService.writeLock(identity.getWorkspaceId())) {
            runtimes.remove(identity.getWorkspaceId());
            statuses.remove(identity.getWorkspaceId());
        }
        publishWorkspaceStatusEvent(identity.getWorkspaceId(), STOPPED, STOPPING, "Workspace is stopped. Reason: " + x.getMessage(), false);
        throw new ServerException(format("Couldn't recover runtime '%s:%s'. Error: %s", identity.getWorkspaceId(), identity.getEnvName(), x.getMessage()));
    } catch (InfrastructureException | ValidationException x) {
        throw new ServerException(format("Couldn't recover runtime '%s:%s'. Error: %s", identity.getWorkspaceId(), identity.getEnvName(), x.getMessage()));
    }
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ServerException(org.eclipse.che.api.core.ServerException) ValidationException(org.eclipse.che.api.core.ValidationException) ConflictException(org.eclipse.che.api.core.ConflictException) NotFoundException(org.eclipse.che.api.core.NotFoundException) InternalRuntime(org.eclipse.che.api.workspace.server.spi.InternalRuntime) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with Environment

use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.

the class InternalEnvironmentFactoryTest method shouldReturnCreatedInternalEnvironment.

@Test
public void shouldReturnCreatedInternalEnvironment() throws Exception {
    // given
    InternalEnvironment expectedEnv = mock(InternalEnvironment.class);
    when(environmentFactory.doCreate(any(), any(), any())).thenReturn(expectedEnv);
    Environment env = mock(Environment.class);
    // when
    InternalEnvironment createdEnv = environmentFactory.create(env);
    // then
    assertEquals(createdEnv, expectedEnv);
}
Also used : Environment(org.eclipse.che.api.core.model.workspace.config.Environment) Test(org.testng.annotations.Test)

Aggregations

Environment (org.eclipse.che.api.core.model.workspace.config.Environment)16 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Test (org.testng.annotations.Test)6 NotFoundException (org.eclipse.che.api.core.NotFoundException)4 ServerException (org.eclipse.che.api.core.ServerException)4 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)4 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)4 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)4 InternalEnvironment (org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 ValidationException (org.eclipse.che.api.core.ValidationException)2 WorkspaceStatus (org.eclipse.che.api.core.model.workspace.WorkspaceStatus)2 Recipe (org.eclipse.che.api.core.model.workspace.config.Recipe)2 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)2 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)2 InternalRuntime (org.eclipse.che.api.workspace.server.spi.InternalRuntime)2 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)2 NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)2