-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
165 lines (135 loc) · 4.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
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
'use strict';
const randomstring = require('randomstring');
const chalk = require('chalk');
const fs = require('fs');
const https = require('https');
const proxy = require('proxy-agent');
class CloudfrontInvalidate {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.proxyURL =
process.env.proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy ||
process.env.HTTPS_PROXY ||
process.env.https_proxy;
this.provider = 'aws';
this.aws = this.serverless.getProvider('aws');
if (this.proxyURL) {
this.setProxy(this.proxyURL);
}
if (this.options.cacert) {
this.handleCaCert(this.options.cacert);
}
this.commands = {
cloudfrontInvalidate: {
usage: "Invalidate Cloudfront Cache",
lifecycleEvents: [
'invalidate'
]
}
};
this.hooks = {
'cloudfrontInvalidate:invalidate': this.invalidate.bind(this),
'after:deploy:deploy': this.afterDeploy.bind(this),
};
}
setProxy(proxyURL) {
this.aws.sdk.config.update({
httpOptions: { agent: proxy(proxyURL) },
});
}
handleCaCert(caCert) {
const cli = this.serverless.cli;
if (!fs.existsSync(caCert)) {
throw new Error("Supplied cacert option to a file that does not exist: " + caCert);
}
this.aws.sdk.config.update({
httpOptions: { agent: new https.Agent({ ca: fs.readFileSync(caCert) }) }
});
cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('ca cert handling enabled')}`);
}
createInvalidation(distributionId, reference, cloudfrontInvalidate) {
const cli = this.serverless.cli;
const cloudfrontInvalidateItems = cloudfrontInvalidate.items;
const params = {
DistributionId: distributionId, /* required */
InvalidationBatch: { /* required */
CallerReference: reference, /* required */
Paths: { /* required */
Quantity: cloudfrontInvalidateItems.length, /* required */
Items: cloudfrontInvalidateItems
}
}
};
return this.aws.request('CloudFront', 'createInvalidation', params).then(
() => {
cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('Invalidation started')}`);
},
err => {
cli.consoleLog(JSON.stringify(err));
cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('Invalidation failed')}`);
throw err;
}
);
}
invalidateElements(elements) {
const cli = this.serverless.cli;
if (this.options.noDeploy) {
cli.consoleLog('skipping invalidation due to noDeploy option');
return;
}
const invalidationPromises = elements.map(element => {
let cloudfrontInvalidate = element;
let reference = randomstring.generate(16);
let distributionId = cloudfrontInvalidate.distributionId;
let stage = cloudfrontInvalidate.stage;
if (stage !== undefined && stage !== `${this.serverless.service.provider.stage}`) {
return;
}
if (distributionId) {
cli.consoleLog(`DistributionId: ${chalk.yellow(distributionId)}`);
return this.createInvalidation(distributionId, reference, cloudfrontInvalidate);
}
if (!cloudfrontInvalidate.distributionIdKey) {
cli.consoleLog('distributionId or distributionIdKey is required');
return;
}
cli.consoleLog(`DistributionIdKey: ${chalk.yellow(cloudfrontInvalidate.distributionIdKey)}`);
// get the id from the output of stack.
const stackName = this.serverless.getProvider('aws').naming.getStackName()
return this.aws.request('CloudFormation', 'describeStacks', { StackName: stackName })
.then(result => {
if (result) {
const outputs = result.Stacks[0].Outputs;
outputs.forEach(output => {
if (output.OutputKey === cloudfrontInvalidate.distributionIdKey) {
distributionId = output.OutputValue;
}
});
}
})
.then(() => this.createInvalidation(distributionId, reference, cloudfrontInvalidate))
.catch(() => {
cli.consoleLog('Failed to get DistributionId from stack output. Please check your serverless template.');
});
});
return Promise.all(invalidationPromises);
}
afterDeploy() {
const elementsToInvalidate = this.serverless.service.custom.cloudfrontInvalidate
.filter((element) => {
if (element.autoInvalidate !== false) {
return true;
}
this.serverless.cli.consoleLog(`Will skip invalidation for the distributionId "${element.distributionId || element.distributionIdKey}" as autoInvalidate is set to false.`);
return false;
});
return this.invalidateElements(elementsToInvalidate);
}
invalidate() {
return this.invalidateElements(this.serverless.service.custom.cloudfrontInvalidate);
}
}
module.exports = CloudfrontInvalidate;