Search in sources :

Example 36 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class WsAgentPingRequestFactory method createRequest.

/**
     * Creates request which can check if workspace agent is pinging.
     *
     * @param machine
     *         machine instance
     * @return instance of {@link HttpJsonRequest}
     * @throws ServerException
     *         if internal server error occurred
     */
public HttpJsonRequest createRequest(Machine machine) throws ServerException {
    Map<String, ? extends Server> servers = machine.getRuntime().getServers();
    Server wsAgentServer = servers.get(Constants.WS_AGENT_PORT);
    if (wsAgentServer == null) {
        LOG.error("{} WorkspaceId: {}, DevMachine Id: {}, found servers: {}", WS_AGENT_SERVER_NOT_FOUND_ERROR, machine.getWorkspaceId(), machine.getId(), servers);
        throw new ServerException(WS_AGENT_SERVER_NOT_FOUND_ERROR);
    }
    String wsAgentPingUrl = wsAgentServer.getProperties().getInternalUrl();
    if (isNullOrEmpty(wsAgentPingUrl)) {
        LOG.error(WS_AGENT_URL_IS_NULL_OR_EMPTY_ERROR);
        throw new ServerException(WS_AGENT_URL_IS_NULL_OR_EMPTY_ERROR);
    }
    // we will always obtain not found response
    if (!wsAgentPingUrl.endsWith("/")) {
        wsAgentPingUrl = wsAgentPingUrl.concat("/");
    }
    return httpJsonRequestFactory.fromUrl(wsAgentPingUrl).setMethod(HttpMethod.GET).setTimeout(wsAgentPingConnectionTimeoutMs);
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) Server(org.eclipse.che.api.core.model.machine.Server)

Example 37 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class CheEnvironmentEngine method addMachine.

private void addMachine(MachineImpl machine) throws ServerException {
    Instance instance = new NoOpMachineInstance(machine);
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(machine.getWorkspaceId())) {
        ensurePreDestroyIsNotExecuted();
        EnvironmentHolder environmentHolder = environments.get(machine.getWorkspaceId());
        if (environmentHolder != null && environmentHolder.status != EnvStatus.STOPPING) {
            environmentHolder.machines.add(instance);
        } else {
            throw new ServerException(format("Can't add machine into environment. Environment of workspace '%s' is missing", machine.getWorkspaceId()));
        }
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) Instance(org.eclipse.che.api.machine.server.spi.Instance) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker)

Example 38 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class CheEnvironmentEngine method replaceMachine.

private void replaceMachine(Instance machine) throws ServerException {
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(machine.getWorkspaceId())) {
        ensurePreDestroyIsNotExecuted();
        EnvironmentHolder environmentHolder = environments.get(machine.getWorkspaceId());
        if (environmentHolder != null) {
            for (int i = 0; i < environmentHolder.machines.size(); i++) {
                if (environmentHolder.machines.get(i).getId().equals(machine.getId())) {
                    environmentHolder.machines.set(i, machine);
                    return;
                }
            }
        }
    }
    // if this area is reachable then environment/machine is not found and machine should be stopped
    try {
        machine.destroy();
    } catch (MachineException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }
    // should not happen
    throw new ServerException(format("Machine with ID '%s' and name '%s' has been stopped because its configuration is not found in the environment of workspace '%s'", machine.getId(), machine.getConfig().getName(), machine.getWorkspaceId()));
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) MachineException(org.eclipse.che.api.machine.server.exception.MachineException)

Example 39 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class CheEnvironmentEngine method startInstance.

