Skip to content

Instantly share code, notes, and snippets.

@zircote
Created May 31, 2011 02:55

Revisions

  1. zircote revised this gist Oct 2, 2011. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions Metadata.php
    Original file line number Diff line number Diff line change
    @@ -22,9 +22,9 @@
    * require_once 'Zircote/Service/Ec2/Metadata.php';
    * $metadata = new Zircote_Service_Ec2_Metadata;
    * try{
    * echo $metadata->__get($_SERVER['argv'][1]);
    * file_put_contents('php://stdout',$metadata->__get($_SERVER['argv'][1]));
    * } catch (Exception $e){
    * echo $e->getMessage();
    * file_put_contents('php://stderr', $e->getMessage());
    * exit(0);
    * }
    * </code>
    @@ -60,7 +60,8 @@ public function __get($name)
    if(!array_key_exists($name, $this->_methods)){
    throw new Exception(sprintf('method [%s] does not exist', $name));
    }
    self::getHttpClient()->getUri()->setPath($this->_methods[$name]);
    self::getHttpClient()->setUri($this->_endpoint)
    ->getUri()->setPath($this->_methods[$name]);
    $result = self::getHttpClient()->request();
    if($result->getStatus() == 200){
    return $result->getBody();
  2. zircote revised this gist Oct 2, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Metadata.php
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ class Zircote_Service_Ec2_Metadata extends Zend_Service_Abstract
    public function __get($name)
    {
    if(!array_key_exists($name, $this->_methods)){
    throw new Exception(sprintf('value [%s] does not exist', $name));
    throw new Exception(sprintf('method [%s] does not exist', $name));
    }
    self::getHttpClient()->getUri()->setPath($this->_methods[$name]);
    $result = self::getHttpClient()->request();
  3. zircote revised this gist Oct 2, 2011. 2 changed files with 34 additions and 20 deletions.
    48 changes: 34 additions & 14 deletions Zircote_Service_Ec2.php → Metadata.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,36 @@
    <?php
    class Zircote_Service_Ec2
    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{
    * echo $metadata->__get($_SERVER['argv'][1]);
    * } catch (Exception $e){
    * echo $e->getMessage();
    * exit(0);
    * }
    * </code>
    */
    class Zircote_Service_Ec2_Metadata extends Zend_Service_Abstract
    {
    /**
    *
    * @var Zend_Http_Client
    */
    protected $_httpClient;
    protected $_endpoint = 'http://169.254.169.254';
    protected $_methods = array(
    'amiID' => '/latest/meta-data/ami-id',
    @@ -29,23 +54,18 @@ class Zircote_Service_Ec2
    'securityGroups' => '/latest/meta-data/security-groups',
    'userData' => '/latest/user-data',
    );
    public function __construct ()
    {
    require_once 'Zend/Http/Client.php';
    $this->_httpClient = new Zend_Http_Client($this->_endpoint);
    }

    public function __get($name)
    {
    if(!array_key_exists($name, $this->_methods)){
    throw new Exception(sprintf('value [%s] does not exist', $name));
    }
    $this->_httpClient->getUri()->setPath($this->_methods[$name]);
    $result = $this->_httpClient->request();
    self::getHttpClient()->getUri()->setPath($this->_methods[$name]);
    $result = self::getHttpClient()->request();
    if($result->getStatus() == 200){
    return $result->getBody();
    } else {
    return false;
    }
    }
    }
    }
    6 changes: 0 additions & 6 deletions ec2.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    #!/usr/bin/env php
    <?php
    require_once 'Zircote/Service/Ec2.php';
    $ec2 = new Zircote_Service_Ec2();

    echo $ec2->__get($_SERVER['argv'][1]);
  4. zircote renamed this gist Oct 2, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. zircote revised this gist Oct 2, 2011. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion Jazsl_Service_Ec2.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    class Service_Ec2
    class Zircote_Service_Ec2
    {
    /**
    *
    4 changes: 2 additions & 2 deletions ec2.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env php
    <?php
    require_once 'library/Jazsl/Service/Ec2.php';
    $ec2 = new Jazsl_Service_Ec2();
    require_once 'Zircote/Service/Ec2.php';
    $ec2 = new Zircote_Service_Ec2();

    echo $ec2->__get($_SERVER['argv'][1]);
  6. zircote revised this gist Oct 2, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Jazsl_Service_Ec2.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    class Jazsl_Service_Ec2
    class Service_Ec2
    {
    /**
    *
  7. zircote revised this gist May 31, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ec2.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/env php
    #!/usr/bin/env php
    <?php
    require_once 'library/Jazsl/Service/Ec2.php';
    $ec2 = new Jazsl_Service_Ec2();
  8. zircote revised this gist May 31, 2011. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions ec2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #!/bin/env php
    <?php
    require_once 'library/Jazsl/Service/Ec2.php';
    $ec2 = new Jazsl_Service_Ec2();

    echo $ec2->__get($_SERVER['argv'][1]);
  9. zircote created this gist May 31, 2011.
    51 changes: 51 additions & 0 deletions Jazsl_Service_Ec2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php
    class Jazsl_Service_Ec2
    {
    /**
    *
    * @var Zend_Http_Client
    */
    protected $_httpClient;
    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 __construct ()
    {
    require_once 'Zend/Http/Client.php';
    $this->_httpClient = new Zend_Http_Client($this->_endpoint);
    }

    public function __get($name)
    {
    if(!array_key_exists($name, $this->_methods)){
    throw new Exception(sprintf('value [%s] does not exist', $name));
    }
    $this->_httpClient->getUri()->setPath($this->_methods[$name]);
    $result = $this->_httpClient->request();
    if($result->getStatus() == 200){
    return $result->getBody();
    } else {
    return false;
    }
    }
    }