use of org.eclipse.che.plugin.machine.ssh.SshMachineProcess 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.plugin.machine.ssh.SshMachineProcess 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.plugin.machine.ssh.SshMachineProcess in project che by eclipse.
the class SshMachineExecAgentLauncher method start.
protected SshMachineProcess start(SshMachineInstance machine, Agent agent) throws ServerException {
Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
SshMachineProcess process = machine.createProcess(command, null);
LineConsumer lineConsumer = new AbstractLineConsumer() {
@Override
public void writeLine(String line) throws IOException {
machine.getLogger().writeLine(line);
}
};
CountDownLatch countDownLatch = new CountDownLatch(1);
executor.execute(ThreadLocalPropagateContext.wrap(() -> {
try {
countDownLatch.countDown();
process.start(lineConsumer);
} catch (ConflictException | MachineException e) {
try {
machine.getLogger().writeLine(format("[ERROR] %s", e.getMessage()));
} catch (IOException ignored) {
}
} finally {
try {
lineConsumer.close();
} catch (IOException ignored) {
}
}
}));
try {
// ensure that code inside of task submitted to executor is called before end of this method
countDownLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return process;
}
use of org.eclipse.che.plugin.machine.ssh.SshMachineProcess in project che by eclipse.
the class SshMachineExecAgentLauncher method launch.
public void launch(SshMachineInstance machine, Agent agent) throws ServerException {
if (isNullOrEmpty(agent.getScript())) {
return;
}
try {
String architecture = detectArchitecture(machine);
machine.copy(archivePathProvider.getPath(architecture), terminalLocation);
final AgentImpl agentCopy = new AgentImpl(agent);
agentCopy.setScript(agent.getScript() + "\n" + terminalRunCommand);
final SshMachineProcess process = start(machine, agentCopy);
LOG.debug("Waiting for agent {} is launched. Workspace ID:{}", agentCopy.getId(), machine.getWorkspaceId());
final long pingStartTimestamp = System.currentTimeMillis();
SshProcessLaunchedChecker agentLaunchingChecker = new SshProcessLaunchedChecker("che-websocket-terminal");
while (System.currentTimeMillis() - pingStartTimestamp < agentMaxStartTimeMs) {
if (agentLaunchingChecker.isLaunched(agentCopy, machine)) {
return;
} else {
Thread.sleep(agentPingDelayMs);
}
}
process.kill();
final String errMsg = format("Fail launching agent %s. Workspace ID:%s", agent.getName(), machine.getWorkspaceId());
LOG.error(errMsg);
throw new ServerException(errMsg);
} catch (MachineException e) {
throw new ServerException(e.getServiceError());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ServerException(format("Launching agent %s is interrupted", agent.getName()));
} catch (ConflictException e) {
// should never happen
throw new ServerException("Internal server error occurs on terminal launching.");
}
}
Aggregations