airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lah...@apache.org
Subject svn commit: r1515530 - in /airavata/sandbox/gsissh/src: main/java/org/apache/airavata/gsi/ssh/api/ main/java/org/apache/airavata/gsi/ssh/impl/ test/java/org/apache/airavata/gsi/ssh/config/ test/java/org/apache/airavata/gsi/ssh/impl/
Date Mon, 19 Aug 2013 17:48:16 GMT
Author: lahiru
Date: Mon Aug 19 17:48:16 2013
New Revision: 1515530

URL: http://svn.apache.org/r1515530
Log:
adding new methods to execute a pbs job.

Modified:
    airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java
    airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SSHApi.java
    airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApi.java
    airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
    airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTest.java

Modified: airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java?rev=1515530&r1=1515529&r2=1515530&view=diff
==============================================================================
--- airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java (original)
+++ airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java Mon Aug
19 17:48:16 2013
@@ -38,13 +38,20 @@ public class SCPTo {
         JSch.setConfig("userauth.gssapi-with-mic", "com.jcraft.jsch.UserAuthGSSAPIWithMICGSSCredentials");
 
     }
+
     private ServerInfo serverInfo;
 
     private AuthenticationInfo authenticationInfo;
 
     private ConfigReader configReader;
 
-
+    /**
+     * We need to pass certificateLocation when we use SCPTo method standalone
+     * @param serverInfo
+     * @param authenticationInfo
+     * @param certificateLocation
+     * @param configReader
+     */
     public SCPTo(ServerInfo serverInfo, AuthenticationInfo authenticationInfo, String certificateLocation,
ConfigReader configReader) {
         System.setProperty("X509_CERT_DIR", certificateLocation);
         this.serverInfo = serverInfo;
@@ -53,14 +60,27 @@ public class SCPTo {
     }
 
     /**
+     * This can be used when use SCPTo method within SSHAPi because SSHApiFactory already
set the system property certificateLocation
+     * @param serverInfo
+     * @param authenticationInfo
+     * @param configReader
+     */
+    public SCPTo(ServerInfo serverInfo, AuthenticationInfo authenticationInfo
+            , ConfigReader configReader) {
+        this.serverInfo = serverInfo;
+        this.authenticationInfo = authenticationInfo;
+        this.configReader = configReader;
+    }
+    /**
      * This  method will scp the lFile to the rFile location
-     * @param rFile   remote file Path to use in scp
-     * @param lFile   local file path to use in scp
+     *
+     * @param rFile remote file Path to use in scp
+     * @param lFile local file path to use in scp
      * @throws IOException
      * @throws JSchException
      * @throws SSHApiException
      */
-    public void scpTo(String rFile, String lFile) throws IOException, JSchException, SSHApiException
{
+    public Session scpTo(String rFile, String lFile) throws IOException, JSchException, SSHApiException
{
         FileInputStream fis = null;
         String prefix = null;
         if (new File(lFile).isDirectory()) {
@@ -102,7 +122,7 @@ public class SCPTo {
         boolean ptimestamp = true;
 
         // exec 'scp -t rfile' remotely
-        String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rFile;
+        String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rFile;
         Channel channel = session.openChannel("exec");
         ((ChannelExec) channel).setCommand(command);
 
@@ -112,83 +132,87 @@ public class SCPTo {
 
         channel.connect();
 
-        if(checkAck(in)!=0){
+        if (checkAck(in) != 0) {
             System.exit(0);
-              }
+        }
 
-              File _lfile = new File(lFile);
+        File _lfile = new File(lFile);
 
-              if(ptimestamp){
-                command="T "+(_lfile.lastModified()/1000)+" 0";
-                // The access time should be sent here,
-                // but it is not accessible with JavaAPI ;-<
-                command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
-                out.write(command.getBytes()); out.flush();
-                if(checkAck(in)!=0){
+        if (ptimestamp) {
+            command = "T " + (_lfile.lastModified() / 1000) + " 0";
+            // The access time should be sent here,
+            // but it is not accessible with JavaAPI ;-<
+            command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
+            out.write(command.getBytes());
+            out.flush();
+            if (checkAck(in) != 0) {
                 System.exit(0);
-                }
-              }
+            }
+        }
 
-              // send "C0644 filesize filename", where filename should not include '/'
-              long filesize=_lfile.length();
-              command="C0644 "+filesize+" ";
-              if(lFile.lastIndexOf('/')>0){
-                command+=lFile.substring(lFile.lastIndexOf('/')+1);
-              }
-              else{
-                command+=lFile;
-              }
-              command+="\n";
-              out.write(command.getBytes()); out.flush();
-              if(checkAck(in)!=0){
+        // send "C0644 filesize filename", where filename should not include '/'
+        long filesize = _lfile.length();
+        command = "C0644 " + filesize + " ";
+        if (lFile.lastIndexOf('/') > 0) {
+            command += lFile.substring(lFile.lastIndexOf('/') + 1);
+        } else {
+            command += lFile;
+        }
+        command += "\n";
+        out.write(command.getBytes());
+        out.flush();
+        if (checkAck(in) != 0) {
             System.exit(0);
-              }
+        }
 
-              // send a content of lFile
-              fis=new FileInputStream(lFile);
-              byte[] buf=new byte[1024];
-              while(true){
-                int len=fis.read(buf, 0, buf.length);
-            if(len<=0) break;
-                out.write(buf, 0, len); //out.flush();
-              }
-              fis.close();
-              fis=null;
-              // send '\0'
-              buf[0]=0; out.write(buf, 0, 1); out.flush();
-              if(checkAck(in)!=0){
+        // send a content of lFile
+        fis = new FileInputStream(lFile);
+        byte[] buf = new byte[1024];
+        while (true) {
+            int len = fis.read(buf, 0, buf.length);
+            if (len <= 0) break;
+            out.write(buf, 0, len); //out.flush();
+        }
+        fis.close();
+        fis = null;
+        // send '\0'
+        buf[0] = 0;
+        out.write(buf, 0, 1);
+        out.flush();
+        if (checkAck(in) != 0) {
             System.exit(0);
-              }
-              out.close();
+        }
+        out.close();
 
-              channel.disconnect();
-              session.disconnect();
+        channel.disconnect();
+        session.disconnect();
+        return session;
     }
 
-    static int checkAck(InputStream in) throws IOException{
-       int b=in.read();
-       // b may be 0 for success,
-       //          1 for error,
-       //          2 for fatal error,
-       //          -1
-       if(b==0) return b;
-       if(b==-1) return b;
-
-       if(b==1 || b==2){
-         StringBuffer sb=new StringBuffer();
-         int c;
-         do {
-       c=in.read();
-       sb.append((char)c);
-         }
-         while(c!='\n');
-         if(b==1){ // error
-       System.out.print(sb.toString());
-         }
-         if(b==2){ // fatal error
-       System.out.print(sb.toString());
-         }
-       }
-       return b;
-     }
+    static int checkAck(InputStream in) throws IOException {
+        int b = in.read();
+        // b may be 0 for success,
+        //          1 for error,
+        //          2 for fatal error,
+        //          -1
+        if (b == 0) return b;
+        if (b == -1) return b;
+
+        if (b == 1 || b == 2) {
+            StringBuffer sb = new StringBuffer();
+            int c;
+            do {
+                c = in.read();
+                sb.append((char) c);
+            }
+            while (c != '\n');
+            if (b == 1) { // error
+                System.out.print(sb.toString());
+            }
+            if (b == 2) { // fatal error
+                System.out.print(sb.toString());
+            }
+        }
+        return b;
+    }
 }

Modified: airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SSHApi.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SSHApi.java?rev=1515530&r1=1515529&r2=1515530&view=diff
==============================================================================
--- airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SSHApi.java (original)
+++ airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SSHApi.java Mon
Aug 19 17:48:16 2013
@@ -44,4 +44,7 @@ public interface SSHApi {
                                  AuthenticationInfo authenticationInfo,
                                  CommandOutput commandOutput) throws SSHApiException;
 
+    void submitJob(CommandInfo commandInfo, ServerInfo serverInfo,
+                   AuthenticationInfo authenticationInfo, CommandOutput commandOutput,
+                   String pbsFilePath,String workingDirectory)throws SSHApiException;
 }

Modified: airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApi.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApi.java?rev=1515530&r1=1515529&r2=1515530&view=diff
==============================================================================
--- airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApi.java
(original)
+++ airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApi.java
Mon Aug 19 17:48:16 2013
@@ -51,6 +51,7 @@ public class DefaultSSHApi implements SS
     /**
      * Initializes default SSH API. During initialization basic configurations
      * are read.
+     *
      * @throws SSHApiException If an error occurred while reading basic configurations.
      */
     public DefaultSSHApi() throws SSHApiException {
@@ -62,8 +63,8 @@ public class DefaultSSHApi implements SS
     }
 
     public void executeCommand(CommandInfo commandInfo, ServerInfo serverInfo,
-                                        AuthenticationInfo authenticationInfo,
-                                        CommandOutput commandOutput) throws SSHApiException
{
+                               AuthenticationInfo authenticationInfo,
+                               CommandOutput commandOutput) throws SSHApiException {
 
         JSch jsch = new ExtendedJSch();
 
@@ -86,7 +87,7 @@ public class DefaultSSHApi implements SS
 
         // Not a good way, but we dont have any choice
         if (session instanceof ExtendedSession) {
-            ((ExtendedSession)session).setAuthenticationInfo(authenticationInfo);
+            ((ExtendedSession) session).setAuthenticationInfo(authenticationInfo);
         }
 
         try {
@@ -114,7 +115,6 @@ public class DefaultSSHApi implements SS
         }
 
 
-
         channel.setInputStream(null);
         ((ChannelExec) channel).setErrStream(commandOutput.getStandardError());
 
@@ -138,4 +138,26 @@ public class DefaultSSHApi implements SS
 
 
     }
+
+    public void submitJob(CommandInfo commandInfo, ServerInfo serverInfo,
+                          AuthenticationInfo authenticationInfo,
+                          CommandOutput commandOutput, String pbsFilePath, String workingDirectory)
throws  SSHApiException {
+        try {
+            SCPTo scpTo = new SCPTo(serverInfo, authenticationInfo, new ConfigReader());
+            scpTo.scpTo(workingDirectory, pbsFilePath);
+        } catch (JSchException e) {
+            throw new SSHApiException("An exception occurred while connecting to server."
+
+                    "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort()
+
+                    " connecting user name - "
+                    + serverInfo.getUserName(), e);
+        }  catch (IOException e){
+           throw new SSHApiException("An exception occurred while connecting to server."
+
+                    "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort()
+
+                    " connecting user name - "
+                    + serverInfo.getUserName(), e);
+        }
+
+        this.executeCommand(commandInfo,serverInfo,authenticationInfo,commandOutput);
+    }
+
 }

Modified: airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java?rev=1515530&r1=1515529&r2=1515530&view=diff
==============================================================================
--- airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
(original)
+++ airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
Mon Aug 19 17:48:16 2013
@@ -38,9 +38,6 @@ public class SCPToTest {
 
     @BeforeTest
     public void setUp() throws Exception {
-        System.setProperty("myproxy.user", "ogce");
-        System.setProperty("myproxy.password", "Jdas7wph");
-        System.setProperty("basedir", "/Users/lahirugunathilake/work/airavata/sandbox/gsissh");
         myProxyUserName = System.getProperty("myproxy.user");
         myProxyPassword = System.getProperty("myproxy.password");
 

Modified: airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTest.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTest.java?rev=1515530&r1=1515529&r2=1515530&view=diff
==============================================================================
--- airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTest.java
(original)
+++ airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTest.java
Mon Aug 19 17:48:16 2013
@@ -39,6 +39,8 @@ public class DefaultSSHApiTest {
     private String myProxyUserName;
     private String myProxyPassword;
     private String certificateLocation;
+    private String pbsFilePath;
+    private String workingDirectory;
 
     @BeforeTest
     public void setUp() throws Exception {
@@ -53,6 +55,9 @@ public class DefaultSSHApiTest {
 
         certificateLocation = pomFileDirectory.getAbsolutePath() + "/certificates";
 
+        pbsFilePath = pomFileDirectory.getAbsolutePath() + File.separator + "src" + File.separator
+ "test" + File.separator + "resources" + File.separator +  "sleep.pbs";
+
+        workingDirectory = File.separator + "home" + File.separator + "ogce" ;
         if (myProxyUserName == null || myProxyPassword == null) {
             System.out.println(">>>>>> Please run tests with my proxy user
name and password. " +
                     "E.g :- mvn clean install -Dmyproxy.user=xxx -Dmyproxy.password=xxx <<<<<<<");
@@ -87,7 +92,7 @@ public class DefaultSSHApiTest {
     }
 
     @Test
-    public void testSubmitJob() throws Exception {
+    public void testSubmitSimpleCommand() throws Exception {
         // Create authentication
         AuthenticationInfo authenticationInfo
                 = new MyProxyAuthenticationInfo(myProxyUserName, myProxyPassword, "myproxy.teragrid.org",
@@ -108,4 +113,27 @@ public class DefaultSSHApiTest {
         // Execute command
         sshApi.executeCommand(commandInfo, serverInfo, authenticationInfo, commandOutput);
     }
+
+    @Test
+    public void testSubmitJob() throws Exception {
+        // Create authentication
+        AuthenticationInfo authenticationInfo
+                = new MyProxyAuthenticationInfo(myProxyUserName, myProxyPassword, "myproxy.teragrid.org",
+                7512, 17280000);
+
+        // Create command
+        CommandInfo commandInfo = new RawCommandInfo("/opt/torque/bin/qsub /home/ogce/sleep.pbs");
+
+        // Server info
+        ServerInfo serverInfo = new ServerInfo("ogce" ,"trestles.sdsc.edu");
+
+        // Output
+        CommandOutput commandOutput = new SystemCommandOutput();
+
+        // Get the API
+        SSHApi sshApi = SSHApiFactory.createSSHApi(this.certificateLocation);
+
+        // Execute command
+        sshApi.submitJob(commandInfo, serverInfo, authenticationInfo, commandOutput, pbsFilePath,
workingDirectory);
+    }
 }



Mime
View raw message