Search in sources :

Example 1 with SshMachineProcess

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;
    }
}
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 2 with SshMachineProcess

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());
    }
}
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 3 with SshMachineProcess

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;
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with SshMachineProcess

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.");
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) AgentImpl(org.eclipse.che.api.agent.shared.model.impl.AgentImpl)

Aggregations

SshMachineProcess (org.eclipse.che.plugin.machine.ssh.SshMachineProcess)4 ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)3 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)3 ConflictException (org.eclipse.che.api.core.ConflictException)2 Command (org.eclipse.che.api.core.model.machine.Command)2 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matcher (java.util.regex.Matcher)1 AgentImpl (org.eclipse.che.api.agent.shared.model.impl.AgentImpl)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