Windows.php 8.2 KB

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