private Instance startInstance(boolean recover, MessageConsumer<MachineLogMessage> environmentLogger, MachineImpl machine, MachineStarter machineStarter) throws ServerException, EnvironmentException {
    LineConsumer machineLogger = null;
    Instance instance = null;
    try {
        addMachine(machine);
        eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
        machineLogger = getMachineLogger(environmentLogger, machine.getId(), machine.getConfig().getName());
        MachineImpl originMachine = new MachineImpl(machine);
        try {
            MachineSourceImpl machineSource = null;
            if (recover) {
                try {
                    SnapshotImpl snapshot = snapshotDao.getSnapshot(machine.getWorkspaceId(), machine.getEnvName(), machine.getConfig().getName());
                    machineSource = snapshot.getMachineSource();
                    // Snapshot image location has SHA-256 digest which needs to be removed,
                    // otherwise it will be pulled without tag and cause problems
                    String imageName = machineSource.getLocation();
                    if (imageName.contains("@sha256:")) {
                        machineSource.setLocation(imageName.substring(0, imageName.indexOf('@')));
                    }
                } catch (NotFoundException e) {
                    try {
                        machineLogger.writeLine("Failed to boot machine from snapshot: snapshot not found. " + "Machine will be created from origin source.");
                    } catch (IOException ignore) {
                    }
                }
            }
            instance = machineStarter.startMachine(machineLogger, machineSource);
        } catch (SourceNotFoundException e) {
            if (recover) {
                LOG.error("Image of snapshot for machine " + machine.getConfig().getName() + " not found. " + "Machine will be created from origin source.");
                machine = originMachine;
                instance = machineStarter.startMachine(machineLogger, null);
            } else {
                throw e;
            }
        }
        replaceMachine(instance);
        eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(machine.getWorkspaceId()));
        return instance;
    } catch (ApiException | RuntimeException e) {
        boolean interrupted = Thread.interrupted();
        removeMachine(machine.getWorkspaceId(), machine.getId());
        if (instance != null) {
            try {
                instance.destroy();
            } catch (Exception destroyingExc) {
                LOG.error(destroyingExc.getLocalizedMessage(), destroyingExc);
            }
        }
        if (machineLogger != null) {
            try {
                machineLogger.writeLine("[ERROR] " + e.getLocalizedMessage());
            } catch (IOException ioEx) {
                LOG.error(ioEx.getLocalizedMessage(), ioEx);
            }
            try {
                machineLogger.close();
            } catch (IOException ioEx) {
                LOG.error(ioEx.getLocalizedMessage(), ioEx);
            }
        }
        eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.ERROR).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
Also used : SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) MachineStatusEvent(org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent) ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ApiException(org.eclipse.che.api.core.ApiException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ApiException(org.eclipse.che.api.core.ApiException)

Example 40 with ServerException

use of org.eclipse.che.api.core.ServerException in project che by eclipse.

the class CheEnvironmentEngine method queuePeekOrFail.

/**
     * Gets head config from the queue associated with the given {@code workspaceId}.
     *
     * <p>Note that this method won't actually poll the queue.
     *
     * <p>Fails if environment start was interrupted by stop(queue doesn't exist).
     *
     * @return machine config which is in the queue head, or null
     * if there are no machine configs left
     * @throws ServerException
     *         if queue doesn't exist which means that {@link #stop(String)} executed
     *         before all the machines started
     * @throws ServerException
     *         if pre destroy has been invoked before peek config retrieved
     */
private String queuePeekOrFail(String workspaceId) throws ServerException {
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
        ensurePreDestroyIsNotExecuted();
        EnvironmentHolder environmentHolder = environments.get(workspaceId);
        if (environmentHolder == null || environmentHolder.startQueue == null) {
            throw new ServerException("Workspace " + workspaceId + " start interrupted. Workspace was stopped before all its machines were started");
        }
        return environmentHolder.startQueue.peek();
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker)

Aggregations

ServerException (org.eclipse.che.api.core.ServerException)143 IOException (java.io.IOException)51 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)37 NotFoundException (org.eclipse.che.api.core.NotFoundException)37 ConflictException (org.eclipse.che.api.core.ConflictException)32 File (java.io.File)22 Test (org.testng.annotations.Test)20 ArrayList (java.util.ArrayList)19 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)19 List (java.util.List)15 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)15 Map (java.util.Map)13 Transactional (com.google.inject.persist.Transactional)12 BadRequestException (org.eclipse.che.api.core.BadRequestException)12 InputStream (java.io.InputStream)11 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)10 String.format (java.lang.String.format)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 HashMap (java.util.HashMap)8