-
Notifications
You must be signed in to change notification settings - Fork 103
/
message.js
196 lines (175 loc) · 6.23 KB
/
message.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const get = require('lodash.get');
const isObject = require('lodash.isobject');
const isString = require('lodash.isstring');
const omit = require('lodash.omit');
const { deprecate } = require('@cumulus/common/util');
/**
* Because both kes and message adapter use Mustache for templating,
* we add curly brackets to items that are using the [$] and {$} syntax
* to produce {{$}} and {[$]}
*
* @param {Object} cumulusConfig - the CumulusConfig portion of a task definition
* @returns {Object} updated CumulusConfig
*/
function fixCumulusMessageSyntax(cumulusConfig) {
if (!cumulusConfig) return {};
deprecate('CumulusConfig', '1.15.0', 'AWS Parameters with task_config');
const test = new RegExp('^([\\{]{1}|[\\[]{1})(\\$.*)([\\]]{1}|[\\}]{1})$');
Object.keys(cumulusConfig).forEach((n) => {
if (isObject(cumulusConfig[n])) {
// eslint-disable-next-line no-param-reassign
cumulusConfig[n] = fixCumulusMessageSyntax(cumulusConfig[n]);
} else if (isString(cumulusConfig[n])) {
const match = cumulusConfig[n].match(test);
if (match) {
// eslint-disable-next-line no-param-reassign
cumulusConfig[n] = `{${match[0]}}`;
}
}
});
return cumulusConfig;
}
/**
* Extracts Cumulus Configuration from each Step Function Workflow
* and returns it as a separate object
*
* @param {Object} config - Kes config object
* @returns {Object} updated kes config object
*/
function extractCumulusConfigFromSF(config) {
const workflowConfigs = {};
// loop through the message adapter config of each step of
// the step function, add curly brackets to values
// with dollar sign and remove config key from the
// definition, otherwise CloudFormation will be mad
// at us.
Object.keys(config.stepFunctions).forEach((name) => {
const sf = config.stepFunctions[name];
workflowConfigs[name] = {};
Object.keys(sf.States).forEach((n) => {
workflowConfigs[name][n] = fixCumulusMessageSyntax(sf.States[n].CumulusConfig);
sf.States[n] = omit(sf.States[n], ['CumulusConfig']);
});
// eslint-disable-next-line no-param-reassign
config.stepFunctions[name] = sf;
});
// eslint-disable-next-line no-param-reassign
config.workflowConfigs = workflowConfigs;
if (Object.keys(workflowConfigs)) deprecate('CumulusConfig', '1.15.0', 'AWS Parameters with task_config');
return config;
}
/**
* Returns the OutputValue of a CloudFormation Output
*
* @param {Object} outputs - list of CloudFormation Outputs
* @param {string} key - the key to return the value of
*
* @returns {string} the output value
*/
function findOutputValue(outputs, key) {
const output = outputs.find((o) => (o.OutputKey === key));
if (output) return output.OutputValue;
return undefined;
}
/**
* Generates a universal Cumulus Message template for a Cumulus Workflow
*
* @param {Object} config - Kes config object
* @param {Array} outputs - an list of CloudFormation outputs
*
* @returns {Object} a Cumulus Message template
*/
function generateWorkflowTemplate(config, outputs) {
// get cmr password from outputs
const cmrPassword = findOutputValue(outputs, 'EncryptedCmrPassword');
const cmr = Object.assign({}, config.cmr, { password: cmrPassword });
// get launchpad passphrase from outputs
const launchpadPassphrase = findOutputValue(outputs, 'EncryptedLaunchpadPassphrase');
const launchpad = Object.assign({}, config.launchpad, { passphrase: launchpadPassphrase });
const bucket = get(config, 'system_bucket');
// add queues
const queues = {};
const queueExecutionLimits = {};
if (config.sqs) {
const queueArns = outputs.filter((o) => o.OutputKey.includes('SQSOutput'));
queueArns.forEach((queue) => {
const queueName = queue.OutputKey.replace('SQSOutput', '');
const queueUrl = queue.OutputValue;
queues[queueName] = queueUrl;
const maxExecutions = get(config.sqs, `${queueName}.maxExecutions`);
if (maxExecutions) {
queueExecutionLimits[queueName] = maxExecutions;
}
});
}
const template = {
cumulus_meta: {
message_source: 'sfn',
system_bucket: bucket,
state_machine: null,
execution_name: null,
workflow_start_time: null
},
meta: {
workflow_name: null,
workflow_tasks: {},
stack: config.stackName,
buckets: config.buckets,
cmr,
launchpad,
distribution_endpoint: config.distribution_endpoint,
collection: {},
provider: {},
template: `s3://${bucket}/${config.stack}/workflow_template.json`,
queues,
queueExecutionLimits
},
payload: {},
exception: null
};
return template;
}
/**
* Generate a Cumulus Message templates for all the workflows
* in the stack and upload to s3
*
* @param {Object} config - Kes config object
* @param {Array} outputs - an list of CloudFormation outputs
* @param {function} uploader - an uploader function
*
* @returns {Promise} undefined
*/
async function generateTemplates(config, outputs, uploader) {
// this function only works if there are step functions defined in the deployment
if (config.stepFunctions) {
const bucket = config.system_bucket;
const stack = config.stackName;
// generate workflow message template and upload it to s3.
const template = generateWorkflowTemplate(config, outputs);
console.log('Uploading Cumulus Universal Workflow Message Template ...');
const key = `${stack}/workflow_template.json`;
await uploader(bucket, key, JSON.stringify(template));
// generate list of workflows and upload it to S3
// this is used by the /workflows endpoint of the API to return list
// of existing workflows
const workflowUploads = Object.keys(config.stepFunctions).map((name) => {
const arn = findOutputValue(outputs, `${name}StateMachine`);
return uploader(bucket, `${stack}/workflows/${name}.json`, JSON.stringify({
name,
arn,
definition: config.stepFunctions[name]
}));
});
await Promise.all(workflowUploads);
// upload the buckets config
await uploader(bucket, `${stack}/workflows/buckets.json`, JSON.stringify(config.buckets));
}
}
module.exports = {
fixCumulusMessageSyntax,
extractCumulusConfigFromSF,
findOutputValue,
generateWorkflowTemplate,
generateTemplates
};