]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Improve SSH
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 10 Mar 2010 10:54:34 +0000 (10:54 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 10 Mar 2010 10:54:34 +0000 (10:54 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@3423 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java

index a4bff705297f65fec9d8b0328732fabf18e0dfe1..d07e6ab6cf4273924f87ab1ecf9e18088c9e648c 100644 (file)
@@ -2,6 +2,8 @@ package org.argeo.slc.jsch;
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -18,6 +20,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
+import org.argeo.slc.core.execution.ExecutionResources;
 import org.argeo.slc.core.execution.tasks.SystemCall;
 import org.springframework.core.io.Resource;
 import org.springframework.util.StringUtils;
@@ -42,6 +45,8 @@ public class RemoteExec extends AbstractJschTask {
        private Boolean forceShell = false;
        private Map<String, String> env = new HashMap<String, String>();
        private Resource stdIn = null;
+       private Resource stdOut = null;
+       private ExecutionResources executionResources;
 
        /**
         * If set, stdout is written to it as a list of lines. Cleared before each
@@ -204,7 +209,6 @@ public class RemoteExec extends AbstractJschTask {
                                log.debug("Run '" + command + "' on " + getSshTarget() + "...");
                        channel.connect();
 
-                       // write commands to shell
                        if (stdIn != null) {
                                Thread stdInThread = new Thread("Stdin " + getSshTarget()) {
                                        @Override
@@ -212,8 +216,7 @@ public class RemoteExec extends AbstractJschTask {
                                                OutputStream out = null;
                                                try {
                                                        out = channel.getOutputStream();
-                                                       IOUtils.copy(stdIn.getInputStream(), channel
-                                                                       .getOutputStream());
+                                                       IOUtils.copy(stdIn.getInputStream(), out);
                                                } catch (IOException e) {
                                                        throw new SlcException("Cannot write stdin on "
                                                                        + getSshTarget(), e);
@@ -257,28 +260,40 @@ public class RemoteExec extends AbstractJschTask {
        }
 
        protected void readStdOut(Channel channel) {
-               BufferedReader stdOut = null;
-               try {
-                       InputStream in = channel.getInputStream();
-                       stdOut = new BufferedReader(new InputStreamReader(in));
-                       String line = null;
-                       while ((line = stdOut.readLine()) != null) {
-                               if (!line.trim().equals("")) {
-                                       if (stdOutLines != null) {
-                                               stdOutLines.add(line);
-                                               if (logEvenIfStdOutLines && !quiet)
-                                                       log.info(line);
-                                       } else {
-                                               if (!quiet)
-                                                       log.info(line);
+               if (stdOut != null) {
+                       OutputStream localStdOut = createOutputStream(stdOut);
+                       try {
+                               IOUtils.copy(channel.getInputStream(), localStdOut);
+                       } catch (IOException e) {
+                               throw new SlcException("Cannot redirect stdout", e);
+                       } finally {
+                               IOUtils.closeQuietly(localStdOut);
+                       }
+               } else {
+                       BufferedReader stdOut = null;
+                       try {
+                               InputStream in = channel.getInputStream();
+                               stdOut = new BufferedReader(new InputStreamReader(in));
+                               String line = null;
+                               while ((line = stdOut.readLine()) != null) {
+                                       if (!line.trim().equals("")) {
+
+                                               if (stdOutLines != null) {
+                                                       stdOutLines.add(line);
+                                                       if (logEvenIfStdOutLines && !quiet)
+                                                               log.info(line);
+                                               } else {
+                                                       if (!quiet)
+                                                               log.info(line);
+                                               }
                                        }
                                }
+                       } catch (IOException e) {
+                               if (log.isDebugEnabled())
+                                       log.error("Cannot read stdout from " + getSshTarget(), e);
+                       } finally {
+                               IOUtils.closeQuietly(stdOut);
                        }
-               } catch (IOException e) {
-                       if (log.isDebugEnabled())
-                               log.error("Cannot read stdout from " + getSshTarget(), e);
-               } finally {
-                       IOUtils.closeQuietly(stdOut);
                }
        }
 
@@ -300,6 +315,23 @@ public class RemoteExec extends AbstractJschTask {
 
        }
 
+       protected OutputStream createOutputStream(Resource target) {
+               FileOutputStream out = null;
+               try {
+
+                       final File file;
+                       if (executionResources != null)
+                               file = new File(executionResources.getAsOsPath(target, true));
+                       else
+                               file = target.getFile();
+                       out = new FileOutputStream(file, false);
+               } catch (IOException e) {
+                       log.error("Cannot get file for " + target, e);
+                       IOUtils.closeQuietly(out);
+               }
+               return out;
+       }
+
        public void setCommand(String command) {
                this.command = command;
        }
@@ -360,4 +392,12 @@ public class RemoteExec extends AbstractJschTask {
                this.stdIn = stdIn;
        }
 
+       public void setStdOut(Resource stdOut) {
+               this.stdOut = stdOut;
+       }
+
+       public void setExecutionResources(ExecutionResources executionResources) {
+               this.executionResources = executionResources;
+       }
+
 }
index 69b8ba21c822ceeac4e6fb6158645985c89d6f29..5da5f79e7ba2511ee774f8e4b62498d17fb65b4e 100644 (file)
@@ -138,6 +138,8 @@ public class ScpTo extends AbstractJschTask {
                                        IOUtils.copy(in, out);
                                        arr = out.toByteArray();
                                        path = resource.getURL().getPath();
+                                       if (path.startsWith("/"))
+                                               path = path.substring(1);
                                }
                                ByteArrayInputStream content = new ByteArrayInputStream(arr);
                                upload(session, content, arr.length, path, resource.toString(),