forked from dougmoscrop/serverless-plugin-split-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-stacks.js
100 lines (82 loc) · 3.35 KB
/
split-stacks.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
'use strict';
const _ = require('lodash');
const semver = require('semver');
const setDeploymentBucketEndpoint = require('./lib/deployment-bucket-endpoint');
const migrateExistingResources = require('./lib/migrate-existing-resources');
const migrateNewResources = require('./lib/migrate-new-resources');
const replaceReferences = require('./lib/replace-references');
const replaceConditions = require('./lib/replace-conditions');
const replaceOutputs = require('./lib/replace-outputs');
const mergeStackResources = require('./lib/merge-stack-resources');
const sequenceStacks = require('./lib/sequence-stacks');
const writeNestedStacks = require('./lib/write-nested-stacks');
const logSummary = require('./lib/log-summary');
const utils = require('./lib/utils');
class ServerlessPluginSplitStacks {
constructor(serverless, options) {
if (!semver.satisfies(serverless.version, '>= 1.13')) {
throw new Error('serverless-plugin-split-stacks requires serverless 1.13 or higher!');
}
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
this.hooks = {
'after:aws:package:finalize:mergeCustomProviderResources': this.split.bind(this),
'aws:deploy:deploy:uploadArtifacts': this.upload.bind(this)
};
Object.assign(this,
utils,
{ setDeploymentBucketEndpoint },
{ migrateExistingResources },
{ migrateNewResources },
{ replaceReferences },
{ replaceConditions },
{ replaceOutputs },
{ mergeStackResources },
{ sequenceStacks },
{ writeNestedStacks },
{ logSummary }
);
const custom = this.serverless.service.custom || {};
this.config = custom.splitStacks || {};
this.stacksMap = ServerlessPluginSplitStacks.stacksMap;
}
split() {
this.rootTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;
this.resourcesById = Object.assign({}, this.rootTemplate.Resources);
this.resourceMigrations = {};
return Promise.resolve()
.then(() => this.setDeploymentBucketEndpoint())
.then(() => this.migrateExistingResources())
.then(() => this.migrateNewResources())
.then(() => this.replaceReferences())
.then(() => this.replaceOutputs())
.then(() => this.replaceConditions())
.then(() => this.mergeStackResources())
.then(() => this.sequenceStacks())
.then(() => this.writeNestedStacks())
.then(() => this.logSummary());
}
upload() {
const deploymentBucketObject = this.serverless.service.provider.deploymentBucketObject;
return this.provider.getServerlessDeploymentBucketName(this.options.stage, this.options.region)
.then(deploymentBucket => {
const files = this.getNestedStackFiles();
return Promise.all(_.map(files, file => {
const params = {
Bucket: deploymentBucket,
Key: file.key,
Body: file.createReadStream(),
ContentType: 'application/json',
};
if (deploymentBucketObject) {
const encryptionParams = this.getEncryptionParams(deploymentBucketObject);
Object.assign(params, encryptionParams);
}
return this.provider.request('S3', 'putObject', params);
}));
});
}
}
module.exports = ServerlessPluginSplitStacks;
module.exports.stacksMap = {}; // legacy, will be removed