Instance.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Cloud
  5. * @subpackage Infrastructure
  6. * @copyright Copyright (c) 2005-2015 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-2015 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. );
  71. /**
  72. * Constructor
  73. *
  74. * @param Adapter $adapter
  75. * @param array $data
  76. * @return void
  77. */
  78. public function __construct($adapter, $data = null)
  79. {
  80. if (!($adapter instanceof Zend_Cloud_Infrastructure_Adapter)) {
  81. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  82. throw new Zend_Cloud_Infrastructure_Exception("You must pass a Zend_Cloud_Infrastructure_Adapter instance");
  83. }
  84. if (is_object($data)) {
  85. if (method_exists($data, 'toArray')) {
  86. $data= $data->toArray();
  87. } elseif ($data instanceof Traversable) {
  88. $data = iterator_to_array($data);
  89. }
  90. }
  91. if (empty($data) || !is_array($data)) {
  92. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  93. throw new Zend_Cloud_Infrastructure_Exception("You must pass an array of parameters");
  94. }
  95. foreach ($this->attributeRequired as $key) {
  96. if (empty($data[$key])) {
  97. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  98. throw new Zend_Cloud_Infrastructure_Exception(sprintf(
  99. 'The param "%s" is a required param for %s',
  100. $key,
  101. __CLASS__
  102. ));
  103. }
  104. }
  105. $this->adapter = $adapter;
  106. $this->attributes = $data;
  107. }
  108. /**
  109. * Get Attribute with a specific key
  110. *
  111. * @param array $data
  112. * @return misc|false
  113. */
  114. public function getAttribute($key)
  115. {
  116. if (!empty($this->attributes[$key])) {
  117. return $this->attributes[$key];
  118. }
  119. return false;
  120. }
  121. /**
  122. * Get all the attributes
  123. *
  124. * @return array
  125. */
  126. public function getAttributes()
  127. {
  128. return $this->attributes;
  129. }
  130. /**
  131. * Get the instance's id
  132. *
  133. * @return string
  134. */
  135. public function getId()
  136. {
  137. return $this->attributes[self::INSTANCE_ID];
  138. }
  139. /**
  140. * Get the instance's image id
  141. *
  142. * @return string
  143. */
  144. public function getImageId()
  145. {
  146. return $this->attributes[self::INSTANCE_IMAGEID];
  147. }
  148. /**
  149. * Get the instance's name
  150. *
  151. * @return string
  152. */
  153. public function getName()
  154. {
  155. return $this->attributes[self::INSTANCE_NAME];
  156. }
  157. /**
  158. * Get the status of the instance
  159. *
  160. * @return string|boolean
  161. */
  162. public function getStatus()
  163. {
  164. return $this->adapter->statusInstance($this->attributes[self::INSTANCE_ID]);
  165. }
  166. /**
  167. * Wait for status $status with a timeout of $timeout seconds
  168. *
  169. * @param string $status
  170. * @param integer $timeout
  171. * @return boolean
  172. */
  173. public function waitStatus($status, $timeout = Adapter::TIMEOUT_STATUS_CHANGE)
  174. {
  175. return $this->adapter->waitStatusInstance($this->attributes[self::INSTANCE_ID], $status, $timeout);
  176. }
  177. /**
  178. * Get the public DNS of the instance
  179. *
  180. * @return string
  181. */
  182. public function getPublicDns()
  183. {
  184. if (!isset($this->attributes[self::INSTANCE_PUBLICDNS])) {
  185. $this->attributes[self::INSTANCE_PUBLICDNS] = $this->adapter->publicDnsInstance($this->attributes[self::INSTANCE_ID]);
  186. }
  187. return $this->attributes[self::INSTANCE_PUBLICDNS];
  188. }
  189. /**
  190. * Get the instance's CPU
  191. *
  192. * @return string
  193. */
  194. public function getCpu()
  195. {
  196. return $this->attributes[self::INSTANCE_CPU];
  197. }
  198. /**
  199. * Get the instance's RAM size
  200. *
  201. * @return string
  202. */
  203. public function getRamSize()
  204. {
  205. return $this->attributes[self::INSTANCE_RAM];
  206. }
  207. /**
  208. * Get the instance's storage size
  209. *
  210. * @return string
  211. */
  212. public function getStorageSize()
  213. {
  214. return $this->attributes[self::INSTANCE_STORAGE];
  215. }
  216. /**
  217. * Get the instance's zone
  218. *
  219. * @return string
  220. */
  221. public function getZone()
  222. {
  223. return $this->attributes[self::INSTANCE_ZONE];
  224. }
  225. /**
  226. * Get the instance's launch time
  227. *
  228. * @return string
  229. */
  230. public function getLaunchTime()
  231. {
  232. return $this->attributes[self::INSTANCE_LAUNCHTIME];
  233. }
  234. /**
  235. * Reboot the instance
  236. *
  237. * @return boolean
  238. */
  239. public function reboot()
  240. {
  241. return $this->adapter->rebootInstance($this->attributes[self::INSTANCE_ID]);
  242. }
  243. /**
  244. * Stop the instance
  245. *
  246. * @return boolean
  247. */
  248. public function stop()
  249. {
  250. return $this->adapter->stopInstance($this->attributes[self::INSTANCE_ID]);
  251. }
  252. /**
  253. * Start the instance
  254. *
  255. * @return boolean
  256. */
  257. public function start()
  258. {
  259. return $this->adapter->startInstance($this->attributes[self::INSTANCE_ID]);
  260. }
  261. /**
  262. * Destroy the instance
  263. *
  264. * @return boolean
  265. */
  266. public function destroy()
  267. {
  268. return $this->adapter->destroyInstance($this->attributes[self::INSTANCE_ID]);
  269. }
  270. /**
  271. * Return the system informations about the $metric of an instance
  272. *
  273. * @param string $metric
  274. * @param null|array $options
  275. * @return array|boolean
  276. */
  277. public function monitor($metric, $options = null)
  278. {
  279. return $this->adapter->monitorInstance($this->attributes[self::INSTANCE_ID], $metric, $options);
  280. }
  281. /**
  282. * Run arbitrary shell script on the instance
  283. *
  284. * @param array $param
  285. * @param string|array $cmd
  286. * @return string|array
  287. */
  288. public function deploy($params, $cmd)
  289. {
  290. return $this->adapter->deployInstance($this->attributes[self::INSTANCE_ID], $params, $cmd);
  291. }
  292. }