Search in sources :

Example 6 with ListLineConsumer

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

the class JschSshClient method execAndGetOutput.

private String execAndGetOutput(String command) throws JSchException, MachineException, IOException {
    ChannelExec exec = (ChannelExec) session.openChannel("exec");
    exec.setCommand(command);
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        InputStream erStream = exec.getErrStream()) {
        exec.connect(connectionTimeout);
        ListLineConsumer listLineConsumer = new ListLineConsumer();
        String line;
        while ((line = reader.readLine()) != null) {
            listLineConsumer.writeLine(line);
        }
        // read stream to wait until command finishes its work
        IoUtil.readStream(erStream);
        if (exec.getExitStatus() != 0) {
            throw new MachineException(format("Error code: %s. Error: %s", exec.getExitStatus(), IoUtil.readAndCloseQuietly(exec.getErrStream())));
        }
        return listLineConsumer.getText();
    } finally {
        exec.disconnect();
    }
}
Also used : ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) BufferedReader(java.io.BufferedReader) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 7 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)

Example 8 with ListLineConsumer

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

the class ProcessIsLaunchedChecker 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), format(CHECK_COMMAND, processNameToWait), "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