Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch
javascript
into master
.
- Loading branch information
Showing
32 changed files
with
656 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env node | ||
|
||
var configuration, display, file, format, labels, $, rest, _ref; | ||
|
||
var fs = require("fs") | ||
, ec2 = require("ec2") | ||
, argv = process.argv.slice(2) | ||
, command = argv.shift() | ||
, parameters = {} | ||
, key | ||
; | ||
|
||
while (argv.length) { | ||
key = argv.shift(); | ||
if ($ = /^\s*\+(.*)/.exec(key)) format = $[1]; | ||
else parameters[key] = argv.shift(); | ||
} | ||
|
||
function build (fields, child) { | ||
return function(lines, line, context) { | ||
var copy, field, item, i, I, j, J, results = []; | ||
for (i = 0, I = context.length; i < I; i++) { | ||
item = context[i]; | ||
copy = line.slice(0); | ||
for (j = 0, J = fields.length; j < J; j++) { | ||
field = fields[j]; | ||
if (child && j === fields.length - 1) { | ||
child(lines, copy, item[field]); | ||
} else { | ||
copy.push(item[field]); | ||
} | ||
} | ||
results.push(!child ? lines.push(copy) : void 0); | ||
} | ||
return results; | ||
}; | ||
} | ||
|
||
function parse (labels, rest, nested) { | ||
var child, field, fields = [], label, proc; | ||
while (!proc) { | ||
if (nested && ($ = /^\s*\](.*)$/.exec(rest))) { | ||
rest = $[1]; | ||
proc = build(fields, null); | ||
} else { | ||
$ = /^\s*(\w[\w\d]*)(.*)$/.exec(rest); | ||
if (!$) { | ||
throw new Error("invalid pattern"); | ||
} | ||
field = $[1], rest = $[2]; | ||
label = field; | ||
if ($ = /^\[(.*)$/.exec(rest)) { | ||
fields.push(field); | ||
$ = parse(labels, $[1], true), child = $[0], rest = $[1]; | ||
proc = build(fields, child); | ||
} else { | ||
if ($ = /^\/(\w[\w\d]*)(.*)$/.exec(rest)) { | ||
label = $[1], rest = $[2]; | ||
} | ||
labels.push(label); | ||
fields.push(field); | ||
rest = rest.replace(/^\s*,/, ''); | ||
if (!rest) proc = build(fields, null); | ||
} | ||
} | ||
} | ||
return [proc, rest]; | ||
} | ||
|
||
labels = []; | ||
if (format) { | ||
$ = parse(labels, format), display = $[0], rest = $[1]; | ||
if (rest && rest.trim().length !== 0) { | ||
throw new Error("invalid pattern."); | ||
} | ||
} | ||
|
||
file = process.env["AWS_CONFIG"] || ("" + process.env["HOME"] + "/.aws"); | ||
configuration = JSON.parse(fs.readFileSync(file, "utf8")); | ||
|
||
ec2 = ec2(configuration); | ||
|
||
ec2(command, parameters, response); | ||
|
||
function response (error, response) { | ||
var line, lines, i, I, results; | ||
if (error) throw error; | ||
if (display) { | ||
lines = []; | ||
display(lines, [], [response]); | ||
for (i = 0, I = lines.length; i < I; i++) { | ||
line = lines[i]; | ||
process.stdout.write(line.join(" ") + "\n"); | ||
} | ||
return results; | ||
} else { | ||
process.stdout.write(JSON.stringify(response, null, 2)); | ||
return process.stdout.write("\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,46 @@ | ||
var ec2 = require("ec2"), sys = require("sys"); | ||
// Require EC2. | ||
var ec2 = require("ec2") | ||
, fs = require("fs") | ||
, path = require("path") | ||
, configuration = path.resolve(process.env.HOME, ".aws") | ||
; | ||
|
||
var AMI = "ami-2d4aa444"; | ||
var VOLUME = "vol-d6bf23bf"; | ||
// Read in the configuration above. | ||
var configuration = JSON.parse(fs.readFileSync(configuration, "utf8")); | ||
|
||
function createRequest() { | ||
return ec2.request( | ||
{ key: process.env["AWS_ACCESS_KEY_ID"] | ||
, secret: process.env["AWS_SECRET_ACCESS_KEY"] | ||
}); | ||
// Create an ec2 function that uses your configuration. | ||
ec2 = ec2(configuration) | ||
|
||
// Run an instance and wait for it to become ready. | ||
ec2("RunInstances", { | ||
ImageId: "ami-2d4aa444", KeyName: "syntechdev_aws_key", MinCount: 1, MaxCount: 1 | ||
}, running); | ||
|
||
|
||
var reservationId, instanceId; | ||
function running (error, response) { | ||
if (error) throw error; | ||
reservationId = response.reservationId | ||
instanceId = response.instancesSet[0].instanceId; | ||
describe(); | ||
} | ||
|
||
function launch () { | ||
var state = {}; | ||
var amazon = ec2.createClient( | ||
{ key: process.env["AWS_ACCESS_KEY_ID"] | ||
, secret: process.env["AWS_SECRET_ACCESS_KEY"] | ||
}); | ||
amazon.on("error", function (error, statusCode) { | ||
console.log(arguments); | ||
if (error.message) { | ||
console.log(sys.inspect(error.message, true, 100)); | ||
console.log(sys.inspect(error.stack, true, 100)); | ||
} else { | ||
console.log(sys.inspect(error, true, 100)); | ||
} | ||
}); | ||
amazon.call("DescribeVolumes", function (struct) { | ||
var volume = struct.volumeSet.filter(function (volume) { return volume.volumeId == VOLUME; })[0]; | ||
if (/^in-use|attaching$/.test(volume.status)) { | ||
throw new Error("Volume is already attached."); | ||
} | ||
}); | ||
amazon.call("RunInstances", | ||
{ ImageId: AMI | ||
, KeyName: "backup_key" | ||
, MinCount: 1 | ||
, MaxCount: 1 | ||
, "Placement.AvailabilityZone": "us-east-1b" | ||
}, function (struct) { | ||
state.reservationId = struct.reservationId; | ||
state.instanceId = struct.instancesSet[0].instanceId; | ||
}); | ||
amazon.poll("DescribeInstances", function (struct) { | ||
var reservation = struct.reservationSet.filter(function (reservation) { | ||
return reservation.reservationId == state.reservationId; | ||
}).pop(): | ||
var instance = reservation.instancesSet.filter(function (instance) { | ||
return instance.instanceId == state.instanceId; | ||
}).pop(); | ||
return instance.instanceState.name == "running"; | ||
}); | ||
amazon.call("AttachVolume", | ||
{ VolumeId: VOLUME | ||
, InstanceId: function () { return state.instanceId; } | ||
, Device: "/dev/sdh" | ||
}); | ||
amazon.poll("DescribeVolumes", { "VolumeId.1": VOLUME }, function (struct) { | ||
var attachment = struct.volumeSet[0].attachmentSet.filter(function (attachment) { | ||
return attachment.instanceId == state.instanceId; | ||
})[0]; | ||
return attachment.status == "attached"; | ||
}); | ||
amazon.on("end", function () { | ||
console.log("Volume attached and running with instance: " + state.instanceId); | ||
}); | ||
amazon.execute(); | ||
function describe () { | ||
ec2("DescribeInstances", {}, starting); | ||
} | ||
|
||
launch(); | ||
function starting (error, response) { | ||
if (error) throw error; | ||
var reservation = response.reservationSet.filter(function (reservation) { | ||
return reservation.reservationId == reservationId; | ||
})[0]; | ||
var instance = reservation.instancesSet.filter(function (instance) { | ||
return instance.instanceId == instanceId; | ||
})[0]; | ||
if (instance.instanceState.name == "running") ready(); | ||
else setTimeout(describe, 2500); | ||
} | ||
|
||
// vim: set ts=2 sw=2 et nowrap: | ||
function ready () { | ||
console.log("Instance created with id: " + instanceId); | ||
} |
Oops, something went wrong.