|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Ebs.php 22047 2010-04-28 22:14:51Z shahar $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Service_Amazon_Ec2_Abstract |
|
25 */ |
|
26 require_once 'Zend/Service/Amazon/Ec2/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * An Amazon EC2 interface to create, describe, attach, detach and delete Elastic Block |
|
30 * Storage Volumes and Snaphsots. |
|
31 * |
|
32 * @category Zend |
|
33 * @package Zend_Service_Amazon |
|
34 * @subpackage Ec2 |
|
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
36 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
37 */ |
|
38 class Zend_Service_Amazon_Ec2_Ebs extends Zend_Service_Amazon_Ec2_Abstract |
|
39 { |
|
40 /** |
|
41 * Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance. |
|
42 * |
|
43 * You must specify an availability zone when creating a volume. The volume and |
|
44 * any instance to which it attaches must be in the same availability zone. |
|
45 * |
|
46 * @param string $size The size of the volume, in GiB. |
|
47 * @param string $availabilityZone The availability zone in which to create the new volume. |
|
48 * @return array |
|
49 */ |
|
50 public function createNewVolume($size, $availabilityZone) |
|
51 { |
|
52 $params = array(); |
|
53 $params['Action'] = 'CreateVolume'; |
|
54 $params['AvailabilityZone'] = $availabilityZone; |
|
55 $params['Size'] = $size; |
|
56 |
|
57 $response = $this->sendRequest($params); |
|
58 $xpath = $response->getXPath(); |
|
59 |
|
60 $return = array(); |
|
61 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); |
|
62 $return['size'] = $xpath->evaluate('string(//ec2:size/text())'); |
|
63 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); |
|
64 $return['createTime'] = $xpath->evaluate('string(//ec2:createTime/text())'); |
|
65 $return['availabilityZone'] = $xpath->evaluate('string(//ec2:availabilityZone/text())'); |
|
66 |
|
67 return $return; |
|
68 } |
|
69 |
|
70 /** |
|
71 * Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance. |
|
72 * |
|
73 * You must specify an availability zone when creating a volume. The volume and |
|
74 * any instance to which it attaches must be in the same availability zone. |
|
75 * |
|
76 * @param string $snapshotId The snapshot from which to create the new volume. |
|
77 * @param string $availabilityZone The availability zone in which to create the new volume. |
|
78 * @return array |
|
79 */ |
|
80 public function createVolumeFromSnapshot($snapshotId, $availabilityZone) |
|
81 { |
|
82 $params = array(); |
|
83 $params['Action'] = 'CreateVolume'; |
|
84 $params['AvailabilityZone'] = $availabilityZone; |
|
85 $params['SnapshotId'] = $snapshotId; |
|
86 |
|
87 $response = $this->sendRequest($params); |
|
88 $xpath = $response->getXPath(); |
|
89 |
|
90 $return = array(); |
|
91 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); |
|
92 $return['size'] = $xpath->evaluate('string(//ec2:size/text())'); |
|
93 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); |
|
94 $return['createTime'] = $xpath->evaluate('string(//ec2:createTime/text())'); |
|
95 $return['availabilityZone'] = $xpath->evaluate('string(//ec2:availabilityZone/text())'); |
|
96 $return['snapshotId'] = $xpath->evaluate('string(//ec2:snapshotId/text())'); |
|
97 |
|
98 return $return; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Lists one or more Amazon EBS volumes that you own, If you do not |
|
103 * specify any volumes, Amazon EBS returns all volumes that you own. |
|
104 * |
|
105 * @param string|array $volumeId The ID or array of ID's of the volume(s) to list |
|
106 * @return array |
|
107 */ |
|
108 public function describeVolume($volumeId = null) |
|
109 { |
|
110 $params = array(); |
|
111 $params['Action'] = 'DescribeVolumes'; |
|
112 |
|
113 if(is_array($volumeId) && !empty($volumeId)) { |
|
114 foreach($volumeId as $k=>$name) { |
|
115 $params['VolumeId.' . ($k+1)] = $name; |
|
116 } |
|
117 } elseif($volumeId) { |
|
118 $params['VolumeId.1'] = $volumeId; |
|
119 } |
|
120 |
|
121 $response = $this->sendRequest($params); |
|
122 |
|
123 $xpath = $response->getXPath(); |
|
124 $nodes = $xpath->query('//ec2:volumeSet/ec2:item', $response->getDocument()); |
|
125 |
|
126 $return = array(); |
|
127 foreach ($nodes as $node) { |
|
128 $item = array(); |
|
129 |
|
130 $item['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $node); |
|
131 $item['size'] = $xpath->evaluate('string(ec2:size/text())', $node); |
|
132 $item['status'] = $xpath->evaluate('string(ec2:status/text())', $node); |
|
133 $item['createTime'] = $xpath->evaluate('string(ec2:createTime/text())', $node); |
|
134 |
|
135 $attachmentSet = $xpath->query('ec2:attachmentSet/ec2:item', $node); |
|
136 if($attachmentSet->length == 1) { |
|
137 $_as = $attachmentSet->item(0); |
|
138 $as = array(); |
|
139 $as['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $_as); |
|
140 $as['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $_as); |
|
141 $as['device'] = $xpath->evaluate('string(ec2:device/text())', $_as); |
|
142 $as['status'] = $xpath->evaluate('string(ec2:status/text())', $_as); |
|
143 $as['attachTime'] = $xpath->evaluate('string(ec2:attachTime/text())', $_as); |
|
144 $item['attachmentSet'] = $as; |
|
145 } |
|
146 |
|
147 $return[] = $item; |
|
148 unset($item, $node); |
|
149 } |
|
150 |
|
151 return $return; |
|
152 } |
|
153 |
|
154 public function describeAttachedVolumes($instanceId) |
|
155 { |
|
156 $volumes = $this->describeVolume(); |
|
157 |
|
158 $return = array(); |
|
159 foreach($volumes as $vol) { |
|
160 if(isset($vol['attachmentSet']) && $vol['attachmentSet']['instanceId'] == $instanceId) { |
|
161 $return[] = $vol; |
|
162 } |
|
163 } |
|
164 |
|
165 return $return; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Attaches an Amazon EBS volume to an instance |
|
170 * |
|
171 * @param string $volumeId The ID of the Amazon EBS volume |
|
172 * @param string $instanceId The ID of the instance to which the volume attaches |
|
173 * @param string $device Specifies how the device is exposed to the instance (e.g., /dev/sdh). |
|
174 * @return array |
|
175 */ |
|
176 public function attachVolume($volumeId, $instanceId, $device) |
|
177 { |
|
178 $params = array(); |
|
179 $params['Action'] = 'AttachVolume'; |
|
180 $params['VolumeId'] = $volumeId; |
|
181 $params['InstanceId'] = $instanceId; |
|
182 $params['Device'] = $device; |
|
183 |
|
184 $response = $this->sendRequest($params); |
|
185 |
|
186 $xpath = $response->getXPath(); |
|
187 |
|
188 $return = array(); |
|
189 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); |
|
190 $return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())'); |
|
191 $return['device'] = $xpath->evaluate('string(//ec2:device/text())'); |
|
192 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); |
|
193 $return['attachTime'] = $xpath->evaluate('string(//ec2:attachTime/text())'); |
|
194 |
|
195 return $return; |
|
196 } |
|
197 |
|
198 /** |
|
199 * Detaches an Amazon EBS volume from an instance |
|
200 * |
|
201 * @param string $volumeId The ID of the Amazon EBS volume |
|
202 * @param string $instanceId The ID of the instance from which the volume will detach |
|
203 * @param string $device The device name |
|
204 * @param boolean $force Forces detachment if the previous detachment attempt did not occur cleanly |
|
205 * (logging into an instance, unmounting the volume, and detaching normally). |
|
206 * This option can lead to data loss or a corrupted file system. Use this option |
|
207 * only as a last resort to detach an instance from a failed instance. The |
|
208 * instance will not have an opportunity to flush file system caches nor |
|
209 * file system meta data. |
|
210 * @return array |
|
211 */ |
|
212 public function detachVolume($volumeId, $instanceId = null, $device = null, $force = false) |
|
213 { |
|
214 $params = array(); |
|
215 $params['Action'] = 'DetachVolume'; |
|
216 $params['VolumeId'] = $volumeId; |
|
217 $params['InstanceId'] = strval($instanceId); |
|
218 $params['Device'] = strval($device); |
|
219 $params['Force'] = strval($force); |
|
220 |
|
221 $response = $this->sendRequest($params); |
|
222 |
|
223 $xpath = $response->getXPath(); |
|
224 |
|
225 $return = array(); |
|
226 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); |
|
227 $return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())'); |
|
228 $return['device'] = $xpath->evaluate('string(//ec2:device/text())'); |
|
229 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); |
|
230 $return['attachTime'] = $xpath->evaluate('string(//ec2:attachTime/text())'); |
|
231 |
|
232 return $return; |
|
233 } |
|
234 |
|
235 /** |
|
236 * Deletes an Amazon EBS volume |
|
237 * |
|
238 * @param string $volumeId The ID of the volume to delete |
|
239 * @return boolean |
|
240 */ |
|
241 public function deleteVolume($volumeId) |
|
242 { |
|
243 $params = array(); |
|
244 $params['Action'] = 'DeleteVolume'; |
|
245 $params['VolumeId'] = $volumeId; |
|
246 |
|
247 $response = $this->sendRequest($params); |
|
248 $xpath = $response->getXPath(); |
|
249 |
|
250 $return = $xpath->evaluate('string(//ec2:return/text())'); |
|
251 |
|
252 return ($return === "true"); |
|
253 } |
|
254 |
|
255 /** |
|
256 * Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups, |
|
257 * to launch instances from identical snapshots, and to save data before shutting down an instance |
|
258 * |
|
259 * @param string $volumeId The ID of the Amazon EBS volume to snapshot |
|
260 * @return array |
|
261 */ |
|
262 public function createSnapshot($volumeId) |
|
263 { |
|
264 $params = array(); |
|
265 $params['Action'] = 'CreateSnapshot'; |
|
266 $params['VolumeId'] = $volumeId; |
|
267 |
|
268 $response = $this->sendRequest($params); |
|
269 |
|
270 $xpath = $response->getXPath(); |
|
271 |
|
272 $return = array(); |
|
273 $return['snapshotId'] = $xpath->evaluate('string(//ec2:snapshotId/text())'); |
|
274 $return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())'); |
|
275 $return['status'] = $xpath->evaluate('string(//ec2:status/text())'); |
|
276 $return['startTime'] = $xpath->evaluate('string(//ec2:startTime/text())'); |
|
277 $return['progress'] = $xpath->evaluate('string(//ec2:progress/text())'); |
|
278 |
|
279 return $return; |
|
280 } |
|
281 |
|
282 /** |
|
283 * Describes the status of Amazon EBS snapshots |
|
284 * |
|
285 * @param string|array $snapshotId The ID or arry of ID's of the Amazon EBS snapshot |
|
286 * @return array |
|
287 */ |
|
288 public function describeSnapshot($snapshotId = null) |
|
289 { |
|
290 $params = array(); |
|
291 $params['Action'] = 'DescribeSnapshots'; |
|
292 |
|
293 if(is_array($snapshotId) && !empty($snapshotId)) { |
|
294 foreach($snapshotId as $k=>$name) { |
|
295 $params['SnapshotId.' . ($k+1)] = $name; |
|
296 } |
|
297 } elseif($snapshotId) { |
|
298 $params['SnapshotId.1'] = $snapshotId; |
|
299 } |
|
300 |
|
301 $response = $this->sendRequest($params); |
|
302 |
|
303 $xpath = $response->getXPath(); |
|
304 $nodes = $xpath->query('//ec2:snapshotSet/ec2:item', $response->getDocument()); |
|
305 |
|
306 $return = array(); |
|
307 foreach ($nodes as $node) { |
|
308 $item = array(); |
|
309 |
|
310 $item['snapshotId'] = $xpath->evaluate('string(ec2:snapshotId/text())', $node); |
|
311 $item['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $node); |
|
312 $item['status'] = $xpath->evaluate('string(ec2:status/text())', $node); |
|
313 $item['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $node); |
|
314 $item['progress'] = $xpath->evaluate('string(ec2:progress/text())', $node); |
|
315 |
|
316 $return[] = $item; |
|
317 unset($item, $node); |
|
318 } |
|
319 |
|
320 return $return; |
|
321 } |
|
322 |
|
323 /** |
|
324 * Deletes a snapshot of an Amazon EBS volume that is stored in Amazon S3 |
|
325 * |
|
326 * @param string $snapshotId The ID of the Amazon EBS snapshot to delete |
|
327 * @return boolean |
|
328 */ |
|
329 public function deleteSnapshot($snapshotId) |
|
330 { |
|
331 $params = array(); |
|
332 $params['Action'] = 'DeleteSnapshot'; |
|
333 $params['SnapshotId'] = $snapshotId; |
|
334 |
|
335 $response = $this->sendRequest($params); |
|
336 |
|
337 $xpath = $response->getXPath(); |
|
338 $return = $xpath->evaluate('string(//ec2:return/text())'); |
|
339 |
|
340 return ($return === "true"); |
|
341 } |
|
342 } |