OutputParameterType[] outputParametersArray = jobExecutionContext.getApplicationContext().
getServiceDescription().getType().getOutputParametersArray();
if(outputParametersArray != null) {
outParamName = outputParametersArray[0].getParameterName();
} else {
throw new GFacProviderException("Output parameter name is not set. Therefore, not being able " +
"to filter the job result from standard out ");
}
sshClient.connect(properties, new HostKeyVerification() {
public boolean verifyHost(String s, SshPublicKey sshPublicKey) throws TransportProtocolException {
log.debug("Verifying Host: " + s);
return true;
}
});
GFacUtils.updateApplicationJobStatus(jobExecutionContext, jobId, ApplicationJobStatus.AUTHENTICATE);
// Initialize the authentication data.
PublicKeyAuthenticationClient publicKeyAuth = new PublicKeyAuthenticationClient();
publicKeyAuth.setUsername(amazonSecurityContext.getUserName());
SshPrivateKeyFile file = SshPrivateKeyFile.
parse(new File(System.getProperty("user.home") + "/.ssh/" + KEY_PAIR_NAME));
SshPrivateKey privateKey = file.toPrivateKey("");
publicKeyAuth.setKey(privateKey);
// Authenticate
int result = sshClient.authenticate(publicKeyAuth);
if(result== AuthenticationProtocolState.FAILED) {
throw new GFacProviderException("The authentication failed");
} else if(result==AuthenticationProtocolState.PARTIAL) {
throw new GFacProviderException("The authentication succeeded but another"
+ "authentication is required");
} else if(result==AuthenticationProtocolState.COMPLETE) {
log.info("ssh client authentication is complete...");
}
GFacUtils.updateApplicationJobStatus(jobExecutionContext, jobId, ApplicationJobStatus.SUBMITTED);
SessionChannelClient session = sshClient.openSessionChannel();
log.info("ssh session successfully opened...");
session.requestPseudoTerminal("vt100", 80, 25, 0, 0, "");
session.startShell();
GFacUtils.updateApplicationJobStatus(jobExecutionContext, jobId, ApplicationJobStatus.EXECUTING);
session.getOutputStream().write(shellCmd.getBytes());
InputStream in = session.getInputStream();
byte buffer[] = new byte[255];
int read;
String executionResult = "";
while((read = in.read(buffer)) > 0) {
String out = new String(buffer, 0, read);
// System.out.println(out);
if(out.startsWith(outParamName)) {
executionResult = out.split("=")[1];
log.debug("Result found in the StandardOut ");
break;
}
}
GFacUtils.updateApplicationJobStatus(jobExecutionContext, jobId, ApplicationJobStatus.RESULTS_RETRIEVE);
executionResult = executionResult.replace("\r","").replace("\n","");
log.info("Result of the job : " + executionResult);
for(OutputParameterType outparamType : outputParametersArray){
/* Assuming that there is just a single result. If you want to add more results, update the necessary
logic below */
String paramName = outparamType.getParameterName();
ActualParameter outParam = new ActualParameter();
outParam.getType().changeType(StringParameterType.type);
((StringParameterType) outParam.getType()).setValue(executionResult);
jobExecutionContext.getOutMessageContext().addParameter(paramName, outParam);
}
GFacUtils.updateApplicationJobStatus(jobExecutionContext, jobId, ApplicationJobStatus.FINISHED);
} catch (InvalidSshKeyException e) {
throw new GFacProviderException("Invalid SSH key", e);
} catch (IOException e) {
throw new GFacProviderException("Error in occurred during IO", e);
} catch (Exception e) {
throw new GFacProviderException("Error parsing standard out for job execution result", e);
}
}