forked from developmentseed/kes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (117 loc) · 3.89 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
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
'use strict';
const deprecate = require('deprecate');
const pLimit = require('p-limit');
const Kes = require('./src/kes');
const Config = require('./src/config');
const utils = require('./src/utils');
/**
* Builds templates nested in the main template
* using the specified config and cf file paths
*
* @param {object} config Kes config object
* @param {object} KesClass the Kes Class
* @param {object} options The options passed by the commander library
* @return {Promise} returns a promise of an updated Kes config object
*/
function buildNestedCfs(config, KesClass, options) {
const limit = pLimit(1);
if (config.nested_templates) {
const nested = config.nested_templates;
console.log('Nested templates are found!');
const ps = Object.keys(nested).map((name) => limit(() => {
console.log(`Compiling nested template for ${name}`);
const newOptions = Object.assign({}, options);
newOptions.cfFile = nested[name].cfFile;
newOptions.configFile = nested[name].configFile;
// no templates are used in nested stacks
delete newOptions.template;
delete newOptions.deployment;
// use the parent stackname
newOptions.stack = config.stack;
newOptions.parent = config;
const nestedConfig = new Config(newOptions);
// get the bucket name from the parent
if (!nestedConfig.bucket) {
nestedConfig.bucket = utils.getSystemBucket(config);
}
// add nested deployment name
nestedConfig.nested_cf_name = name;
const kes = new KesClass(nestedConfig);
return kes.uploadCF().then((uri) => {
config.nested_templates[name].url = uri;
});
}));
return Promise.all(ps)
.then(() => config)
.catch(utils.failure);
}
return Promise.resolve(config);
}
/**
* Builds, uploads and deploy a Cloudformation based on options passed
* from the commander library
*
* @param {object} options Options passed by the commander library
* @param {string} cmd the argument selected in the CLI, e.g. deploy, update, etc.
* @return {undefined}
*/
function buildCf(options, cmd) {
const KesClass = utils.determineKesClass(options, Kes);
let parentConfig;
try {
parentConfig = new Config(options);
}
catch (e) {
return Promise.reject(e);
}
return buildNestedCfs(parentConfig, KesClass, options).then((config) => {
const kes = new KesClass(config);
switch (cmd) {
case 'create':
deprecate('"kes cf create" command is deprecated. Use "kes cf deploy" instead');
return kes.createStack();
case 'update':
deprecate('"kes cf update" command is deprecated. Use "kes cf deploy" instead');
return kes.updateStack();
case 'upsert':
deprecate('"kes cf upsert" command is deprecated. Use "kes cf deploy" instead');
return kes.upsertStack();
case 'deploy':
return kes.deployStack();
case 'validate':
return kes.validateTemplate();
case 'compile':
return kes.compileCF();
case 'delete':
return kes.deleteStack();
default:
console.log('Wrong choice. Accepted arguments: [create|update|upsert|deploy|validate|compile]');
}
});
}
/**
* Builds and uploads a lambda function based on the options passed by the commander
* @param {object} options Options passed by the commander library
* @param {string} cmd the argument selected in the CLI, e.g. lambda name
* @return {undefined}
*/
function buildLambda(options, cmd) {
if (cmd) {
const KesClass = utils.determineKesClass(options, Kes);
const config = new Config(options);
const kes = new KesClass(config);
kes.updateSingleLambda(cmd).then(r => utils.success(r)).catch(e => utils.failure(e));
}
else {
utils.failure(new Error('Lambda name is missing'));
}
}
module.exports = {
Kes,
Config,
utils,
buildCf,
buildLambda,
Lambda: require('./src/lambda'),
local: require('./src/local')
};