Search in sources :

Example 1 with SshException

use of org.platformlayer.ops.ssh.SshException in project platformlayer by platformlayer.

the class SshOpsTarget method doUpload.

@Override
public void doUpload(FileUpload upload) throws OpsException {
    InputStream dataStream;
    try {
        dataStream = upload.data.getInputStream();
    } catch (IOException e) {
        throw new OpsException("Error opening data stream", e);
    }
    long dataLength = upload.data.getLength();
    try {
        log.info("Uploading file over ssh: " + upload.path);
        sshConnection.sshCopyData(dataStream, dataLength, upload.path.getPath(), upload.mode, needSudo());
    } catch (IOException e) {
        throw new OpsException("Error during file upload", e);
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new OpsException("Error during file upload", e);
    } catch (SshException e) {
        throw new OpsException("Error during file upload", e);
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException)

Example 2 with SshException

use of org.platformlayer.ops.ssh.SshException 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 SshException

use of org.platformlayer.ops.ssh.SshException 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 4 with SshException

use of org.platformlayer.ops.ssh.SshException in project platformlayer by platformlayer.

the class MinaSshConnectionWrapper method connect.

public void connect(TimeSpan connectTimeout) throws SshException {
    if (state != SshConnectionState.NotConnected) {
        throw new IllegalStateException();
    }
    try {
        SshClient client = sshContext.client;
        System.out.println("New SSH connection to " + connectionInfo.getHost());
        ConnectFuture connect = client.connect(connectionInfo.getSocketAddress());
        if (!connect.await(connectTimeout.getTotalMilliseconds())) {
            connect.cancel();
            throw new SshException("Timeout while waiting for SSH connection to " + connectionInfo.getHost());
        }
        this.sshClientSession = connect.getSession();
        if (this.sshClientSession == null) {
            throw new IllegalStateException();
        }
        this.state = SshConnectionState.Connected;
    } catch (Exception e) {
        ExceptionUtils.handleInterrupted(e);
        throw new SshException("Error connecting to SSH server @" + connectionInfo.getSocketAddress(), e);
    }
}
Also used : SshClient(org.apache.sshd.SshClient) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) SshException(org.platformlayer.ops.ssh.SshException) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException)

Example 5 with SshException

use of org.platformlayer.ops.ssh.SshException in project platformlayer by platformlayer.

the class MinaSshConnectionWrapper method openSession.

public ChannelSession openSession(String command) throws SshException {
    try {
        boolean useAgentForwarding = sshContext.isAgentForwarding();
        ChannelSession clientChannel = BugFixChannelExec.createExecChannel(sshClientSession, command, useAgentForwarding);
        return clientChannel;
    } catch (Exception e) {
        ExceptionUtils.handleInterrupted(e);
        throw new SshException("Error creating channel", e);
    }
}
Also used : ChannelSession(org.apache.sshd.client.channel.ChannelSession) SshException(org.platformlayer.ops.ssh.SshException) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException)

Aggregations

SshException (org.platformlayer.ops.ssh.SshException)10 IOException (java.io.IOException)8 RuntimeSshException (org.apache.sshd.common.RuntimeSshException)5 TimeSpan (com.fathomdb.TimeSpan)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ChannelSession (org.apache.sshd.client.channel.ChannelSession)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 SshClient (org.apache.sshd.SshClient)1 ConnectFuture (org.apache.sshd.client.future.ConnectFuture)1 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)1 ProcessExecutionException (org.platformlayer.ops.process.ProcessExecutionException)1 IServerKeyVerifier (org.platformlayer.ops.ssh.IServerKeyVerifier)1 SshConnection (org.platformlayer.ops.ssh.SshConnection)1