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 ", jobExecutionContext);
}
sshClient.connect(properties, new HostKeyVerification() {
public boolean verifyHost(String s, SshPublicKey sshPublicKey) throws TransportProtocolException {
log.debug("Verifying Host: " + s);
return true;
}
});
// Initialize the authentication data.
PublicKeyAuthenticationClient publicKeyAuth = new PublicKeyAuthenticationClient();
publicKeyAuth.setUsername("ec2-user");
SshPrivateKeyFile file = SshPrivateKeyFile.parse(new File(PRIVATE_KEY_FILE_PATH));
SshPrivateKey privateKey = file.toPrivateKey("");
publicKeyAuth.setKey(privateKey);
// Authenticate
int result = sshClient.authenticate(publicKeyAuth);
if(result== AuthenticationProtocolState.FAILED) {
throw new GFacProviderException("The authentication failed", jobExecutionContext);
} else if(result==AuthenticationProtocolState.PARTIAL) {
throw new GFacProviderException("The authentication succeeded but another"
+ "authentication is required", jobExecutionContext);
} else if(result==AuthenticationProtocolState.COMPLETE) {
log.info("ssh client authentication is complete...");
}
SessionChannelClient session = sshClient.openSessionChannel();
log.info("ssh session successfully opened...");
session.requestPseudoTerminal("vt100", 80, 25, 0, 0, "");
session.startShell();
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);
if(out.startsWith(outParamName)) {
executionResult = out.split("=")[1];
log.debug("Result found in the StandardOut ");
break;
}
}
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);
}
} 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);
}
}