airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lah...@apache.org
Subject svn commit: r1518890 - in /airavata/sandbox/gsissh/src: main/java/org/apache/airavata/gsi/ssh/api/ main/java/org/apache/airavata/gsi/ssh/api/job/ main/java/org/apache/airavata/gsi/ssh/impl/ test/java/org/apache/airavata/gsi/ssh/impl/
Date Fri, 30 Aug 2013 04:20:43 GMT
Author: lahiru
Date: Fri Aug 30 04:20:42 2013
New Revision: 1518890

URL: http://svn.apache.org/r1518890
Log:
adding error handling to the API.

Added:
    airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/StandardOutReader.java
Modified:
    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/api/job/JobDescriptor.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/impl/DefaultSSHApiTest.java

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=1518890&r1=1518889&r2=1518890&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 Fri
Aug 30 04:20:42 2013
@@ -46,11 +46,29 @@ public interface SSHApi {
                                  AuthenticationInfo authenticationInfo,
                                  CommandOutput commandOutput) throws SSHApiException;
 
-    void submitAsyncJobWithPBS(ServerInfo serverInfo,
+    /**
+     *  can be used to submit a job with a given pbs file
+     * @param serverInfo
+     * @param authenticationInfo
+     * @param pbsFilePath
+     * @param jobDescriptor
+     * @return The jobID
+     * @throws SSHApiException
+     */
+    String submitAsyncJobWithPBS(ServerInfo serverInfo,
                                AuthenticationInfo authenticationInfo,
                                String pbsFilePath, JobDescriptor jobDescriptor) throws SSHApiException;
-//
-    void submitAsyncJob(ServerInfo serverInfo,
+
+    /**
+     * can be used to submit a job without giving a pbs, but providing job attributes
+     * @param serverInfo
+     * @param authenticationInfo
+     * @param jobDescriptor
+     * @return The jobID
+     * @throws SSHApiException
+     */
+
+    String submitAsyncJob(ServerInfo serverInfo,
                                AuthenticationInfo authenticationInfo,
                                JobDescriptor jobDescriptor) throws SSHApiException;
 }

Modified: airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/job/JobDescriptor.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/job/JobDescriptor.java?rev=1518890&r1=1518889&r2=1518890&view=diff
==============================================================================
--- airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/job/JobDescriptor.java
(original)
+++ airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/job/JobDescriptor.java
Fri Aug 30 04:20:42 2013
@@ -41,9 +41,6 @@ public class JobDescriptor {
 
     private JobDescriptorDocument jobDescriptionDocument;
 
-    private CommandInfo commandInfo;
-
-    private CommandOutput commandOutput;
 
     public JobDescriptor() {
         jobDescriptionDocument = JobDescriptorDocument.Factory.newInstance();
@@ -58,7 +55,6 @@ public class JobDescriptor {
     public JobDescriptor(CommandOutput commandOutput) {
         jobDescriptionDocument = JobDescriptorDocument.Factory.newInstance();
         jobDescriptionDocument.addNewJobDescriptor();
-        this.commandOutput = commandOutput;
     }
 
 
@@ -78,21 +74,6 @@ public class JobDescriptor {
         return jobDescriptor;
     }
 
-    public void setCommandInfo(CommandInfo commandInfo) {
-        this.commandInfo = commandInfo;
-    }
-
-    public void setCommandOutput(CommandOutput commandOutput) {
-        this.commandOutput = commandOutput;
-    }
-
-    public CommandInfo getCommandInfo() {
-        return commandInfo;
-    }
-
-    public CommandOutput getCommandOutput() {
-        return commandOutput;
-    }
 
     //todo write bunch of setter getters to set and get jobdescription parameters
     public void setWorkingDirectory(String workingDirectory) {

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=1518890&r1=1518889&r2=1518890&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
Fri Aug 30 04:20:42 2013
@@ -36,6 +36,8 @@ import javax.xml.transform.stream.Stream
 import javax.xml.transform.stream.StreamSource;
 import java.io.*;
 import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * User: AmilaJ (amilaj@apache.org)
@@ -50,7 +52,6 @@ public class DefaultSSHApi implements SS
 
     private static final Logger log = LoggerFactory.getLogger(DefaultSSHApi.class);
 
-
     private ConfigReader configReader;
 
     /**
@@ -67,6 +68,14 @@ public class DefaultSSHApi implements SS
         }
     }
 
+    /**
+     * @param commandInfo        Encapsulated information about command. E.g :- executable
name
+     *                           parameters etc ...
+     * @param serverInfo         The SSHing server information.
+     * @param authenticationInfo Security data needs to be communicated with remote server.
+     * @param commandOutput      The output of the command.
+     * @throws SSHApiException
+     */
     public void executeCommand(CommandInfo commandInfo, ServerInfo serverInfo,
                                AuthenticationInfo authenticationInfo,
                                CommandOutput commandOutput) throws SSHApiException {
@@ -144,9 +153,16 @@ public class DefaultSSHApi implements SS
 
     }
 
-    public void submitAsyncJobWithPBS(ServerInfo serverInfo,
-                                      AuthenticationInfo authenticationInfo,
-                                      String pbsFilePath, JobDescriptor jobDescriptor) throws
SSHApiException {
+    /**
+     * @param serverInfo
+     * @param authenticationInfo
+     * @param pbsFilePath
+     * @param jobDescriptor
+     * @throws SSHApiException
+     */
+    public String submitAsyncJobWithPBS(ServerInfo serverInfo,
+                                        AuthenticationInfo authenticationInfo,
+                                        String pbsFilePath, JobDescriptor jobDescriptor)
throws SSHApiException {
         try {
 
             SCPTo scpTo = new SCPTo(serverInfo, authenticationInfo, new ConfigReader());
@@ -163,21 +179,35 @@ public class DefaultSSHApi implements SS
                     " connecting user name - "
                     + serverInfo.getUserName(), e);
         }
-
         // since this is a constant we do not ask users to fill this
         RawCommandInfo rawCommandInfo = new RawCommandInfo("/opt/torque/bin/qsub " +
                 jobDescriptor.getWorkingDirectory() + File.separator + FilenameUtils.getName(pbsFilePath));
 
-        this.executeCommand(rawCommandInfo, serverInfo, authenticationInfo, jobDescriptor.getCommandOutput());
+        StandardOutReader jobIDReaderCommandOutput = new StandardOutReader();
+        this.executeCommand(rawCommandInfo, serverInfo, authenticationInfo, jobIDReaderCommandOutput);
+
+        //Check whether pbs submission is successful or not, if it failed throw and exception
in submitJob method
+        // with the error thrown in qsub command
+        if (jobIDReaderCommandOutput.getErrorifAvailable().equals("")) {
+            return jobIDReaderCommandOutput.getStdOutput();
+        } else {
+            throw new SSHApiException(jobIDReaderCommandOutput.getStandardError().toString());
+        }
     }
 
-    public void submitAsyncJob(ServerInfo serverInfo, AuthenticationInfo authenticationInfo,
JobDescriptor jobDescriptor) throws SSHApiException {
+    /**
+     * @param serverInfo
+     * @param authenticationInfo
+     * @param jobDescriptor
+     * @throws SSHApiException
+     */
+    public String submitAsyncJob(ServerInfo serverInfo, AuthenticationInfo authenticationInfo,
JobDescriptor jobDescriptor) throws SSHApiException {
         TransformerFactory factory = TransformerFactory.newInstance();
         String xsltPath = "src" + File.separator + "main" + File.separator + "resources"
+ File.separator + "PBSTemplate.xslt";
         Source xslt = new StreamSource(new File(xsltPath));
         Transformer transformer = null;
         StringWriter results = new StringWriter();
-
+        File tempPBSFile = null;
         try {
             // generate the pbs script using xslt
             transformer = factory.newTransformer(xslt);
@@ -192,13 +222,13 @@ public class DefaultSSHApi implements SS
             int number = new SecureRandom().nextInt();
             number = (number < 0 ? -number : number);
 
-            File tempPBSFile = new File(Integer.toString(number) + ".pbs");
+            tempPBSFile = new File(Integer.toString(number) + ".pbs");
             FileUtils.writeStringToFile(tempPBSFile, results.toString());
 
             //reusing submitAsyncJobWithPBS method to submit a job
 
-            this.submitAsyncJobWithPBS(serverInfo, authenticationInfo, tempPBSFile.getAbsolutePath(),
jobDescriptor);
-            tempPBSFile.delete();
+            String jobID = this.submitAsyncJobWithPBS(serverInfo, authenticationInfo, tempPBSFile.getAbsolutePath(),
jobDescriptor);
+            return jobID;
         } catch (TransformerConfigurationException e) {
             throw new SSHApiException("Error parsing PBS transformation", e);
         } catch (TransformerException e) {
@@ -208,6 +238,8 @@ public class DefaultSSHApi implements SS
                     "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort()
+
                     " connecting user name - "
                     + serverInfo.getUserName(), e);
+        } finally {
+            tempPBSFile.delete();
         }
 
 

Added: airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/StandardOutReader.java
URL: http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/StandardOutReader.java?rev=1518890&view=auto
==============================================================================
--- airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/StandardOutReader.java
(added)
+++ airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/impl/StandardOutReader.java
Fri Aug 30 04:20:42 2013
@@ -0,0 +1,82 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.gsi.ssh.impl;
+
+import com.jcraft.jsch.Channel;
+import org.apache.airavata.gsi.ssh.api.CommandOutput;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class StandardOutReader implements CommandOutput {
+
+    String stdOutput = null;
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    public void onOutput(Channel channel) {
+        try {
+            StringBuffer pbsOutput = new StringBuffer("");
+            InputStream inputStream = channel.getInputStream();
+            byte[] tmp = new byte[1024];
+            while (true) {
+                while (inputStream.available() > 0) {
+                    int i = inputStream.read(tmp, 0, 1024);
+                    if (i < 0) break;
+                    pbsOutput.append(new String(tmp, 0, i));
+                }
+                if (channel.isClosed()) {
+                    String output = pbsOutput.toString();
+                    this.setStdOutput(output);
+                    break;
+                }
+                try {
+                    Thread.sleep(1000);
+                } catch (Exception ignored) {
+                }
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+    public OutputStream getStandardError() {
+        return os;
+    }
+
+    public String getErrorifAvailable(){
+        return os.toString();
+    }
+
+    public void exitCode(int code) {
+        System.out.println("Program exit code - " + code);
+    }
+
+    public String getStdOutput() {
+        return stdOutput;
+    }
+
+    public void setStdOutput(String stdOutput) {
+        this.stdOutput = stdOutput;
+    }
+}

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=1518890&r1=1518889&r2=1518890&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
Fri Aug 30 04:20:42 2013
@@ -135,14 +135,9 @@ public class DefaultSSHApiTest {
                 = new MyProxyAuthenticationInfo(myProxyUserName, myProxyPassword, "myproxy.teragrid.org",
                 7512, 17280000);
 
-        // Create command
-        CommandInfo commandInfo = new RawCommandInfo("/opt/torque/bin/qsub /home/ogce/sleep1.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);
@@ -150,12 +145,12 @@ public class DefaultSSHApiTest {
         // Execute command
         System.out.println("Target PBS file path: " + workingDirectory);
         System.out.println("Local PBS File path: " + pbsFilePath);
-        JobDescriptor jobDescriptor = new JobDescriptor(commandOutput);
+        JobDescriptor jobDescriptor = new JobDescriptor();
         //Here we give working directory as a file name to replace the file, to allow multiple
test runs with the same
         //file name
         jobDescriptor.setWorkingDirectory(workingDirectory);
-        sshApi.submitAsyncJobWithPBS(serverInfo, authenticationInfo, pbsFilePath, jobDescriptor);
-        System.out.println(jobDescriptor.toXML());
+        String jobID = sshApi.submitAsyncJobWithPBS(serverInfo, authenticationInfo, pbsFilePath,
jobDescriptor);
+        System.out.println("JobID returned : " + jobID);
     }
 
     @Test
@@ -168,8 +163,6 @@ public class DefaultSSHApiTest {
         // Server info
         ServerInfo serverInfo = new ServerInfo("ogce", "trestles.sdsc.edu");
 
-        // Output
-        CommandOutput commandOutput = new SystemCommandOutput();
 
         // Get the API
         SSHApi sshApi = SSHApiFactory.createSSHApi(this.certificateLocation);
@@ -178,7 +171,7 @@ public class DefaultSSHApiTest {
         System.out.println("Target PBS file path: " + workingDirectory);
         System.out.println("Local PBS File path: " + pbsFilePath);
         String workingDirectory = File.separator + "home" + File.separator + "ogce" + File.separator
+ "gsissh";
-        JobDescriptor jobDescriptor = new JobDescriptor(commandOutput);
+        JobDescriptor jobDescriptor = new JobDescriptor();
         jobDescriptor.setWorkingDirectory(workingDirectory);
         jobDescriptor.setShellName("/bin/bash");
         jobDescriptor.setJobName("GSI_SSH_SLEEP_JOB");
@@ -195,6 +188,7 @@ public class DefaultSSHApiTest {
         inputs.add("Hello World");
         jobDescriptor.setInputValues(inputs);
         System.out.println(jobDescriptor.toXML());
-        sshApi.submitAsyncJob(serverInfo, authenticationInfo, jobDescriptor);
+        String jobID = sshApi.submitAsyncJob(serverInfo, authenticationInfo, jobDescriptor);
+        System.out.println("JobID returned : " + jobID);
     }
 }



Mime
View raw message