Permalink
Browse files

More error handling for Windows timeouts, adding LICENSE.txt, finetun…

…e juju
  • Loading branch information...
1 parent 4bc9a9e commit 27d1586e6e8b0f34cfe67bba1a9279785abeff41 @DukeyToo DukeyToo committed Dec 16, 2011
View
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2011 Perfect API LLC
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -8,7 +8,7 @@ exports.generateAMI = function(instanceId, name, tag, callback) {
});
var newImageId;
-
+
console.log('About to create image from ' + instanceId + ' with name ' + name);
client.call("CreateImage", {
@@ -17,21 +17,24 @@ exports.generateAMI = function(instanceId, name, tag, callback) {
Name: name
}, function(response) {
newImageId = response.imageId;
-
+ console.log('requested AMI, waiting...');
+
//tag the image...
client.call("CreateTags", {
"ResourceId.1": newImageId,
"Tag.1.Key": "spec",
"Tag.1.Value": tag
}, function(response) {
//tags done
+ console.log('Tagged new AMI');
});
//finally terminate the instance...
client.call("TerminateInstances", {
InstanceId: instanceId
}, function(response) {
//terminated
+ console.log('terminated image');
});
});
@@ -46,6 +49,15 @@ exports.generateAMI = function(instanceId, name, tag, callback) {
return (imageSet.length > 0);
});
+ client.on("error", function (err) {
+ if (err=="Error: connect Unknown system errno 10060") {
+ //lets assume the timeout is on the poll. In that case, (maybe) we can ignore the error
+ console.log('Encountered ' + err + '. Ignoring.');
+ } else {
+ callback('error generating ami - ' + err);
+ }
+ });
+
// When all of the Amazon Query API calls and polls complete, we know that our
// Amazon EC2 instance is ready for use.
client.on("end", function () {
@@ -84,7 +84,7 @@ getImage = function(
if (err) {
callback(err);
} else {
- var client = ec2.createClient(
+ var client = ec2.createClient(
{ key: process.env["AWS_ACCESS_KEY_ID"]
, secret: process.env["AWS_SECRET_ACCESS_KEY"]
});
@@ -120,8 +120,10 @@ getImage = function(
}
});
- client.on("error", function(err) {callback(err); return; });
-
+ client.on("error", function (err) {
+ callback('error finding image - ' + err);
+ });
+
// Run the transaction described above.
client.execute();
}
@@ -13,8 +13,8 @@ exports.generateInstance = function(userData, baseAMI, callback) {
reservationId,
newImageId;
- // Call the "RunInstances" action to create a new EC2 instance. The Amazon Query
- // API call will return immediately, but the instance will take a while to boot.
+ // Call the "RunInstances" action to create a new EC2 instance. The Amazon Query
+ // API call will return immediately, but the instance will take a while to initialize.
client.call("RunInstances", {
ImageId: baseAMI,
UserData: userData,
@@ -23,10 +23,35 @@ exports.generateInstance = function(userData, baseAMI, callback) {
}, function (response) {
reservationId = response.reservationId;
instanceId = response.instancesSet[0].instanceId;
+
+ poll(client, reservationId, instanceId);
+ });
+
+ client.on("error", function (err) {
+ if (err=="Error: connect Unknown system errno 10060") {
+ //lets assume the timeout is on the poll. In that case, we can ignore the error
+ console.log('Encountered ' + err + '. Ignoring & restarting polling');
+ poll(client, reservationId, instanceId);
+ } else {
+ callback('error generating instance - ' + err);
+ }
+ });
+
+ // When all of the Amazon Query API calls and polls complete, we know that our
+ // Amazon EC2 instance is ready for use.
+ client.on("end", function () {
+ console.log("Instance created with id: " + instanceId);
+ callback(null, instanceId);
});
- // Therefore, we poll the "DescribeInstances" action, calling it once every
- // second until the instance state indicates that it is running.
+ // Run the trasaction described above.
+ client.execute();
+
+};
+
+function poll(client, reservationId, instanceId) {
+ //We poll the "DescribeInstances" action, calling it once every
+ // second until the instance state indicates that it is done.
client.poll("DescribeInstances", function (struct) {
var reservation = struct.reservationSet.filter(function (reservation) {
return reservation.reservationId == reservationId;
@@ -36,22 +61,11 @@ exports.generateInstance = function(userData, baseAMI, callback) {
return instance.instanceId == instanceId;
})[0];
+ //console.log('polling for instance to finish...')
return (instance.instanceState.name == "stopped"); //because the script automatically stops the instance once user-data.sh completes
} else {
//happens when the original RunInstances call has not yet returned. (It returns "immediately", but still async)
return false;
}
});
-
- // When all of the Amazon Query API calls and polls complete, we know that our
- // Amazon EC2 instance is ready for use.
- client.on("end", function () {
- console.log("Instance created with id: " + instanceId);
- callback(null, instanceId);
- });
-
- // Run the trasaction described above.
- client.execute();
-
-};
-
+}
@@ -1,4 +1,4 @@
apt-get install -y juju
-sudo -u $MY_USER sh -c "HOME=/home/$MY_USER && cd && ssh-keygen -q -t dsa"
+sudo -u $MY_USER sh -c "HOME=/home/$MY_USER && cd && ssh-keygen -q -t dsa -f ~/.ssh/id_dsa -N \"\""
@@ -11,3 +11,7 @@ export HOME="/home/root"
#ensure apt-get packages do not prompt
export DEBIAN_FRONTEND=noninteractive
+#save some information for debug purposes
+echo "The user-data script has basename `basename $0`, dirname `dirname $0`"
+echo "The present working directory is `pwd`"
+

0 comments on commit 27d1586

Please sign in to comment.