ExecutionContext context = invocationContext.getExecutionContext();
ExecutionModel model = context.getExecutionModel();
List<String> cmdList = new ArrayList<String>();
SSHClient ssh = new SSHClient();
try {
/*
* Notifier
*/
NotificationService notifier = context.getNotificationService();
/*
* Builder Command
*/
cmdList.add(context.getExecutionModel().getExecutable());
cmdList.addAll(context.getExecutionModel().getInputParameters());
// create process builder from command
String command = buildCommand(cmdList);
//redirect StdOut and StdErr
command += SPACE + "1>" + SPACE + model.getStdOut();
command += SPACE + "2>" + SPACE + model.getStderr();
// get the env of the host and the application
Map<String, String> nv = context.getExecutionModel().getEnv();
// extra env's
nv.put(GFacConstants.INPUT_DATA_DIR, context.getExecutionModel().getInputDataDir());
nv.put(GFacConstants.OUTPUT_DATA_DIR, context.getExecutionModel().getOutputDataDir());
// log info
log.info("Command = " + buildCommand(cmdList));
for (String key : nv.keySet()) {
log.info("Env[" + key + "] = " + nv.get(key));
}
// notify start
DurationObj compObj = notifier.computationStarted();
/*
* Create ssh connection
*/
ssh.loadKnownHosts();
ssh.connect(model.getHost());
ssh.authPublickey(privateKeyFilePath);
final Session session = ssh.startSession();
try {
/*
* Build working Directory
*/
log.info("WorkingDir = " + model.getWorkingDir());
session.exec("mkdir -p " + model.getWorkingDir());
session.exec("cd " + model.getWorkingDir());
/*
* Set environment
*/
for (String key : nv.keySet()) {
session.setEnvVar(key, nv.get(key));
}
/*
* Execute
*/
Command cmd = session.exec(command);
log.info("stdout=" + GfacUtils.readFromStream(session.getInputStream()));
cmd.join(5, TimeUnit.SECONDS);
// notify end
notifier.computationFinished(compObj);
/*
* check return value. usually not very helpful to draw conclusions
* based on return values so don't bother. just provide warning in
* the log messages
*/
if (cmd.getExitStatus() != 0) {
log.error("Process finished with non zero return value. Process may have failed");
} else {
log.info("Process finished with return value of zero.");
}
File logDir = new File("./service_logs");
if (!logDir.exists()) {
logDir.mkdir();
}
// Get the Stdouts and StdErrs
QName x = QName.valueOf(invocationContext.getServiceName());
String timeStampedServiceName = GfacUtils.createServiceDirName(x);
File localStdOutFile = new File(logDir, timeStampedServiceName + ".stdout");
File localStdErrFile = new File(logDir, timeStampedServiceName + ".stderr");
SCPFileTransfer fileTransfer = ssh.newSCPFileTransfer();
fileTransfer.download(model.getStdOut(), localStdOutFile.getAbsolutePath());
fileTransfer.download(model.getStderr(), localStdErrFile.getAbsolutePath());
context.getExecutionModel().setStdoutStr(GfacUtils.readFile(localStdOutFile.getAbsolutePath()));
context.getExecutionModel().setStderrStr(GfacUtils.readFile(localStdErrFile.getAbsolutePath()));
// set to context
OutputUtils.fillOutputFromStdout(invocationContext.getMessageContext("output"), context.getExecutionModel().getStdoutStr(), context.getExecutionModel().getStderrStr());
} catch (Exception e) {
throw e;
} finally {
try {
session.close();
} catch (Exception e) {
}
}
} catch (Exception e) {
throw new GfacException(e.getMessage(), e);
} finally {
try {
ssh.disconnect();
} catch (Exception e) {
}
}
}