airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shame...@apache.org
Subject [1/2] airavata git commit: Test ssh session with test channel to verify channel creation is working, if not re initialize a new ssh session
Date Mon, 21 Mar 2016 18:19:07 GMT
Repository: airavata
Updated Branches:
  refs/heads/develop 6d692ee34 -> 8496ae6c1


Test ssh session with test channel to verify channel creation is working, if not re initialize
a new ssh session


Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/47409bad
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/47409bad
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/47409bad

Branch: refs/heads/develop
Commit: 47409bad84d7e4996c5e03dd45a2931fae6ae8fb
Parents: 6d692ee
Author: Shameera Rathnayaka <shameerainfo@gmail.com>
Authored: Mon Mar 21 14:05:55 2016 -0400
Committer: Shameera Rathnayaka <shameerainfo@gmail.com>
Committed: Mon Mar 21 14:05:55 2016 -0400

----------------------------------------------------------------------
 .../org/apache/airavata/gfac/impl/Factory.java  | 48 ++++++++++++++++++--
 .../airavata/gfac/impl/HPCRemoteCluster.java    | 32 ++++++-------
 2 files changed, 60 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/47409bad/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/Factory.java
----------------------------------------------------------------------
diff --git a/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/Factory.java
b/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/Factory.java
index 957b932..310fb12 100644
--- a/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/Factory.java
+++ b/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/Factory.java
@@ -20,6 +20,8 @@
  */
 package org.apache.airavata.gfac.impl;
 
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.ChannelExec;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.Session;
@@ -35,6 +37,7 @@ import org.apache.airavata.gfac.core.GFacEngine;
 import org.apache.airavata.gfac.core.GFacException;
 import org.apache.airavata.gfac.core.GFacUtils;
 import org.apache.airavata.gfac.core.JobManagerConfiguration;
+import org.apache.airavata.gfac.core.SSHApiException;
 import org.apache.airavata.gfac.core.authentication.AuthenticationInfo;
 import org.apache.airavata.gfac.core.authentication.SSHKeyAuthentication;
 import org.apache.airavata.gfac.core.cluster.OutputParser;
@@ -63,9 +66,7 @@ import org.apache.airavata.messaging.core.impl.RabbitMQStatusPublisher;
 import org.apache.airavata.model.appcatalog.computeresource.*;
 import org.apache.airavata.model.appcatalog.gatewayprofile.ComputeResourcePreference;
 import org.apache.airavata.model.appcatalog.gatewayprofile.StoragePreference;
-import org.apache.airavata.model.commons.ErrorModel;
 import org.apache.airavata.model.data.movement.DataMovementProtocol;
-import org.apache.airavata.model.status.TaskState;
 import org.apache.airavata.registry.core.experiment.catalog.impl.RegistryFactory;
 import org.apache.airavata.registry.cpi.AppCatalog;
 import org.apache.airavata.registry.cpi.AppCatalogException;
