PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Service/Amazon/Ec2/Instance/Windows.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 195 lines | 93 code | 25 blank | 77 comment | 2 complexity | 0cd6188c61eab919042df183948a0272 MD5 | raw file
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Service_Amazon_Ec2_Abstract
  24. */
  25. require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
  26. /**
  27. * @see Zend_Crypt_Hmac
  28. */
  29. require_once 'Zend/Crypt/Hmac.php';
  30. /**
  31. * @see Zend_Json
  32. */
  33. require_once 'Zend/Json.php';
  34. /**
  35. * An Amazon EC2 interface that allows yout to run, terminate, reboot and describe Amazon
  36. * Ec2 Instances.
  37. *
  38. * @category Zend
  39. * @package Zend_Service_Amazon
  40. * @subpackage Ec2
  41. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Service_Amazon_Ec2_Instance_Windows extends Zend_Service_Amazon_Ec2_Abstract
  45. {
  46. /**
  47. * Bundles an Amazon EC2 instance running Windows
  48. *
  49. * @param string $instanceId The instance you want to bundle
  50. * @param string $s3Bucket Where you want the ami to live on S3
  51. * @param string $s3Prefix The prefix you want to assign to the AMI on S3
  52. * @param integer $uploadExpiration The expiration of the upload policy. Amazon recommends 12 hours or longer.
  53. * This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
  54. * @return array containing the information on the new bundle operation
  55. */
  56. public function bundle($instanceId, $s3Bucket, $s3Prefix, $uploadExpiration = 1440)
  57. {
  58. $params = array();
  59. $params['Action'] = 'BundleInstance';
  60. $params['InstanceId'] = $instanceId;
  61. $params['Storage.S3.AWSAccessKeyId'] = $this->_getAccessKey();
  62. $params['Storage.S3.Bucket'] = $s3Bucket;
  63. $params['Storage.S3.Prefix'] = $s3Prefix;
  64. $uploadPolicy = $this->_getS3UploadPolicy($s3Bucket, $s3Prefix, $uploadExpiration);
  65. $params['Storage.S3.UploadPolicy'] = $uploadPolicy;
  66. $params['Storage.S3.UploadPolicySignature'] = $this->_signS3UploadPolicy($uploadPolicy);
  67. $response = $this->sendRequest($params);
  68. $xpath = $response->getXPath();
  69. $return = array();
  70. $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
  71. $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
  72. $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
  73. $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
  74. $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
  75. $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
  76. $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
  77. $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');
  78. return $return;
  79. }
  80. /**
  81. * Cancels an Amazon EC2 bundling operation
  82. *
  83. * @param string $bundleId The ID of the bundle task to cancel
  84. * @return array Information on the bundle task
  85. */
  86. public function cancelBundle($bundleId)
  87. {
  88. $params = array();
  89. $params['Action'] = 'CancelBundleTask';
  90. $params['BundleId'] = $bundleId;
  91. $response = $this->sendRequest($params);
  92. $xpath = $response->getXPath();
  93. $return = array();
  94. $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
  95. $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
  96. $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
  97. $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
  98. $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
  99. $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
  100. $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
  101. $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');
  102. return $return;
  103. }
  104. /**
  105. * Describes current bundling tasks
  106. *
  107. * @param string|array $bundleId A single or a list of bundle tasks that you want
  108. * to find information for.
  109. * @return array Information for the task that you requested
  110. */
  111. public function describeBundle($bundleId = '')
  112. {
  113. $params = array();
  114. $params['Action'] = 'DescribeBundleTasks';
  115. if(is_array($bundleId) && !empty($bundleId)) {
  116. foreach($bundleId as $k=>$name) {
  117. $params['bundleId.' . ($k+1)] = $name;
  118. }
  119. } elseif(!empty($bundleId)) {
  120. $params['bundleId.1'] = $bundleId;
  121. }
  122. $response = $this->sendRequest($params);
  123. $xpath = $response->getXPath();
  124. $items = $xpath->evaluate('//ec2:bundleInstanceTasksSet/ec2:item');
  125. $return = array();
  126. foreach($items as $item) {
  127. $i = array();
  128. $i['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $item);
  129. $i['bundleId'] = $xpath->evaluate('string(ec2:bundleId/text())', $item);
  130. $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item);
  131. $i['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $item);
  132. $i['updateTime'] = $xpath->evaluate('string(ec2:updateTime/text())', $item);
  133. $i['progress'] = $xpath->evaluate('string(ec2:progress/text())', $item);
  134. $i['storage']['s3']['bucket'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:bucket/text())', $item);
  135. $i['storage']['s3']['prefix'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:prefix/text())', $item);
  136. $return[] = $i;
  137. unset($i);
  138. }
  139. return $return;
  140. }
  141. /**
  142. * Generates the S3 Upload Policy Information
  143. *
  144. * @param string $bucketName Which bucket you want the ami to live in on S3
  145. * @param string $prefix The prefix you want to assign to the AMI on S3
  146. * @param integer $expireInMinutes The expiration of the upload policy. Amazon recommends 12 hours or longer.
  147. * This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
  148. * @return string Base64 encoded string that is the upload policy
  149. */
  150. protected function _getS3UploadPolicy($bucketName, $prefix, $expireInMinutes = 1440)
  151. {
  152. $arrParams = array();
  153. $arrParams['expiration'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", (time() + ($expireInMinutes * 60)));
  154. $arrParams['conditions'][] = array('bucket' => $bucketName);
  155. $arrParams['conditions'][] = array('acl' => 'ec2-bundle-read');
  156. $arrParams['conditions'][] = array('starts-with', '$key', $prefix);
  157. return base64_encode(Zend_Json::encode($arrParams));
  158. }
  159. /**
  160. * Signed S3 Upload Policy
  161. *
  162. * @param string $policy Base64 Encoded string that is the upload policy
  163. * @return string SHA1 encoded S3 Upload Policy
  164. */
  165. protected function _signS3UploadPolicy($policy)
  166. {
  167. $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA1', $policy, Zend_Crypt_Hmac::BINARY);
  168. return $hmac;
  169. }
  170. }