Search in sources :

Example 11 with MachineImpl

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

the class WorkspaceService method checkAgentHealth.

@GET
@Path("/{id}/check")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get state of the workspace agent by the workspace id")
@ApiResponses({ @ApiResponse(code = 200, message = "The response contains requested workspace entity"), @ApiResponse(code = 404, message = "The workspace with specified id does not exist"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public WsAgentHealthStateDto checkAgentHealth(@ApiParam(value = "Workspace id") @PathParam("id") String id) throws NotFoundException, ServerException {
    final WorkspaceImpl workspace = workspaceManager.getWorkspace(id);
    if (WorkspaceStatus.RUNNING != workspace.getStatus()) {
        return newDto(WsAgentHealthStateDto.class).withWorkspaceStatus(workspace.getStatus());
    }
    final MachineImpl devMachine = workspace.getRuntime().getDevMachine();
    if (devMachine == null) {
        return newDto(WsAgentHealthStateDto.class).withWorkspaceStatus(workspace.getStatus()).withCode(NOT_FOUND.getStatusCode()).withReason("Workspace Agent isn't available if Dev machine isn't RUNNING");
    }
    final WsAgentHealthStateDto check = agentHealthChecker.check(devMachine);
    check.setWorkspaceStatus(workspace.getStatus());
    return check;
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) WsAgentHealthStateDto(org.eclipse.che.api.workspace.shared.dto.WsAgentHealthStateDto) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 12 with MachineImpl

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

the class WorkspaceRuntimes method snapshotAndUpdateStatus.

/** Creates a snapshot and changes status SNAPSHOTTING -> RUNNING. */
private void snapshotAndUpdateStatus(String workspaceId) throws NotFoundException, ConflictException, ServerException {
    eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.SNAPSHOTTING).withEventType(EventType.SNAPSHOT_CREATING).withPrevStatus(WorkspaceStatus.RUNNING));
    WorkspaceRuntimeImpl runtime = getRuntime(workspaceId);
    List<MachineImpl> machines = runtime.getMachines();
    machines.sort(comparing(m -> !m.getConfig().isDev(), Boolean::compare));
    LOG.info("Creating snapshot of workspace '{}', machines to snapshot: '{}'", workspaceId, machines.size());
    List<SnapshotImpl> newSnapshots = new ArrayList<>(machines.size());
    for (MachineImpl machine : machines) {
        try {
            newSnapshots.add(envEngine.saveSnapshot(workspaceId, machine.getId()));
        } catch (ServerException | NotFoundException x) {
            if (machine.getConfig().isDev()) {
                compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
                eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.SNAPSHOT_CREATION_ERROR).withPrevStatus(WorkspaceStatus.SNAPSHOTTING).withError(x.getMessage()));
                throw x;
            }
            LOG.warn(format("Couldn't create snapshot of machine '%s:%s' in workspace '%s'", machine.getEnvName(), machine.getConfig().getName(), workspaceId));
        }
    }
    LOG.info("Saving new snapshots metadata, workspace id '{}'", workspaceId);
    try {
        List<SnapshotImpl> removed = snapshotDao.replaceSnapshots(workspaceId, runtime.getActiveEnv(), newSnapshots);
        if (!removed.isEmpty()) {
            LOG.info("Removing old snapshots binaries, workspace id '{}', snapshots to remove '{}'", workspaceId, removed.size());
            removeBinaries(removed);
        }
    } catch (SnapshotException x) {
        LOG.error(format("Couldn't remove existing snapshots metadata for workspace '%s'", workspaceId), x);
        LOG.info("Removing newly created snapshots, workspace id '{}', snapshots to remove '{}'", workspaceId, newSnapshots.size());
        removeBinaries(newSnapshots);
        compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
        eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.SNAPSHOT_CREATION_ERROR).withPrevStatus(WorkspaceStatus.SNAPSHOTTING).withError(x.getMessage()));
        throw x;
    }
    compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
    eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withStatus(WorkspaceStatus.RUNNING).withWorkspaceId(workspaceId).withEventType(EventType.SNAPSHOT_CREATED).withPrevStatus(WorkspaceStatus.SNAPSHOTTING));
}
Also used : ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE(org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE) AgentLauncher(org.eclipse.che.api.agent.server.launcher.AgentLauncher) Agent(org.eclipse.che.api.agent.shared.model.Agent) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) StripedLocks(org.eclipse.che.commons.lang.concurrent.StripedLocks) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) RUNNING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) Future(java.util.concurrent.Future) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) Map(java.util.Map) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) EventService(org.eclipse.che.api.core.notification.EventService) CancellationException(java.util.concurrent.CancellationException) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) CountDownLatch(java.util.concurrent.CountDownLatch) STARTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) List(java.util.List) Environment(org.eclipse.che.api.core.model.workspace.Environment) CheEnvironmentEngine(org.eclipse.che.api.environment.server.CheEnvironmentEngine) AgentKey(org.eclipse.che.api.agent.shared.model.AgentKey) EventType(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent.EventType) AgentLauncherFactory(org.eclipse.che.api.agent.server.launcher.AgentLauncherFactory) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) AgentRegistry(org.eclipse.che.api.agent.server.AgentRegistry) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) HashSet(java.util.HashSet) WebsocketMessageConsumer(org.eclipse.che.api.core.util.WebsocketMessageConsumer) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Objects.requireNonNull(java.util.Objects.requireNonNull) ConflictException(org.eclipse.che.api.core.ConflictException) Comparator.comparing(java.util.Comparator.comparing) Instance(org.eclipse.che.api.machine.server.spi.Instance) SnapshotDao(org.eclipse.che.api.machine.server.spi.SnapshotDao) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Logger(org.slf4j.Logger) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) SNAPSHOTTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.SNAPSHOTTING) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) NotFoundException(org.eclipse.che.api.core.NotFoundException) TimeUnit(java.util.concurrent.TimeUnit) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) AgentSorter(org.eclipse.che.api.agent.server.impl.AgentSorter) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MachineStartedHandler(org.eclipse.che.api.environment.server.MachineStartedHandler) Collections(java.util.Collections) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException)

