Created
October 24, 2012 13:35
-
-
Save tomellis/3946086 to your computer and use it in GitHub Desktop.
Simple AWS PHP SDK example for Eucalyptus
This file contains 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
<?php | |
// Source AWS PHP SDK functions | |
require_once '../sdk.class.php'; | |
// Instantiate the AmazonEC2 class | |
$options = array( | |
'key' => 'ENTER-YOUR-ACCESS-KEY-HERE', | |
'secret' => 'ENTER-YOUR-SECRET-KEY-HERE', | |
); | |
$ec2 = new AmazonEC2($options); | |
$ec2->enable_debug_mode(); | |
$ec2->disable_ssl(); | |
$ec2->set_hostname("http://eucalyptus.YOURDOMAIN.COM:8773/services/Eucalyptus"); | |
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form | |
?> | |
<html> | |
<head> | |
<title>Run an instance</title> | |
</head> | |
<body> | |
<form method="post" action="<?php echo $PHP_SELF;?>"> | |
Image:<select name="emi-id"> | |
<?php | |
// grab list of available EMI | |
$response = $ec2->describe_images(); | |
// error if response is not ok | |
if (!($response->isOK())) { | |
echo "<p class='error'>ERROR! Could not retrieve instance list!</p>"; | |
return; | |
} | |
// Create empty array. We need to sort images as they could be eki, eri or emi. We only want emi. | |
$images = array( ); | |
// Get all image ids and push them into array $images | |
foreach ($response->body->imagesSet->item as $item) { | |
$image = (string) $item->imageId; | |
array_push($images, $image); | |
} | |
// grep array and return an array containing only emis | |
$emis = preg_grep('/^emi*/',$images); | |
// For every emi, add another option in the drop-down box | |
foreach ($emis as $emi) { | |
echo "<option value='" .$emi. "'>" .$emi. "</option>"; | |
} | |
?> | |
</select><br /> | |
Zone:<select name="zone-id"> | |
<?php | |
// grab list of available Availability Zones | |
$response = $ec2->describe_availability_zones(); | |
// error if response is not ok | |
if (!($response->isOK())) { | |
echo "<p class='error'>ERROR! Could not retrieve zone list!</p>"; | |
return; | |
} | |
// Get all zones | |
foreach ($response->body->availabilityZoneInfo->item as $item) { | |
$zone = (string) $item->zoneName; | |
echo "<option value='" .$zone. "'>" .$zone. "</option>"; | |
} | |
?> | |
</select><br /> | |
Instance Type: <select name="instancetype"> | |
<option value="m1.small">m1.small</option> | |
<option value="c1.medium">c1.medium</option> | |
<option value="m1.large">m1.large</option> | |
<option value="m1.xlarge">m1.xlarge</option> | |
<option value="c1.xlarge">c1.xlarge</option> | |
</select><br /> | |
Amount:<input type="number" name="amount" min="1" max="20" value="1"><br /> | |
<input type="submit" value="Launch" name="submit"> | |
</form> | |
</body> | |
</html> | |
<?php | |
} | |
else { | |
// Grab elements from form | |
$emi = $_POST["emi-id"]; | |
$instancetype = $_POST['instancetype']; | |
$zoneid = $_POST['zone-id']; | |
$keyname = "my-key"; | |
$securitygroup = "default"; | |
$amount = $_POST['amount']; | |
$sleeptime = 30; | |
$username = root; | |
// Boot an instance of the image | |
$response = $ec2->run_instances($emi, $amount, $amount, array( | |
'KeyName' => $keyname, | |
'InstanceType' => $instancetype, | |
'SecurityGroup' => $securitygroup, | |
'Placement' => array('AvailabilityZone' => $zoneid), | |
)); | |
if (!($response->isOK())) { | |
echo "<p class='error'>ERROR! Could not create new instance!</p>"; | |
return; | |
} | |
$instance = $response->body->instancesSet->item->instanceId; | |
$message .= "<p>Your instance has been successfully created.</p>"; | |
$message .= ("<p>Instance ID is: <b>$instance</b><br />"); | |
$instance = $response->body->instancesSet->item->instanceType; | |
$message .= ("Instance Type is: <b>$instancetype</b><br />"); | |
$instance = $response->body->instancesSet->item->Placement; | |
$message .= ("Instance is launched in: <b>$zoneid</b></p>"); | |
// Give instance some time to start up | |
sleep ($sleeptime); | |
// Get the hostname from a call to the DescribeImages operation. | |
$response = $ec2->describe_instances($instance); | |
if (!($response->isOK())) { | |
echo "<p class='error'>ERROR! Could not retrieve hostname for instance!</p>"; | |
return; | |
} | |
$hostname = $response->body->reservationSet->item->instancesSet->item->dnsName; | |
// Output the message | |
$message .= "<p>Your instance hostname is: <b>$hostname</b></p>"; | |
$message .= "<p>You can connect to your instance using this command:<br>" . | |
"<b>ssh -i $keyname.pem $username@" .$hostname. "</b></p>"; | |
echo $message; | |
} | |
// Query current instances | |
$response = $ec2->describe_instances(); | |
if (!($response->isOK())) { | |
echo "<p class='error'>ERROR! Could not retrieve instance list!</p>"; | |
return; | |
} | |
// Display instances | |
echo "<table align='left' border='1'>"; | |
echo "<tr><b><td>Instance-Id</td><td>State</td><td>Public DNS</td><td>Instance-Type</td><td>Launch Time</td><td>Availability Zone</td></b></tr>"; | |
foreach ($response->body->reservationSet->item as $item) | |
{ | |
$instanceId = (string) $item->instancesSet->item->instanceId; | |
$instanceState = (string) $item->instancesSet->item->instanceState->name; | |
$instancePubDNS = (string) $item->instancesSet->item->dnsName; | |
$instanceType = (string) $item->instancesSet->item->instanceType; | |
$instanceTime = (string) $item->instancesSet->item->launchTime; | |
$instanceLoc = (string) $item->instancesSet->item->placement->availabilityZone; | |
echo '<tr> <td> ' . $instanceId . ' </td> <td>' . $instanceState . '</td> <td>' . $instancePubDNS . '</td> <td>' . $instanceType . '</td> <td>' . $instanceTime . ' </td> <td>' . $instanceLoc . '</td> </tr>'; | |
} | |
echo "</table>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment