Permalink
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (77 sloc) 2.81 KB

Getting started with pkgcloud & Openstack

The Openstack node.js SDK is available as part of pkgcloud, a multi-provider cloud provisioning package

Pkgcloud currently supports Openstack Nova (compute) and Openstack Swift (storage).

To install pkgcloud from the command line:

npm install pkgcloud

Don't have npm or node yet? Get it now.

Using pkgcloud

In this example, we're going to create a Openstack compute client, create two servers, and then output their details to the command line.

Note: We're going to use lodash.js for some convenience functions. Note: For DevStack, change AuthUrl to http://:5000

var pkgcloud = require('pkgcloud'),
    _ = require('lodash');

// create our client with your openstack credentials
var client = pkgcloud.compute.createClient({
  provider: 'openstack',
  username: 'your-user-name',
  password: 'your-password',
  region: 'RegionOne', //default for DevStack, might be different on other OpenStack distributions
  authUrl: 'https://your-identity-service'
});

// 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: 'm1.tiny' });

        // Pick an image based on Ubuntu 12.04
        var image = _.findWhere(images, { name: 'cirros-0.3.2-x86_64-uec' }); // Check if this version is correct

        // Create our first server
        client.createServer({
            name: 'server1',
            image: image,
            flavor: flavor
        }, handleServerResponse);

        // Create our second server
        client.createServer({
            name: 'server2',
            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: RUNNING on our server, and then callback
    server.setWait({ status: server.STATUS.running }, 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');
    });
}