-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (141 loc) · 4.98 KB
/
index.js
File metadata and controls
161 lines (141 loc) · 4.98 KB
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
'use strict';
const chalk = require('chalk');
const cfnDiff = require('@aws-cdk/cloudformation-diff');
const fs = require('fs-extra');
const AWS = require('aws-sdk');
const { JSONPath } = require('jsonpath-plus');
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.commands = {
diff: {
usage: 'Compares new AWS CloudFormation templates against old ones',
lifecycleEvents: ['diff'],
},
};
this.hooks = {
'before:diff:diff': async () => {
const provider = this.serverless.service.provider.name;
if (!this.serverless.getProvider(provider)) {
const errorMessage = `The specified provider "${provider}" does not exist.`;
throw new Error(errorMessage, 'INVALID_PROVIDER');
}
if (!this.options.package && !this.serverless.service.package.path) {
await this.serverless.pluginManager.spawn('package');
}
},
'diff:diff': this.diffCmd.bind(this),
};
this.options.stage = this.options.stage
|| (this.serverless.service.defaults && this.serverless.service.defaults.stage)
|| 'dev';
this.options.region = this.options.region
|| (this.serverless.service.defaults && this.serverless.service.defaults.region)
|| 'us-east-1';
AWS.config.update({ region: this.options.region });
this.cloudFormation = new AWS.CloudFormation();
this.newTemplateFile = '.serverless/cloudformation-template-update-stack.json';
}
diffCmd() {
let stackName;
if (this.serverless.service.provider
&& typeof this.serverless.service.provider.stackName !== 'undefined'
&& this.serverless.service.provider.stackName !== '') {
stackName = this.serverless.service.provider.stackName;
} else {
stackName = `${this.serverless.service.service}-${this.options.stage}`;
}
if (this.serverless.service.provider
&& typeof this.serverless.service.provider.preV1Resources !== 'undefined'
&& this.serverless.service.provider.preV1Resources === true) {
stackName += '-r';
}
const params = {
StackName: stackName,
TemplateStage: 'Processed',
};
this.serverless.cli.log('Downloading currently deployed template');
const promise = this.cloudFormation.getTemplate(params).promise();
return promise.then(
(data) => {
const oldTemplate = JSON.parse(data.TemplateBody);
this.templateDiff(oldTemplate);
return Promise.resolve(oldTemplate);
},
(err) => {
if (err.code === 'ValidationError') {
const oldTemplate = {};
this.templateDiff(oldTemplate);
return Promise.resolve(oldTemplate);
}
return Promise.reject(err.message);
},
);
}
templateDiff(oldTemplate) {
const { newTemplateFile } = this;
this.serverless.cli.log('Running diff against deployed template');
return fs.stat(newTemplateFile)
.then(() => {
const newTemplate = JSON.parse(fs.readFileSync(newTemplateFile, 'utf8'));
const diff = cfnDiff.diffTemplate(oldTemplate, newTemplate);
if (!diff.isEmpty) {
const stream = process.stdout;
const config = this.serverless.service.custom;
const diffConfig = config && config.diff;
let tableWidth;
let excludes = [];
let reportPath;
if (diffConfig) {
tableWidth = diffConfig.tableWidth || 0;
excludes = diffConfig.excludes || [];
reportPath = diffConfig.reportPath;
}
tableWidth = process.env.DIFF_TABLE_WIDTH || tableWidth;
if (tableWidth) {
stream.columns = 80;
}
excludes.forEach((exclude) => {
const result = JSONPath({
resultType: 'all',
json: diff,
path: exclude,
});
result.forEach((res) => {
delete res.parent[res.parentProperty];
});
});
if (reportPath) {
const report = {
create: 0,
update: 0,
delete: 0,
};
Object.values(diff.resources.diffs).forEach((res) => {
if (res.isAddition) {
report.create += 1;
} else if (res.isRemoval) {
report.delete += 1;
} else {
report.update += 1;
}
});
fs.writeFile(diffConfig.reportPath, JSON.stringify(report, null, 4));
}
cfnDiff.formatDifferences(stream, diff);
} else {
console.log(chalk.green('There were no differences'));
}
return Promise.resolve(diff);
})
.catch((err) => {
if (err.code === 'ENOENT') {
const errorPrefix = `${newTemplateFile} could not be found`;
return Promise.reject(errorPrefix);
}
return Promise.reject(err);
});
}
}
module.exports = ServerlessPlugin;