Example 13 with MachineImpl

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

the class WorkspaceServiceTest method testWorkspaceLinks.

@Test
public void testWorkspaceLinks() throws Exception {
    // given
    final WorkspaceImpl workspace = createWorkspace(createConfigDto());
    EnvironmentImpl environment = workspace.getConfig().getEnvironments().get(workspace.getConfig().getDefaultEnv());
    assertNotNull(environment);
    final WorkspaceRuntimeImpl runtime = new WorkspaceRuntimeImpl(workspace.getConfig().getDefaultEnv(), null);
    MachineConfigImpl devMachineConfig = MachineConfigImpl.builder().setDev(true).setEnvVariables(emptyMap()).setServers(emptyList()).setLimits(new MachineLimitsImpl(1024)).setSource(new MachineSourceImpl("type").setContent("content")).setName(environment.getMachines().keySet().iterator().next()).setType("type").build();
    runtime.setDevMachine(new MachineImpl(devMachineConfig, "machine123", workspace.getId(), workspace.getConfig().getDefaultEnv(), USER_ID, MachineStatus.RUNNING, new MachineRuntimeInfoImpl(emptyMap(), emptyMap(), singletonMap("8080/https", new ServerImpl("wsagent", "https", "address", "url", new ServerPropertiesImpl("path", "internaladdress", "internalurl"))))));
    runtime.getMachines().add(runtime.getDevMachine());
    workspace.setStatus(RUNNING);
    workspace.setRuntime(runtime);
    when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);
    // when
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).when().get(SECURE_PATH + "/workspace/" + workspace.getId());
    // then
    assertEquals(response.getStatusCode(), 200);
    final WorkspaceDto workspaceDto = unwrapDto(response, WorkspaceDto.class);
    final Set<String> actualRels = workspaceDto.getLinks().stream().map(Link::getRel).collect(toSet());
    final Set<String> expectedRels = new HashSet<>(asList(LINK_REL_START_WORKSPACE, LINK_REL_REMOVE_WORKSPACE, GET_ALL_USER_WORKSPACES, LINK_REL_GET_SNAPSHOT, LINK_REL_GET_WORKSPACE_EVENTS_CHANNEL, LINK_REL_IDE_URL, LINK_REL_SELF, LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL, LINK_REL_ENVIRONMENT_STATUS_CHANNEL));
    assertTrue(actualRels.equals(expectedRels), format("Links difference: '%s'. \n" + "Returned links: '%s', \n" + "Expected links: '%s'.", Sets.symmetricDifference(actualRels, expectedRels), actualRels.toString(), expectedRels.toString()));
    assertNotNull(workspaceDto.getRuntime().getLink(LINK_REL_STOP_WORKSPACE), "Runtime doesn't contain stop link");
    assertNotNull(workspaceDto.getRuntime().getLink(WSAGENT_REFERENCE), "Runtime doesn't contain wsagent link");
    assertNotNull(workspaceDto.getRuntime().getLink(WSAGENT_WEBSOCKET_REFERENCE), "Runtime doesn't contain wsagent.websocket link");
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) WorkspaceDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceDto) Matchers.anyString(org.mockito.Matchers.anyString) MachineRuntimeInfoImpl(org.eclipse.che.api.machine.server.model.impl.MachineRuntimeInfoImpl) Response(com.jayway.restassured.response.Response) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) ServerPropertiesImpl(org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)13 ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)9 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)7 WorkspaceRuntimeImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)7 Test (org.testng.annotations.Test)6 NotFoundException (org.eclipse.che.api.core.NotFoundException)5 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)5 Instance (org.eclipse.che.api.machine.server.spi.Instance)5 ConflictException (org.eclipse.che.api.core.ConflictException)4 ServerException (org.eclipse.che.api.core.ServerException)4 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)4 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)4 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)4 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)4 String.format (java.lang.String.format)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2