forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (102 loc) · 3.15 KB
/
index.js
File metadata and controls
121 lines (102 loc) · 3.15 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
'use strict';
const BbPromise = require('bluebird');
const validate = require('../lib/validate');
const fetch = require('node-fetch');
class AwsRollbackFunction {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.provider = this.serverless.getProvider('aws');
Object.assign(this, validate);
this.commands = {
rollback: {
commands: {
function: {
usage: 'Rollback the function to a specific version',
lifecycleEvents: [
'rollback',
],
options: {
function: {
usage: 'Name of the function',
shortcut: 'f',
required: true,
},
version: {
usage: 'Version of the function',
shortcut: 'v',
required: true,
},
stage: {
usage: 'Stage of the function',
shortcut: 's',
},
region: {
usage: 'Region of the function',
shortcut: 'r',
},
},
},
},
},
};
this.hooks = {
'rollback:function:rollback': () => BbPromise.bind(this)
.then(this.validate)
.then(this.getFunctionToBeRestored)
.then(this.fetchFunctionCode)
.then(this.restoreFunction),
};
}
getFunctionToBeRestored() {
const funcName = this.options.function;
let funcVersion = this.options.version;
// versions need to be string so that AWS understands it
funcVersion = String(this.options.version);
this.serverless.cli.log(`Rolling back function "${funcName}" to version "${funcVersion}"...`);
const funcObj = this.serverless.service.getFunction(funcName);
const params = {
FunctionName: funcObj.name,
Qualifier: funcVersion,
};
return this.provider.request(
'Lambda',
'getFunction',
params
)
.then((func) => func)
.catch((error) => {
if (error.message.match(/not found/)) {
const errorMessage = [
`Function "${funcName}" with version "${funcVersion}" not found.`,
` Please check if you've deployed "${funcName}"`,
` and version "${funcVersion}" is available for this function.`,
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
throw new Error(error.message);
});
}
fetchFunctionCode(func) {
const codeUrl = func.Code.Location;
return fetch(codeUrl).then((response) => response.buffer());
}
restoreFunction(zipBuffer) {
const funcName = this.options.function;
this.serverless.cli.log('Restoring function...');
const funcObj = this.serverless.service.getFunction(funcName);
const params = {
FunctionName: funcObj.name,
ZipFile: zipBuffer,
};
return this.provider.request(
'Lambda',
'updateFunctionCode',
params
).then(() => {
this.serverless.cli.log(`Successfully rolled back function "${this.options.function}"`);
});
}
}
module.exports = AwsRollbackFunction;