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);
}
}
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();
}
}
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations