web/lib/Zend/Service/Amazon/Ec2/Instance/Windows.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service_Amazon
       
    17  * @subpackage Ec2
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Windows.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_Amazon_Ec2_Abstract
       
    25  */
       
    26 require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Crypt_Hmac
       
    30  */
       
    31 require_once 'Zend/Crypt/Hmac.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Json
       
    35  */
       
    36 require_once 'Zend/Json.php';
       
    37 
       
    38 /**
       
    39  * An Amazon EC2 interface that allows yout to run, terminate, reboot and describe Amazon
       
    40  * Ec2 Instances.
       
    41  *
       
    42  * @category   Zend
       
    43  * @package    Zend_Service_Amazon
       
    44  * @subpackage Ec2
       
    45  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    46  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    47  */
       
    48 class Zend_Service_Amazon_Ec2_Instance_Windows extends Zend_Service_Amazon_Ec2_Abstract
       
    49 {
       
    50     /**
       
    51      * Bundles an Amazon EC2 instance running Windows
       
    52      *
       
    53      * @param string $instanceId        The instance you want to bundle
       
    54      * @param string $s3Bucket          Where you want the ami to live on S3
       
    55      * @param string $s3Prefix          The prefix you want to assign to the AMI on S3
       
    56      * @param integer $uploadExpiration The expiration of the upload policy.  Amazon recommends 12 hours or longer.
       
    57      *                                  This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
       
    58      * @return array                    containing the information on the new bundle operation
       
    59      */
       
    60     public function bundle($instanceId, $s3Bucket, $s3Prefix, $uploadExpiration = 1440)
       
    61     {
       
    62         $params = array();
       
    63         $params['Action'] = 'BundleInstance';
       
    64         $params['InstanceId'] = $instanceId;
       
    65         $params['Storage.S3.AWSAccessKeyId'] = $this->_getAccessKey();
       
    66         $params['Storage.S3.Bucket'] = $s3Bucket;
       
    67         $params['Storage.S3.Prefix'] = $s3Prefix;
       
    68         $uploadPolicy = $this->_getS3UploadPolicy($s3Bucket, $s3Prefix, $uploadExpiration);
       
    69         $params['Storage.S3.UploadPolicy'] = $uploadPolicy;
       
    70         $params['Storage.S3.UploadPolicySignature'] = $this->_signS3UploadPolicy($uploadPolicy);
       
    71 
       
    72         $response = $this->sendRequest($params);
       
    73 
       
    74         $xpath = $response->getXPath();
       
    75 
       
    76         $return = array();
       
    77         $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
       
    78         $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
       
    79         $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
       
    80         $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
       
    81         $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
       
    82         $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
       
    83         $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
       
    84         $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');
       
    85 
       
    86         return $return;
       
    87     }
       
    88 
       
    89     /**
       
    90      * Cancels an Amazon EC2 bundling operation
       
    91      *
       
    92      * @param string $bundleId          The ID of the bundle task to cancel
       
    93      * @return array                    Information on the bundle task
       
    94      */
       
    95     public function cancelBundle($bundleId)
       
    96     {
       
    97         $params = array();
       
    98         $params['Action'] = 'CancelBundleTask';
       
    99         $params['BundleId'] = $bundleId;
       
   100 
       
   101         $response = $this->sendRequest($params);
       
   102 
       
   103         $xpath = $response->getXPath();
       
   104 
       
   105         $return = array();
       
   106         $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
       
   107         $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
       
   108         $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
       
   109         $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
       
   110         $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
       
   111         $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
       
   112         $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
       
   113         $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');
       
   114 
       
   115         return $return;
       
   116     }
       
   117 
       
   118     /**
       
   119      * Describes current bundling tasks
       
   120      *
       
   121      * @param string|array $bundleId            A single or a list of bundle tasks that you want
       
   122      *                                          to find information for.
       
   123      * @return array                            Information for the task that you requested
       
   124      */
       
   125     public function describeBundle($bundleId = '')
       
   126     {
       
   127         $params = array();
       
   128         $params['Action'] = 'DescribeBundleTasks';
       
   129 
       
   130         if(is_array($bundleId) && !empty($bundleId)) {
       
   131             foreach($bundleId as $k=>$name) {
       
   132                 $params['bundleId.' . ($k+1)] = $name;
       
   133             }
       
   134         } elseif(!empty($bundleId)) {
       
   135             $params['bundleId.1'] = $bundleId;
       
   136         }
       
   137 
       
   138         $response = $this->sendRequest($params);
       
   139 
       
   140         $xpath = $response->getXPath();
       
   141 
       
   142         $items = $xpath->evaluate('//ec2:bundleInstanceTasksSet/ec2:item');
       
   143         $return = array();
       
   144 
       
   145         foreach($items as $item) {
       
   146             $i = array();
       
   147             $i['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $item);
       
   148             $i['bundleId'] = $xpath->evaluate('string(ec2:bundleId/text())', $item);
       
   149             $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item);
       
   150             $i['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $item);
       
   151             $i['updateTime'] = $xpath->evaluate('string(ec2:updateTime/text())', $item);
       
   152             $i['progress'] = $xpath->evaluate('string(ec2:progress/text())', $item);
       
   153             $i['storage']['s3']['bucket'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:bucket/text())', $item);
       
   154             $i['storage']['s3']['prefix'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:prefix/text())', $item);
       
   155 
       
   156             $return[] = $i;
       
   157             unset($i);
       
   158         }
       
   159 
       
   160 
       
   161         return $return;
       
   162     }
       
   163 
       
   164     /**
       
   165      * Generates the S3 Upload Policy Information
       
   166      *
       
   167      * @param string $bucketName        Which bucket you want the ami to live in on S3
       
   168      * @param string $prefix            The prefix you want to assign to the AMI on S3
       
   169      * @param integer $expireInMinutes  The expiration of the upload policy.  Amazon recommends 12 hours or longer.
       
   170      *                                  This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
       
   171      * @return string                   Base64 encoded string that is the upload policy
       
   172      */
       
   173     protected function _getS3UploadPolicy($bucketName, $prefix, $expireInMinutes = 1440)
       
   174     {
       
   175         $arrParams = array();
       
   176         $arrParams['expiration'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", (time() + ($expireInMinutes * 60)));
       
   177         $arrParams['conditions'][] = array('bucket' => $bucketName);
       
   178         $arrParams['conditions'][] = array('acl' => 'ec2-bundle-read');
       
   179         $arrParams['conditions'][] = array('starts-with', '$key', $prefix);
       
   180 
       
   181         return base64_encode(Zend_Json::encode($arrParams));
       
   182     }
       
   183 
       
   184     /**
       
   185      * Signed S3 Upload Policy
       
   186      *
       
   187      * @param string $policy            Base64 Encoded string that is the upload policy
       
   188      * @return string                   SHA1 encoded S3 Upload Policy
       
   189      */
       
   190     protected function _signS3UploadPolicy($policy)
       
   191     {
       
   192         $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA1', $policy, Zend_Crypt_Hmac::BINARY);
       
   193         return $hmac;
       
   194     }
       
   195 }