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 * An Amazon EC2 interface to create, describe, attach, detach and delete Elastic Block 0030 * Storage Volumes and Snaphsots. 0031 * 0032 * @category Zend 0033 * @package Zend_Service_Amazon 0034 * @subpackage Ec2 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 */ 0038 class Zend_Service_Amazon_Ec2_Ebs extends Zend_Service_Amazon_Ec2_Abstract 0039 { 0040 /** 0041 * Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance. 0042 * 0043 * You must specify an availability zone when creating a volume. The volume and 0044 * any instance to which it attaches must be in the same availability zone. 0045 * 0046 * @param string $size The size of the volume, in GiB. 0047 * @param string $availabilityZone The availability zone in which to create the new volume. 0048 * @return array 0049 */ 0050 public function createNewVolume($size, $availabilityZone) 0051 { 0052 $params = array(); 0053 $params['Action'] = 'CreateVolume'; 0054 $params['AvailabilityZone'] = $availabilityZone; 0055 $params['Size'] = $size; 0056 0057 $response = $this->sendRequest($params); 0058 $xpath = $response->getXPath(); 0059 0060 $return = array(); 0061 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); 0062 $return['size'] = $xpath->evaluate('string(//ec2:size/text())'); 0063 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); 0064 $return['createTime'] = $xpath->evaluate('string(//ec2:createTime/text())'); 0065 $return['availabilityZone'] = $xpath->evaluate('string(//ec2:availabilityZone/text())'); 0066 0067 return $return; 0068 } 0069 0070 /** 0071 * Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance. 0072 * 0073 * You must specify an availability zone when creating a volume. The volume and 0074 * any instance to which it attaches must be in the same availability zone. 0075 * 0076 * @param string $snapshotId The snapshot from which to create the new volume. 0077 * @param string $availabilityZone The availability zone in which to create the new volume. 0078 * @return array 0079 */ 0080 public function createVolumeFromSnapshot($snapshotId, $availabilityZone) 0081 { 0082 $params = array(); 0083 $params['Action'] = 'CreateVolume'; 0084 $params['AvailabilityZone'] = $availabilityZone; 0085 $params['SnapshotId'] = $snapshotId; 0086 0087 $response = $this->sendRequest($params); 0088 $xpath = $response->getXPath(); 0089 0090 $return = array(); 0091 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); 0092 $return['size'] = $xpath->evaluate('string(//ec2:size/text())'); 0093 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); 0094 $return['createTime'] = $xpath->evaluate('string(//ec2:createTime/text())'); 0095 $return['availabilityZone'] = $xpath->evaluate('string(//ec2:availabilityZone/text())'); 0096 $return['snapshotId'] = $xpath->evaluate('string(//ec2:snapshotId/text())'); 0097 0098 return $return; 0099 } 0100 0101 /** 0102 * Lists one or more Amazon EBS volumes that you own, If you do not 0103 * specify any volumes, Amazon EBS returns all volumes that you own. 0104 * 0105 * @param string|array $volumeId The ID or array of ID's of the volume(s) to list 0106 * @return array 0107 */ 0108 public function describeVolume($volumeId = null) 0109 { 0110 $params = array(); 0111 $params['Action'] = 'DescribeVolumes'; 0112 0113 if(is_array($volumeId) && !empty($volumeId)) { 0114 foreach($volumeId as $k=>$name) { 0115 $params['VolumeId.' . ($k+1)] = $name; 0116 } 0117 } elseif($volumeId) { 0118 $params['VolumeId.1'] = $volumeId; 0119 } 0120 0121 $response = $this->sendRequest($params); 0122 0123 $xpath = $response->getXPath(); 0124 $nodes = $xpath->query('//ec2:volumeSet/ec2:item', $response->getDocument()); 0125 0126 $return = array(); 0127 foreach ($nodes as $node) { 0128 $item = array(); 0129 0130 $item['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $node); 0131 $item['size'] = $xpath->evaluate('string(ec2:size/text())', $node); 0132 $item['status'] = $xpath->evaluate('string(ec2:status/text())', $node); 0133 $item['createTime'] = $xpath->evaluate('string(ec2:createTime/text())', $node); 0134 0135 $attachmentSet = $xpath->query('ec2:attachmentSet/ec2:item', $node); 0136 if($attachmentSet->length == 1) { 0137 $_as = $attachmentSet->item(0); 0138 $as = array(); 0139 $as['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $_as); 0140 $as['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $_as); 0141 $as['device'] = $xpath->evaluate('string(ec2:device/text())', $_as); 0142 $as['status'] = $xpath->evaluate('string(ec2:status/text())', $_as); 0143 $as['attachTime'] = $xpath->evaluate('string(ec2:attachTime/text())', $_as); 0144 $item['attachmentSet'] = $as; 0145 } 0146 0147 $return[] = $item; 0148 unset($item, $node); 0149 } 0150 0151 return $return; 0152 } 0153 0154 public function describeAttachedVolumes($instanceId) 0155 { 0156 $volumes = $this->describeVolume(); 0157 0158 $return = array(); 0159 foreach($volumes as $vol) { 0160 if(isset($vol['attachmentSet']) && $vol['attachmentSet']['instanceId'] == $instanceId) { 0161 $return[] = $vol; 0162 } 0163 } 0164 0165 return $return; 0166 } 0167 0168 /** 0169 * Attaches an Amazon EBS volume to an instance 0170 * 0171 * @param string $volumeId The ID of the Amazon EBS volume 0172 * @param string $instanceId The ID of the instance to which the volume attaches 0173 * @param string $device Specifies how the device is exposed to the instance (e.g., /dev/sdh). 0174 * @return array 0175 */ 0176 public function attachVolume($volumeId, $instanceId, $device) 0177 { 0178 $params = array(); 0179 $params['Action'] = 'AttachVolume'; 0180 $params['VolumeId'] = $volumeId; 0181 $params['InstanceId'] = $instanceId; 0182 $params['Device'] = $device; 0183 0184 $response = $this->sendRequest($params); 0185 0186 $xpath = $response->getXPath(); 0187 0188 $return = array(); 0189 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); 0190 $return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())'); 0191 $return['device'] = $xpath->evaluate('string(//ec2:device/text())'); 0192 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); 0193 $return['attachTime'] = $xpath->evaluate('string(//ec2:attachTime/text())'); 0194 0195 return $return; 0196 } 0197 0198 /** 0199 * Detaches an Amazon EBS volume from an instance 0200 * 0201 * @param string $volumeId The ID of the Amazon EBS volume 0202 * @param string $instanceId The ID of the instance from which the volume will detach 0203 * @param string $device The device name 0204 * @param boolean $force Forces detachment if the previous detachment attempt did not occur cleanly 0205 * (logging into an instance, unmounting the volume, and detaching normally). 0206 * This option can lead to data loss or a corrupted file system. Use this option 0207 * only as a last resort to detach an instance from a failed instance. The 0208 * instance will not have an opportunity to flush file system caches nor 0209 * file system meta data. 0210 * @return array 0211 */ 0212 public function detachVolume($volumeId, $instanceId = null, $device = null, $force = false) 0213 { 0214 $params = array(); 0215 $params['Action'] = 'DetachVolume'; 0216 $params['VolumeId'] = $volumeId; 0217 $params['InstanceId'] = strval($instanceId); 0218 $params['Device'] = strval($device); 0219 $params['Force'] = strval($force); 0220 0221 $response = $this->sendRequest($params); 0222 0223 $xpath = $response->getXPath(); 0224 0225 $return = array(); 0226 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); 0227 $return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())'); 0228 $return['device'] = $xpath->evaluate('string(//ec2:device/text())'); 0229 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); 0230 $return['attachTime'] = $xpath->evaluate('string(//ec2:attachTime/text())'); 0231 0232 return $return; 0233 } 0234 0235 /** 0236 * Deletes an Amazon EBS volume 0237 * 0238 * @param string $volumeId The ID of the volume to delete 0239 * @return boolean 0240 */ 0241 public function deleteVolume($volumeId) 0242 { 0243 $params = array(); 0244 $params['Action'] = 'DeleteVolume'; 0245 $params['VolumeId'] = $volumeId; 0246 0247 $response = $this->sendRequest($params); 0248 $xpath = $response->getXPath(); 0249 0250 $return = $xpath->evaluate('string(//ec2:return/text())'); 0251 0252 return ($return === "true"); 0253 } 0254 0255 /** 0256 * Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups, 0257 * to launch instances from identical snapshots, and to save data before shutting down an instance 0258 * 0259 * @param string $volumeId The ID of the Amazon EBS volume to snapshot 0260 * @return array 0261 */ 0262 public function createSnapshot($volumeId) 0263 { 0264 $params = array(); 0265 $params['Action'] = 'CreateSnapshot'; 0266 $params['VolumeId'] = $volumeId; 0267 0268 $response = $this->sendRequest($params); 0269 0270 $xpath = $response->getXPath(); 0271 0272 $return = array(); 0273 $return['snapshotId'] = $xpath->evaluate('string(//ec2:snapshotId/text())'); 0274 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); 0275 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); 0276 $return['startTime'] = $xpath->evaluate('string(//ec2:startTime/text())'); 0277 $return['progress'] = $xpath->evaluate('string(//ec2:progress/text())'); 0278 0279 return $return; 0280 } 0281 0282 /** 0283 * Describes the status of Amazon EBS snapshots 0284 * 0285 * @param string|array $snapshotId The ID or arry of ID's of the Amazon EBS snapshot 0286 * @return array 0287 */ 0288 public function describeSnapshot($snapshotId = null) 0289 { 0290 $params = array(); 0291 $params['Action'] = 'DescribeSnapshots'; 0292 0293 if(is_array($snapshotId) && !empty($snapshotId)) { 0294 foreach($snapshotId as $k=>$name) { 0295 $params['SnapshotId.' . ($k+1)] = $name; 0296 } 0297 } elseif($snapshotId) { 0298 $params['SnapshotId.1'] = $snapshotId; 0299 } 0300 0301 $response = $this->sendRequest($params); 0302 0303 $xpath = $response->getXPath(); 0304 $nodes = $xpath->query('//ec2:snapshotSet/ec2:item', $response->getDocument()); 0305 0306 $return = array(); 0307 foreach ($nodes as $node) { 0308 $item = array(); 0309 0310 $item['snapshotId'] = $xpath->evaluate('string(ec2:snapshotId/text())', $node); 0311 $item['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $node); 0312 $item['status'] = $xpath->evaluate('string(ec2:status/text())', $node); 0313 $item['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $node); 0314 $item['progress'] = $xpath->evaluate('string(ec2:progress/text())', $node); 0315 0316 $return[] = $item; 0317 unset($item, $node); 0318 } 0319 0320 return $return; 0321 } 0322 0323 /** 0324 * Deletes a snapshot of an Amazon EBS volume that is stored in Amazon S3 0325 * 0326 * @param string $snapshotId The ID of the Amazon EBS snapshot to delete 0327 * @return boolean 0328 */ 0329 public function deleteSnapshot($snapshotId) 0330 { 0331 $params = array(); 0332 $params['Action'] = 'DeleteSnapshot'; 0333 $params['SnapshotId'] = $snapshotId; 0334 0335 $response = $this->sendRequest($params); 0336 0337 $xpath = $response->getXPath(); 0338 $return = $xpath->evaluate('string(//ec2:return/text())'); 0339 0340 return ($return === "true"); 0341 } 0342 }