grc276-blog-laravel /src/application/libraries/Zend/Service/Amazon/Ec2/Ebs.php

Language PHP Lines 343
MD5 Hash 0188dea4b6c23f14979e82716d37383d Estimated Cost $4,516 (why?)
Repository https://bitbucket.org/masnug/grc276-blog-laravel View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Service_Amazon
 * @subpackage Ec2
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Ebs.php 23775 2011-03-01 17:25:24Z ralph $
 */

/**
 * @see Zend_Service_Amazon_Ec2_Abstract
 */
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';

/**
 * An Amazon EC2 interface to create, describe, attach, detach and delete Elastic Block
 * Storage Volumes and Snaphsots.
 *
 * @category   Zend
 * @package    Zend_Service_Amazon
 * @subpackage Ec2
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Service_Amazon_Ec2_Ebs extends Zend_Service_Amazon_Ec2_Abstract
{
    /**
     * Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance.
     *
     * You must specify an availability zone when creating a volume. The volume and
     * any instance to which it attaches must be in the same availability zone.
     *
     * @param string $size                  The size of the volume, in GiB.
     * @param string $availabilityZone      The availability zone in which to create the new volume.
     * @return array
     */
    public function createNewVolume($size, $availabilityZone)
    {
        $params = array();
        $params['Action'] = 'CreateVolume';
        $params['AvailabilityZone'] = $availabilityZone;
        $params['Size'] = $size;

        $response = $this->sendRequest($params);
        $xpath = $response->getXPath();

        $return = array();
        $return['volumeId']             = $xpath->evaluate('string(//ec2:volumeId/text())');
        $return['size']                 = $xpath->evaluate('string(//ec2:size/text())');
        $return['status']               = $xpath->evaluate('string(//ec2:status/text())');
        $return['createTime']           = $xpath->evaluate('string(//ec2:createTime/text())');
        $return['availabilityZone']     = $xpath->evaluate('string(//ec2:availabilityZone/text())');

        return $return;
    }

    /**
     * Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance.
     *
     * You must specify an availability zone when creating a volume. The volume and
     * any instance to which it attaches must be in the same availability zone.
     *
     * @param string $snapshotId            The snapshot from which to create the new volume.
     * @param string $availabilityZone      The availability zone in which to create the new volume.
     * @return array
     */
    public function createVolumeFromSnapshot($snapshotId, $availabilityZone)
    {
        $params = array();
        $params['Action'] = 'CreateVolume';
        $params['AvailabilityZone'] = $availabilityZone;
        $params['SnapshotId'] = $snapshotId;

        $response = $this->sendRequest($params);
        $xpath = $response->getXPath();

        $return = array();
        $return['volumeId']             = $xpath->evaluate('string(//ec2:volumeId/text())');
        $return['size']                 = $xpath->evaluate('string(//ec2:size/text())');
        $return['status']               = $xpath->evaluate('string(//ec2:status/text())');
        $return['createTime']           = $xpath->evaluate('string(//ec2:createTime/text())');
        $return['availabilityZone']     = $xpath->evaluate('string(//ec2:availabilityZone/text())');
        $return['snapshotId']           = $xpath->evaluate('string(//ec2:snapshotId/text())');

        return $return;
    }

    /**
     * Lists one or more Amazon EBS volumes that you own, If you do not
     * specify any volumes, Amazon EBS returns all volumes that you own.
     *
     * @param string|array $volumeId        The ID or array of ID's of the volume(s) to list
     * @return array
     */
    public function describeVolume($volumeId = null)
    {
        $params = array();
        $params['Action'] = 'DescribeVolumes';

        if(is_array($volumeId) && !empty($volumeId)) {
            foreach($volumeId as $k=>$name) {
                $params['VolumeId.' . ($k+1)] = $name;
            }
        } elseif($volumeId) {
            $params['VolumeId.1'] = $volumeId;
        }

        $response = $this->sendRequest($params);

        $xpath  = $response->getXPath();
        $nodes = $xpath->query('//ec2:volumeSet/ec2:item', $response->getDocument());

        $return = array();
        foreach ($nodes as $node) {
            $item = array();

            $item['volumeId']   = $xpath->evaluate('string(ec2:volumeId/text())', $node);
            $item['size']       = $xpath->evaluate('string(ec2:size/text())', $node);
            $item['status']     = $xpath->evaluate('string(ec2:status/text())', $node);
            $item['createTime'] = $xpath->evaluate('string(ec2:createTime/text())', $node);

            $attachmentSet = $xpath->query('ec2:attachmentSet/ec2:item', $node);
            if($attachmentSet->length == 1) {
                $_as = $attachmentSet->item(0);
                $as = array();
                $as['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $_as);
                $as['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $_as);
                $as['device'] = $xpath->evaluate('string(ec2:device/text())', $_as);
                $as['status'] = $xpath->evaluate('string(ec2:status/text())', $_as);
                $as['attachTime'] = $xpath->evaluate('string(ec2:attachTime/text())', $_as);
                $item['attachmentSet'] = $as;
            }

            $return[] = $item;
            unset($item, $node);
        }

        return $return;
    }

    public function describeAttachedVolumes($instanceId)
    {
        $volumes = $this->describeVolume();

        $return = array();
        foreach($volumes as $vol) {
            if(isset($vol['attachmentSet']) && $vol['attachmentSet']['instanceId'] == $instanceId) {
                $return[] = $vol;
            }
        }

        return $return;
    }

    /**
     * Attaches an Amazon EBS volume to an instance
     *
     * @param string $volumeId              The ID of the Amazon EBS volume
     * @param string $instanceId            The ID of the instance to which the volume attaches
     * @param string $device                Specifies how the device is exposed to the instance (e.g., /dev/sdh).
     * @return array
     */
    public function attachVolume($volumeId, $instanceId, $device)
    {
        $params = array();
        $params['Action']       = 'AttachVolume';
        $params['VolumeId']     = $volumeId;
        $params['InstanceId']   = $instanceId;
        $params['Device']       = $device;

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();

        $return = array();
        $return['volumeId']     = $xpath->evaluate('string(//ec2:volumeId/text())');
        $return['instanceId']   = $xpath->evaluate('string(//ec2:instanceId/text())');
        $return['device']       = $xpath->evaluate('string(//ec2:device/text())');
        $return['status']       = $xpath->evaluate('string(//ec2:status/text())');
        $return['attachTime']   = $xpath->evaluate('string(//ec2:attachTime/text())');

        return $return;
    }

    /**
     * Detaches an Amazon EBS volume from an instance
     *
     * @param string $volumeId              The ID of the Amazon EBS volume
     * @param string $instanceId            The ID of the instance from which the volume will detach
     * @param string $device                The device name
     * @param boolean $force                Forces detachment if the previous detachment attempt did not occur cleanly
     *                                      (logging into an instance, unmounting the volume, and detaching normally).
     *                                      This option can lead to data loss or a corrupted file system. Use this option
     *                                      only as a last resort to detach an instance from a failed instance. The
     *                                      instance will not have an opportunity to flush file system caches nor
     *                                      file system meta data.
     * @return array
     */
    public function detachVolume($volumeId, $instanceId = null, $device = null, $force = false)
    {
        $params = array();
        $params['Action']       = 'DetachVolume';
        $params['VolumeId']     = $volumeId;
        $params['InstanceId']   = strval($instanceId);
        $params['Device']       = strval($device);
        $params['Force']        = strval($force);

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();

        $return = array();
        $return['volumeId']     = $xpath->evaluate('string(//ec2:volumeId/text())');
        $return['instanceId']   = $xpath->evaluate('string(//ec2:instanceId/text())');
        $return['device']       = $xpath->evaluate('string(//ec2:device/text())');
        $return['status']       = $xpath->evaluate('string(//ec2:status/text())');
        $return['attachTime']   = $xpath->evaluate('string(//ec2:attachTime/text())');

        return $return;
    }

    /**
     * Deletes an Amazon EBS volume
     *
     * @param string $volumeId              The ID of the volume to delete
     * @return boolean
     */
    public function deleteVolume($volumeId)
    {
        $params = array();
        $params['Action']       = 'DeleteVolume';
        $params['VolumeId']     = $volumeId;

        $response = $this->sendRequest($params);
        $xpath = $response->getXPath();

        $return = $xpath->evaluate('string(//ec2:return/text())');

        return ($return === "true");
    }

    /**
     * Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups,
     * to launch instances from identical snapshots, and to save data before shutting down an instance
     *
     * @param string $volumeId              The ID of the Amazon EBS volume to snapshot
     * @return array
     */
    public function createSnapshot($volumeId)
    {
        $params = array();
        $params['Action']       = 'CreateSnapshot';
        $params['VolumeId']     = $volumeId;

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();

        $return = array();
        $return['snapshotId']   = $xpath->evaluate('string(//ec2:snapshotId/text())');
        $return['volumeId']     = $xpath->evaluate('string(//ec2:volumeId/text())');
        $return['status']       = $xpath->evaluate('string(//ec2:status/text())');
        $return['startTime']    = $xpath->evaluate('string(//ec2:startTime/text())');
        $return['progress']     = $xpath->evaluate('string(//ec2:progress/text())');

        return $return;
    }

    /**
     * Describes the status of Amazon EBS snapshots
     *
     * @param string|array $snapshotId      The ID or arry of ID's of the Amazon EBS snapshot
     * @return array
     */
    public function describeSnapshot($snapshotId = null)
    {
        $params = array();
        $params['Action'] = 'DescribeSnapshots';

        if(is_array($snapshotId) && !empty($snapshotId)) {
            foreach($snapshotId as $k=>$name) {
                $params['SnapshotId.' . ($k+1)] = $name;
            }
        } elseif($snapshotId) {
            $params['SnapshotId.1'] = $snapshotId;
        }

        $response = $this->sendRequest($params);

        $xpath  = $response->getXPath();
        $nodes = $xpath->query('//ec2:snapshotSet/ec2:item', $response->getDocument());

        $return = array();
        foreach ($nodes as $node) {
            $item = array();

            $item['snapshotId'] = $xpath->evaluate('string(ec2:snapshotId/text())', $node);
            $item['volumeId']   = $xpath->evaluate('string(ec2:volumeId/text())', $node);
            $item['status']     = $xpath->evaluate('string(ec2:status/text())', $node);
            $item['startTime']  = $xpath->evaluate('string(ec2:startTime/text())', $node);
            $item['progress']   = $xpath->evaluate('string(ec2:progress/text())', $node);

            $return[] = $item;
            unset($item, $node);
        }

        return $return;
    }

    /**
     * Deletes a snapshot of an Amazon EBS  volume that is stored in Amazon S3
     *
     * @param string $snapshotId            The ID of the Amazon EBS snapshot to delete
     * @return boolean
     */
    public function deleteSnapshot($snapshotId)
    {
        $params = array();
        $params['Action']       = 'DeleteSnapshot';
        $params['SnapshotId']   = $snapshotId;

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();
        $return = $xpath->evaluate('string(//ec2:return/text())');

        return ($return === "true");
    }
}
Back to Top