PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cloud/Infrastructure/Instance.php

http://github.com/zendframework/zf2
PHP | 333 lines | 158 code | 28 blank | 147 comment | 8 complexity | d771e7ba35fec0d9717f085db30e1d05 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend\Cloud
  5. * @subpackage Infrastructure
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. /**
  10. * namespace
  11. */
  12. namespace Zend\Cloud\Infrastructure;
  13. /**
  14. * Instance of an infrastructure service
  15. *
  16. * @package Zend\Cloud
  17. * @subpackage Infrastructure
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. class Instance
  22. {
  23. const STATUS_RUNNING = 'running';
  24. const STATUS_STOPPED = 'stopped';
  25. const STATUS_SHUTTING_DOWN = 'shutting-down';
  26. const STATUS_REBOOTING = 'rebooting';
  27. const STATUS_TERMINATED = 'terminated';
  28. const STATUS_PENDING = 'pending';
  29. const STATUS_REBUILD = 'rebuild';
  30. const INSTANCE_ID = 'id';
  31. const INSTANCE_IMAGEID = 'imageId';
  32. const INSTANCE_NAME = 'name';
  33. const INSTANCE_STATUS = 'status';
  34. const INSTANCE_PUBLICDNS = 'publicDns';
  35. const INSTANCE_CPU = 'cpu';
  36. const INSTANCE_RAM = 'ram';
  37. const INSTANCE_STORAGE = 'storageSize';
  38. const INSTANCE_ZONE = 'zone';
  39. const INSTANCE_METADATA = 'metadata';
  40. const INSTANCE_LAUNCHTIME = 'launchTime';
  41. const MONITOR_CPU = 'CpuUsage';
  42. const MONITOR_RAM = 'RamUsage';
  43. const MONITOR_NETWORK_IN = 'NetworkIn';
  44. const MONITOR_NETWORK_OUT = 'NetworkOut';
  45. const MONITOR_DISK = 'DiskUsage';
  46. const MONITOR_DISK_WRITE = 'DiskWrite';
  47. const MONITOR_DISK_READ = 'DiskRead';
  48. const MONITOR_START_TIME = 'StartTime';
  49. const MONITOR_END_TIME = 'EndTime';
  50. const SSH_USERNAME = 'username';
  51. const SSH_PASSWORD = 'password';
  52. const SSH_PRIVATE_KEY = 'privateKey';
  53. const SSH_PUBLIC_KEY = 'publicKey';
  54. const SSH_PASSPHRASE = 'passphrase';
  55. /**
  56. * @var Zend\Cloud\Infrastructure\Adapter
  57. */
  58. protected $adapter;
  59. /**
  60. * Instance's attribute
  61. *
  62. * @var array
  63. */
  64. protected $attributes;
  65. /**
  66. * Attributes required for an instance
  67. *
  68. * @var array
  69. */
  70. protected $attributeRequired = array(
  71. self::INSTANCE_ID,
  72. self::INSTANCE_STATUS,
  73. self::INSTANCE_IMAGEID,
  74. self::INSTANCE_ZONE,
  75. //self::INSTANCE_RAM,
  76. //self::INSTANCE_STORAGE,
  77. );
  78. /**
  79. * Constructor
  80. *
  81. * @param Adapter $adapter
  82. * @param array $data
  83. * @return void
  84. */
  85. public function __construct(Adapter $adapter, $data = null)
  86. {
  87. if (!($adapter instanceof Adapter)) {
  88. throw new Exception\InvalidArgumentException("You must pass a Zend\Cloud\Infrastructure\Adapter instance");
  89. }
  90. if (is_object($data)) {
  91. if (method_exists($data, 'toArray')) {
  92. $data= $data->toArray();
  93. } elseif ($data instanceof \Traversable) {
  94. $data = iterator_to_array($data);
  95. }
  96. }
  97. if (empty($data) || !is_array($data)) {
  98. throw new Exception\InvalidArgumentException("You must pass an array of parameters");
  99. }
  100. foreach ($this->attributeRequired as $key) {
  101. if (empty($data[$key])) {
  102. throw new Exception\InvalidArgumentException(sprintf(
  103. 'The param "%s" is a required param for %s',
  104. $key,
  105. __CLASS__
  106. ));
  107. }
  108. }
  109. $this->adapter = $adapter;
  110. $this->attributes = $data;
  111. }
  112. /**
  113. * Get Attribute with a specific key
  114. *
  115. * @param array $data
  116. * @return misc|false
  117. */
  118. public function getAttribute($key)
  119. {
  120. if (!empty($this->attributes[$key])) {
  121. return $this->attributes[$key];
  122. }
  123. return false;
  124. }
  125. /**
  126. * Get all the attributes
  127. *
  128. * @return array
  129. */
  130. public function getAttributes()
  131. {
  132. return $this->attributes;
  133. }
  134. /**
  135. * Get the instance's id
  136. *
  137. * @return string
  138. */
  139. public function getId()
  140. {
  141. return $this->attributes[self::INSTANCE_ID];
  142. }
  143. /**
  144. * Get the instance's image id
  145. *
  146. * @return string
  147. */
  148. public function getImageId()
  149. {
  150. return $this->attributes[self::INSTANCE_IMAGEID];
  151. }
  152. /**
  153. * Get the instance's name
  154. *
  155. * @return string
  156. */
  157. public function getName()
  158. {
  159. return $this->attributes[self::INSTANCE_NAME];
  160. }
  161. /**
  162. * Get the status of the instance
  163. *
  164. * @return string|boolean
  165. */
  166. public function getStatus()
  167. {
  168. return $this->adapter->statusInstance($this->attributes[self::INSTANCE_ID]);
  169. }
  170. /**
  171. * Get the metadata of the instance
  172. *
  173. * @return array
  174. */
  175. public function getMetadata()
  176. {
  177. return $this->attributes[self::INSTANCE_METADATA];
  178. }
  179. /**
  180. * Wait for status $status with a timeout of $timeout seconds
  181. *
  182. * @param string $status
  183. * @param integer $timeout
  184. * @return boolean
  185. */
  186. public function waitStatus($status, $timeout = Adapter::TIMEOUT_STATUS_CHANGE)
  187. {
  188. return $this->adapter->waitStatusInstance($this->attributes[self::INSTANCE_ID], $status, $timeout);
  189. }
  190. /**
  191. * Get the public DNS of the instance
  192. *
  193. * @return string
  194. */
  195. public function getPublicDns()
  196. {
  197. if (!isset($this->attributes[self::INSTANCE_PUBLICDNS])) {
  198. $this->attributes[self::INSTANCE_PUBLICDNS] = $this->adapter->publicDnsInstance($this->attributes[self::INSTANCE_ID]);
  199. }
  200. return $this->attributes[self::INSTANCE_PUBLICDNS];
  201. }
  202. /**
  203. * Get the instance's CPU
  204. *
  205. * @return string
  206. */
  207. public function getCpu()
  208. {
  209. return $this->attributes[self::INSTANCE_CPU];
  210. }
  211. /**
  212. * Get the instance's RAM size
  213. *
  214. * @return string
  215. */
  216. public function getRamSize()
  217. {
  218. return $this->attributes[self::INSTANCE_RAM];
  219. }
  220. /**
  221. * Get the instance's storage size
  222. *
  223. * @return string
  224. */
  225. public function getStorageSize()
  226. {
  227. return $this->attributes[self::INSTANCE_STORAGE];
  228. }
  229. /**
  230. * Get the instance's zone
  231. *
  232. * @return string
  233. */
  234. public function getZone()
  235. {
  236. return $this->attributes[self::INSTANCE_ZONE];
  237. }
  238. /**
  239. * Get the instance's launch time
  240. *
  241. * @return string
  242. */
  243. public function getLaunchTime()
  244. {
  245. return $this->attributes[self::INSTANCE_LAUNCHTIME];
  246. }
  247. /**
  248. * Reboot the instance
  249. *
  250. * @return boolean
  251. */
  252. public function reboot()
  253. {
  254. return $this->adapter->rebootInstance($this->attributes[self::INSTANCE_ID]);
  255. }
  256. /**
  257. * Stop the instance
  258. *
  259. * @return boolean
  260. */
  261. public function stop()
  262. {
  263. return $this->adapter->stopInstance($this->attributes[self::INSTANCE_ID]);
  264. }
  265. /**
  266. * Start the instance
  267. *
  268. * @return boolean
  269. */
  270. public function start()
  271. {
  272. return $this->adapter->startInstance($this->attributes[self::INSTANCE_ID]);
  273. }
  274. /**
  275. * Destroy the instance
  276. *
  277. * @return boolean
  278. */
  279. public function destroy()
  280. {
  281. return $this->adapter->destroyInstance($this->attributes[self::INSTANCE_ID]);
  282. }
  283. /**
  284. * Return the system informations about the $metric of an instance
  285. *
  286. * @param string $metric
  287. * @param null|array $options
  288. * @return array|boolean
  289. */
  290. public function monitor($metric, $options = null)
  291. {
  292. return $this->adapter->monitorInstance($this->attributes[self::INSTANCE_ID], $metric, $options);
  293. }
  294. /**
  295. * Run arbitrary shell script on the instance
  296. *
  297. * @param array $param
  298. * @param string|array $cmd
  299. * @return string|array
  300. */
  301. public function deploy($params, $cmd)
  302. {
  303. return $this->adapter->deployInstance($this->attributes[self::INSTANCE_ID], $params, $cmd);
  304. }
  305. }