Last active
December 25, 2015 16:49
-
-
Save kenperkins/7008533 to your computer and use it in GitHub Desktop.
Example of creating two servers with pkgcloud
This file contains hidden or 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
var pkgcloud = require('pkgcloud'), | |
_ = require('underscore'); | |
// create our client with your rackspace credentials | |
var client = pkgcloud.providers.compute.createClient({ | |
provider: 'rackspace', | |
username: process.argv[2], | |
apiKey: process.argv[3], | |
region: 'DFW' | |
}); | |
// first we're going to get our flavors | |
client.getFlavors(function (err, flavors) { | |
if (err) { | |
console.dir(err); | |
return; | |
} | |
// then get our base images | |
client.getImages(function (err, images) { | |
if (err) { | |
console.dir(err); | |
return; | |
} | |
// Pick a 512MB instance flavor | |
var flavor = _.findWhere(flavors, { name: '512MB Standard Instance' }); | |
// Pick an image based on Ubuntu 12.04 | |
var image = _.findWhere(images, { name: 'Ubuntu 12.04 LTS (Precise Pangolin)' }); | |
// Create our first server | |
client.createServer({ | |
name: 'web-01', | |
image: image, | |
flavor: flavor | |
}, handleServerResponse); | |
// Create our second server | |
client.createServer({ | |
name: 'web-02', | |
image: image, | |
flavor: flavor | |
}, handleServerResponse); | |
}); | |
}); | |
// This function will handle our server creation, | |
// as well as waiting for the server to come online after we've | |
// created it. | |
function handleServerResponse(err, server) { | |
if (err) { | |
console.dir(err); | |
return; | |
} | |
console.log('SERVER CREATED: ' + server.name + ', waiting for active status'); | |
// Wait for status: ACTIVE on our server, and then callback | |
server.setWait({ status: 'ACTIVE' }, 5000, function (err) { | |
if (err) { | |
console.dir(err); | |
return; | |
} | |
console.log('SERVER INFO'); | |
console.log(server.name); | |
console.log(server.status); | |
console.log(server.id); | |
console.log('Make sure you DELETE server: ' + server.id + | |
' in order to not accrue billing charges'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment