forked from serverless-heaven/serverless-aws-alias
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
191 lines (170 loc) · 4.65 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
/**
* Serverless AWS alias plugin
*/
const BbPromise = require('bluebird')
, _ = require('lodash')
, Path = require('path')
, validate = require('./lib/validate')
, configureAliasStack = require('./lib/configureAliasStack')
, createAliasStack = require('./lib/createAliasStack')
, updateAliasStack = require('./lib/updateAliasStack')
, aliasRestructureStack = require('./lib/aliasRestructureStack')
, stackInformation = require('./lib/stackInformation')
, listAliases = require('./lib/listAliases')
, removeAlias = require('./lib/removeAlias')
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts');
class AwsAlias {
constructor(serverless, options) {
this._serverless = serverless;
this._options = options;
this._provider = this._serverless.getProvider('aws');
this._stage = this._options.stage || this._serverless.service.provider.stage;
this._alias = this._options.alias || this._stage;
this._stackName = this._provider.naming.getStackName();
// Make alias available as ${self:provider.alias}
this._serverless.service.provider.alias = this._alias;
/**
* Load stack helpers from Serverless installation.
*/
const monitorStack = require(
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'monitorStack')
);
const setBucketName = require(
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'setBucketName')
);
_.assign(
this,
validate,
configureAliasStack,
createAliasStack,
updateAliasStack,
listAliases,
removeAlias,
aliasRestructureStack,
stackInformation,
uploadAliasArtifacts,
setBucketName,
monitorStack
);
this._commands = {
alias: {
commands: {
deploy: {
usage: 'Internal use only',
lifecycleEvents: [
'validate',
'uploadArtifacts',
'updateAliasStack',
'done'
]
},
remove: {
usage: 'Remove a deployed alias',
lifecycleEvents: [
'remove'
],
options: {
alias: {
usage: 'Name of the alias',
shortcut: 'a',
required: true
},
verbose: {
usage: 'Enable verbose output',
shortcut: 'v',
required: false
}
}
}
}
}
};
this._hooks = {
'before:deploy:initialize': () => BbPromise.bind(this)
.then(this.validate),
// Create alias stack definition and modify base stack
'after:deploy:initialize': () => BbPromise.bind(this)
.then(this.configureAliasStack),
// Setup provider configuration reuses some of the functions of the AwsDeploy plugin
'after:deploy:setupProviderConfiguration': () => BbPromise.bind(this)
.then(this.createAliasStack),
'before:deploy:deploy': () => BbPromise.bind(this)
.then(this.aliasStackLoadCurrentCFStackAndDependencies)
.spread(this.aliasRestructureStack),
'after:deploy:deploy': () => BbPromise.bind(this)
.then(this.setBucketName)
.then(() => {
// Workaround for the missing functionality to hide commands
this._triggeredFromHook = true;
// Spawn alias:deploy lifecycle
return this._serverless.pluginManager.run(['alias', 'deploy']);
}),
'alias:deploy:validate': () => {
return this._triggeredFromHook ? BbPromise.resolve() : BbPromise.reject(new Error('Internal use only'));
},
'alias:deploy:uploadArtifacts': () => BbPromise.bind(this)
.then(this.uploadAliasArtifacts),
'alias:deploy:updateAliasStack': () => BbPromise.bind(this)
.then(this.updateAliasStack),
'alias:deploy:done': () => {
this._serverless.cli.log(`Successfully deployed alias ${this._alias}`);
return BbPromise.resolve();
},
'after:info:info': () => BbPromise.bind(this)
.then(this.listAliases),
'alias:remove:remove': () => BbPromise.bind(this)
.then(this.validate)
.then(this.aliasStackLoadCurrentCFStackAndDependencies)
.spread(this.removeAlias)
};
}
/**
* Expose the supported commands as read-only property.
*/
get commands() {
return this._commands;
}
/**
* Expose the supported hooks as read-only property.
*/
get hooks() {
return this._hooks;
}
/**
* Expose the options as read-only property.
*/
get options() {
return this._options;
}
/**
* Expose the supported provider as read-only property.
*/
get provider() {
return this._provider;
}
/**
* Expose the serverless object as read-only property.
*/
get serverless() {
return this._serverless;
}
/**
* Expose the stack name as read-only property.
*/
get stackName() {
return this._stackName;
}
_cleanup() {
this._serverless.cli.log('Cleanup !!!!');
}
}
module.exports = AwsAlias;