Search in sources :

Example 1 with SSHProcess

use of io.cdap.cdap.runtime.spi.ssh.SSHProcess in project cdap by caskdata.

the class DefaultSSHSession method executeAndWait.

@Override
public String executeAndWait(List<String> commands) throws IOException {
    SSHProcess process = execute(commands);
    // Reading will be blocked until the process finished
    String out = CharStreams.toString(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
    String err = CharStreams.toString(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
    // Await uninterruptedly
    boolean interrupted = false;
    try {
        while (true) {
            try {
                int exitCode = process.waitFor();
                if (exitCode != 0) {
                    throw new IOException("Commands execution failed with exit code (" + exitCode + ") Commands: " + commands + ", Output: " + out + " Error: " + err);
                }
                return out;
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
    } finally {
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) SSHProcess(io.cdap.cdap.runtime.spi.ssh.SSHProcess)

Example 2 with SSHProcess

use of io.cdap.cdap.runtime.spi.ssh.SSHProcess in project cdap by caskdata.

the class SSHRemoteProcessController method killProcess.

private void killProcess(int signal) throws IOException, InterruptedException {
    // SSH and kill the process
    try (SSHSession session = new DefaultSSHSession(sshConfig)) {
        SSHProcess process = session.execute(String.format("pkill -%d -f -- -Dcdap.runid=%s", signal, programRunId.getRun()));
        // Reading will be blocked until the process finished
        ByteStreams.toByteArray(process.getInputStream());
        String err = CharStreams.toString(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
        int exitCode = process.waitFor();
        // If the exit code is 1, it means there is no such process, which is fine from the termination perspective
        if (exitCode == 0 || exitCode == 1) {
            return;
        }
        throw new IllegalStateException("Failed to kill remote process for program run " + programRunId + " due to error " + err);
    }
}
Also used : SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession) InputStreamReader(java.io.InputStreamReader) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession) SSHProcess(io.cdap.cdap.runtime.spi.ssh.SSHProcess)

Example 3 with SSHProcess

use of io.cdap.cdap.runtime.spi.ssh.SSHProcess in project cdap by caskdata.

the class SSHRemoteProcessController method isRunning.

@Override
public boolean isRunning() throws Exception {
    // Try to SSH into the host and see if the CDAP runtime process is running or not
    try (SSHSession session = new DefaultSSHSession(sshConfig)) {
        SSHProcess process = session.execute("pgrep -f -- -Dcdap.runid=" + programRunId.getRun());
        // Reading will be blocked until the process finished.
        // The output is not needed, just read it to avoid filling up the network buffer.
        ByteStreams.toByteArray(process.getInputStream());
        ByteStreams.toByteArray(process.getErrorStream());
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            LOG.info("Received exit code {} when checking for remote process for program run {}.", exitCode, programRunId);
        }
        return exitCode == 0;
    } catch (IOException e) {
        // If there is error performing SSH, check if the cluster still exist and running
        LOG.debug("Failed to use SSH to determine if the remote process is running for {}. Check cluster status instead.", programRunId, e);
        Cluster cluster = GSON.fromJson(programOpts.getArguments().getOption(ProgramOptionConstants.CLUSTER), Cluster.class);
        String userId = programOpts.getArguments().getOption(ProgramOptionConstants.USER_ID);
        ClusterStatus clusterStatus = provisioningService.getClusterStatus(programRunId, programOpts, cluster, userId);
        // The cluster status has to be RUNNING in order for the remote process still has a chance that is running
        return clusterStatus == ClusterStatus.RUNNING;
    }
}
Also used : SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster) IOException(java.io.IOException) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession) SSHProcess(io.cdap.cdap.runtime.spi.ssh.SSHProcess) ClusterStatus(io.cdap.cdap.runtime.spi.provisioner.ClusterStatus)

Example 4 with SSHProcess

use of io.cdap.cdap.runtime.spi.ssh.SSHProcess in project cdap by caskdata.

the class DefaultSSHSession method execute.

@Override
public SSHProcess execute(List<String> commands) throws IOException {
    try {
        Channel channel = session.openChannel("exec");
        try {
            ChannelExec channelExec = (ChannelExec) channel;
            // Should get the stream before connecting.
            // Otherwise JSch will write the output to some default stream, causing data missing from
            // the InputStream that acquired later.
            SSHProcess process = new DefaultSSHProcess(channelExec, channelExec.getOutputStream(), channelExec.getInputStream(), channelExec.getErrStream());
            channelExec.setCommand(String.join(";", commands));
            channelExec.connect();
            return process;
        } catch (Exception e) {
            channel.disconnect();
            throw e;
        }
    } catch (JSchException e) {
        throw new IOException(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) Channel(com.jcraft.jsch.Channel) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec) SSHProcess(io.cdap.cdap.runtime.spi.ssh.SSHProcess) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Aggregations

SSHProcess (io.cdap.cdap.runtime.spi.ssh.SSHProcess)4 IOException (java.io.IOException)3 DefaultSSHSession (io.cdap.cdap.common.ssh.DefaultSSHSession)2 SSHSession (io.cdap.cdap.runtime.spi.ssh.SSHSession)2 InputStreamReader (java.io.InputStreamReader)2 Channel (com.jcraft.jsch.Channel)1 ChannelExec (com.jcraft.jsch.ChannelExec)1 JSchException (com.jcraft.jsch.JSchException)1 Cluster (io.cdap.cdap.runtime.spi.provisioner.Cluster)1 ClusterStatus (io.cdap.cdap.runtime.spi.provisioner.ClusterStatus)1