-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlola.js
More file actions
executable file
·244 lines (217 loc) · 8.1 KB
/
lola.js
File metadata and controls
executable file
·244 lines (217 loc) · 8.1 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env node
import { program } from 'commander';
import chalk from 'chalk';
import Logging from './src/logging.js';
import Config from './src/config.js';
import Options from './src/options.js';
import Commands from './src/commands.js';
// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
console.log('\n\nOperation cancelled by user');
process.exit(0);
});
// Handle uncaught promise rejections
process.on('unhandledRejection', (reason, promise) => {
if (reason && reason.message && reason.message.includes('User force closed')) {
console.log('\n\nOperation cancelled by user');
process.exit(0);
}
console.error('Unhandled promise rejection:', reason);
process.exit(1);
});
const start = async (command) => {
Logging.logIfVerbose('Reading config file', program.verbose);
let config = {};
try {
// Read config file (not optional)
Logging.logIfVerbose('Reading config', program.verbose);
config = await Config.readConfigFile(program.configFile);
Logging.logIfVerbose(config, program.verbose);
} catch (err) {
Logging.logError('Config', err);
process.exit(1);
}
try {
// Validate (and add stuff to) config.
Logging.logIfVerbose('Validating config', program.verbose);
config = await Config.validateConfig(config);
} catch (err) {
Logging.logError('Config', err);
process.exit(1);
}
let options = {};
try {
const programOpts = program.opts();
// Read options file (optional).
Logging.logIfVerbose('Reading options', programOpts.verbose);
options = await Options.readOptionsFile(programOpts.optionsFile);
if (programOpts.optionsStack) {
if (Object.keys(config.stacks).indexOf(programOpts.optionsStack) === -1) {
Logging.logError('Stack not found in config file', programOpts.optionsStack);
process.exit(1);
}
options.stacks = [programOpts.optionsStack];
}
if (programOpts.optionsEnvironment) {
if (Object.keys(config.environments).indexOf(programOpts.optionsEnvironment) === -1) {
Logging.logError('Env not found in config file', programOpts.optionsEnvironment);
process.exit(1);
}
options.environments = [programOpts.optionsEnvironment];
}
Logging.logIfVerbose(options, programOpts.verbose);
// Validate (and add stuff to) options.
Logging.logIfVerbose('Validating options', programOpts.verbose);
options = await Options.validateOptions(options, config);
} catch (err) {
Logging.logError('Options', err);
// If validation fails or is interrupted, exit gracefully
if (err.message && err.message.includes('User force closed')) {
process.exit(0);
}
process.exit(1);
}
// Check if options are valid before proceeding
if (!options || !options.stacks || !options.environments) {
Logging.logError('Options', 'Invalid options configuration');
process.exit(1);
}
// Use for...of instead of forEach for proper async handling
for (const stackName of options.stacks) {
for (const env of options.environments) {
// Run said action.
const commands = new Commands(config, stackName, env);
const fullStackName = commands.getFullStackName();
// Banner.
Logging.log(`${chalk.green(`${command.toUpperCase()}`)}: ${chalk.yellow.bold(`${fullStackName}`)}`);
switch (command) {
default:
Logging.logError('Unknown command', command);
break;
case 'validate':
try {
await commands.runValidate();
Logging.logOk(`Template ${fullStackName}`, 'Template is valid');
} catch (error) {
Logging.logError(`Template ${fullStackName}`, error.message);
}
break;
case 'status':
try {
const output = await commands.runStatus();
Logging.logOk(`Status ${fullStackName}`, 'Stack found');
Logging.logOk(`Status ${fullStackName}`, output, true);
} catch (error) {
Logging.logError(`Status ${fullStackName}`, error.message);
}
break;
case 'deploy':
try {
await commands.runDeploy();
Logging.logOk(`Deploy ${fullStackName}`, 'Done');
} catch (error) {
Logging.logError(`Deploy ${fullStackName}`, error.message);
}
break;
case 'delete':
try {
await commands.runDelete();
Logging.logOk(`Delete ${fullStackName}`, 'Done');
} catch (error) {
Logging.logError(`Delete ${fullStackName}`, error.message);
}
break;
case 'action':
try {
await commands.runAction();
Logging.logOk(`Running action on ${fullStackName}`, 'Done');
} catch (error) {
Logging.logError(`Error running action on ${fullStackName}`, error.message);
}
break;
case 'protection':
try {
const feedback = await commands.runTerminationProtection();
Logging.logOk(`Running protection on ${fullStackName}`, feedback);
} catch (error) {
Logging.logError(`Error running protection on ${fullStackName}`, error.message);
}
break;
case 'changeSet':
try {
const output = await commands.runChangeSet();
Logging.logOk(`ChangeSet ${fullStackName}`, output, true);
} catch (error) {
Logging.logError(`ChangeSet ${fullStackName}`, error.message);
}
break;
}
}
}
};
program
.version('2.0.1')
.description('Reads a config file (lola.yml) and acts on the AWS Cloudformation stacks defined in that file')
.option('-c, --config-file <configFile>', 'Optional config file')
.option('-o, --options-file <optionsFile>', 'Optional deploy options file')
.option('-v, --verbose', 'Verbose output')
.option('-s, --options-stack <optionsStack>', 'Stack')
.option('-e, --options-environment <optionsEnvironment>', 'Environment');
program
.command('validate')
.alias('v')
.description('Validates a stack')
.action(() => {
start('validate');
});
program
.command('status')
.alias('s')
.description('Get the status of a stack')
.action(() => {
start('status');
});
program
.command('deploy')
.alias('d')
.description('Deploys a stack')
.action(() => {
start('deploy');
});
program
.command('delete')
.alias('x')
.description('Deletes a stack')
.action(() => {
start('delete');
});
program
.command('action')
.alias('a')
.description('Runs an action on a stack/env')
.action(() => {
start('action');
});
program
.command('protection')
.alias('p')
.description('Toggles termination protection on a stack/env')
.action(() => {
start('protection');
});
program
.command('changeSet')
.alias('c')
.description('Create and view changeset of a stack/env')
.action(() => {
start('changeSet');
});
const lola = async () => {
// Require a command.
if (process.argv[2] === undefined) {
program.outputHelp();
process.exit(1);
}
program.parse(process.argv);
};
export default lola;