-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.js
98 lines (86 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
const log = require('@cumulus/common/log');
const Task = require('@cumulus/common/task');
const aws = require('@cumulus/common/aws');
const promisify = require('util.promisify');
const FtpClient = require('ftp');
const SftpClient = require('sftpjs');
const pdrMod = require('./pdr');
const stringToStream = require('string-to-stream');
/**
* Task that retrieves PDRs from a SIPS server
* Input payload: none
* Output payload: An array of objects with keys `pdr_file_name`, `s3_bucket`, and `s3_key`
* representing the original file name and location in S3 of any newly downloaded PDRs
*/
module.exports = class DiscoverPdr extends Task {
/**
* Main task entry point
* @return An object referencing the oldest PDR on the server
*/
async run() {
// Vars needed from config to connect to the SIPS server
const { conn_type, host, port, username, password } =
this.message.provider.config.gateway_config.conn_config;
// The folder on the SIPS server holding the PDRS and the S3 bucket to which they should
// be copied
const { folder, bucket } = this.config;
const keyPrefix = `${this.config.key_prefix}/pdr`;
let client;
if (conn_type.toUpperCase() === 'FTP') {
client = new FtpClient();
}
else {
client = new SftpClient();
}
const clientReady = promisify(client.once).bind(client);
client.connect({
host: host,
port: port,
user: username,
password: password
});
await clientReady('ready');
let returnValue;
try {
// Get the list of PDRs
const pdrList = await pdrMod.getPdrList(client, folder, bucket, keyPrefix);
const S3UploadPromises = pdrList.map(async (pdrEntry) => {
const fileName = pdrEntry.name;
log.info(`FILE: ${fileName}`);
// Get the file contents
const pdr = await pdrMod.getPdr(client, folder, fileName);
log.debug('SUCCESSFULLY RETRIEVED PDR FROM SIPS SERVER');
// Write the contents out to S3
const s3PdrKey = `${keyPrefix}/${fileName}`;
const pdrStream = stringToStream(pdr.pdr);
await aws.uploadS3FileStream(pdrStream, bucket, s3PdrKey);
log.debug(`PDR stored at [${s3PdrKey} in S3 bucket [${bucket}]`);
return {
pdr_file_name: fileName,
s3_bucket: bucket,
s3_key: s3PdrKey
};
});
returnValue = await Promise.all(S3UploadPromises);
}
catch (e) {
log.error('Failed to download file');
log.error(e);
throw e;
}
finally {
// Close the connection to the SIPS server
client.end();
}
return returnValue;
}
/**
* Entry point for Lambda
* @param {array} args The arguments passed by AWS Lambda
* @return The handler return value
*/
static handler(...args) {
return DiscoverPdr.handle(...args);
}
};