Search in sources :

Example 1 with ListLineConsumer

use of org.eclipse.che.api.core.util.ListLineConsumer 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 2 with ListLineConsumer

use of org.eclipse.che.api.core.util.ListLineConsumer in project che by eclipse.

the class SshMachineExecAgentLauncher method detectArchitecture.

private String detectArchitecture(SshMachineInstance machine) throws ConflictException, MachineException {
    // uname -sm shows OS and CPU architecture
    // Examples of output:
    // Windows 10 amd64
    // MSYS_NT-6.3 x86_64
    // (empty line)
    // Ubuntu Linux 14.04 amd64
    // Linux x86_64
    // OS X amd64
    // Darwin x86_64
    // Samsung Artik arm7
    // Linux armv7l
    SshMachineProcess getUnameOutput = machine.createProcess(new CommandImpl("discover machine architecture", "uname -sm", null), null);
    ListLineConsumer lineConsumer = new ListLineConsumer();
    getUnameOutput.start(lineConsumer);
    String unameOutput = lineConsumer.getText();
    Matcher matcher = UNAME_OUTPUT.matcher(unameOutput);
    if (matcher.matches()) {
        String os = matcher.group("os").toLowerCase();
        String arch = matcher.group("architecture").toLowerCase();
        StringBuilder result = new StringBuilder();
        if (os.contains("linux")) {
            result.append("linux_");
        } else if (os.contains("darwin")) {
            result.append("darwin_");
        } else if (os.contains("msys")) {
            result.append("windows_");
        } else {
            LOG.error(format("Architecture discovering fails. Machine %s. uname output:%s", machine.getId(), unameOutput));
            return DEFAULT_ARCHITECTURE;
        }
        if (arch.contains("x86_64")) {
            result.append("amd64");
        } else if (arch.contains("armv7l")) {
            result.append("arm7");
        } else if (arch.contains("armv6l")) {
            result.append("arm6");
        } else if (arch.contains("armv5l")) {
            result.append("arm5");
        } else {
            LOG.error(format("Architecture discovering fails. Machine %s. uname output:%s", machine.getId(), unameOutput));
            return DEFAULT_ARCHITECTURE;
        }
        return result.toString();
    } else {
        LOG.error(format("Architecture discovering fails. Machine %s. uname output:%s", machine.getId(), unameOutput));
        return DEFAULT_ARCHITECTURE;
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Matcher(java.util.regex.Matcher) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess)

Example 3 with ListLineConsumer

use of org.eclipse.che.api.core.util.ListLineConsumer in project che by eclipse.

the class SshProcessLaunchedChecker method isLaunched.

public boolean isLaunched(Agent agent, SshMachineInstance machine) throws MachineException {
    Command command = new CommandImpl(format("Wait for %s, try %d", agent.getId(), ++counter), format(CHECK_COMMAND, processNameToWait), "test");
    try (ListLineConsumer lineConsumer = new ListLineConsumer()) {
        SshMachineProcess waitProcess = machine.createProcess(command, null);
        waitProcess.start(lineConsumer);
        return lineConsumer.getText().endsWith("[STDOUT] 0");
    } catch (ConflictException e) {
        throw new MachineException(e.getServiceError());
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) ConflictException(org.eclipse.che.api.core.ConflictException) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess) MachineException(org.eclipse.che.api.machine.server.exception.MachineException)

Example 4 with ListLineConsumer

use of org.eclipse.che.api.core.util.ListLineConsumer in project che by eclipse.

the class DockerProcess method checkAlive.

@Override
public void checkAlive() throws MachineException, NotFoundException {
    // Read pid from file and run 'kill -0 [pid]' command.
    final String isAliveCmd = format("[ -r %1$s ] && kill -0 $(cat %1$s) || echo 'Unable read PID file'", pidFilePath);
    final ListLineConsumer output = new ListLineConsumer();
    final String[] command = { "/bin/sh", "-c", isAliveCmd };
    Exec exec;
    try {
        exec = docker.createExec(CreateExecParams.create(container, command).withDetach(false));
    } catch (IOException e) {
        throw new MachineException(format("Error occurs while initializing command %s in docker container %s: %s", Arrays.toString(command), container, e.getMessage()), e);
    }
    try {
        docker.startExec(StartExecParams.create(exec.getId()), new LogMessagePrinter(output));
    } catch (IOException e) {
        throw new MachineException(format("Error occurs while executing command %s in docker container %s: %s", Arrays.toString(exec.getCommand()), container, e.getMessage()), e);
    }
    // 'kill -0 [pid]' is silent if process is running or print "No such process" message otherwise
    if (!output.getText().isEmpty()) {
        throw new NotFoundException(format("Process with pid %s not found", getPid()));
    }
}
Also used : ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Exec(org.eclipse.che.plugin.docker.client.Exec) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException)

Example 5 with ListLineConsumer

use of org.eclipse.che.api.core.util.ListLineConsumer in project che by eclipse.

the class CommandExistsAgentChecker method isLaunched.

@Override
public boolean isLaunched(Agent agent, InstanceProcess process, Instance machine) throws MachineException {
    Command command = new CommandImpl(format("Wait for %s, try %d", agent.getId(), ++counter), checkingCommand, "test");
    try (ListLineConsumer lineConsumer = new ListLineConsumer()) {
        InstanceProcess waitProcess = machine.createProcess(command, null);
        waitProcess.start(lineConsumer);
        return lineConsumer.getText().endsWith("[STDOUT] 0");
    } catch (ConflictException e) {
        throw new MachineException(e.getServiceError());
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess)

Aggregations

ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)8 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)7 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)4 IOException (java.io.IOException)3 ConflictException (org.eclipse.che.api.core.ConflictException)3 Command (org.eclipse.che.api.core.model.machine.Command)3 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)3 Exec (org.eclipse.che.plugin.docker.client.Exec)2 SshMachineProcess (org.eclipse.che.plugin.machine.ssh.SshMachineProcess)2 ChannelExec (com.jcraft.jsch.ChannelExec)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Matcher (java.util.regex.Matcher)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 ServerException (org.eclipse.che.api.core.ServerException)1 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)1 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)1