ssh.connect(model.getHost());
// TODO how to authenticate with system
ssh.authPublickey(System.getProperty("user.name"));
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);