Wednesday, April 17, 2013

Basic utilization of the CloudStack API with PHP

http://kirkjantzer.blogspot.com/2013/04/basic-utilization-of-cloudstack-api.html

Wednesday, April 17, 2013 3:43 PMBasic utilization of the CloudStack API with PHPThe blog of Kirk JantzerKirk
If you're not aware, I've been playing with the private cloud / IaaS solutionCloudStack over the last several weeks. It has been quite the learning experience, and I'm just getting started :-)

My most recent adventure has been checking out the API. I am in no way a developer, and have never worked with an API previously, however I do have some experience with PHP. I wasn't looking to recreate the wonderful CloudStack UI, I just wanted something specific: I needed to be able to deploy a lot of instances at once. While the UI is beautiful, building more than one instance can be VERY tedious. Image clicking through all these tabs to build 50 instances - heck, 10 instances:



After going through this a few times, I needed a new way. Like always, I started "Googling" to see what options were available, however, I couldn't find exactly what I wanted. This was when I started looking to see what it was going to take to build what I wanted to myself. Turns out, not that much :-) After about an hour or two this was my result:



Admittedly, it isn't pretty, but in its basic form it serves my purpose. At present it only uses a predefined zone, iso, diskoffering, etc; but I fully intend to make drop-downs of available options (zones/diskofferings/etc) that will be pulled from CloudStack when the page is loaded, as well as functions based around your API key and what your restrictions are. This will all come in time, but I don't think it's a bad start for an afternoons work :-D

Here is an example of the process:

Note there are currently no instances

I've filled in the fields with the desired data

The output after clicking "Build 'em!"

BAM! The result in CloudStack. :-)

Without further ado, here is the PHP code that is makes up the page above - yes, <70 lines of some basic PHP code can enable you to build hundreds of servers in seconds!!


 <html>  
<head>
<title>CloudStack API</title>
</head>
<?php
if(isset($_POST['submit']))
{
$prefix = $_POST['prefix'];
$endnum = ($_POST['startnum'] + $_POST['quantity']) - 1;
$startnum = $_POST['startnum'];
if (empty($prefix) || empty($startnum) || empty($_POST['quantity']))
{
echo "<b>You missed a field!</b><br />";
}
else
{
echo "You have submitted for the following servers to be built: <br />";
for ($i=$startnum; $i<=$endnum; $i++)
{
echo $prefix . $i . "<br />";
$apikey = "apikey=**********************************************************************************";
$secretkey = "**********************************************************************************";
$zoneid = "zoneId=********-****-****-****-************";
$templateid = "templateId=********-****-****-****-************";
$serviceOfferingId = "serviceOfferingId=********-****-****-****-************";
$diskofferingid = "diskOfferingId=********-****-****-****-************";
$hypervisor = "hypervisor=XenServer";
$response = "response=json";
$urlcommand_deployvm = "command=deployVirtualMachine" . "&" . $zoneid . "&" . $diskofferingid . "&displayname=" . $prefix . $i . "&" . $hypervisor . "&name=" . $prefix . $i . "&" . $serviceOfferingId . "&" . $templateid . "&" . $apikey;
$hashcommand = "apikey=" . $apikey . "&command=deployVirtualMachine&zoneId=" . $zoneid . "&diskOfferingId=" . $diskofferingid . "&displayname=" . $prefix . $i . "&hypervisor=" . $hypervisor . "&name=" . $prefix . $i . "&serviceOfferingId=" . $serviceOfferingId . "&templateId=" . $templateid;
$hash = hash_hmac("sha1", strtolower($command), $secretkey.true, TRUE);
$base64encoded = base64_encode($hash);
$signature = "signature=" . urlencode($base64encoded);
$url = "http://masternode:8096/client/api?" . $urlcommand_deployvm . "&" . $signature . "&" . $response;
$responseconents = file_get_contents($url);
$responsejson = json_decode($responsecontents);
}
}
echo "<br />";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="wrapper" style="width: 500px;margin: auto;border: 1px solid black">
<div id="header">
<h1>Welcome</h1>
<p>This is the CloudStack API frontend. Please use the fields below to build your VMs in mass :-)</p>
</div>
<div id="content">
<div id="col1" style="float:left;width:248px;text-align:right">Server Name Prefix: </div>
<div id="col2" style="float:left;width:248px;"><input type="text" name="prefix" placeholder="Example: RACKPOOL"><br /></div>
</div>
<div id="content">
<div id="col1" style="float:left;width:248px;text-align:right">Start Number: </div>
<div id="col2" style="float:left;width:248px;"><input type="text" name="startnum" placeholder="Example: 21"><br /></div>
</div>
<div id="content">
<div id="col1" style="float:left;width:248px;text-align:right">Quantity: </div>
<div id="col2" style="float:left;width:248px;"><input type="text" name="quantity" placeholder="Example: 5"><br /><br /></div>
</div>
<div id="footer" align="center">
<input type="submit" name="submit" value="Build 'em!">
</div>
</div>
</form>
</html>

Of mention, please read the CloudStack API documentation. You must enable (in 'global settings') the API port of your choosing to be able to make API calls. After that, you need to create an API key for your user. In addition, the URL I am using was obtained when I used the GUI to build an instance, then I just scanned the management server log (located in /var/log/cloud/management/management-server.log) to see the actual syntax of a 'deployvirtualmachine' API call. Also, the server that is running this PHP page has the JSON PHP extension enabled - make sure you do that if you're going to use this method (a quick Google will yield numerous results). Also of note, the majority of CloudStack API calls require a signature to be sent along with the API request - you'll notice I didn't include that in this script - yet. At present, this works for me, but I will be changing the script to reflect the changes I've mentioned - I'll update/repost when I do so.

As always, questions/comments/critiques are welcome.




No comments:

Post a Comment