Rackspace.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Cloud_Infrastructure
  5. * @subpackage Adapter
  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. require_once 'Zend/Service/Rackspace/Servers.php';
  10. require_once 'Zend/Cloud/Infrastructure/Instance.php';
  11. require_once 'Zend/Cloud/Infrastructure/InstanceList.php';
  12. require_once 'Zend/Cloud/Infrastructure/Image.php';
  13. require_once 'Zend/Cloud/Infrastructure/ImageList.php';
  14. require_once 'Zend/Cloud/Infrastructure/Adapter/AbstractAdapter.php';
  15. /**
  16. * Rackspace servers adapter for infrastructure service
  17. *
  18. * @package Zend_Cloud_Infrastructure
  19. * @subpackage Adapter
  20. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  21. * @license http://framework.zend.com/license/new-bsd New BSD License
  22. */
  23. class Zend_Cloud_Infrastructure_Adapter_Rackspace extends Zend_Cloud_Infrastructure_Adapter_AbstractAdapter
  24. {
  25. /**
  26. * RACKSPACE constants
  27. */
  28. const RACKSPACE_USER = 'rackspace_user';
  29. const RACKSPACE_KEY = 'rackspace_key';
  30. const RACKSPACE_REGION = 'rackspace_region';
  31. const RACKSPACE_ZONE_USA = 'USA';
  32. const RACKSPACE_ZONE_UK = 'UK';
  33. const MONITOR_CPU_SAMPLES = 3;
  34. /**
  35. * Rackspace Servers Instance
  36. *
  37. * @var Zend_Service_Rackspace_Servers
  38. */
  39. protected $rackspace;
  40. /**
  41. * Rackspace access user
  42. *
  43. * @var string
  44. */
  45. protected $accessUser;
  46. /**
  47. * Rackspace access key
  48. *
  49. * @var string
  50. */
  51. protected $accessKey;
  52. /**
  53. * Rackspace Region
  54. *
  55. * @var string
  56. */
  57. protected $region;
  58. /**
  59. * Flavors
  60. *
  61. * @var array
  62. */
  63. protected $flavors;
  64. /**
  65. * Map array between Rackspace and Infrastructure status
  66. *
  67. * @var array
  68. */
  69. protected $mapStatus = array (
  70. 'ACTIVE' => Zend_Cloud_Infrastructure_Instance::STATUS_RUNNING,
  71. 'SUSPENDED' => Zend_Cloud_Infrastructure_Instance::STATUS_STOPPED,
  72. 'BUILD' => Zend_Cloud_Infrastructure_Instance::STATUS_REBUILD,
  73. 'REBUILD' => Zend_Cloud_Infrastructure_Instance::STATUS_REBUILD,
  74. 'QUEUE_RESIZE' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  75. 'PREP_RESIZE' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  76. 'RESIZE' => Zend_Cloud_Infrastructure_Instance::STATUS_REBUILD,
  77. 'VERIFY_RESIZE' => Zend_Cloud_Infrastructure_Instance::STATUS_REBUILD,
  78. 'PASSWORD' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  79. 'RESCUE' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  80. 'REBOOT' => Zend_Cloud_Infrastructure_Instance::STATUS_REBOOTING,
  81. 'HARD_REBOOT' => Zend_Cloud_Infrastructure_Instance::STATUS_REBOOTING,
  82. 'SHARE_IP' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  83. 'SHARE_IP_NO_CONFIG' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  84. 'DELETE_IP' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING,
  85. 'UNKNOWN' => Zend_Cloud_Infrastructure_Instance::STATUS_PENDING
  86. );
  87. /**
  88. * Constructor
  89. *
  90. * @param array|Zend_Config $options
  91. * @return void
  92. */
  93. public function __construct($options = array())
  94. {
  95. if (is_object($options)) {
  96. if (method_exists($options, 'toArray')) {
  97. $options= $options->toArray();
  98. } elseif ($options instanceof Traversable) {
  99. $options = iterator_to_array($options);
  100. }
  101. }
  102. if (empty($options) || !is_array($options)) {
  103. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  104. throw new Zend_Cloud_Infrastructure_Exception('Invalid options provided');
  105. }
  106. if (!isset($options[self::RACKSPACE_USER])) {
  107. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  108. throw new Zend_Cloud_Infrastructure_Exception('Rackspace access user not specified!');
  109. }
  110. if (!isset($options[self::RACKSPACE_KEY])) {
  111. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  112. throw new Zend_Cloud_Infrastructure_Exception('Rackspace access key not specified!');
  113. }
  114. $this->accessUser = $options[self::RACKSPACE_USER];
  115. $this->accessKey = $options[self::RACKSPACE_KEY];
  116. if (isset($options[self::RACKSPACE_REGION])) {
  117. switch ($options[self::RACKSPACE_REGION]) {
  118. case self::RACKSPACE_ZONE_UK:
  119. $this->region= Zend_Service_Rackspace_Servers::UK_AUTH_URL;
  120. break;
  121. case self::RACKSPACE_ZONE_USA:
  122. $this->region = Zend_Service_Rackspace_Servers::US_AUTH_URL;
  123. break;
  124. default:
  125. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  126. throw new Zend_Cloud_Infrastructure_Exception('The region is not valid');
  127. }
  128. } else {
  129. $this->region = Zend_Service_Rackspace_Servers::US_AUTH_URL;
  130. }
  131. try {
  132. $this->rackspace = new Zend_Service_Rackspace_Servers($this->accessUser,$this->accessKey, $this->region);
  133. } catch (Exception $e) {
  134. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  135. throw new Zend_Cloud_Infrastructure_Exception('Error on create: ' . $e->getMessage(), $e->getCode(), $e);
  136. }
  137. if (isset($options[self::HTTP_ADAPTER])) {
  138. $this->rackspace->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
  139. }
  140. }
  141. /**
  142. * Convert the attributes of Rackspace server into attributes of Infrastructure
  143. *
  144. * @param array $attr
  145. * @return array|boolean
  146. */
  147. protected function convertAttributes($attr)
  148. {
  149. $result = array();
  150. if (!empty($attr) && is_array($attr)) {
  151. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_ID] = $attr['id'];
  152. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_NAME] = $attr['name'];
  153. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_STATUS] = $this->mapStatus[$attr['status']];
  154. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_IMAGEID] = $attr['imageId'];
  155. if ($this->region==Zend_Service_Rackspace_Servers::US_AUTH_URL) {
  156. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_ZONE] = self::RACKSPACE_ZONE_USA;
  157. } else {
  158. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_ZONE] = self::RACKSPACE_ZONE_UK;
  159. }
  160. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_RAM] = $this->flavors[$attr['flavorId']]['ram'];
  161. $result[Zend_Cloud_Infrastructure_Instance::INSTANCE_STORAGE] = $this->flavors[$attr['flavorId']]['disk'];
  162. }
  163. return $result;
  164. }
  165. /**
  166. * Return a list of the available instancies
  167. *
  168. * @return InstanceList|boolean
  169. */
  170. public function listInstances()
  171. {
  172. $this->adapterResult = $this->rackspace->listServers(true);
  173. if ($this->adapterResult===false) {
  174. return false;
  175. }
  176. $array= $this->adapterResult->toArray();
  177. $result = array();
  178. foreach ($array as $instance) {
  179. $result[]= $this->convertAttributes($instance);
  180. }
  181. return new Zend_Cloud_Infrastructure_InstanceList($this, $result);
  182. }
  183. /**
  184. * Return the status of an instance
  185. *
  186. * @param string
  187. * @return string|boolean
  188. */
  189. public function statusInstance($id)
  190. {
  191. $this->adapterResult = $this->rackspace->getServer($id);
  192. if ($this->adapterResult===false) {
  193. return false;
  194. }
  195. $array= $this->adapterResult->toArray();
  196. return $this->mapStatus[$array['status']];
  197. }
  198. /**
  199. * Return the public DNS name/Ip address of the instance
  200. *
  201. * @param string $id
  202. * @return string|boolean
  203. */
  204. public function publicDnsInstance($id)
  205. {
  206. $this->adapterResult = $this->rackspace->getServerPublicIp($id);
  207. if (empty($this->adapterResult)) {
  208. return false;
  209. }
  210. return $this->adapterResult[0];
  211. }
  212. /**
  213. * Reboot an instance
  214. *
  215. * @param string $id
  216. * @return boolean
  217. */
  218. public function rebootInstance($id)
  219. {
  220. return $this->rackspace->rebootServer($id,true);
  221. }
  222. /**
  223. * Create a new instance
  224. *
  225. * @param string $name
  226. * @param array $options
  227. * @return Instance|boolean
  228. */
  229. public function createInstance($name, $options)
  230. {
  231. if (empty($name)) {
  232. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  233. throw new Zend_Cloud_Infrastructure_Exception('You must specify the name of the instance');
  234. }
  235. if (empty($options) || !is_array($options)) {
  236. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  237. throw new Zend_Cloud_Infrastructure_Exception('The options must be an array');
  238. }
  239. // @todo create an generic abstract definition for an instance?
  240. $metadata= array();
  241. if (isset($options['metadata'])) {
  242. $metadata= $options['metadata'];
  243. unset($options['metadata']);
  244. }
  245. $files= array();
  246. if (isset($options['files'])) {
  247. $files= $options['files'];
  248. unset($options['files']);
  249. }
  250. $options['name']= $name;
  251. $this->adapterResult = $this->rackspace->createServer($options,$metadata,$files);
  252. if ($this->adapterResult===false) {
  253. return false;
  254. }
  255. return new Zend_Cloud_Infrastructure_Instance($this, $this->convertAttributes($this->adapterResult->toArray()));
  256. }
  257. /**
  258. * Stop an instance
  259. *
  260. * @param string $id
  261. * @return boolean
  262. */
  263. public function stopInstance($id)
  264. {
  265. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  266. throw new Zend_Cloud_Infrastructure_Exception('The stopInstance method is not implemented in the adapter');
  267. }
  268. /**
  269. * Start an instance
  270. *
  271. * @param string $id
  272. * @return boolean
  273. */
  274. public function startInstance($id)
  275. {
  276. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  277. throw new Zend_Cloud_Infrastructure_Exception('The startInstance method is not implemented in the adapter');
  278. }
  279. /**
  280. * Destroy an instance
  281. *
  282. * @param string $id
  283. * @return boolean
  284. */
  285. public function destroyInstance($id)
  286. {
  287. $this->adapterResult= $this->rackspace->deleteServer($id);
  288. return $this->adapterResult;
  289. }
  290. /**
  291. * Return a list of all the available instance images
  292. *
  293. * @return ImageList|boolean
  294. */
  295. public function imagesInstance()
  296. {
  297. $this->adapterResult = $this->rackspace->listImages(true);
  298. if ($this->adapterResult===false) {
  299. return false;
  300. }
  301. $images= $this->adapterResult->toArray();
  302. $result= array();
  303. foreach ($images as $image) {
  304. if (strtolower($image['status'])==='active') {
  305. if (strpos($image['name'],'Windows')!==false) {
  306. $platform = Zend_Cloud_Infrastructure_Image::IMAGE_WINDOWS;
  307. } else {
  308. $platform = Zend_Cloud_Infrastructure_Image::IMAGE_LINUX;
  309. }
  310. if (strpos($image['name'],'x64')!==false) {
  311. $arch = Zend_Cloud_Infrastructure_Image::ARCH_64BIT;
  312. } else {
  313. $arch = Zend_Cloud_Infrastructure_Image::ARCH_32BIT;
  314. }
  315. $result[]= array (
  316. Zend_Cloud_Infrastructure_Image::IMAGE_ID => $image['id'],
  317. Zend_Cloud_Infrastructure_Image::IMAGE_NAME => $image['name'],
  318. Zend_Cloud_Infrastructure_Image::IMAGE_DESCRIPTION => $image['name'],
  319. Zend_Cloud_Infrastructure_Image::IMAGE_ARCHITECTURE => $arch,
  320. Zend_Cloud_Infrastructure_Image::IMAGE_PLATFORM => $platform,
  321. );
  322. }
  323. }
  324. return new Zend_Cloud_Infrastructure_ImageList($result,$this->adapterResult);
  325. }
  326. /**
  327. * Return all the available zones
  328. *
  329. * @return array
  330. */
  331. public function zonesInstance()
  332. {
  333. return array(self::RACKSPACE_ZONE_USA,self::RACKSPACE_ZONE_UK);
  334. }
  335. /**
  336. * Return the system information about the $metric of an instance
  337. * NOTE: it works only for Linux servers
  338. *
  339. * @param string $id
  340. * @param string $metric
  341. * @param null|array $options
  342. * @return array|boolean
  343. */
  344. public function monitorInstance($id, $metric, $options = null)
  345. {
  346. if (!function_exists("ssh2_connect")) {
  347. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  348. throw new Zend_Cloud_Infrastructure_Exception('Monitor requires the PHP "SSH" extension (ext/ssh2)');
  349. }
  350. if (empty($id)) {
  351. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  352. throw new Zend_Cloud_Infrastructure_Exception('You must specify the id of the instance to monitor');
  353. }
  354. if (empty($metric)) {
  355. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  356. throw new Zend_Cloud_Infrastructure_Exception('You must specify the metric to monitor');
  357. }
  358. if (!in_array($metric,$this->validMetrics)) {
  359. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  360. throw new Zend_Cloud_Infrastructure_Exception(sprintf('The metric "%s" is not valid', $metric));
  361. }
  362. if (!empty($options) && !is_array($options)) {
  363. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  364. throw new Zend_Cloud_Infrastructure_Exception('The options must be an array');
  365. }
  366. switch ($metric) {
  367. case Zend_Cloud_Infrastructure_Instance::MONITOR_CPU:
  368. $cmd= 'top -b -n '.self::MONITOR_CPU_SAMPLES.' | grep \'Cpu\'';
  369. break;
  370. case Zend_Cloud_Infrastructure_Instance::MONITOR_RAM:
  371. $cmd= 'top -b -n 1 | grep \'Mem\'';
  372. break;
  373. case Zend_Cloud_Infrastructure_Instance::MONITOR_DISK:
  374. $cmd= 'df --total | grep total';
  375. break;
  376. }
  377. if (empty($cmd)) {
  378. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  379. throw new Zend_Cloud_Infrastructure_Exception('The metric specified is not supported by the adapter');
  380. }
  381. $params= array(
  382. Zend_Cloud_Infrastructure_Instance::SSH_USERNAME => $options['username'],
  383. Zend_Cloud_Infrastructure_Instance::SSH_PASSWORD => $options['password']
  384. );
  385. $exec_time= time();
  386. $result= $this->deployInstance($id,$params,$cmd);
  387. if (empty($result)) {
  388. return false;
  389. }
  390. $monitor = array();
  391. $num = 0;
  392. $average = 0;
  393. $outputs= explode("\n",$result);
  394. foreach ($outputs as $output) {
  395. if (!empty($output)) {
  396. switch ($metric) {
  397. case Zend_Cloud_Infrastructure_Instance::MONITOR_CPU:
  398. if (preg_match('/(\d+\.\d)%us/', $output,$match)) {
  399. $usage = (float) $match[1];
  400. }
  401. break;
  402. case Zend_Cloud_Infrastructure_Instance::MONITOR_RAM:
  403. if (preg_match('/(\d+)k total/', $output,$match)) {
  404. $total = (integer) $match[1];
  405. }
  406. if (preg_match('/(\d+)k used/', $output,$match)) {
  407. $used = (integer) $match[1];
  408. }
  409. if ($total>0) {
  410. $usage= (float) $used/$total;
  411. }
  412. break;
  413. case Zend_Cloud_Infrastructure_Instance::MONITOR_DISK:
  414. if (preg_match('/(\d+)%/', $output,$match)) {
  415. $usage = (float) $match[1];
  416. }
  417. break;
  418. }
  419. $monitor['series'][] = array (
  420. 'timestamp' => $exec_time,
  421. 'value' => number_format($usage,2).'%'
  422. );
  423. $average += $usage;
  424. $exec_time+= 60; // seconds
  425. $num++;
  426. }
  427. }
  428. if ($num>0) {
  429. $monitor['average'] = number_format($average/$num,2).'%';
  430. }
  431. return $monitor;
  432. }
  433. /**
  434. * Get the adapter
  435. *
  436. * @return Zend_Service_Rackspace_Servers
  437. */
  438. public function getAdapter()
  439. {
  440. return $this->rackspace;
  441. }
  442. /**
  443. * Get last HTTP request
  444. *
  445. * @return string
  446. */
  447. public function getLastHttpRequest()
  448. {
  449. return $this->rackspace->getHttpClient()->getLastRequest();
  450. }
  451. /**
  452. * Get the last HTTP response
  453. *
  454. * @return Zend_Http_Response
  455. */
  456. public function getLastHttpResponse()
  457. {
  458. return $this->rackspace->getHttpClient()->getLastResponse();
  459. }
  460. }