-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathstackInformation.js
198 lines (159 loc) · 5.46 KB
/
stackInformation.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
197
198
/**
* Helper to retrieve and manage stack and alias information.
*/
const BbPromise = require('bluebird');
const _ = require('lodash');
module.exports = {
/**
* Load the currently deployed CloudFormation template.
*/
aliasStackLoadCurrentTemplate() {
const stackName = this._provider.naming.getStackName();
const params = {
StackName: stackName,
TemplateStage: 'Processed'
};
return this._provider.request('CloudFormation',
'getTemplate',
params)
.then(cfData => {
try {
return BbPromise.resolve(JSON.parse(cfData.TemplateBody));
} catch (e) {
return BbPromise.reject(new Error('Received malformed response from CloudFormation'));
}
})
.catch(() => {
return BbPromise.resolve({ Resources: {}, Outputs: {} });
});
},
aliasStackGetAliasStackNames() {
const params = {
ExportName: `${this._provider.naming.getStackName()}-ServerlessAliasReference`
};
return this._provider.request('CloudFormation',
'listImports',
params)
.then(cfData => BbPromise.resolve(cfData.Imports));
},
aliasStackLoadTemplate(stackName, processed) {
const params = {
StackName: stackName,
TemplateStage: processed ? 'Processed' : 'Original'
};
return this._provider.request('CloudFormation',
'getTemplate',
params)
.then(cfData => {
return BbPromise.resolve(JSON.parse(cfData.TemplateBody));
})
.catch(err => {
return BbPromise.reject(new Error(`Unable to retrieve template for ${stackName}: ${err.statusCode}`));
});
},
/**
* Load all deployed alias stack templates excluding the current alias.
*/
aliasStackLoadAliasTemplates() {
return this.aliasStackGetAliasStackNames() // eslint-disable-line lodash/prefer-lodash-method
.mapSeries(stack => BbPromise.join(BbPromise.resolve(stack), this.aliasStackLoadTemplate(stack)))
.map(stackInfo => ({ stack: stackInfo[0], template: stackInfo[1] }))
.catch(err => {
if (err.statusCode === 400) {
// The export is not yet there. Can happen on the very first alias stack deployment.
return BbPromise.resolve([]);
}
return BbPromise.reject(err);
});
},
aliasStacksDescribeStage() {
const stackName = this._provider.naming.getStackName();
return this._provider.request('CloudFormation',
'describeStackResources',
{ StackName: stackName });
},
aliasStacksDescribeResource(resourceId) {
const stackName = this._provider.naming.getStackName();
return this._provider.request('CloudFormation',
'describeStackResources',
{
StackName: stackName,
LogicalResourceId: resourceId
});
},
aliasStacksDescribeAliases() {
const params = {
ExportName: `${this._provider.naming.getStackName()}-ServerlessAliasReference`
};
return this._provider.request('CloudFormation',
'listImports',
params)
.then(cfData => BbPromise.resolve(cfData.Imports))
.mapSeries(stack => {
const describeParams = {
StackName: stack
};
return this._provider.request('CloudFormation',
'describeStackResources',
describeParams);
});
},
aliasGetExports() {
const fetchExports = (result, token) => {
const params = {};
if (token) {
params.NextToken = token;
}
return this._provider.request('CloudFormation', 'listExports', params)
.then(cfData => {
const newResult = _.reduce(cfData.Exports, (__, cfExport) => {
__[cfExport.Name] = cfExport.Value;
return __;
}, result);
if (cfData.NextToken) {
return fetchExports(newResult, cfData.NextToken);
}
return newResult;
});
};
return fetchExports({});
},
aliasStackLoadCurrentCFStackAndDependencies() {
return BbPromise.join(
BbPromise.bind(this).then(this.aliasStackLoadCurrentTemplate),
BbPromise.bind(this).then(this.aliasStackLoadAliasTemplates)
)
.spread((currentTemplate, aliasStackTemplates) => {
const removed = _.filter(aliasStackTemplates, ['stack', `${this._provider.naming.getStackName()}-${this._alias}`]);
const filteredAliasStackTemplates = _.reject(aliasStackTemplates, ['stack', `${this._provider.naming.getStackName()}-${this._alias}`]);
const currentAliasStackTemplate = _.get(_.first(removed), 'template', {});
const deployedAliasStackTemplates = _.map(filteredAliasStackTemplates, template => template.template);
this._serverless.service.provider.deployedCloudFormationTemplate = currentTemplate;
this._serverless.service.provider.deployedCloudFormationAliasTemplate = currentAliasStackTemplate;
this._serverless.service.provider.deployedAliasTemplates = filteredAliasStackTemplates;
return BbPromise.resolve([ currentTemplate, deployedAliasStackTemplates, currentAliasStackTemplate ]);
});
},
aliasDescribeAliasStack(aliasName) {
const stackName = `${this._provider.naming.getStackName()}-${aliasName}`;
return this._provider.request('CloudFormation',
'describeStackResources',
{ StackName: stackName });
},
aliasGetAliasFunctionVersions(aliasName) {
return this.aliasDescribeAliasStack(aliasName)
.then(resources => {
const versions = _.filter(resources.StackResources, [ 'ResourceType', 'AWS::Lambda::Version' ]);
return _.map(versions, version => ({
functionName: /:function:(.*):/.exec(version.PhysicalResourceId)[1],
functionVersion: _.last(_.split(version.PhysicalResourceId, ':'))
}));
});
},
aliasGetAliasLatestFunctionVersionByFunctionName(aliasName, functionName) {
return this._provider.request('Lambda',
'getAlias',
{ FunctionName: functionName, Name: aliasName })
.then(result => _.get(result, 'FunctionVersion', null));
},
};