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;
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(
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) {
// 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();
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 {
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;
+ }
+
}