File indexing completed on 2024-12-29 05:27:29
0001 <?php 0002 /** 0003 * LICENSE 0004 * 0005 * This source file is subject to the new BSD license that is bundled 0006 * with this package in the file LICENSE.txt. 0007 * It is also available through the world-wide-web at this URL: 0008 * http://framework.zend.com/license/new-bsd 0009 * If you did not receive a copy of the license and are unable to 0010 * obtain it through the world-wide-web, please send an email 0011 * to license@zend.com so we can send you a copy immediately. 0012 * 0013 * @category Zend 0014 * @package Zend_Cloud 0015 * @subpackage DocumentService 0016 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0017 * @license http://framework.zend.com/license/new-bsd New BSD License 0018 */ 0019 0020 // require_once 'Zend/Cloud/Infrastructure/Adapter.php'; 0021 // require_once 'Zend/Cloud/Infrastructure/Instance.php'; 0022 0023 /** 0024 * Abstract infrastructure service adapter 0025 * 0026 * @category Zend 0027 * @package Zend_Cloud_Infrastructure 0028 * @subpackage Adapter 0029 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0030 * @license http://framework.zend.com/license/new-bsd New BSD License 0031 */ 0032 abstract class Zend_Cloud_Infrastructure_Adapter_AbstractAdapter implements Zend_Cloud_Infrastructure_Adapter 0033 { 0034 /** 0035 * Store the last response from the adpter 0036 * 0037 * @var array 0038 */ 0039 protected $adapterResult; 0040 0041 /** 0042 * Valid metrics for monitor 0043 * 0044 * @var array 0045 */ 0046 protected $validMetrics = array( 0047 Zend_Cloud_Infrastructure_Instance::MONITOR_CPU, 0048 Zend_Cloud_Infrastructure_Instance::MONITOR_RAM, 0049 Zend_Cloud_Infrastructure_Instance::MONITOR_DISK, 0050 Zend_Cloud_Infrastructure_Instance::MONITOR_DISK_READ, 0051 Zend_Cloud_Infrastructure_Instance::MONITOR_DISK_WRITE, 0052 Zend_Cloud_Infrastructure_Instance::MONITOR_NETWORK_IN, 0053 Zend_Cloud_Infrastructure_Instance::MONITOR_NETWORK_OUT, 0054 ); 0055 0056 /** 0057 * Get the last result of the adapter 0058 * 0059 * @return array 0060 */ 0061 public function getAdapterResult() 0062 { 0063 return $this->adapterResult; 0064 } 0065 0066 /** 0067 * Wait for status $status with a timeout of $timeout seconds 0068 * 0069 * @param string $id 0070 * @param string $status 0071 * @param integer $timeout 0072 * @return boolean 0073 */ 0074 public function waitStatusInstance($id, $status, $timeout = self::TIMEOUT_STATUS_CHANGE) 0075 { 0076 if (empty($id) || empty($status)) { 0077 return false; 0078 } 0079 0080 $num = 0; 0081 while (($num<$timeout) && ($this->statusInstance($id) != $status)) { 0082 sleep(self::TIME_STEP_STATUS_CHANGE); 0083 $num += self::TIME_STEP_STATUS_CHANGE; 0084 } 0085 return ($num < $timeout); 0086 } 0087 0088 /** 0089 * Run arbitrary shell script on an instance 0090 * 0091 * @param string $id 0092 * @param array $param 0093 * @param string|array $cmd 0094 * @return string|array 0095 */ 0096 public function deployInstance($id, $params, $cmd) 0097 { 0098 if (!function_exists("ssh2_connect")) { 0099 // require_once 'Zend/Cloud/Infrastructure/Exception.php'; 0100 throw new Zend_Cloud_Infrastructure_Exception('Deployment requires the PHP "SSH" extension (ext/ssh2)'); 0101 } 0102 0103 if (empty($id)) { 0104 // require_once 'Zend/Cloud/Infrastructure/Exception.php'; 0105 throw new Zend_Cloud_Infrastructure_Exception('You must specify the instance where to deploy'); 0106 } 0107 0108 if (empty($cmd)) { 0109 // require_once 'Zend/Cloud/Infrastructure/Exception.php'; 0110 throw new Zend_Cloud_Infrastructure_Exception('You must specify the shell commands to run on the instance'); 0111 } 0112 0113 if (empty($params) 0114 || empty($params[Zend_Cloud_Infrastructure_Instance::SSH_USERNAME]) 0115 || (empty($params[Zend_Cloud_Infrastructure_Instance::SSH_PASSWORD]) 0116 && empty($params[Zend_Cloud_Infrastructure_Instance::SSH_KEY])) 0117 ) { 0118 // require_once 'Zend/Cloud/Infrastructure/Exception.php'; 0119 throw new Zend_Cloud_Infrastructure_Exception('You must specify the params for the SSH connection'); 0120 } 0121 0122 $host = $this->publicDnsInstance($id); 0123 if (empty($host)) { 0124 // require_once 'Zend/Cloud/Infrastructure/Exception.php'; 0125 throw new Zend_Cloud_Infrastructure_Exception(sprintf( 0126 'The instance identified by "%s" does not exist', 0127 $id 0128 )); 0129 } 0130 0131 $conn = ssh2_connect($host); 0132 if (!ssh2_auth_password($conn, $params[Zend_Cloud_Infrastructure_Instance::SSH_USERNAME], 0133 $params[Zend_Cloud_Infrastructure_Instance::SSH_PASSWORD])) { 0134 // require_once 'Zend/Cloud/Infrastructure/Exception.php'; 0135 throw new Zend_Cloud_Infrastructure_Exception('SSH authentication failed'); 0136 } 0137 0138 if (is_array($cmd)) { 0139 $result = array(); 0140 foreach ($cmd as $command) { 0141 $stream = ssh2_exec($conn, $command); 0142 $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); 0143 0144 stream_set_blocking($errorStream, true); 0145 stream_set_blocking($stream, true); 0146 0147 $output = stream_get_contents($stream); 0148 $error = stream_get_contents($errorStream); 0149 0150 if (empty($error)) { 0151 $result[$command] = $output; 0152 } else { 0153 $result[$command] = $error; 0154 } 0155 } 0156 } else { 0157 $stream = ssh2_exec($conn, $cmd); 0158 $result = stream_set_blocking($stream, true); 0159 $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); 0160 0161 stream_set_blocking($errorStream, true); 0162 stream_set_blocking($stream, true); 0163 0164 $output = stream_get_contents($stream); 0165 $error = stream_get_contents($errorStream); 0166 0167 if (empty($error)) { 0168 $result = $output; 0169 } else { 0170 $result = $error; 0171 } 0172 } 0173 return $result; 0174 } 0175 }