Task Name
|
UCSD - SFTP task with credential policy
|
Description
|
|
Prerequisites
|
Validated on UCSD 6.6
|
Category
|
Workflow Task
|
Components
|
|
User Inputs
|
|
Output
|
Result of operation
|
Installation
- Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
- Unzip the file on your computer. Should end up with a .WFDX file.
- Log in to UCS Director as a user that has "system-admin" privileges.
- Navigate to "Policies-->Orchestration" and click on "Import".
- Click "Browse" and navigate to the location on your computer where the .WFD file resides. Choose the .WFD file and click "Open".
- Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
- Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
- Click "Submit".
- A new folder should appear in "Policies-->Orchestration" that contains the imported workflow. You will now need to update the included tasks with information about the specific environment.
A big thank you for this contribution goes out to: Sabina Al Farah
Workflow example:
importPackage(java.io);
importPackage(com.jcraft.jsch);
importPackage(com.cloupia.lib.connector.account.credential);
//var host = "10.105.XX.XX";
//var localFile = "/root/hello/mytest.text";
//var remoteDir = "/root/ABC/TEST123";
//var credentialPolicy = "Name of policy";
//file will be copied from "/root/hello/mytest.text on UCSD host to "/root/ABC/TEST123/mytest.text on remotehost
var host = input.host;
var credentialPolicy = input.credentialPolicy;
var localFile = input.localFile;
var remoteDir = input.remoteDir;
var policy = PersistenceUtil.getCredentialPolicyByName(credentialPolicy);
var password = policy.getPassword();
var user = policy.getUserName();
var port = policy.getPort();
uploadFileSFTP();
//========================
function uploadFileSFTP() {
var success = false;
var session = null;
var channel = null;
try {
var ssh = new JSch();
session = ssh.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel("sftp");
channel.connect();
logger.addInfo("SFTP connection to " + host + " established ");
var sftp = channel;
var path = "";
var paths = remoteDir.split("/");
for (i = 0; i < paths.length; i++) {
if (!paths[i].equals("")) {
path = path + "/" + paths[i];
logger.addInfo("Create Remote Directory " + path);
try {
sftp.stat(path);
} catch (e) {
// dir does not exist.
sftp.mkdir(path);
}
}
}
var localPaths = localFile.split("/");
var fileName = localPaths[localPaths.length - 1];
logger.addInfo("Put localFile " + localFile + " into remoteDir " + remoteDir + "/" + fileName);
sftp.put(localFile, remoteDir + "/" + fileName);
success = true;
logger.addInfo("File upload successful !! ");
} catch (e) {
logger.addError("Error: " + e.message);
} finally {
if (channel !== null) {
channel.disconnect();
}
if (sftp !== null) {
sftp.disconnect();
}
if (session !== null) {
session.disconnect();
}
}
if (!success) {
ctxt.setFailed("Unable to transfer the file!");
ctxt.exit();
}
}