Managing Azure IaaS from Node.js

A Service Management Service was recently added to the Node SDK for Windows Azure. This allows you to interact with the Azure management API, to create, deleted and manage Virtual Machines in Azure.

Getting Set Up

To get started you need to get your management certificates in order. This is probably most easily achieved by creating new ones.

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
$ openssl x509 -inform pem -in mycert.pem -outform der -out mycert.cer

The mycert.cer file can be uploaded to the Management Portal. The other files we’ll use in our node app.

Next, let’s install the Azure SDK:

$ [sudo] npm install azure

Creating the ServiceManagementService

Now let’s write some JavaScript. To work with the Service Management API, we’ll need an instance of the ServiceManagementService:

var azure = require('azure');
var auth = {keyfile:'mycert.pem',certfile:'mycert.pem'};
var sms = azure.createServiceManagementService('SUBSCRIPTION_ID', auth);

Using the ServiceManagementService to call Azure

Now we have an instance of the SMS, we can start querying the Azure infrastructure. To get a list of Hosted Services, use this function:

sms.listHostedServices(function(err,data){
  if (err) console.log(err);
  console.log(data.body);
});

We can get more information about a particular Hosted Service:

sms.getDeploymentBySlot('HOSTED_SERVICE','production',function(err,data){
  if (err) console.log(err);
  console.log(data.body);  
});

It’s simple to create a new Hosted Service:

sms.createHostedService("NewHostedService", {Location:"North Europe"}, function(err,data){
  if (err) console.log(err);
  // success!
});

To deploy a new Virtual Machine is a bit more involved:

var vmrole = {RoleName: "NewRole",ConfigurationSets: []};

vmrole.ConfigurationSets[0] = {
  ConfigurationSetType: "WindowsProvisioningConfiguration",
  ComputerName: "MACHINE_NAME",
  AdminPassword: "PASSWORD",
  ResetPasswordOnFirstLogon: false };

vmrole.ConfigurationSets[1] = {
  ConfigurationSetType: "NetworkConfiguration",
  InputEndpoints: [
    {LocalPort: 80,Port: 80,Name: "HTTP",Protocol: "tcp"},
    {LocalPort: 3389,Port: 3389,Name: "RDP",Protocol: "tcp"}]}

vmrole.OSVirtualHardDisk = { SourceImageName: "NAME_OF_YOUR_IMAGE"};

sms.createDeployment("NewHostedService","NewDeployment",vmrole,{DeploymentSlot:"Production"}, function(err,data){
  if (err) console.log(err);
  // success! 	
});

I have managed to deploy VMs, start/stop them, mount/unmount disks and delete everything again. :¬)

Gotchas

It’s a bit ugly in places. For disks you have to specify the Lun (the logical number of the disk, between 0-15) when you mount it. This number is not reported to you when you query the machine, and there’s no way of unmounting the disk without specify the Lun, so it’s something you have to keep track of yourself.

Disks are also reported as being mounted on the deployment of a role, but you have request a mount/unmount on the instance. This is a bit confusing and inconsistent.

Further Reading

To explore more of the functionality, you could take a look at this documentation (the first part is currently wrong, but the api is well documented):

https://github.com/WindowsAzure/azure-content/blob/master/ITPro/Linux/HowTo/howto-servicemgt-api.md

Alternatively you could just read the source code of the module:

https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/serviceManagement/servicemanagementservice.js

Conclusions

One of the most important feature of any cloud is it’s programmability. Getting the SDK for this right is very important, and it’s surprising that this area isn’t mature. You can do just about everything through PowerShell and the cross-platform tools, but raw programmability is important too.

This SDK is not finished, it’s not documented, and it’s a bit ugly in places. However, it’s a great start.

It’s interesting to see Microsoft making this functionality available for Node.js before .NET languages. We know that the cross-platform tools are Node, so it’s obvious why, it’s just an interesting position to be in.

Ok, there is a sample in C# (the Microsoft.Samples.WindowsAzure.ServiceManagement namespace) but it’s undocumented, unsupported, and not straight forward to use. Richard Conway’s Fluent Management library is good, but not complete (I’m sure Richard would appreciate help!).

The only other viable option seems to be Python.

Advertisements