X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjsch%2FRemoteExec.java;h=1295209867bcc84bb6fdd6f72c0780c7c2701c7d;hb=c9672b7c16ee662ff452fff8405a67cac091a15e;hp=80b65139861587f32be5f4440b033b18dcfdab3d;hpb=e7ddf0b3a271a2f22108ee5b9fba69888f5c78ec;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java index 80b651398..129520986 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java @@ -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 commands = new ArrayList(); private String command; + private SystemCall systemCall; + private List systemCalls = new ArrayList(); + + 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 systemCalls) { + this.systemCalls = systemCalls; + } + }