Ebs.php 13 KB

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