AbstractAdapter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_Cloud
  15. * @subpackage DocumentService
  16. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. require_once 'Zend/Cloud/Infrastructure/Adapter.php';
  20. require_once 'Zend/Cloud/Infrastructure/Instance.php';
  21. /**
  22. * Abstract infrastructure service adapter
  23. *
  24. * @category Zend
  25. * @package Zend_Cloud_Infrastructure
  26. * @subpackage Adapter
  27. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. abstract class Zend_Cloud_Infrastructure_Adapter_AbstractAdapter implements Zend_Cloud_Infrastructure_Adapter
  31. {
  32. /**
  33. * Store the last response from the adpter
  34. *
  35. * @var array
  36. */
  37. protected $adapterResult;
  38. /**
  39. * Valid metrics for monitor
  40. *
  41. * @var array
  42. */
  43. protected $validMetrics = array(
  44. Zend_Cloud_Infrastructure_Instance::MONITOR_CPU,
  45. Zend_Cloud_Infrastructure_Instance::MONITOR_RAM,
  46. Zend_Cloud_Infrastructure_Instance::MONITOR_DISK,
  47. Zend_Cloud_Infrastructure_Instance::MONITOR_DISK_READ,
  48. Zend_Cloud_Infrastructure_Instance::MONITOR_DISK_WRITE,
  49. Zend_Cloud_Infrastructure_Instance::MONITOR_NETWORK_IN,
  50. Zend_Cloud_Infrastructure_Instance::MONITOR_NETWORK_OUT,
  51. );
  52. /**
  53. * Get the last result of the adapter
  54. *
  55. * @return array
  56. */
  57. public function getAdapterResult()
  58. {
  59. return $this->adapterResult;
  60. }
  61. /**
  62. * Wait for status $status with a timeout of $timeout seconds
  63. *
  64. * @param string $id
  65. * @param string $status
  66. * @param integer $timeout
  67. * @return boolean
  68. */
  69. public function waitStatusInstance($id, $status, $timeout = self::TIMEOUT_STATUS_CHANGE)
  70. {
  71. if (empty($id) || empty($status)) {
  72. return false;
  73. }
  74. $num = 0;
  75. while (($num<$timeout) && ($this->statusInstance($id) != $status)) {
  76. sleep(self::TIME_STEP_STATUS_CHANGE);
  77. $num += self::TIME_STEP_STATUS_CHANGE;
  78. }
  79. return ($num < $timeout);
  80. }
  81. /**
  82. * Run arbitrary shell script on an instance
  83. *
  84. * @param string $id
  85. * @param array $param
  86. * @param string|array $cmd
  87. * @return string|array
  88. */
  89. public function deployInstance($id, $params, $cmd)
  90. {
  91. if (!function_exists("ssh2_connect")) {
  92. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  93. throw new Zend_Cloud_Infrastructure_Exception('Deployment requires the PHP "SSH" extension (ext/ssh2)');
  94. }
  95. if (empty($id)) {
  96. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  97. throw new Zend_Cloud_Infrastructure_Exception('You must specify the instance where to deploy');
  98. }
  99. if (empty($cmd)) {
  100. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  101. throw new Zend_Cloud_Infrastructure_Exception('You must specify the shell commands to run on the instance');
  102. }
  103. if (empty($params)
  104. || empty($params[Zend_Cloud_Infrastructure_Instance::SSH_USERNAME])
  105. || (empty($params[Zend_Cloud_Infrastructure_Instance::SSH_PASSWORD])
  106. && empty($params[Zend_Cloud_Infrastructure_Instance::SSH_KEY]))
  107. ) {
  108. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  109. throw new Zend_Cloud_Infrastructure_Exception('You must specify the params for the SSH connection');
  110. }
  111. $host = $this->publicDnsInstance($id);
  112. if (empty($host)) {
  113. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  114. throw new Zend_Cloud_Infrastructure_Exception(sprintf(
  115. 'The instance identified by "%s" does not exist',
  116. $id
  117. ));
  118. }
  119. $conn = ssh2_connect($host);
  120. if (!ssh2_auth_password($conn, $params[Zend_Cloud_Infrastructure_Instance::SSH_USERNAME],
  121. $params[Zend_Cloud_Infrastructure_Instance::SSH_PASSWORD])) {
  122. require_once 'Zend/Cloud/Infrastructure/Exception.php';
  123. throw new Zend_Cloud_Infrastructure_Exception('SSH authentication failed');
  124. }
  125. if (is_array($cmd)) {
  126. $result = array();
  127. foreach ($cmd as $command) {
  128. $stream = ssh2_exec($conn, $command);
  129. $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
  130. stream_set_blocking($errorStream, true);
  131. stream_set_blocking($stream, true);
  132. $output = stream_get_contents($stream);
  133. $error = stream_get_contents($errorStream);
  134. if (empty($error)) {
  135. $result[$command] = $output;
  136. } else {
  137. $result[$command] = $error;
  138. }
  139. }
  140. } else {
  141. $stream = ssh2_exec($conn, $cmd);
  142. $result = stream_set_blocking($stream, true);
  143. $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
  144. stream_set_blocking($errorStream, true);
  145. stream_set_blocking($stream, true);
  146. $output = stream_get_contents($stream);
  147. $error = stream_get_contents($errorStream);
  148. if (empty($error)) {
  149. $result = $output;
  150. } else {
  151. $result = $error;
  152. }
  153. }
  154. return $result;
  155. }
  156. }