]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java
Improve JSCH support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / RemoteExec.java
index 80b65139861587f32be5f4440b033b18dcfdab3d..1295209867bcc84bb6fdd6f72c0780c7c2701c7d 100644 (file)
@@ -10,6 +10,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.tasks.SystemCall;
 
 import com.jcraft.jsch.Channel;
 import com.jcraft.jsch.ChannelExec;
@@ -18,11 +19,30 @@ import com.jcraft.jsch.Session;
 public class RemoteExec extends AbstractJschTask {
        private final static Log log = LogFactory.getLog(RemoteExec.class);
 
+       private Boolean failOnBadExitStatus = true;
+
        private List<String> commands = new ArrayList<String>();
        private String command;
+       private SystemCall systemCall;
+       private List<SystemCall> systemCalls = new ArrayList<SystemCall>();
+
+       public void run(Session session) {
+               // convert system calls
+               if (systemCall != null) {
+                       if (command != null)
+                               throw new SlcException("Cannot specify command AND systemCall");
+                       command = convertSystemCall(systemCall);
+               }
 
-       public void run() {
-               Session session = openSession();
+               if (systemCalls.size() != 0) {
+                       if (commands.size() != 0)
+                               throw new SlcException(
+                                               "Cannot specify commands AND systemCalls");
+                       for (SystemCall systemCall : systemCalls)
+                               commands.add(convertSystemCall(systemCall));
+               }
+
+               // execute command(s)
                if (command != null) {
                        if (commands.size() != 0)
                                throw new SlcException(
@@ -37,7 +57,12 @@ public class RemoteExec extends AbstractJschTask {
                                remoteExec(session, cmd);
                        }
                }
-               session.disconnect();
+       }
+
+       protected String convertSystemCall(SystemCall systemCall) {
+               // TODO: prepend environemnt variables
+               // TODO: deal with exec dir
+               return systemCall.asCommand();
        }
 
        protected void remoteExec(Session session, String command) {
@@ -52,16 +77,12 @@ public class RemoteExec extends AbstractJschTask {
                        // channel.setInputStream(System.in);
                        channel.setInputStream(null);
 
-                       // channel.setOutputStream(System.out);
-
-                       // FileOutputStream fos=new FileOutputStream("/tmp/stderr");
-                       // ((ChannelExec)channel).setErrStream(fos);
                        ((ChannelExec) channel).setErrStream(System.err);
 
                        InputStream in = channel.getInputStream();
 
                        if (log.isDebugEnabled())
-                               log.debug("Exec '" + command + "' on " + getSshTarget() + "...");
+                               log.debug("Run '" + command + "' on " + getSshTarget() + "...");
 
                        channel.connect();
 
@@ -70,17 +91,25 @@ public class RemoteExec extends AbstractJschTask {
                                execIn = new BufferedReader(new InputStreamReader(in));
                                String line = null;
                                while ((line = execIn.readLine()) != null) {
-                                       log.info(line);
+                                       if (!line.trim().equals(""))
+                                               log.info(line);
                                }
-                               // while (in.available() > 0) {
-                               // int i = in.read(tmp, 0, 1024);
-                               // if (i < 0)
-                               // break;
-                               // log.info(new String(tmp, 0, i));
-                               // }
+
                                if (channel.isClosed()) {
-                                       log.info("Remote execution exit status: "
-                                                       + channel.getExitStatus());
+                                       int exitStatus = channel.getExitStatus();
+                                       if (exitStatus == 0) {
+                                               if (log.isTraceEnabled())
+                                                       log.trace("Remote execution exit status: "
+                                                                       + exitStatus);
+                                       } else {
+                                               String msg = "Remote execution failed with "
+                                                               + " exit status: " + exitStatus;
+                                               if (failOnBadExitStatus)
+                                                       throw new SlcException(msg);
+                                               else
+                                                       log.error(msg);
+                                       }
+
                                        break;
                                }
                                try {
@@ -105,4 +134,16 @@ public class RemoteExec extends AbstractJschTask {
                this.commands = commands;
        }
 
+       public void setFailOnBadExitStatus(Boolean failOnBadExitStatus) {
+               this.failOnBadExitStatus = failOnBadExitStatus;
+       }
+
+       public void setSystemCall(SystemCall systemCall) {
+               this.systemCall = systemCall;
+       }
+
+       public void setSystemCalls(List<SystemCall> systemCalls) {
+               this.systemCalls = systemCalls;
+       }
+
 }