|
1 <?php |
|
2 /** |
|
3 * @category Zend |
|
4 * @package Zend_Cloud |
|
5 * @subpackage Infrastructure |
|
6 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
7 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Instance of an infrastructure service |
|
12 * |
|
13 * @package Zend_Cloud |
|
14 * @subpackage Infrastructure |
|
15 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
16 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
17 */ |
|
18 class Zend_Cloud_Infrastructure_Instance |
|
19 { |
|
20 const STATUS_RUNNING = 'running'; |
|
21 const STATUS_STOPPED = 'stopped'; |
|
22 const STATUS_SHUTTING_DOWN = 'shutting-down'; |
|
23 const STATUS_REBOOTING = 'rebooting'; |
|
24 const STATUS_TERMINATED = 'terminated'; |
|
25 const STATUS_PENDING = 'pending'; |
|
26 const STATUS_REBUILD = 'rebuild'; |
|
27 const INSTANCE_ID = 'id'; |
|
28 const INSTANCE_IMAGEID = 'imageId'; |
|
29 const INSTANCE_NAME = 'name'; |
|
30 const INSTANCE_STATUS = 'status'; |
|
31 const INSTANCE_PUBLICDNS = 'publicDns'; |
|
32 const INSTANCE_CPU = 'cpu'; |
|
33 const INSTANCE_RAM = 'ram'; |
|
34 const INSTANCE_STORAGE = 'storageSize'; |
|
35 const INSTANCE_ZONE = 'zone'; |
|
36 const INSTANCE_LAUNCHTIME = 'launchTime'; |
|
37 const MONITOR_CPU = 'CpuUsage'; |
|
38 const MONITOR_RAM = 'RamUsage'; |
|
39 const MONITOR_NETWORK_IN = 'NetworkIn'; |
|
40 const MONITOR_NETWORK_OUT = 'NetworkOut'; |
|
41 const MONITOR_DISK = 'DiskUsage'; |
|
42 const MONITOR_DISK_WRITE = 'DiskWrite'; |
|
43 const MONITOR_DISK_READ = 'DiskRead'; |
|
44 const MONITOR_START_TIME = 'StartTime'; |
|
45 const MONITOR_END_TIME = 'EndTime'; |
|
46 const SSH_USERNAME = 'username'; |
|
47 const SSH_PASSWORD = 'password'; |
|
48 const SSH_PRIVATE_KEY = 'privateKey'; |
|
49 const SSH_PUBLIC_KEY = 'publicKey'; |
|
50 const SSH_PASSPHRASE = 'passphrase'; |
|
51 |
|
52 /** |
|
53 * @var Zend_Cloud_Infrastructure_Adapter |
|
54 */ |
|
55 protected $adapter; |
|
56 |
|
57 /** |
|
58 * Instance's attribute |
|
59 * |
|
60 * @var array |
|
61 */ |
|
62 protected $attributes; |
|
63 |
|
64 /** |
|
65 * Attributes required for an instance |
|
66 * |
|
67 * @var array |
|
68 */ |
|
69 protected $attributeRequired = array( |
|
70 self::INSTANCE_ID, |
|
71 self::INSTANCE_STATUS, |
|
72 self::INSTANCE_IMAGEID, |
|
73 self::INSTANCE_ZONE |
|
74 ); |
|
75 |
|
76 /** |
|
77 * Constructor |
|
78 * |
|
79 * @param Adapter $adapter |
|
80 * @param array $data |
|
81 * @return void |
|
82 */ |
|
83 public function __construct($adapter, $data = null) |
|
84 { |
|
85 if (!($adapter instanceof Zend_Cloud_Infrastructure_Adapter)) { |
|
86 require_once 'Zend/Cloud/Infrastructure/Exception.php'; |
|
87 throw new Zend_Cloud_Infrastructure_Exception("You must pass a Zend_Cloud_Infrastructure_Adapter instance"); |
|
88 } |
|
89 |
|
90 if (is_object($data)) { |
|
91 if (method_exists($data, 'toArray')) { |
|
92 $data= $data->toArray(); |
|
93 } elseif ($data instanceof Traversable) { |
|
94 $data = iterator_to_array($data); |
|
95 } |
|
96 } |
|
97 |
|
98 if (empty($data) || !is_array($data)) { |
|
99 require_once 'Zend/Cloud/Infrastructure/Exception.php'; |
|
100 throw new Zend_Cloud_Infrastructure_Exception("You must pass an array of parameters"); |
|
101 } |
|
102 |
|
103 foreach ($this->attributeRequired as $key) { |
|
104 if (empty($data[$key])) { |
|
105 require_once 'Zend/Cloud/Infrastructure/Exception.php'; |
|
106 throw new Zend_Cloud_Infrastructure_Exception(sprintf( |
|
107 'The param "%s" is a required param for %s', |
|
108 $key, |
|
109 __CLASS__ |
|
110 )); |
|
111 } |
|
112 } |
|
113 |
|
114 $this->adapter = $adapter; |
|
115 $this->attributes = $data; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Get Attribute with a specific key |
|
120 * |
|
121 * @param array $data |
|
122 * @return misc|false |
|
123 */ |
|
124 public function getAttribute($key) |
|
125 { |
|
126 if (!empty($this->attributes[$key])) { |
|
127 return $this->attributes[$key]; |
|
128 } |
|
129 return false; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Get all the attributes |
|
134 * |
|
135 * @return array |
|
136 */ |
|
137 public function getAttributes() |
|
138 { |
|
139 return $this->attributes; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Get the instance's id |
|
144 * |
|
145 * @return string |
|
146 */ |
|
147 public function getId() |
|
148 { |
|
149 return $this->attributes[self::INSTANCE_ID]; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Get the instance's image id |
|
154 * |
|
155 * @return string |
|
156 */ |
|
157 public function getImageId() |
|
158 { |
|
159 return $this->attributes[self::INSTANCE_IMAGEID]; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Get the instance's name |
|
164 * |
|
165 * @return string |
|
166 */ |
|
167 public function getName() |
|
168 { |
|
169 return $this->attributes[self::INSTANCE_NAME]; |
|
170 } |
|
171 |
|
172 /** |
|
173 * Get the status of the instance |
|
174 * |
|
175 * @return string|boolean |
|
176 */ |
|
177 public function getStatus() |
|
178 { |
|
179 return $this->adapter->statusInstance($this->attributes[self::INSTANCE_ID]); |
|
180 } |
|
181 |
|
182 /** |
|
183 * Wait for status $status with a timeout of $timeout seconds |
|
184 * |
|
185 * @param string $status |
|
186 * @param integer $timeout |
|
187 * @return boolean |
|
188 */ |
|
189 public function waitStatus($status, $timeout = Adapter::TIMEOUT_STATUS_CHANGE) |
|
190 { |
|
191 return $this->adapter->waitStatusInstance($this->attributes[self::INSTANCE_ID], $status, $timeout); |
|
192 } |
|
193 |
|
194 /** |
|
195 * Get the public DNS of the instance |
|
196 * |
|
197 * @return string |
|
198 */ |
|
199 public function getPublicDns() |
|
200 { |
|
201 if (!isset($this->attributes[self::INSTANCE_PUBLICDNS])) { |
|
202 $this->attributes[self::INSTANCE_PUBLICDNS] = $this->adapter->publicDnsInstance($this->attributes[self::INSTANCE_ID]); |
|
203 } |
|
204 return $this->attributes[self::INSTANCE_PUBLICDNS]; |
|
205 } |
|
206 |
|
207 /** |
|
208 * Get the instance's CPU |
|
209 * |
|
210 * @return string |
|
211 */ |
|
212 public function getCpu() |
|
213 { |
|
214 return $this->attributes[self::INSTANCE_CPU]; |
|
215 } |
|
216 |
|
217 /** |
|
218 * Get the instance's RAM size |
|
219 * |
|
220 * @return string |
|
221 */ |
|
222 public function getRamSize() |
|
223 { |
|
224 return $this->attributes[self::INSTANCE_RAM]; |
|
225 } |
|
226 |
|
227 /** |
|
228 * Get the instance's storage size |
|
229 * |
|
230 * @return string |
|
231 */ |
|
232 public function getStorageSize() |
|
233 { |
|
234 return $this->attributes[self::INSTANCE_STORAGE]; |
|
235 } |
|
236 |
|
237 /** |
|
238 * Get the instance's zone |
|
239 * |
|
240 * @return string |
|
241 */ |
|
242 public function getZone() |
|
243 { |
|
244 return $this->attributes[self::INSTANCE_ZONE]; |
|
245 } |
|
246 |
|
247 /** |
|
248 * Get the instance's launch time |
|
249 * |
|
250 * @return string |
|
251 */ |
|
252 public function getLaunchTime() |
|
253 { |
|
254 return $this->attributes[self::INSTANCE_LAUNCHTIME]; |
|
255 } |
|
256 |
|
257 /** |
|
258 * Reboot the instance |
|
259 * |
|
260 * @return boolean |
|
261 */ |
|
262 public function reboot() |
|
263 { |
|
264 return $this->adapter->rebootInstance($this->attributes[self::INSTANCE_ID]); |
|
265 } |
|
266 |
|
267 /** |
|
268 * Stop the instance |
|
269 * |
|
270 * @return boolean |
|
271 */ |
|
272 public function stop() |
|
273 { |
|
274 return $this->adapter->stopInstance($this->attributes[self::INSTANCE_ID]); |
|
275 } |
|
276 |
|
277 /** |
|
278 * Start the instance |
|
279 * |
|
280 * @return boolean |
|
281 */ |
|
282 public function start() |
|
283 { |
|
284 return $this->adapter->startInstance($this->attributes[self::INSTANCE_ID]); |
|
285 } |
|
286 |
|
287 /** |
|
288 * Destroy the instance |
|
289 * |
|
290 * @return boolean |
|
291 */ |
|
292 public function destroy() |
|
293 { |
|
294 return $this->adapter->destroyInstance($this->attributes[self::INSTANCE_ID]); |
|
295 } |
|
296 |
|
297 /** |
|
298 * Return the system informations about the $metric of an instance |
|
299 * |
|
300 * @param string $metric |
|
301 * @param null|array $options |
|
302 * @return array|boolean |
|
303 */ |
|
304 public function monitor($metric, $options = null) |
|
305 { |
|
306 return $this->adapter->monitorInstance($this->attributes[self::INSTANCE_ID], $metric, $options); |
|
307 } |
|
308 |
|
309 /** |
|
310 * Run arbitrary shell script on the instance |
|
311 * |
|
312 * @param array $param |
|
313 * @param string|array $cmd |
|
314 * @return string|array |
|
315 */ |
|
316 public function deploy($params, $cmd) |
|
317 { |
|
318 return $this->adapter->deployInstance($this->attributes[self::INSTANCE_ID], $params, $cmd); |
|
319 } |
|
320 } |