Created
May 31, 2011 02:55
-
-
Save zircote/999797 to your computer and use it in GitHub Desktop.
A Zend Framework class to fetch EC2 instance meta-data
This file contains hidden or 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 | |
require_once 'Zend/Service/Abstract.php'; | |
/** | |
* | |
* | |
* @author [email protected] | |
* | |
* <code> | |
* <?php | |
* $metadata = new Zircote_Service_Ec2_Metadata; | |
* echo $metadata->amiID; | |
* echo $metadata->hostname; | |
* echo $metadata->instanceId | |
* echo $metadata->kernelId | |
* echo $metadata->userData; | |
* echo $metadata->publicHostname; | |
* </code> | |
* <p>CLI example (but note curl -s would probably be more suitable)</p> | |
* <code> | |
* #!/usr/bin/env php | |
* <?php | |
* require_once 'Zircote/Service/Ec2/Metadata.php'; | |
* $metadata = new Zircote_Service_Ec2_Metadata; | |
* try{ | |
* file_put_contents('php://stdout',$metadata->__get($_SERVER['argv'][1])); | |
* } catch (Exception $e){ | |
* file_put_contents('php://stderr', $e->getMessage()); | |
* exit(0); | |
* } | |
* </code> | |
*/ | |
class Zircote_Service_Ec2_Metadata extends Zend_Service_Abstract | |
{ | |
protected $_endpoint = 'http://169.254.169.254'; | |
protected $_methods = array( | |
'amiID' => '/latest/meta-data/ami-id', | |
'amiLaunchIndex' => '/latest/meta-data/ami-launch-index', | |
'amiManifestPath' => '/latest/meta-data/ami-manifest-path', | |
'ancestorAmiIds' => '/latest/meta-data/ancestor-ami-ids', | |
'blockDeviceMapping' => '/latest/meta-data/block-device-mapping/', | |
'hostname' => '/latest/meta-data/hostname', | |
'instanceAction' => '/latest/meta-data/instance-action', | |
'instanceId' => '/latest/meta-data/instance-id', | |
'instanceType' => '/latest/meta-data/instance-type', | |
'kernelId' => '/latest/meta-data/kernel-id', | |
'localHostname' => '/latest/meta-data/local-hostname', | |
'localIpv4' => '/latest/meta-data/local-ipv4', | |
'placement' => '/latest/meta-data/placement/', | |
'publicHostname' => '/latest/meta-data/public-hostname', | |
'publicIpv4' => '/latest/meta-data/public-ipv4', | |
'publicKeys' => '/latest/meta-data/public-keys/', | |
'ramdiskId' => '/latest/meta-data/ramdisk-id', | |
'reservationId' => '/latest/meta-data/reservation-id', | |
'securityGroups' => '/latest/meta-data/security-groups', | |
'userData' => '/latest/user-data', | |
); | |
public function __get($name) | |
{ | |
if(!array_key_exists($name, $this->_methods)){ | |
throw new Exception(sprintf('method [%s] does not exist', $name)); | |
} | |
self::getHttpClient()->setUri($this->_endpoint) | |
->getUri()->setPath($this->_methods[$name]); | |
$result = self::getHttpClient()->request(); | |
if($result->getStatus() == 200){ | |
return $result->getBody(); | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment