use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.
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"));
}
}
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.
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()));
}
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project che-server by eclipse-che.
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);
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.
the class InternalEnvironmentFactoryTest method testApplyContainerSourceAttributeToTheMachineThatComesFromRecipe.
@Test
public void testApplyContainerSourceAttributeToTheMachineThatComesFromRecipe() throws Exception {
// given
final Environment sourceEnv = mock(Environment.class);
final InternalEnvironment internalEnv = mock(InternalEnvironment.class);
final InternalMachineConfig internalMachine = new InternalMachineConfig();
when(internalEnv.getMachines()).thenReturn(ImmutableMap.of("internalMachine", internalMachine));
when(environmentFactory.doCreate(any(), any(), any())).thenReturn(internalEnv);
// when
InternalEnvironment resultEnv = environmentFactory.create(sourceEnv);
// then
assertEquals(resultEnv.getMachines().get("internalMachine").getAttributes().get(CONTAINER_SOURCE_ATTRIBUTE), RECIPE_CONTAINER_SOURCE);
}
use of org.eclipse.che.api.core.model.workspace.config.Environment in project devspaces-images by redhat-developer.
the class LimitsCheckingWorkspaceManager method checkRamResourcesAvailability.
@VisibleForTesting
void checkRamResourcesAvailability(String accountId, String namespace, WorkspaceConfig config, @Nullable String envName) throws NotFoundException, ServerException, ConflictException {
if (config.getEnvironments().isEmpty()) {
return;
}
final Environment environment = config.getEnvironments().get(firstNonNull(envName, config.getDefaultEnv()));
final ResourceImpl ramToUse = new ResourceImpl(RamResourceType.ID, environmentRamCalculator.calculate(environment), RamResourceType.UNIT);
try {
resourceManager.checkResourcesAvailability(accountId, singletonList(ramToUse));
} catch (NoEnoughResourcesException e) {
final Resource requiredRam = // starting of workspace requires only RAM resource
e.getRequiredResources().get(0);
final Resource availableRam = getResourceOrDefault(e.getAvailableResources(), RamResourceType.ID, 0, RamResourceType.UNIT);
final Resource usedRam = getResourceOrDefault(resourceManager.getUsedResources(accountId), RamResourceType.ID, 0, RamResourceType.UNIT);
throw new LimitExceededException(format("Workspace %s/%s needs %s to start. Your account has %s available and %s in use. " + "The workspace can't be start. Stop other workspaces or grant more resources.", namespace, config.getName(), printResourceInfo(requiredRam), printResourceInfo(availableRam), printResourceInfo(usedRam)));
}
}
Aggregations