PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Service/Amazon/Ec2/Ebs.php

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