By David Elliott, Technical Director at StratoGen
Last week I had an interesting meeting with a software development company looking to move their products to a software-as-a-service (SaaS) model. It’s a common scenario these days and one which lends itself very well to VMware and in particular the vCloud Director cloud hosting platform.
One of the features they loved was the vCloud API which gives them the ability to deploy pre-configured virtual machines from a ‘master template’ on the fly.
The great thing about the API is that it breaks down complex tasks into simple steps that can be achieved with just a few lines of code. For our software company, this means the process of signing up new clients can be easily automated; configuring and deploying a new virtual machine for each new customer.
To demonstrate what this functionality would look like in real life I’m going to run through a simplified example written in PHP. We’ll be using the vCloud SDK, which is a wrapper for the vCloud API consisting of a PHP class library, developer documentation and a number of example applications. VMware also provides API wrappers for Java and .Net developers.
Creating our development environment
We’ll use a Linux distribution (CentOS 5.5) onto which we install PHP 5.3.1 and the PEAR HTTP_Request2 library. These are the only prerequisites for the SDK. Interestingly, the pear library is still classed as beta at the time of writing..
I’m going to use the webtatic repository to quickly install our required packages as the versions supplied by CentOS are older than stipulated by the SDK. For a production environment, it would be better to build the latest versions from source.
So let’s get started. After installing CentOS we need to perform the following commands:
$ yum update
$ rpm -ivh http://repo.webtatic.com/yum/centos/5/`uname -i`/webtatic-release-5-1.noarch.rpm
$ yum --enablerepo=webtatic install php php-xml php-pear php-mbstring.`uname -i`
$ pear install channel://pear.php.net/Net_URL2-0.3.1
$ pear install channel://pear.php.net/HTTP_Request2-2.0.0beta3
Lastly, download the vCloud SDK for PHP which can be found at http://www.vmware.com/go/vcloudsdkforphp. The SDK is available as a .zip for windows servers or .tar for Linux. Installation is simply a case of extracting the files into a suitable directory on your server.
Here’s the code for our entire application:
The first section set’s the connection parameters to our vCloud Director server – in a VMware hosting environment this would be obtained from your service provider. Next, I set the parameters for my account. I have created a catalog and placed a vApp template inside it called “MyTemplate”. This will be customised and deployed by our script.
The real work comes in the 5 steps in the ‘try’ segment.
Step 1 logs in to the vCloud Director account. Steps 2 to 4 get references to my organisation, virtual datacentre and finally the vApp template. We are then ready in step 5 to deploy a new vApp from the template. Notice how I can do this in a single line of code.
After invoking the PHP code our new vApp is created.
The example shows how a small amount of code can be very productive with the API. Of course we would need to add a lot to this before it is ready for production (in particular error checking!) but I just wanted to highlight how simple the API is to use, and hopefully inspire you to write some code of your own which leverages this powerful interface.
In my next post I hope to cover how we can use the API to implement auto-scaling by powering up additional servers in response to work load.
David Elliott is Technical Director of StratoGen, a leading VMware hosting provider with a worldwide client base. StratoGen hosted VMware products are based on vCloud Director and include a 100% uptime guarantee. For updates on future articles follow StratoGen on twitter, or subscribe to the StratoGen Blog.