@@ -423,8 +424,21 @@ public abstract class Factory {
 		SSHKeyAuthentication authentication = null;
 		String key = serverInfo.getUserName() + "_" + serverInfo.getHost() + "_" + serverInfo.getPort();
 		Session session = sessionMap.get(key);
-		if (session == null || !session.isConnected()) {
-			// FIXME - move following info logs to debug
+		boolean valid = isValidSession(session);
+		// FIXME - move following info logs to debug
+		if (valid) {
+			log.info("SSH Session validation succeeded, key :" + key);
+			valid = testChannelCreation(session);
+			if (valid) {
+				log.info("Channel creation test succeeded, key :" + key);
+			} else {
+				log.info("Channel creation test failed, key :" + key);
+			}
+		} else {
+			log.info("Session validation failed, key :" + key);
+		}
+
+		if (!valid) {
 			if (session != null) {
 				log.info("Reinitialize a new SSH session for :" + key);
 			} else {
@@ -460,6 +474,32 @@ public abstract class Factory {
 
 	}
 
+	private static boolean testChannelCreation(Session session) {
+
+		String command = "ls ";
+		Channel channel = null;
+		try {
+			channel = session.openChannel("exec");
+			StandardOutReader stdOutReader = new StandardOutReader();
+			((ChannelExec) channel).setCommand(command);
+			((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
+			channel.connect();
+			stdOutReader.onOutput(channel);
+		} catch (JSchException e) {
+			log.error("Test Channel creation failed.", e);
+			return false;
+		} finally {
+			if (channel != null) {
+				channel.disconnect();
+			}
+		}
+		return true;
+	}
+
+	private static boolean isValidSession(Session session) {
+		return session != null && session.isConnected();
+	}
+
 	public static Task getArchiveTask() {
 		return new ArchiveTask();
 	}

http://git-wip-us.apache.org/repos/asf/airavata/blob/47409bad/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/HPCRemoteCluster.java
----------------------------------------------------------------------
diff --git a/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/HPCRemoteCluster.java
b/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/HPCRemoteCluster.java
index 8e8b7ed..06a611b 100644
--- a/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/HPCRemoteCluster.java
+++ b/modules/gfac/gfac-impl/src/main/java/org/apache/airavata/gfac/impl/HPCRemoteCluster.java
@@ -63,7 +63,7 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 			jSch = new JSch();
 			jSch.addIdentity(UUID.randomUUID().toString(), authentication.getPrivateKey(), authentication.getPublicKey(),
 					authentication.getPassphrase().getBytes());
-			session = getOpenSession();
+			session = Factory.getSSHSession(authenticationInfo, serverInfo);
 		} catch (JSchException e) {
 			throw new AiravataException("JSch initialization error ", e);
 		}
@@ -109,7 +109,7 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 		while (retry > 0) {
 			try {
 				if (!session.isConnected()) {
-					session = getOpenSession();
+					session = Factory.getSSHSession(authenticationInfo, serverInfo);
 				}
 				log.info("Transferring localhost:" + localFile  + " to " + serverInfo.getHost() + ":"
+ remoteFile);
 				SSHUtils.scpTo(localFile, remoteFile, session);
@@ -118,8 +118,8 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 				retry--;
 				if (!session.isConnected()) {
 					try {
-						session = getOpenSession();
-					} catch (JSchException e1) {
+						session = Factory.getSSHSession(authenticationInfo, serverInfo);
+					} catch (AiravataException e1) {
 						throw new SSHApiException("JSch Session connection failed", e1);
 					}
 				}
@@ -140,7 +140,7 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 		while(retry>0) {
 			try {
 				if (!session.isConnected()) {
-					session = getOpenSession();
+					session = Factory.getSSHSession(authenticationInfo, serverInfo);
 				}
 				log.info("Transferring " + serverInfo.getHost() + ":" + remoteFile + " To localhost:"
+ localFile);
 				SSHUtils.scpFrom(remoteFile, localFile, session);
@@ -149,8 +149,8 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 				retry--;
 				if (!session.isConnected()) {
 					try {
-						session = getOpenSession();
-					} catch (JSchException e1) {
+						session = Factory.getSSHSession(authenticationInfo, serverInfo);
+					} catch (AiravataException e1) {
 						throw new SSHApiException("JSch Session connection failed", e1);
 					}
 				}
@@ -170,7 +170,7 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 			if (!session.isConnected()) {
 				// FIXME - move following info log to debug
 				log.info("Reinitialize a new SSH session for key :" + serverInfo.getUserName() + "-"
+ serverInfo.getHost() + "-" + serverInfo.getPort());
-				session = getOpenSession();
+				session = Factory.getSSHSession(authenticationInfo, serverInfo);
 			} else {
 				// FIXME - move following info logs to debug
 				log.info("Reuse SSH session for key :" + serverInfo.getUserName() + "-" + serverInfo.getHost()
+ "-" + serverInfo.getPort());
@@ -181,7 +181,7 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
             } else {
                 SSHUtils.scpThirdParty(sourceFile, session, destinationFile, clientSession,
ignoreEmptyFile);
             }
-        } catch (IOException | JSchException e) {
+        } catch (IOException | AiravataException | JSchException e) {
 			throw new SSHApiException("Failed scp file:" + sourceFile + " to remote file "
 					+destinationFile , e);
 		}
@@ -193,14 +193,14 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 			if (!session.isConnected()) {
 				// FIXME - move following info log to debug
 				log.info("Reinitialize a new SSH session for key :" + serverInfo.getUserName() + "-"
+ serverInfo.getHost() + "-" + serverInfo.getPort());
-				session = getOpenSession();
+				session = Factory.getSSHSession(authenticationInfo, serverInfo);
 			} else {
 				// FIXME - move following info log to debug
 				log.info("Reuse SSH session for key :" + serverInfo.getUserName() + "-" + serverInfo.getHost()
+ "-" + serverInfo.getPort());
 			}
 			log.info("Creating directory: " + serverInfo.getHost() + ":" + directoryPath);
 			SSHUtils.makeDirectory(directoryPath, session);
-		} catch (JSchException | IOException e) {
+		} catch (JSchException | AiravataException | IOException e) {
 			throw new SSHApiException("Failed to create directory " + serverInfo.getHost() + ":" +
directoryPath, e);
 		}
 	}
@@ -246,11 +246,11 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 	public List<String> listDirectory(String directoryPath) throws SSHApiException {
 		try {
 			if (!session.isConnected()) {
-				session = getOpenSession();
+				session = Factory.getSSHSession(authenticationInfo, serverInfo);
 			}
 			log.info("Creating directory: " + serverInfo.getHost() + ":" + directoryPath);
 			return SSHUtils.listDirectory(directoryPath, session);
-		} catch (JSchException | IOException e) {
+		} catch (JSchException | AiravataException | IOException e) {
 			throw new SSHApiException("Failed to list directory " + serverInfo.getHost() + ":" + directoryPath,
e);
 		}
 	}
@@ -297,7 +297,7 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 		ChannelExec channelExec = null;
 		try {
 			if (!session.isConnected()) {
-				session = getOpenSession();
+				session = Factory.getSSHSession(authenticationInfo, serverInfo);
 			}
 			channelExec = ((ChannelExec) session.openChannel("exec"));
 			channelExec.setCommand(command);
@@ -306,9 +306,9 @@ public class HPCRemoteCluster extends AbstractRemoteCluster{
 			log.info("Executing command {}", commandInfo.getCommand());
 			channelExec.connect();
 			commandOutput.onOutput(channelExec);
-		} catch (JSchException e) {
+		} catch (JSchException | AiravataException e) {
 			throw new SSHApiException("Unable to execute command - ", e);
-		}finally {
+		} finally {
 			//Only disconnecting the channel, session can be reused
 			if (channelExec != null) {
 				commandOutput.exitCode(channelExec.getExitStatus());


Mime
View raw message