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

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

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 529 lines | 273 code | 87 blank | 169 comment | 28 complexity | 15f8a3496d590dce438fb71f446161a5 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: Instance.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 that allows yout to run, terminate, reboot and describe Amazon
  28. * Ec2 Instances.
  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_Instance extends Zend_Service_Amazon_Ec2_Abstract
  37. {
  38. /**
  39. * Constant for Small Instance TYpe
  40. */
  41. const SMALL = 'm1.small';
  42. /**
  43. * Constant for Large Instance TYpe
  44. */
  45. const LARGE = 'm1.large';
  46. /**
  47. * Constant for X-Large Instance TYpe
  48. */
  49. const XLARGE = 'm1.xlarge';
  50. /**
  51. * Constant for High CPU Medium Instance TYpe
  52. */
  53. const HCPU_MEDIUM = 'c1.medium';
  54. /**
  55. * Constant for High CPU X-Large Instance TYpe
  56. */
  57. const HCPU_XLARGE = 'c1.xlarge';
  58. /**
  59. * Launches a specified number of Instances.
  60. *
  61. * If Amazon EC2 cannot launch the minimum number AMIs you request, no
  62. * instances launch. If there is insufficient capacity to launch the
  63. * maximum number of AMIs you request, Amazon EC2 launches as many
  64. * as possible to satisfy the requested maximum values.
  65. *
  66. * Every instance is launched in a security group. If you do not specify
  67. * a security group at launch, the instances start in your default security group.
  68. * For more information on creating security groups, see CreateSecurityGroup.
  69. *
  70. * An optional instance type can be specified. For information
  71. * about instance types, see Instance Types.
  72. *
  73. * You can provide an optional key pair ID for each image in the launch request
  74. * (for more information, see CreateKeyPair). All instances that are created
  75. * from images that use this key pair will have access to the associated public
  76. * key at boot. You can use this key to provide secure access to an instance of an
  77. * image on a per-instance basis. Amazon EC2 public images use this feature to
  78. * provide secure access without passwords.
  79. *
  80. * Launching public images without a key pair ID will leave them inaccessible.
  81. *
  82. * @param array $options An array that contins the options to start an instance.
  83. * Required Values:
  84. * imageId string ID of the AMI with which to launch instances.
  85. * Optional Values:
  86. * minCount integer Minimum number of instances to launch.
  87. * maxCount integer Maximum number of instances to launch.
  88. * keyName string Name of the key pair with which to launch instances.
  89. * securityGruop string|array Names of the security groups with which to associate the instances.
  90. * userData string The user data available to the launched instances. This should not be Base64 encoded.
  91. * instanceType constant Specifies the instance type.
  92. * placement string Specifies the availability zone in which to launch the instance(s). By default, Amazon EC2 selects an availability zone for you.
  93. * kernelId string The ID of the kernel with which to launch the instance.
  94. * ramdiskId string The ID of the RAM disk with which to launch the instance.
  95. * blockDeviceVirtualName string Specifies the virtual name to map to the corresponding device name. For example: instancestore0
  96. * blockDeviceName string Specifies the device to which you are mapping a virtual name. For example: sdb
  97. * monitor boolean Turn on CloudWatch Monitoring for an instance.
  98. * @return array
  99. */
  100. public function run(array $options)
  101. {
  102. $_defaultOptions = array(
  103. 'minCount' => 1,
  104. 'maxCount' => 1,
  105. 'instanceType' => Zend_Service_Amazon_Ec2_Instance::SMALL
  106. );
  107. // set / override the defualt optoins if they are not passed into the array;
  108. $options = array_merge($_defaultOptions, $options);
  109. if(!isset($options['imageId'])) {
  110. require_once 'Zend/Service/Amazon/Ec2/Exception.php';
  111. throw new Zend_Service_Amazon_Ec2_Exception('No Image Id Provided');
  112. }
  113. $params = array();
  114. $params['Action'] = 'RunInstances';
  115. $params['ImageId'] = $options['imageId'];
  116. $params['MinCount'] = $options['minCount'];
  117. $params['MaxCount'] = $options['maxCount'];
  118. if(isset($options['keyName'])) {
  119. $params['KeyName'] = $options['keyName'];
  120. }
  121. if(is_array($options['securityGroup']) && !empty($options['securityGroup'])) {
  122. foreach($options['securityGroup'] as $k=>$name) {
  123. $params['SecurityGroup.' . ($k+1)] = $name;
  124. }
  125. } elseif(isset($options['securityGroup'])) {
  126. $params['SecurityGroup.1'] = $options['securityGroup'];
  127. }
  128. if(isset($options['userData'])) {
  129. $params['UserData'] = base64_encode($options['userData']);
  130. }
  131. if(isset($options['instanceType'])) {
  132. $params['InstanceType'] = $options['instanceType'];
  133. }
  134. if(isset($options['placement'])) {
  135. $params['Placement.AvailabilityZone'] = $options['placement'];
  136. }
  137. if(isset($options['kernelId'])) {
  138. $params['KernelId'] = $options['kernelId'];
  139. }
  140. if(isset($options['ramdiskId'])) {
  141. $params['RamdiskId'] = $options['ramdiskId'];
  142. }
  143. if(isset($options['blockDeviceVirtualName']) && isset($options['blockDeviceName'])) {
  144. $params['BlockDeviceMapping.n.VirtualName'] = $options['blockDeviceVirtualName'];
  145. $params['BlockDeviceMapping.n.DeviceName'] = $options['blockDeviceName'];
  146. }
  147. if(isset($options['monitor']) && $options['monitor'] === true) {
  148. $params['Monitoring.Enabled'] = true;
  149. }
  150. $response = $this->sendRequest($params);
  151. $xpath = $response->getXPath();
  152. $return = array();
  153. $return['reservationId'] = $xpath->evaluate('string(//ec2:reservationId/text())');
  154. $return['ownerId'] = $xpath->evaluate('string(//ec2:ownerId/text())');
  155. $gs = $xpath->query('//ec2:groupSet/ec2:item');
  156. foreach($gs as $gs_node) {
  157. $return['groupSet'][] = $xpath->evaluate('string(ec2:groupId/text())', $gs_node);
  158. unset($gs_node);
  159. }
  160. unset($gs);
  161. $is = $xpath->query('//ec2:instancesSet/ec2:item');
  162. foreach($is as $is_node) {
  163. $item = array();
  164. $item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $is_node);
  165. $item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $is_node);
  166. $item['instanceState']['code'] = $xpath->evaluate('string(ec2:instanceState/ec2:code/text())', $is_node);
  167. $item['instanceState']['name'] = $xpath->evaluate('string(ec2:instanceState/ec2:name/text())', $is_node);
  168. $item['privateDnsName'] = $xpath->evaluate('string(ec2:privateDnsName/text())', $is_node);
  169. $item['dnsName'] = $xpath->evaluate('string(ec2:dnsName/text())', $is_node);
  170. $item['keyName'] = $xpath->evaluate('string(ec2:keyName/text())', $is_node);
  171. $item['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $is_node);
  172. $item['amiLaunchIndex'] = $xpath->evaluate('string(ec2:amiLaunchIndex/text())', $is_node);
  173. $item['launchTime'] = $xpath->evaluate('string(ec2:launchTime/text())', $is_node);
  174. $item['availabilityZone'] = $xpath->evaluate('string(ec2:placement/ec2:availabilityZone/text())', $is_node);
  175. $return['instances'][] = $item;
  176. unset($item);
  177. unset($is_node);
  178. }
  179. unset($is);
  180. return $return;
  181. }
  182. /**
  183. * Returns information about instances that you own.
  184. *
  185. * If you specify one or more instance IDs, Amazon EC2 returns information
  186. * for those instances. If you do not specify instance IDs, Amazon EC2
  187. * returns information for all relevant instances. If you specify an invalid
  188. * instance ID, a fault is returned. If you specify an instance that you do
  189. * not own, it will not be included in the returned results.
  190. *
  191. * Recently terminated instances might appear in the returned results.
  192. * This interval is usually less than one hour.
  193. *
  194. * @param string|array $instaceId Set of instances IDs of which to get the status.
  195. * @param boolean Ture to ignore Terminated Instances.
  196. * @return array
  197. */
  198. public function describe($instanceId = null, $ignoreTerminated = false)
  199. {
  200. $params = array();
  201. $params['Action'] = 'DescribeInstances';
  202. if(is_array($instanceId) && !empty($instanceId)) {
  203. foreach($instanceId as $k=>$name) {
  204. $params['InstanceId.' . ($k+1)] = $name;
  205. }
  206. } elseif($instanceId) {
  207. $params['InstanceId.1'] = $instanceId;
  208. }
  209. $response = $this->sendRequest($params);
  210. $xpath = $response->getXPath();
  211. $nodes = $xpath->query('//ec2:reservationSet/ec2:item');
  212. $return = array();
  213. $return['instances'] = array();
  214. foreach($nodes as $node) {
  215. if($xpath->evaluate('string(ec2:instancesSet/ec2:item/ec2:instanceState/ec2:code/text())', $node) == 48 && $ignoreTerminated) continue;
  216. $item = array();
  217. $item['reservationId'] = $xpath->evaluate('string(ec2:reservationId/text())', $node);
  218. $item['ownerId'] = $xpath->evaluate('string(ec2:ownerId/text())', $node);
  219. $gs = $xpath->query('ec2:groupSet/ec2:item', $node);
  220. foreach($gs as $gs_node) {
  221. $item['groupSet'][] = $xpath->evaluate('string(ec2:groupId/text())', $gs_node);
  222. unset($gs_node);
  223. }
  224. unset($gs);
  225. $is = $xpath->query('ec2:instancesSet/ec2:item', $node);
  226. foreach($is as $is_node) {
  227. $item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $is_node);
  228. $item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $is_node);
  229. $item['instanceState']['code'] = $xpath->evaluate('string(ec2:instanceState/ec2:code/text())', $is_node);
  230. $item['instanceState']['name'] = $xpath->evaluate('string(ec2:instanceState/ec2:name/text())', $is_node);
  231. $item['privateDnsName'] = $xpath->evaluate('string(ec2:privateDnsName/text())', $is_node);
  232. $item['dnsName'] = $xpath->evaluate('string(ec2:dnsName/text())', $is_node);
  233. $item['keyName'] = $xpath->evaluate('string(ec2:keyName/text())', $is_node);
  234. $item['productCode'] = $xpath->evaluate('string(ec2:productCodesSet/ec2:item/ec2:productCode/text())', $is_node);
  235. $item['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $is_node);
  236. $item['launchTime'] = $xpath->evaluate('string(ec2:launchTime/text())', $is_node);
  237. $item['availabilityZone'] = $xpath->evaluate('string(ec2:placement/ec2:availabilityZone/text())', $is_node);
  238. $item['kernelId'] = $xpath->evaluate('string(ec2:kernelId/text())', $is_node);
  239. $item['ramediskId'] = $xpath->evaluate('string(ec2:ramediskId/text())', $is_node);
  240. $item['amiLaunchIndex'] = $xpath->evaluate('string(ec2:amiLaunchIndex/text())', $is_node);
  241. $item['monitoringState'] = $xpath->evaluate('string(ec2:monitoring/ec2:state/text())', $is_node);
  242. $return['instances'][] = $item;
  243. unset($is_node);
  244. }
  245. unset($item);
  246. unset($is);
  247. }
  248. return $return;
  249. }
  250. /**
  251. * Returns information about instances that you own that were started from
  252. * a specific imageId
  253. *
  254. * Recently terminated instances might appear in the returned results.
  255. * This interval is usually less than one hour.
  256. *
  257. * @param string $imageId The imageId used to start the Instance.
  258. * @param boolean Ture to ignore Terminated Instances.
  259. * @return array
  260. */
  261. public function describeByImageId($imageId, $ignoreTerminated = false)
  262. {
  263. $arrInstances = $this->describe(null, $ignoreTerminated);
  264. $return = array();
  265. foreach($arrInstances['instances'] as $instance) {
  266. if($instance['imageId'] !== $imageId) continue;
  267. $return[] = $instance;
  268. }
  269. return $return;
  270. }
  271. /**
  272. * Shuts down one or more instances. This operation is idempotent; if you terminate
  273. * an instance more than once, each call will succeed.
  274. *
  275. * Terminated instances will remain visible after termination (approximately one hour).
  276. *
  277. * @param string|array $instanceId One or more instance IDs returned.
  278. * @return array
  279. */
  280. public function terminate($instanceId)
  281. {
  282. $params = array();
  283. $params['Action'] = 'TerminateInstances';
  284. if(is_array($instanceId) && !empty($instanceId)) {
  285. foreach($instanceId as $k=>$name) {
  286. $params['InstanceId.' . ($k+1)] = $name;
  287. }
  288. } elseif($instanceId) {
  289. $params['InstanceId.1'] = $instanceId;
  290. }
  291. $response = $this->sendRequest($params);
  292. $xpath = $response->getXPath();
  293. $nodes = $xpath->query('//ec2:instancesSet/ec2:item');
  294. $return = array();
  295. foreach($nodes as $node) {
  296. $item = array();
  297. $item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $node);
  298. $item['shutdownState']['code'] = $xpath->evaluate('string(ec2:shutdownState/ec2:code/text())', $node);
  299. $item['shutdownState']['name'] = $xpath->evaluate('string(ec2:shutdownState/ec2:name/text())', $node);
  300. $item['previousState']['code'] = $xpath->evaluate('string(ec2:previousState/ec2:code/text())', $node);
  301. $item['previousState']['name'] = $xpath->evaluate('string(ec2:previousState/ec2:name/text())', $node);
  302. $return[] = $item;
  303. unset($item);
  304. }
  305. return $return;
  306. }
  307. /**
  308. * Requests a reboot of one or more instances.
  309. *
  310. * This operation is asynchronous; it only queues a request to reboot the specified instance(s). The operation
  311. * will succeed if the instances are valid and belong to the user. Requests to reboot terminated instances are ignored.
  312. *
  313. * @param string|array $instanceId One or more instance IDs.
  314. * @return boolean
  315. */
  316. public function reboot($instanceId)
  317. {
  318. $params = array();
  319. $params['Action'] = 'RebootInstances';
  320. if(is_array($instanceId) && !empty($instanceId)) {
  321. foreach($instanceId as $k=>$name) {
  322. $params['InstanceId.' . ($k+1)] = $name;
  323. }
  324. } elseif($instanceId) {
  325. $params['InstanceId.1'] = $instanceId;
  326. }
  327. $response = $this->sendRequest($params);
  328. $xpath = $response->getXPath();
  329. $return = $xpath->evaluate('string(//ec2:return/text())');
  330. return ($return === "true");
  331. }
  332. /**
  333. * Retrieves console output for the specified instance.
  334. *
  335. * Instance console output is buffered and posted shortly after instance boot, reboot, and termination.
  336. * Amazon EC2 preserves the most recent 64 KB output which will be available for at least one hour after the most recent post.
  337. *
  338. * @param string $instanceId An instance ID
  339. * @return array
  340. */
  341. public function consoleOutput($instanceId)
  342. {
  343. $params = array();
  344. $params['Action'] = 'GetConsoleOutput';
  345. $params['InstanceId'] = $instanceId;
  346. $response = $this->sendRequest($params);
  347. $xpath = $response->getXPath();
  348. $return = array();
  349. $return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())');
  350. $return['timestamp'] = $xpath->evaluate('string(//ec2:timestamp/text())');
  351. $return['output'] = base64_decode($xpath->evaluate('string(//ec2:output/text())'));
  352. return $return;
  353. }
  354. /**
  355. * Returns true if the specified product code is attached to the specified instance.
  356. * The operation returns false if the product code is not attached to the instance.
  357. *
  358. * The confirmProduct operation can only be executed by the owner of the AMI.
  359. * This feature is useful when an AMI owner is providing support and wants to
  360. * verify whether a user's instance is eligible.
  361. *
  362. * @param string $productCode The product code to confirm.
  363. * @param string $instanceId The instance for which to confirm the product code.
  364. * @return array|boolean An array if the product code is attached to the instance, false if it is not.
  365. */
  366. public function confirmProduct($productCode, $instanceId)
  367. {
  368. $params = array();
  369. $params['Action'] = 'ConfirmProductInstance';
  370. $params['ProductCode'] = $productCode;
  371. $params['InstanceId'] = $instanceId;
  372. $response = $this->sendRequest($params);
  373. $xpath = $response->getXPath();
  374. $result = $xpath->evaluate('string(//ec2:result/text())');
  375. if($result === "true") {
  376. $return['result'] = true;
  377. $return['ownerId'] = $xpath->evaluate('string(//ec2:ownerId/text())');
  378. return $return;
  379. }
  380. return false;
  381. }
  382. /**
  383. * Turn on Amazon CloudWatch Monitoring for an instance or a list of instances
  384. *
  385. * @param array|string $instanceId The instance or list of instances you want to enable monitoring for
  386. * @return array
  387. */
  388. public function monitor($instanceId)
  389. {
  390. $params = array();
  391. $params['Action'] = 'MonitorInstances';
  392. if(is_array($instanceId) && !empty($instanceId)) {
  393. foreach($instanceId as $k=>$name) {
  394. $params['InstanceId.' . ($k+1)] = $name;
  395. }
  396. } elseif($instanceId) {
  397. $params['InstanceId.1'] = $instanceId;
  398. }
  399. $response = $this->sendRequest($params);
  400. $xpath = $response->getXPath();
  401. $items = $xpath->query('//ec2:instancesSet/ec2:item');
  402. $arrReturn = array();
  403. foreach($items as $item) {
  404. $i = array();
  405. $i['instanceid'] = $xpath->evaluate('string(//ec2:instanceId/text())', $item);
  406. $i['monitorstate'] = $xpath->evaluate('string(//ec2:monitoring/ec2:state/text())');
  407. $arrReturn[] = $i;
  408. unset($i);
  409. }
  410. return $arrReturn;
  411. }
  412. /**
  413. * Turn off Amazon CloudWatch Monitoring for an instance or a list of instances
  414. *
  415. * @param array|string $instanceId The instance or list of instances you want to disable monitoring for
  416. * @return array
  417. */
  418. public function unmonitor($instanceId)
  419. {
  420. $params = array();
  421. $params['Action'] = 'UnmonitorInstances';
  422. if(is_array($instanceId) && !empty($instanceId)) {
  423. foreach($instanceId as $k=>$name) {
  424. $params['InstanceId.' . ($k+1)] = $name;
  425. }
  426. } elseif($instanceId) {
  427. $params['InstanceId.1'] = $instanceId;
  428. }
  429. $response = $this->sendRequest($params);
  430. $xpath = $response->getXPath();
  431. $items = $xpath->query('//ec2:instancesSet/ec2:item');
  432. $arrReturn = array();
  433. foreach($items as $item) {
  434. $i = array();
  435. $i['instanceid'] = $xpath->evaluate('string(//ec2:instanceId/text())', $item);
  436. $i['monitorstate'] = $xpath->evaluate('string(//ec2:monitoring/ec2:state/text())');
  437. $arrReturn[] = $i;
  438. unset($i);
  439. }
  440. return $arrReturn;
  441. }
  442. }