-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
97 lines (88 loc) · 3.28 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
/**
* @author Miguel A. Calles, MBA <[email protected]>
*/
'use strict';
/**
* Class for the Serverless plugin.
*/
class StackTerminationProtection {
/**
*
* @param {*} serverless Serverless object.
* @param {*} options Options object.
*/
constructor(serverless, options, { log } = {}) {
this.serverless = serverless;
this.options = options;
this.hooks = {
// TODO: Add hook to disable protection prior to sls remove
'after:deploy:deploy': this.afterDeployDeploy.bind(this),
};
// TODO: Updating termination protection without deploying.
this.commands = {};
/* backward compatibility for Serverless V3 logging */
this.log = log ? log : {
debug: this.serverless.cli.log,
info: this.serverless.cli.log,
warning: this.serverless.cli.log,
};
}
/**
* Function for the 'after:deploy:deploy' hook.
* @return {Promise<void>}
*/
afterDeployDeploy() {
let protectedStages = [];
if (
this.serverless.service.custom &&
this.serverless.service.custom.serverlessTerminationProtection &&
this.serverless.service.custom.serverlessTerminationProtection.stages &&
Array.isArray(this.serverless.service.custom.serverlessTerminationProtection.stages)
) {
protectedStages = this.serverless.service.custom.serverlessTerminationProtection.stages;
}
this.provider = this.serverless.getProvider('aws');
const serviceName = this.serverless.service.getServiceName();
const stage = this.provider.getStage();
this.stackName = this.serverless.service.provider.stackName || `${serviceName}-${stage}`;
let isProtected = true;
// if disable option is set or stage is not included in predefined list, then set protection to false
if (
Boolean(this.options['disable-termination-protection']) ||
(this.options.param || []).includes('disable-termination-protection') ||
(protectedStages.length !== 0 && !protectedStages.includes(stage))
) {
isProtected = false;
}
return this.updateTerminationProtection(isProtected)
.then(() => this.log.info(
`${isProtected ? 'En' : 'Dis'}abled termination protection`,
))
.catch((err) => {
this.log.warning('Failed to update termination protection');
this.log.debug(JSON.stringify(err));
});
}
/**
* Update the AWS CloudFormation stack termination protection.
* @param {boolean} isEnabled
* @return {Promise<string | Error>}
*/
updateTerminationProtection(isEnabled) {
const params = {
EnableTerminationProtection: isEnabled,
StackName: this.stackName,
};
return new Promise((resolve, reject) => {
this.provider
.request(
'CloudFormation',
'updateTerminationProtection',
params
)
.then((data) => resolve(data.StackId))
.catch((err) => reject(err));
});
}
}
module.exports = StackTerminationProtection;