Search in sources :

Example 6 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 7 with SshException

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

the class MinaSshConnection method activateConnection.

private void activateConnection(MinaSshConnectionWrapper sshConnection, TimeSpan connectTimeout, TimeSpan keyExchangeTimeout) throws IOException, SshException {
    boolean okay = false;
    try {
        if (!sshConnection.isConnected()) {
            log.info("Making new SSH connection to " + getHost());
            sshConnection.setServerKeyVerifier(this.getServerKeyVerifier());
            if (connectTimeout == null) {
                connectTimeout = TimeSpan.ZERO;
            }
            if (keyExchangeTimeout == null) {
                keyExchangeTimeout = TimeSpan.ZERO;
            }
            sshConnection.connect(connectTimeout);
            if (!sshConnection.isConnected()) {
                throw new IllegalStateException("Connection completed, but could not get connection details");
            }
        } else {
            IServerKeyVerifier myServerKeyVerifier = getServerKeyVerifier();
            myServerKeyVerifier.verifyPooled(sshConnection.getServerKeyVerifier());
        }
        if (!sshConnection.isAuthenticationComplete()) {
            // Authenticate
            boolean isAuthenticated = sshConnection.authenticateWithPublicKey(getUser(), getKeyPair(), keyExchangeTimeout);
            if (isAuthenticated == false) {
                // valid
                throw new SshException("Authentication failed.  Tried to connect to " + getUser() + "@" + sshConnection.getConnectionInfo().getHost());
            } else {
                log.debug("SSH authentication succeeded");
            }
        }
        okay = true;
    } finally {
        if (!okay) {
            // If we fail to activate for any reason, we reset the
            // connection so that we start clean
            log.info("Resetting connection after failure to connect");
            close();
        }
    }
}
Also used : IServerKeyVerifier(org.platformlayer.ops.ssh.IServerKeyVerifier) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 8 with SshException

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

Example 9 with SshException

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

the class MinaSshConnection method sshExecute.

int sshExecute(String command, final OutputStream stdout, final OutputStream stderr, ProcessStartListener listener, TimeSpan timeout) throws SshException, IOException, InterruptedException {
    ChannelSession sshChannel = null;
    try {
        sshChannel = ensureConnected().openSession(command);
        sshChannel.setIn(new ByteArrayInputStream(new byte[0]));
        sshChannel.setOut(stdout);
        sshChannel.setErr(stderr);
        try {
            sshChannel.open().await(DEFAULT_SSH_CONNECT_TIMEOUT.getTotalMilliseconds());
        } catch (Exception e) {
            ExceptionUtils.handleInterrupted(e);
            throw new SshException("Ssh error opening channel", e);
        }
        if (listener != null) {
            throw new UnsupportedOperationException();
        // listener.startedProcess(sshChannel.getStdin());
        }
        if (timeout == null) {
            timeout = TimeSpan.ZERO;
        }
        // Wait for everything to finish
        int flags = sshChannel.waitFor(ClientChannel.EOF | ClientChannel.CLOSED, timeout.getTotalMilliseconds());
        if ((flags & ClientChannel.TIMEOUT) != 0) {
            closeAndRemoveFromPool();
            throw new SshException("Timeout while waiting for SSH task to complete.  Timeout was " + timeout);
        }
        flags = sshChannel.waitFor(ClientChannel.EXIT_STATUS, 30000);
        if ((flags & ClientChannel.TIMEOUT) != 0) {
            closeAndRemoveFromPool();
            throw new SshException("Timeout while waiting for exit code.  Timeout was " + timeout);
        }
        Integer exitCode = getExitStatus(sshChannel);
        if (exitCode == null) {
            closeAndRemoveFromPool();
            throw new SshException("No exit code returned");
        }
        return exitCode;
    } finally {
        if (sshChannel != null) {
            sshChannel.close(false);
        }
    }
}
Also used : ChannelSession(org.apache.sshd.client.channel.ChannelSession) ByteArrayInputStream(java.io.ByteArrayInputStream) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) IOException(java.io.IOException)

Example 10 with SshException

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

the class SshOpsTarget method executeCommandUnchecked.

@Override
protected ProcessExecution executeCommandUnchecked(Command command) throws ProcessExecutionException {
    try {
        String commandString = command.buildCommandString();
        TimeSpan timeout = command.getTimeout();
        if (command.getKeyPair() != null) {
            SshConnection agentConnection = sshConnection.buildAgentConnection(command.getKeyPair());
            try {
                return agentConnection.sshExecute(commandString, timeout);
            } finally {
                agentConnection.close();
            }
        } else {
            return sshConnection.sshExecute(commandString, timeout);
        }
    } catch (IOException e) {
        throw new ProcessExecutionException("Error during command execution", e);
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new ProcessExecutionException("Error during command execution", e);
    } catch (SshException e) {
        throw new ProcessExecutionException("Error during command execution", e);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) SshConnection(org.platformlayer.ops.ssh.SshConnection) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) 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