File indexing completed on 2025-03-02 05:29:46

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_Amazon
0017  * @subpackage Ec2
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Service_Amazon_Ec2_Abstract
0025  */
0026 // require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
0027 
0028 /**
0029  * @see Zend_Crypt_Hmac
0030  */
0031 // require_once 'Zend/Crypt/Hmac.php';
0032 
0033 /**
0034  * @see Zend_Json
0035  */
0036 // require_once 'Zend/Json.php';
0037 
0038 /**
0039  * An Amazon EC2 interface that allows yout to run, terminate, reboot and describe Amazon
0040  * Ec2 Instances.
0041  *
0042  * @category   Zend
0043  * @package    Zend_Service_Amazon
0044  * @subpackage Ec2
0045  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0046  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0047  */
0048 class Zend_Service_Amazon_Ec2_Instance_Windows extends Zend_Service_Amazon_Ec2_Abstract
0049 {
0050     /**
0051      * Bundles an Amazon EC2 instance running Windows
0052      *
0053      * @param string $instanceId        The instance you want to bundle
0054      * @param string $s3Bucket          Where you want the ami to live on S3
0055      * @param string $s3Prefix          The prefix you want to assign to the AMI on S3
0056      * @param integer $uploadExpiration The expiration of the upload policy.  Amazon recommends 12 hours or longer.
0057      *                                  This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
0058      * @return array                    containing the information on the new bundle operation
0059      */
0060     public function bundle($instanceId, $s3Bucket, $s3Prefix, $uploadExpiration = 1440)
0061     {
0062         $params = array();
0063         $params['Action'] = 'BundleInstance';
0064         $params['InstanceId'] = $instanceId;
0065         $params['Storage.S3.AWSAccessKeyId'] = $this->_getAccessKey();
0066         $params['Storage.S3.Bucket'] = $s3Bucket;
0067         $params['Storage.S3.Prefix'] = $s3Prefix;
0068         $uploadPolicy = $this->_getS3UploadPolicy($s3Bucket, $s3Prefix, $uploadExpiration);
0069         $params['Storage.S3.UploadPolicy'] = $uploadPolicy;
0070         $params['Storage.S3.UploadPolicySignature'] = $this->_signS3UploadPolicy($uploadPolicy);
0071 
0072         $response = $this->sendRequest($params);
0073 
0074         $xpath = $response->getXPath();
0075 
0076         $return = array();
0077         $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
0078         $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
0079         $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
0080         $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
0081         $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
0082         $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
0083         $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
0084         $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');
0085 
0086         return $return;
0087     }
0088 
0089     /**
0090      * Cancels an Amazon EC2 bundling operation
0091      *
0092      * @param string $bundleId          The ID of the bundle task to cancel
0093      * @return array                    Information on the bundle task
0094      */
0095     public function cancelBundle($bundleId)
0096     {
0097         $params = array();
0098         $params['Action'] = 'CancelBundleTask';
0099         $params['BundleId'] = $bundleId;
0100 
0101         $response = $this->sendRequest($params);
0102 
0103         $xpath = $response->getXPath();
0104 
0105         $return = array();
0106         $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
0107         $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
0108         $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
0109         $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
0110         $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
0111         $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
0112         $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
0113         $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');
0114 
0115         return $return;
0116     }
0117 
0118     /**
0119      * Describes current bundling tasks
0120      *
0121      * @param string|array $bundleId            A single or a list of bundle tasks that you want
0122      *                                          to find information for.
0123      * @return array                            Information for the task that you requested
0124      */
0125     public function describeBundle($bundleId = '')
0126     {
0127         $params = array();
0128         $params['Action'] = 'DescribeBundleTasks';
0129 
0130         if(is_array($bundleId) && !empty($bundleId)) {
0131             foreach($bundleId as $k=>$name) {
0132                 $params['bundleId.' . ($k+1)] = $name;
0133             }
0134         } elseif(!empty($bundleId)) {
0135             $params['bundleId.1'] = $bundleId;
0136         }
0137 
0138         $response = $this->sendRequest($params);
0139 
0140         $xpath = $response->getXPath();
0141 
0142         $items = $xpath->evaluate('//ec2:bundleInstanceTasksSet/ec2:item');
0143         $return = array();
0144 
0145         foreach($items as $item) {
0146             $i = array();
0147             $i['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $item);
0148             $i['bundleId'] = $xpath->evaluate('string(ec2:bundleId/text())', $item);
0149             $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item);
0150             $i['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $item);
0151             $i['updateTime'] = $xpath->evaluate('string(ec2:updateTime/text())', $item);
0152             $i['progress'] = $xpath->evaluate('string(ec2:progress/text())', $item);
0153             $i['storage']['s3']['bucket'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:bucket/text())', $item);
0154             $i['storage']['s3']['prefix'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:prefix/text())', $item);
0155 
0156             $return[] = $i;
0157             unset($i);
0158         }
0159 
0160 
0161         return $return;
0162     }
0163 
0164     /**
0165      * Generates the S3 Upload Policy Information
0166      *
0167      * @param string $bucketName        Which bucket you want the ami to live in on S3
0168      * @param string $prefix            The prefix you want to assign to the AMI on S3
0169      * @param integer $expireInMinutes  The expiration of the upload policy.  Amazon recommends 12 hours or longer.
0170      *                                  This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
0171      * @return string                   Base64 encoded string that is the upload policy
0172      */
0173     protected function _getS3UploadPolicy($bucketName, $prefix, $expireInMinutes = 1440)
0174     {
0175         $arrParams = array();
0176         $arrParams['expiration'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", (time() + ($expireInMinutes * 60)));
0177         $arrParams['conditions'][] = array('bucket' => $bucketName);
0178         $arrParams['conditions'][] = array('acl' => 'ec2-bundle-read');
0179         $arrParams['conditions'][] = array('starts-with', '$key', $prefix);
0180 
0181         return base64_encode(Zend_Json::encode($arrParams));
0182     }
0183 
0184     /**
0185      * Signed S3 Upload Policy
0186      *
0187      * @param string $policy            Base64 Encoded string that is the upload policy
0188      * @return string                   SHA1 encoded S3 Upload Policy
0189      */
0190     protected function _signS3UploadPolicy($policy)
0191     {
0192         $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA1', $policy, Zend_Crypt_Hmac::BINARY);
0193         return $hmac;
0194     }
0195 }