-
Notifications
You must be signed in to change notification settings - Fork 1
/
opsworks-cli.js
374 lines (317 loc) · 11.6 KB
/
opsworks-cli.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Third-Party Dependencies
var app = require('commander');
var AWS = require('aws-sdk');
var exec_sh = require('exec-sh');
// Node Stuff
var fs = require('fs');
// Internal Dependencies
var config = require('./config.json');
var out = require('./lib/out');
var util = require('./lib/util')
var fetcher = require('./lib/fetcher');
var AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
var AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
if(AWS_ACCESS_KEY_ID === undefined || AWS_SECRET_ACCESS_KEY === undefined){
console.log("AWS credentials must be set");
process.exit(1);
}
var awsOptions = { "accessKeyId": AWS_ACCESS_KEY_ID, "secretAccessKey": AWS_SECRET_ACCESS_KEY, "region": "us-east-1" };
AWS.config.update(awsOptions);
var opsworks = new AWS.OpsWorks();
app.version(require('./package.json').version);
app.command('describe [stack]')
.description('List the layers in a stack')
.action(function(stack, options){
fetcher.getStackId({StackName:stack}, function(StackId){
opsworks.describeLayers({StackId:StackId}, function(error, data){
if(error){
console.log(error);
}
else{
data.Layers.forEach(function(layer, index, layers){
out.layerOverview(layer);
});
}
});
});
});
app.command('list [stack] [layer]')
.description('List the instances in a layer')
.action(function(stack, layer, options){
fetcher.getLayerId({StackName:stack, LayerName:layer}, function(StackId, LayerId){
if(LayerId==null){
console.log('Layer ' + layer + ' not found');
process.exit(1);
}
opsworks.describeInstances({LayerId:LayerId}, function(error, data){
if(error){
console.log(error);
}
else{
data.Instances.forEach(function(instance, index, instances){
out.instanceOverview(instance);
});
}
});
});
});
app.command('ssh [stack] [layer]')
.description('Log into a single instance or an entire layer')
.option('-i, --identity <identity>', 'The location of the key to use')
.option('-h, --hostname [hostname]', 'The hostname of a single instance to log in to')
.action(function(stack, layer, options){
if(typeof options.hostname != 'undefined'){
}
console.log('Creating an SSH connection to all instances on %s::%s', stack, layer);
fetcher.getLayerId({StackName:stack, LayerName:layer}, function(StackId, LayerId){
if(LayerId==null){
console.log('Layer ' + layer + ' not found');
process.exit(1);
}
opsworks.describeInstances({LayerId:LayerId}, function(error, data){
if(error){
console.log(error);
}
else{
var hosts = new Array();
for(var i=0; i<data.Instances.length; i++){
var instance = data.Instances[i];
if(instance.Status=='online'){
// Are we only looking for one instance and is this the one?
if(typeof options.hostname != 'undefined'){
//console.log("Hostname is " + options.hostname);
//console.log("Instance is " + instance.Hostname);
if(instance.Hostname === options.hostname){
hosts.push(data.Instances[i].PublicIp);
break;
}
}
else{
hosts.push(data.Instances[i].PublicIp);
}
}
}
//console.log("Reaching out to %s hosts", hosts.length);
// Start the options string with a space so it doesn't collide with the preceding argument
var sshOptionsString = " ";
var sshOpts = config.ssh.options;
if(sshOpts !== undefined){
for(var key in sshOpts){
if(sshOpts.hasOwnProperty(key)){
sshOptionsString += "-o "+key+"="+sshOpts[key];
}
}
}
// Get the default key from the config file
var keyToUse = config.ssh.identity;
// Override the default if supplied as a command-line argument
if(typeof options.identity != 'undefined'){
keyToUse = options.identity;
}
var sshCommand = __dirname+"/bin/ssh.sh -n " + layer + " -u ubuntu" + sshOptionsString + " -i " + keyToUse + " " + hosts.join(" ");
exec_sh(sshCommand);
}
});
});
});
app.command('config [key] [value]')
.description('Update settings in the OpsWorks config file')
.action(function(key, value, options){
if(key === undefined || value === undefined){
console.log('A key and value must be defined');
process.exit(1);
}
var keyPaths = key.split(".");
var walk = function(currentPath, next, valueToSet){
if(typeof currentPath[next] == 'undefined'){
currentPath[next] = {};
}
if(arguments.length==3){
currentPath[next] = valueToSet;
}
return currentPath[next];
};
var currentlyAt = config;
keyPaths.forEach(function(element, index, array){
console.log('Descending to ' + element);
if(index==array.length-1){
currentlyAt = walk(currentlyAt, element, value);
}
else{
currentlyAt = walk(currentlyAt, element);
}
});
//config[key] = value;
fs.writeFile(__dirname+'/config.json', JSON.stringify(config, null, '\t'), function(err){
if(err){
console.log('Config file could not be saved');
console.log(err.message);
}
else{
console.log('Config file updated');
}
});
});
app.command('add [stack] [layer]')
.description('Add one or more instances to a layer')
.option('--start', 'Starts the instance immediately.')
.option('-h, --hostname [hostname]', 'Supply the hostname to be used for the instance.')
.option('-p, --prefix [prefix]', 'Supply a prefix to be used for the instance hostname.', '')
.option('-k, --keypair [keypair]', 'Specify which key pair to use when logging into the instance.')
.option('-a, --ami [ami]', 'Specify a custom AMI to boot.')
.option('-s,--size [size]', 'Specify the size of the EC2 instance.', 'c1.medium')
.option('-c,--count [count]', 'Specify the number of EC2 instances to add', 1)
// Scaling Type not yet supported
//.option('--scaling-type [scaling-type]', 'Specify the scaling type of the instance (accepts \'timer\' or \'load\')')
.option('--availability-zone [availability-zone]', 'Specify the availability zone if the distribute flag has not been used')
.option('--distribute', 'Use this flag to automatically distribute nodes across all of the stack\'s availability zones')
.action(function(stack, layer, options){
// Is the instance size valid?
if(!util.validateInstanceType(options.size)){
console.log('%s is not a valid instance type.', options.size);
process.exit(1);
}
if(typeof options.scalingType != 'undefined'){
if(!util.validateAutoScalingType(options.scalingType)){
console.log('%s is not a valid scaling type', options.scalingType);
process.exit(1);
}
}
var startIn = function(count, zones, options){
fetcher.getLayerId({StackName:stack, LayerName:layer}, function(StackId, LayerId){
if(LayerId==null){
console.log('Layer ' + layer + ' not found');
process.exit(1);
}
for(var i=0; i < count; i++){
var AvailabilityZone = zones[i%zones.length];
options.hostname = options.prefix+'-'+i;
util.createInstance(StackId, LayerId, AvailabilityZone, options);
}
});
};
if(typeof options.availabilityZone != 'undefined'){
util.validateAvailabilityZone(options.availabilityZone, function(valid){
if(!valid){
console.log('%s is not a valid availability zone', options.availabilityZone);
process.exit(1);
}
else{
// Create Instance
console.log("Starting " + options.count + " in " + options.availabilityZone);
startIn(options.count, [options.availabilityZone], options);
}
});
}
else if(options.distribute){
console.log("Starting " + options.count + " in distributed mode");
fetcher.getAvailabilityZones(function(data){
var zones = new Array();
for(var i=0; i<data.AvailabilityZones.length; i++){
zones.push(data.AvailabilityZones[i].ZoneName);
}
startIn(options.count, zones, options);
});
}
else{
console.log('You must either use --distribute or assign an availability zone using --availability-zone');
process.exit(1);
}
});
app.command('stop [stack] [layer]')
.description('Stops an instance either by using Stack/Layer/Hostname')
.option('-p, --prefix [prefix]', 'Instances with a hostname starting with this prefix will be stopped')
.option('--all', 'All instances in this layer will be stopped', '')
.action(function(stack, layer, options){
if(!options.all && typeof options.prefix == 'undefined'){
console.log('Need to pass a hostname prefix ');
}
fetcher.getLayerId({StackName:stack, LayerName:layer}, function(StackId, LayerId){
if(LayerId==null){
console.log('Layer ' + layer + ' not found');
process.exit(1);
}
opsworks.describeInstances({LayerId:LayerId}, function(error, data){
if(error){
console.log(error);
}
else{
var hosts = new Array();
for(var i=0; i<data.Instances.length; i++){
var instance = data.Instances[i];
if(options.all || instance.Hostname.indexOf(options.prefix)===0){
console.log("Stopping %s", instance.Hostname);
opsworks.stopInstance({InstanceId:instance.InstanceId}, function(error, data){
if(error){
console.log('Failed to stop instance %s (%s)', instance.Hostname, instance.InstanceId);
process.exit(1);
}
});
}
}
}
});
});
});
app.command('delete [stack] [layer]')
.description('Deletes stopped instances in a layer by hostname prefix or an \'all\' flag')
.option('-p, --prefix [prefix]', 'Stopped instances with a hostname starting with this prefix will be deleted')
.option('--all-stopped', 'All stopped instances in this layer will be deleted', '')
.action(function(stack, layer, options){
if(!options.allStopped && typeof options.prefix == 'undefined'){
console.log('Need to pass a hostname prefix ');
}
fetcher.getLayerId({StackName:stack, LayerName:layer}, function(StackId, LayerId){
if(LayerId==null){
console.log('Layer ' + layer + ' not found');
process.exit(1);
}
opsworks.describeInstances({LayerId:LayerId}, function(error, data){
if(error){
console.log(error);
}
else{
var hosts = new Array();
for(var i=0; i<data.Instances.length; i++){
var instance = data.Instances[i];
if(instance.Status=="stopped" && (options.allStopped || instance.Hostname.indexOf(options.prefix)===0)){
console.log("Deleting %s", instance.Hostname);
opsworks.deleteInstance({InstanceId:instance.InstanceId}, function(error, data){
if(error){
console.log('Failed to delete instance %s (%s)', instance.Hostname, instance.InstanceId);
process.exit(1);
}
});
}
}
}
});
});
});
app.command('start [stack] [layer] [hostname]')
.description('Start an instance')
.action(function(stack, layer, hostname, options){
console.log('NOT IMPLEMENTED:\tStarting %s::%s::%s', stack, layer, hostname);
});
app.command('deploy [app] [stack] [layer]')
.description('Deploy an application to a layer.')
.action(function(app, stack, layer, options){
console.log('NOT IMPLEMENTED:\tDeploying %s on %s::%s', app, stack, layer);
});
app.command('undeploy [app] [stack] [layer]')
.description('Undeploys an application from a layer')
.action(function(app, stack, layer, options){
console.log('NOT IMPLEMENTED:\tUndeploying %s from %s::%s', app, stack, layer);
});
app.command('update-cookbooks [stack] [layer]')
.description('Updates the custom cookbooks within a layer')
.action(function(stack, layer, options){
console.log('NOT IMPLEMENTED:\tUpdating custom cookbooks in %s::%s', stack, layer);
});
app.command('exec-recipe [recipe] [stack] [layer]')
.description('Executes a recipe on instances within a layer')
.action(function(recipe, stack, layer, options){
console.log('NOT IMPLEMENTED:\tExecuting recipe %s on %s::%s', recipe, stack, layer);
});
// Process the arguments (This needs to happen after all of the commands are declared)
app.parse(process.argv);