Search in sources :

Example 1 with RuntimeSshException

use of org.apache.sshd.common.RuntimeSshException in project platformlayer by platformlayer.

the class MinaSshConnection method sshExecute0.

@Override
protected ProcessExecution sshExecute0(String command, TimeSpan timeout) throws SshException, IOException, InterruptedException {
    try {
        ByteArrayOutputStream stdoutBinary = new ByteArrayOutputStream();
        ByteArrayOutputStream stderrBinary = new ByteArrayOutputStream();
        int exitCode = sshExecute(command, stdoutBinary, stderrBinary, null, timeout);
        ProcessExecution processExecution = new ProcessExecution(command, exitCode, stdoutBinary.toByteArray(), stderrBinary.toByteArray());
        return processExecution;
    } catch (RuntimeSshException e) {
        throw new SshException("Unexpected SSH error", e);
    }
}
Also used : ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 2 with RuntimeSshException

use of org.apache.sshd.common.RuntimeSshException in project platformlayer by platformlayer.

the class MinaSshConnection method sshReadFile0.

@Override
protected byte[] sshReadFile0(String remoteFile, boolean sudo) throws IOException, InterruptedException, SshException {
    MinaSshConnectionWrapper sshConnection = ensureConnected();
    MinaScpClient scp = new MinaScpClient(sshConnection);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TimeSpan timeout = TimeSpan.FIVE_MINUTES;
        scp.get(remoteFile, baos, timeout, sudo);
        return baos.toByteArray();
    } catch (IOException ioException) {
        throw new SshException("Cannot read file", ioException);
    } catch (SshException sshException) {
        String message = sshException.getMessage();
        if (message != null && message.endsWith(": No such file or directory")) {
            return null;
        }
        throw sshException;
    } catch (RuntimeSshException e) {
        throw new SshException("Error reading file", e);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 3 with RuntimeSshException

use of org.apache.sshd.common.RuntimeSshException in project karaf by apache.

the class Main method connectWithRetries.

private static ClientSession connectWithRetries(SshClient client, ClientConfig config) throws Exception {
    ClientSession session = null;
    int retries = 0;
    do {
        ConnectFuture future = client.connect(config.getUser(), config.getHost(), config.getPort());
        future.await();
        try {
            session = future.getSession();
        } catch (RuntimeSshException ex) {
            if (retries++ < config.getRetryAttempts()) {
                Thread.sleep(config.getRetryDelay() * 1000);
                System.out.println("retrying (attempt " + retries + ") ...");
            } else {
                throw ex;
            }
        }
    } while (session == null);
    return session;
}
Also used : ClientSession(org.apache.sshd.client.session.ClientSession) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 4 with RuntimeSshException

use of org.apache.sshd.common.RuntimeSshException in project karaf by apache.

the class DeployMojo method connect.

private ClientSession connect(SshClient client) throws IOException, InterruptedException {
    int retries = 0;
    ClientSession session = null;
    do {
        final ConnectFuture future = client.connect(user, host, port);
        future.await();
        try {
            session = future.getSession();
        } catch (RuntimeSshException ex) {
            if (retries++ < attempts) {
                Thread.sleep(TimeUnit.SECONDS.toMillis(delay));
                getLog().info("retrying (attempt " + retries + ") ...");
            } else {
                throw ex;
            }
        }
    } while (session == null);
    return session;
}
Also used : ClientSession(org.apache.sshd.client.session.ClientSession) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 5 with RuntimeSshException

use of org.apache.sshd.common.RuntimeSshException in project platformlayer by platformlayer.

the class MinaSshConnection method sshCopyData0.

@Override
protected void sshCopyData0(InputStream fileData, long dataLength, String remoteFile, String mode, boolean sudo) throws IOException, InterruptedException, SshException {
    int lastSlash = remoteFile.lastIndexOf('/');
    if (lastSlash == -1) {
        throw new IllegalArgumentException("Expected dest file to be absolute path: " + remoteFile);
    }
    MinaSshConnectionWrapper sshConnection = ensureConnected();
    MinaScpClient scp = new MinaScpClient(sshConnection);
    String remoteDir = remoteFile.substring(0, lastSlash);
    String filename = remoteFile.substring(lastSlash + 1);
    try {
        TimeSpan timeout = TimeSpan.FIVE_MINUTES;
        scp.put(fileData, dataLength, filename, remoteDir, mode, timeout, sudo);
    } catch (IOException ioException) {
        throw new SshException("Cannot doing scp on file", ioException);
    } catch (RuntimeSshException e) {
        throw new SshException("Error doing scp on file", e);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Aggregations

RuntimeSshException (org.apache.sshd.common.RuntimeSshException)7 ConnectFuture (org.apache.sshd.client.future.ConnectFuture)4 ClientSession (org.apache.sshd.client.session.ClientSession)4 SshException (org.platformlayer.ops.ssh.SshException)3 TimeSpan (com.fathomdb.TimeSpan)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)1