Instance.php 8.1 KB

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