You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by di...@apache.org on 2018/04/05 20:02:12 UTC

[airavata] branch develop updated: Adding file exists api to adaptor

This is an automated email from the ASF dual-hosted git repository.

dimuthuupe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/develop by this push:
     new 9d84826  Adding file exists api to adaptor
9d84826 is described below

commit 9d8482693151b5cf0e8fd91e73662bc18997316d
Author: dimuthu <di...@gmail.com>
AuthorDate: Thu Apr 5 16:02:04 2018 -0400

    Adding file exists api to adaptor
---
 .../apache/airavata/agents/api/AgentAdaptor.java   |  2 +
 .../helix/agent/local/LocalAgentAdaptor.java       |  5 ++
 .../airavata/helix/agent/ssh/SshAgentAdaptor.java  | 56 ++++++++++++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/modules/airavata-helix/agent-api/src/main/java/org/apache/airavata/agents/api/AgentAdaptor.java b/modules/airavata-helix/agent-api/src/main/java/org/apache/airavata/agents/api/AgentAdaptor.java
index ede7376..0039826 100644
--- a/modules/airavata-helix/agent-api/src/main/java/org/apache/airavata/agents/api/AgentAdaptor.java
+++ b/modules/airavata-helix/agent-api/src/main/java/org/apache/airavata/agents/api/AgentAdaptor.java
@@ -42,5 +42,7 @@ public interface AgentAdaptor {
 
     public List<String> listDirectory(String path) throws AgentException;
 
+    public Boolean doesFileExist(String filePath) throws AgentException;
+
     public List<String> getFileNameFromExtension(String fileName, String parentPath) throws AgentException;
 }
diff --git a/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/local/LocalAgentAdaptor.java b/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/local/LocalAgentAdaptor.java
index 7a2e905..cb640f3 100644
--- a/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/local/LocalAgentAdaptor.java
+++ b/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/local/LocalAgentAdaptor.java
@@ -63,6 +63,11 @@ public class LocalAgentAdaptor implements AgentAdaptor {
     }
 
     @Override
+    public Boolean doesFileExist(String filePath) throws AgentException {
+        throw new AgentException("Operation not implemented");
+    }
+
+    @Override
     public List<String> getFileNameFromExtension(String fileName, String parentPath) throws AgentException {
         throw new AgentException("Operation not implemented");
     }
diff --git a/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/ssh/SshAgentAdaptor.java b/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/ssh/SshAgentAdaptor.java
index e652150..702afe7 100644
--- a/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/ssh/SshAgentAdaptor.java
+++ b/modules/airavata-helix/agent-impl/ssh-agent/src/main/java/org/apache/airavata/helix/agent/ssh/SshAgentAdaptor.java
@@ -477,6 +477,62 @@ public class SshAgentAdaptor implements AgentAdaptor {
     }
 
     @Override
+    public Boolean doesFileExist(String filePath) throws AgentException {
+        String command = "ls " + filePath;
+        ChannelExec channelExec = null;
+        try {
+            channelExec = (ChannelExec)session.openChannel("exec");
+            StandardOutReader stdOutReader = new StandardOutReader();
+
+            channelExec.setCommand(command);
+
+            InputStream out = channelExec.getInputStream();
+            InputStream err = channelExec.getErrStream();
+
+            channelExec.connect();
+
+            stdOutReader.readStdOutFromStream(out);
+            stdOutReader.readStdErrFromStream(err);
+            if (stdOutReader.getStdError().contains("ls:")) {
+                logger.info("Invalid file path " + filePath + ". stderr : " + stdOutReader.getStdError());
+                return false;
+            } else {
+                String[] potentialFiles = stdOutReader.getStdOut().split("\n");
+                if (potentialFiles.length > 1) {
+                    logger.info("More than one file matching to given path " + filePath);
+                    return false;
+                } else if (potentialFiles.length == 0) {
+                    logger.info("No file found for given path " + filePath);
+                    return false;
+                } else {
+                    if (potentialFiles[0].trim().equals(new File(filePath).getName())) {
+                        return true;
+                    } else {
+                        logger.info("Returned file name " + potentialFiles[0].trim() + " does not match with given name " + new File(filePath).getName());
+                        return false;
+                    }
+                }
+            }
+        } catch (JSchException e) {
+            logger.error("Unable to retrieve command output. Command - " + command +
+                    " on server - " + session.getHost() + ":" + session.getPort() +
+                    " connecting user name - "
+                    + session.getUserName(), e);
+            throw new AgentException("Unable to retrieve command output. Command - " + command +
+                    " on server - " + session.getHost() + ":" + session.getPort() +
+                    " connecting user name - "
+                    + session.getUserName(), e);
+        } catch (IOException e) {
+            logger.error("Error while handling streams", e);
+            throw new AgentException("Error while handling streams", e);
+        } finally {
+            if (channelExec != null) {
+                channelExec.disconnect();
+            }
+        }
+    }
+
+    @Override
     public List<String> getFileNameFromExtension(String fileName, String parentPath) throws AgentException {
         throw new AgentException("Operation not implemented");
     }

-- 
To stop receiving notification emails like this one, please contact
dimuthuupe@apache.org.