use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.
the class WorkspaceRuntimes method startAsync.
/**
* Starts all machines from specified workspace environment, creates workspace runtime instance
* based on that environment.
*
* <p>During the start of the workspace its runtime is visible with {@link
* WorkspaceStatus#STARTING} status.
*
* @param workspace workspace which environment should be started
* @param envName optional environment name to run
* @param options whether machines should be recovered(true) or not(false)
* @return completable future of start execution.
* @throws ConflictException when workspace is already running
* @throws ConflictException when start is interrupted
* @throws NotFoundException when any not found exception occurs during environment start
* @throws ServerException other error occurs during environment start
* @see WorkspaceStatus#STARTING
* @see WorkspaceStatus#RUNNING
*/
@Traced
public CompletableFuture<Void> startAsync(WorkspaceImpl workspace, @Nullable String envName, Map<String, String> options) throws ConflictException, NotFoundException, ServerException {
TracingTags.WORKSPACE_ID.set(workspace.getId());
final String workspaceId = workspace.getId();
if (isStartRefused.get()) {
throw new ConflictException(format("Start of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", workspace.getName()));
}
WorkspaceConfigImpl config = workspace.getConfig();
if (config == null) {
config = devfileConverter.convert(workspace.getDevfile());
}
if (envName == null) {
envName = config.getDefaultEnv();
}
String infraNamespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
if (isNullOrEmpty(infraNamespace)) {
throw new ServerException(String.format("Workspace does not have infrastructure namespace " + "specified. Please set value of '%s' workspace attribute.", WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE));
}
final RuntimeIdentity runtimeId = new RuntimeIdentityImpl(workspaceId, envName, EnvironmentContext.getCurrent().getSubject().getUserId(), infraNamespace);
try {
InternalEnvironment internalEnv = createInternalEnvironment(config.getEnvironments().get(envName), config.getAttributes(), config.getCommands(), config.getDevfile());
RuntimeContext runtimeContext = infrastructure.prepare(runtimeId, internalEnv);
InternalRuntime runtime = runtimeContext.getRuntime();
try (Unlocker ignored = lockService.writeLock(workspaceId)) {
final WorkspaceStatus existingStatus = statuses.putIfAbsent(workspaceId, STARTING);
if (existingStatus != null) {
throw new ConflictException(format("Could not start workspace '%s' because its state is '%s'", workspaceId, existingStatus));
}
setRuntimesId(workspaceId);
runtimes.put(workspaceId, runtime);
}
LOG.info("Starting workspace '{}/{}' with id '{}' by user '{}'", workspace.getNamespace(), workspace.getName(), workspace.getId(), sessionUserNameOr("undefined"));
publishWorkspaceStatusEvent(workspaceId, STARTING, STOPPED, null, true, options);
return CompletableFuture.runAsync(ThreadLocalPropagateContext.wrap(new StartRuntimeTask(workspace, options, runtime)), sharedPool.getExecutor());
} catch (ValidationException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ConflictException(e.getLocalizedMessage());
} catch (InfrastructureException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ServerException(e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method attributesIsSetWhenRuntimeAbnormallyStopped.
@Test
public void attributesIsSetWhenRuntimeAbnormallyStopped() throws Exception {
String error = "Some kind of error happened";
EventService localEventService = new EventService();
WorkspaceRuntimes localRuntimes = new WorkspaceRuntimes(localEventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
localRuntimes.init();
RuntimeIdentityDto identity = DtoFactory.newDto(RuntimeIdentityDto.class).withWorkspaceId("workspace123").withEnvName("my-env").withOwnerId("myId");
mockWorkspaceWithConfig(identity);
RuntimeContext context = mockContext(identity);
when(context.getRuntime()).thenReturn(new TestInternalRuntime(context));
when(statuses.remove(anyString())).thenReturn(WorkspaceStatus.RUNNING);
RuntimeAbnormalStoppedEvent event = new RuntimeAbnormalStoppedEvent(identity, error);
localRuntimes.recoverOne(infrastructure, identity);
ArgumentCaptor<WorkspaceImpl> captor = ArgumentCaptor.forClass(WorkspaceImpl.class);
// when
localEventService.publish(event);
// then
verify(workspaceDao, atLeastOnce()).update(captor.capture());
WorkspaceImpl ws = captor.getAllValues().get(captor.getAllValues().size() - 1);
assertNotNull(ws.getAttributes().get(STOPPED_ATTRIBUTE_NAME));
assertTrue(Boolean.valueOf(ws.getAttributes().get(STOPPED_ABNORMALLY_ATTRIBUTE_NAME)));
assertEquals(ws.getAttributes().get(ERROR_MESSAGE_ATTRIBUTE_NAME), error);
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method mockContext.
private RuntimeContext mockContext(RuntimeIdentity identity) throws ValidationException, InfrastructureException {
RuntimeContext context = mock(RuntimeContext.class);
InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
lenient().doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
lenient().doReturn(context).when(infrastructure).prepare(eq(identity), eq(internalEnvironment));
lenient().when(context.getInfrastructure()).thenReturn(infrastructure);
lenient().when(context.getIdentity()).thenReturn(identity);
lenient().when(context.getRuntime()).thenReturn(new TestInternalRuntime(context));
lenient().when(context.getEnvironment()).thenReturn(internalEnvironment);
List<Warning> warnings = new ArrayList<>();
warnings.add(createWarning());
lenient().when(internalEnvironment.getWarnings()).thenReturn(warnings);
return context;
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldInjectRuntime.
@Test
public void shouldInjectRuntime() throws Exception {
// given
WorkspaceImpl workspace = new WorkspaceImpl();
workspace.setId("ws123");
when(statuses.get("ws123")).thenReturn(WorkspaceStatus.RUNNING);
ImmutableMap<String, Machine> machines = ImmutableMap.of("machine", new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING));
RuntimeIdentity identity = new RuntimeIdentityImpl("ws123", "my-env", "myId", "infraNamespace");
RuntimeContext context = mockContext(identity);
ConcurrentHashMap<String, InternalRuntime<?>> runtimesStorage = new ConcurrentHashMap<>();
TestInternalRuntime testRuntime = new TestInternalRuntime(context, machines, WorkspaceStatus.STARTING);
runtimesStorage.put("ws123", testRuntime);
WorkspaceRuntimes localRuntimes = new WorkspaceRuntimes(runtimesStorage, eventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
// when
localRuntimes.injectRuntime(workspace);
// then
assertEquals(workspace.getStatus(), WorkspaceStatus.RUNNING);
assertEquals(workspace.getRuntime(), asRuntime(testRuntime));
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldRecoverRuntimeWhenThereIsNotCachedOneDuringInjecting.
@Test
public void shouldRecoverRuntimeWhenThereIsNotCachedOneDuringInjecting() throws Exception {
// given
RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "my-env", "myId", "infraNamespace");
mockWorkspaceWithConfig(identity);
when(statuses.get("workspace123")).thenReturn(WorkspaceStatus.STARTING);
RuntimeContext context = mockContext(identity);
doReturn(context).when(infrastructure).prepare(eq(identity), any());
ImmutableMap<String, Machine> machines = ImmutableMap.of("machine", new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING));
TestInternalRuntime testRuntime = new TestInternalRuntime(context, machines, WorkspaceStatus.STARTING);
when(context.getRuntime()).thenReturn(testRuntime);
doReturn(mock(InternalEnvironment.class)).when(testEnvFactory).create(any());
doReturn(ImmutableSet.of(identity)).when(infrastructure).getIdentities();
// when
WorkspaceImpl workspace = new WorkspaceImpl();
workspace.setId("workspace123");
runtimes.injectRuntime(workspace);
// then
assertEquals(workspace.getStatus(), WorkspaceStatus.STARTING);
assertEquals(workspace.getRuntime(), asRuntime(testRuntime));
}
Aggregations