-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackConfigPlugin.ts
More file actions
executable file
·134 lines (123 loc) · 3.54 KB
/
StackConfigPlugin.ts
File metadata and controls
executable file
·134 lines (123 loc) · 3.54 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
import * as AWS from 'aws-sdk';
import * as BPromise from 'bluebird';
import CLI from 'serverless/lib/classes/CLI';
import { getCloudFormationInstance, getS3Instance } from './lib/AWS';
import backupConfig from './lib/backupConfig';
import download from './lib/download';
import getValues from './lib/getValues';
import putValues from './lib/putValues';
import validate from './lib/validate';
import {
IBackupConfig,
IPluginConfig,
IServerless,
IStackConfig,
IStackConfigCommands,
IStackConfigHooks,
IStackConfigOptions,
} from './types';
export default class StackConfigPlugin implements IStackConfig {
private serverless: IServerless;
private logger: CLI;
private options: IStackConfigOptions;
private service: any;
private backup: IBackupConfig | boolean;
private config: IPluginConfig;
private commands: IStackConfigCommands;
private hooks: IStackConfigHooks;
private getS3Instance;
private getCloudFormationInstance;
/**
* Create a new instance.
*
* @param {Object} serverless the Serverless instance
* @param {Object} options passed in options
*/
constructor(serverless: IServerless, options: IStackConfigOptions) {
this.serverless = serverless;
this.logger = this.serverless.cli;
this.options = options;
this.service = this.serverless.service;
this.backup = false;
if (this.service.custom) {
this.config = this.service.custom['stack-config'] || {};
}
const credentials = new AWS.SharedIniFileCredentials({ profile: this.serverless.processedInput.options.profile });
AWS.config.credentials = credentials;
this.commands = this.defineCommands();
this.hooks = this.defineHooks();
this.getS3Instance = getS3Instance;
this.getCloudFormationInstance = getCloudFormationInstance;
}
/**
* Define plugin commands.
*
* @returns {IStackConfigCommands}
*/
public defineCommands(): IStackConfigCommands {
const commonOptions = {
path: {
required: false,
shortcut: 'p',
usage:
'Specify the location of the `stack-outputs.json` file ' + '(e.g. "--path .serverless or -p .serverless)',
},
profile: {
shortcut: 'p',
usage: 'AWS profile name',
},
region: {
shortcut: 'r',
usage: 'Region of the service',
},
stage: {
shortcut: 's',
usage: 'Stage of the service',
},
verbose: {
shortcut: 'v',
usage: 'Show all stack events during deployment',
},
};
return {
outputs: {
commands: {
download: {
lifecycleEvents: ['validate', 'download'],
options: commonOptions,
usage: 'Download combined config file',
},
},
lifecycleEvents: ['validate', 'getValues', 'putValues', 'backup'],
options: commonOptions,
usage: 'Save stack Outputs to file',
},
};
}
/**
* Define plugin hooks.
*
* @returns {IStackConfigHooks}
*/
public defineHooks(): IStackConfigHooks {
return {
'after:deploy:deploy': () =>
BPromise.bind(this)
.then(validate)
.then(getValues)
.then(putValues)
.then(backupConfig),
'outputs:getValues': () =>
BPromise.bind(this)
.then(validate)
.then(getValues)
.then(putValues)
.then(backupConfig),
// tslint:disable-next-line:object-literal-sort-keys
'outputs:download:download': () =>
BPromise.bind(this)
.then(validate)
.then(download),
};
}
}