/library/Zend/Cloud/Infrastructure/Adapter/AbstractAdapter.php

https://github.com/bruisedlee/zf2 · PHP · 173 lines · 93 code · 20 blank · 60 comment · 20 complexity · d88790cc790309b7cbbd98cf3bc870c2 MD5 · raw file

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