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()));
}
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;
}
}
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());
}
}
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()));
}
}
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());
}
}
Aggregations