|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Service/Amazon/Ec2/Abstract.php'; 00027 00031 require_once 'Zend/Crypt/Hmac.php'; 00032 00036 require_once 'Zend/Json.php'; 00037 00048 class Zend_Service_Amazon_Ec2_Instance_Windows extends Zend_Service_Amazon_Ec2_Abstract 00049 { 00060 public function bundle($instanceId, $s3Bucket, $s3Prefix, $uploadExpiration = 1440) 00061 { 00062 $params = array(); 00063 $params['Action'] = 'BundleInstance'; 00064 $params['InstanceId'] = $instanceId; 00065 $params['Storage.S3.AWSAccessKeyId'] = $this->_getAccessKey(); 00066 $params['Storage.S3.Bucket'] = $s3Bucket; 00067 $params['Storage.S3.Prefix'] = $s3Prefix; 00068 $uploadPolicy = $this->_getS3UploadPolicy($s3Bucket, $s3Prefix, $uploadExpiration); 00069 $params['Storage.S3.UploadPolicy'] = $uploadPolicy; 00070 $params['Storage.S3.UploadPolicySignature'] = $this->_signS3UploadPolicy($uploadPolicy); 00071 00072 $response = $this->sendRequest($params); 00073 00074 $xpath = $response->getXPath(); 00075 00076 $return = array(); 00077 $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())'); 00078 $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())'); 00079 $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())'); 00080 $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())'); 00081 $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())'); 00082 $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())'); 00083 $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())'); 00084 $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())'); 00085 00086 return $return; 00087 } 00088 00095 public function cancelBundle($bundleId) 00096 { 00097 $params = array(); 00098 $params['Action'] = 'CancelBundleTask'; 00099 $params['BundleId'] = $bundleId; 00100 00101 $response = $this->sendRequest($params); 00102 00103 $xpath = $response->getXPath(); 00104 00105 $return = array(); 00106 $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())'); 00107 $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())'); 00108 $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())'); 00109 $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())'); 00110 $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())'); 00111 $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())'); 00112 $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())'); 00113 $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())'); 00114 00115 return $return; 00116 } 00117 00125 public function describeBundle($bundleId = '') 00126 { 00127 $params = array(); 00128 $params['Action'] = 'DescribeBundleTasks'; 00129 00130 if(is_array($bundleId) && !empty($bundleId)) { 00131 foreach($bundleId as $k=>$name) { 00132 $params['bundleId.' . ($k+1)] = $name; 00133 } 00134 } elseif(!empty($bundleId)) { 00135 $params['bundleId.1'] = $bundleId; 00136 } 00137 00138 $response = $this->sendRequest($params); 00139 00140 $xpath = $response->getXPath(); 00141 00142 $items = $xpath->evaluate('//ec2:bundleInstanceTasksSet/ec2:item'); 00143 $return = array(); 00144 00145 foreach($items as $item) { 00146 $i = array(); 00147 $i['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $item); 00148 $i['bundleId'] = $xpath->evaluate('string(ec2:bundleId/text())', $item); 00149 $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item); 00150 $i['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $item); 00151 $i['updateTime'] = $xpath->evaluate('string(ec2:updateTime/text())', $item); 00152 $i['progress'] = $xpath->evaluate('string(ec2:progress/text())', $item); 00153 $i['storage']['s3']['bucket'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:bucket/text())', $item); 00154 $i['storage']['s3']['prefix'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:prefix/text())', $item); 00155 00156 $return[] = $i; 00157 unset($i); 00158 } 00159 00160 00161 return $return; 00162 } 00163 00173 protected function _getS3UploadPolicy($bucketName, $prefix, $expireInMinutes = 1440) 00174 { 00175 $arrParams = array(); 00176 $arrParams['expiration'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", (time() + ($expireInMinutes * 60))); 00177 $arrParams['conditions'][] = array('bucket' => $bucketName); 00178 $arrParams['conditions'][] = array('acl' => 'ec2-bundle-read'); 00179 $arrParams['conditions'][] = array('starts-with', '$key', $prefix); 00180 00181 return base64_encode(Zend_Json::encode($arrParams)); 00182 } 00183 00190 protected function _signS3UploadPolicy($policy) 00191 { 00192 $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA1', $policy, Zend_Crypt_Hmac::BINARY); 00193 return $hmac; 00194 } 00195 }