Search in sources :

Example 6 with MachineException

use of org.eclipse.che.api.machine.server.exception.MachineException 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 7 with MachineException

use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.

the class CheEnvironmentEngine method getMachineLogger.

private LineConsumer getMachineLogger(MessageConsumer<MachineLogMessage> environmentLogger, String machineId, String machineName) throws ServerException {
    createMachineLogsDir(machineId);
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            environmentLogger.consume(new MachineLogMessageImpl(machineName, line));
        }
    };
    try {
        return new ConcurrentCompositeLineConsumer(new ConcurrentFileLineConsumer(getMachineLogsFile(machineId)), lineConsumer);
    } catch (IOException e) {
        throw new MachineException(format("Unable create log file '%s' for machine '%s'.", e.getLocalizedMessage(), machineId));
    }
}
Also used : 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) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) MachineLogMessageImpl(org.eclipse.che.api.machine.server.model.impl.MachineLogMessageImpl) IOException(java.io.IOException)

Example 8 with MachineException

use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.

the class AbstractAgentLauncher method launch.

@Override
public void launch(Instance machine, Agent agent) throws ServerException {
    if (isNullOrEmpty(agent.getScript())) {
        return;
    }
    ListLineConsumer agentLogger = new ListLineConsumer();
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            machine.getLogger().writeLine(line);
            agentLogger.writeLine(line);
        }
    };
    try {
        final InstanceProcess process = start(machine, agent, lineConsumer);
        LOG.debug("Waiting for agent {} is launched. Workspace ID:{}", agent.getId(), machine.getWorkspaceId());
        final long pingStartTimestamp = System.currentTimeMillis();
        while (System.currentTimeMillis() - pingStartTimestamp < agentMaxStartTimeMs) {
            if (agentLaunchingChecker.isLaunched(agent, process, machine)) {
                return;
            } else {
                Thread.sleep(agentPingDelayMs);
            }
        }
        process.kill();
    } catch (MachineException e) {
        logAsErrorAgentStartLogs(agent.getName(), agentLogger.getText());
        throw new ServerException(e.getServiceError());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new ServerException(format("Launching agent %s is interrupted", agent.getName()));
    } finally {
        try {
            lineConsumer.close();
        } catch (IOException ignored) {
        }
        agentLogger.close();
    }
    logAsErrorAgentStartLogs(agent.getName(), agentLogger.getText());
    throw new ServerException(format("Fail launching agent %s. Workspace ID:%s", agent.getName(), machine.getWorkspaceId()));
}
Also used : ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ServerException(org.eclipse.che.api.core.ServerException) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException)

Example 9 with MachineException

use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.

the class AbstractAgentLauncherTest method shouldThrowServerExceptionIfMachineExceptionIsThrownByAgentCheck.

@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "agent launcher test exception")
public void shouldThrowServerExceptionIfMachineExceptionIsThrownByAgentCheck() throws Exception {
    // given
    when(agentChecker.isLaunched(any(Agent.class), any(InstanceProcess.class), any(Instance.class))).thenThrow(new MachineException("agent launcher test exception"));
    // when
    launcher.launch(machine, agent);
}
Also used : Agent(org.eclipse.che.api.agent.shared.model.Agent) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) Test(org.testng.annotations.Test)

Example 10 with MachineException

use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.

the class SshMachineInstanceProvider method createInstance.

/**
     * Creates instance from scratch or by reusing a previously one by using specified {@link MachineSource}
     * data in {@link MachineConfig}.
     *
     * @param machine
     *         machine description
     * @param lineConsumer
     *         output for instance creation logs
     * @return newly created {@link SshMachineInstance}
     * @throws UnsupportedRecipeException
     *         if specified {@code recipe} is not supported
     * @throws InvalidRecipeException
     *         if {@code recipe} is invalid
     * @throws NotFoundException
     *         if instance described by {@link MachineSource} doesn't exists
     * @throws MachineException
     *         if other error occurs
     */
public SshMachineInstance createInstance(Machine machine, LineConsumer lineConsumer) throws NotFoundException, MachineException {
    requireNonNull(machine, "Non null machine required");
    requireNonNull(lineConsumer, "Non null logs consumer required");
    requireNonNull(machine.getConfig().getSource().getLocation(), "Location in machine source is required");
    if (machine.getConfig().isDev()) {
        throw new MachineException("Dev machine is not supported for Ssh machine implementation");
    }
    Recipe recipe = recipeDownloader.getRecipe(machine.getConfig());
    SshMachineRecipe sshMachineRecipe = GSON.fromJson(recipe.getScript(), SshMachineRecipe.class);
    SshClient sshClient = sshMachineFactory.createSshClient(sshMachineRecipe, machine.getConfig().getEnvVariables());
    sshClient.start();
    SshMachineInstance instance = sshMachineFactory.createInstance(machine, sshClient, lineConsumer);
    instance.setStatus(MachineStatus.RUNNING);
    return instance;
}
Also used : Recipe(org.eclipse.che.api.core.model.machine.Recipe) MachineException(org.eclipse.che.api.machine.server.exception.MachineException)

Aggregations

MachineException (org.eclipse.che.api.machine.server.exception.MachineException)35 IOException (java.io.IOException)17 ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)7 ConflictException (org.eclipse.che.api.core.ConflictException)6 ServerException (org.eclipse.che.api.core.ServerException)6 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)6 JSchException (com.jcraft.jsch.JSchException)5 File (java.io.File)5 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)5 Instance (org.eclipse.che.api.machine.server.spi.Instance)5 Test (org.testng.annotations.Test)5 Agent (org.eclipse.che.api.agent.shared.model.Agent)4 Command (org.eclipse.che.api.core.model.machine.Command)4 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)4 Exec (org.eclipse.che.plugin.docker.client.Exec)4 URL (java.net.URL)3 NotFoundException (org.eclipse.che.api.core.NotFoundException)3 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)3 ChannelExec (com.jcraft.jsch.ChannelExec)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)2