1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.cloud.infrastructure">
<title>Zend_Cloud_Infrastructure</title>
<sect2 id="zend.cloud.infrastructure.intro">
<title>Overview</title>
<para>
The <classname>Zend_Cloud_Infrastructure</classname> is a class to manage different
cloud computing infrastructures using a common <acronym>API</acronym>.
</para>
<para>
In order to provide a common class API for different cloud vendors we implemented
a small set of basic operations for the management of instances (servers) in a cloud infrastructure.
These basic operations are:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>create a new instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>delete a new instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>start/stop an instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>reboot an instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>list of the available instances</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>get the status of an instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>wait for a status change of an instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>get the public IP or DNS name of the instance</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>list all the available images for new instances</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>list all the available geographical zones for new instances</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>monitor an instance getting the systems information (CPU%, RAM%, DISK%, NETWORK% usage)</emphasis>;
</para>
</listitem>
<listitem>
<para>
<emphasis>deploy of an instance (run arbitrary shell script on an instance)</emphasis>;
</para>
</listitem>
</itemizedlist>
<note>
<title>Deployment of an instance</title>
<para>
For the deploy operations we used the <ulink url="http://www.php.net/manual/en/book.ssh2.php">
SSH2 PHP extension (ext/ssh2)</ulink> to connect on an instance and execute shell script. The SSH2
extensions can be used to connect only to Gnu/Linux instances (servers).
</para>
</note>
<para>
This class is managed by a factory to initialize specific cloud computing adapters.
</para>
</sect2>
<sect2 id="zend.cloud.infrastructure.quick-start">
<title>Quick Start</title>
<para>
To use this class you have to initialize the factory with a specific adapters. You can
check the supported apadters in the specific Chapter <link linkend="zend.cloud.infrastructure.adapter">Zend_Cloud_Infrastructure_Adapter</link>.
We are planning to support other cloud computing vendors very soon.
</para>
<para>
For instance, to work with the AMAZON EC2 adapter you have to initialize the class with
following parameters:
</para>
<programlisting language="php"><![CDATA[
$key = 'key';
$secret = 'secret';
$region = 'region';
$infrastructure = Zend_Cloud_Infrastructure_Factory::getAdapter(array(
Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Ec2',
Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY => $key,
Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY => $secret,
Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION => $region,
));
]]></programlisting>
<para>
<classname>Zend_Cloud_Infrastructure</classname> has only a couple of methods that are vendor specific.
These methods are the creation of a new instance and the monitoring of an instance.
For instance, below is reported an example that shows how to create a new instance using the Amazon EC2 adapter:
</para>
<programlisting language="php"><![CDATA[
$param= array (
'imageId' => 'your-image-id',
'instanceType' => 'your-instance-type',
);
$instance= $infrastructure->createInstance('name of the instance', $param);
printf ("Name of the instance: %s\n", $instance->getName());
printf ("ID of the instance : %s\n", $instance->getId());
]]></programlisting>
<para>
The interface of the <methodname>createInstance</methodname> is always the same, only
the content of <emphasis>$param</emphasis> is specific to the adapter. for more
information about the adapter supported by Zend_Cloud_Infrastructure go to the specific
<link linkend="zend.cloud.infrastructure.adapter">page of the manual</link>.
</para>
<para>
The <classname>Zend_Cloud_Infrastructure</classname> uses the classes
<classname>Zend_Cloud_Infrastructure_Instance</classname> and
<classname>Zend_Cloud_Infrastructure_Image</classname> to manage the instances (servers)
and the images of an instance.
</para>
</sect2>
<sect2 id="zend.cloud.infrastructure.methods">
<title>Available Methods</title>
<variablelist>
<varlistentry id="zend.cloud.infrastructure.methods.create-instance">
<term>
<methodsynopsis>
<methodname>createInstance</methodname>
<methodparam>
<funcparams>string $name, array $options</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Create an instance.
The return value is an instance of <classname>Zend_Cloud_Infrastructure_Instance</classname>.
In case of error the return is <emphasis>false</emphasis>.
</para>
<para>
<emphasis>$name</emphasis> is the name of the instance to create
</para>
<para>
<emphasis>$options</emphasis> is the array contains the specific parameter for the cloud adapter.
For more info read the Chapter of <link linkend="zend.cloud.infrastructure.adapter">Zend_Cloud_Infrastructure_Adapter</link>.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.deploy-instance">
<term>
<methodsynopsis>
<methodname>deployInstance</methodname>
<methodparam>
<funcparams>string $id, array $param, string|array $cmd</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Run arbitrary shell scripts on an instance.
Return a string or an array contains all the standard output (errors included) of the scripts executed in the instance.
<note><title>Requirement</title>
<para>
In order to use the deployInstance method you have to install the SSH2 extension (ext/ssh2) of PHP.
The SSH2 extensions can be used to connect only to Gnu/Linux instances (servers).
For more info about the SSH2 extension, <ulink url="http://www.php.net/manual/en/book.ssh2.php">click here</ulink>.
</para>
</note>
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
<para>
<emphasis>$param</emphasis> is an array contains the username and the password to be used for the SSH connection.
The username and the password must be specified using the following constants key of the <classname>Zend_Cloud_Infrastructure_Instance</classname>:
SSH_USERNAME, SSH_PASSWORD.
</para>
<para>
<emphasis>$cmd</emphasis> is a string (or an array) contains the commands line to be executed in the instance.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.destroy-instance">
<term>
<methodsynopsis>
<methodname>destroyInstance</methodname>
<methodparam>
<funcparams>string $id</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Destroy an instance.
Return <emphasis>true</emphasis> in case of success, <emphasis>false</emphasis> in case of error.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.get-adapter">
<term>
<methodsynopsis>
<methodname>getAdapter</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return the adapter object.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.get-adapter-result">
<term>
<methodsynopsis>
<methodname>getAdapterResult</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return the original adapter result.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.get-last-http-request">
<term>
<methodsynopsis>
<methodname>getLastHttpRequest</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return the last HTTP Request of the adapter.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.get-last-http-response">
<term>
<methodsynopsis>
<methodname>getLastHttpResponse</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return the last HTTP Response of the adapter.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.images-instance">
<term>
<methodsynopsis>
<methodname>imagesInstance</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return all the available images to use for an instance.
The return value is an instance of <classname>Zend_Cloud_Infrastructure_ImageList</classname>
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.list-instances">
<term>
<methodsynopsis>
<methodname>listInstances</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return the list of of the available instances.
The return is an instance of <classname>Zend_Cloud_Infrastructure_InstanceList</classname>.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.monitor-instance">
<term>
<methodsynopsis>
<methodname>monitorInstance</methodname>
<methodparam>
<funcparams>string $id,string $metric,array $options=null</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Monitor an instance. Return the system information about the metric of an instance.
The return value is an array that contains samples of values, timestamp and the elaboration of the average value.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance;
</para>
<para>
<emphasis>$metric</emphasis> is the metric to be monitored. The allowed metrics are reported as contants of the
<classname>Zend_Cloud_Infrastructure_Instance</classname> class: MONITOR_CPU, MONITOR_RAM,
MONITOR_NETWORK_IN, MONITOR_NETWORK_OUT, MONITOR_DISK, MONITOR_DISK_WRITE, MONITOR_DISK_READ.
</para>
<para>
<emphasis>$options</emphasis> is the optional array contains the adapter specific options.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.public-dns-instance">
<term>
<methodsynopsis>
<methodname>publicDnsInstance</methodname>
<methodparam>
<funcparams>string $id</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return the public DNS name or the IP address of the instance. The return value is a string.
In case of error the return is <emphasis>false</emphasis>.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.reboot-instance">
<term>
<methodsynopsis>
<methodname>rebootInstance</methodname>
<methodparam>
<funcparams>string $id</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Reboot an instance.
Return <emphasis>true</emphasis> in case of success, <emphasis>false</emphasis> in case of error.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.start-instance">
<term>
<methodsynopsis>
<methodname>startInstance</methodname>
<methodparam>
<funcparams>string $id</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Start an instance.
Return <emphasis>true</emphasis> in case of success, <emphasis>false</emphasis> in case of error.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.status-instance">
<term>
<methodsynopsis>
<methodname>statusInstance</methodname>
<methodparam>
<funcparams>string $id</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Get the status of an instance. The return value is a string.
The available status are reported in the following constants of the class <classname>Zend_Cloud_Infrastructure_Instance</classname>:
STATUS_STOPPED, STATUS_RUNNING, STATUS_SHUTTING_DOWN, STATUS_REBOOTING,
STATUS_TERMINATED, STATUS_PENDING, STATUS_REBUILD.
In case of error the return is <emphasis>false</emphasis>.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.stop-instance">
<term>
<methodsynopsis>
<methodname>stopInstance</methodname>
<methodparam>
<funcparams>string $id</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Stop an instance.
Return <emphasis>true</emphasis> in case of success, <emphasis>false</emphasis> in case of error.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.wait-status-instance">
<term>
<methodsynopsis>
<methodname>waitStatusInstance</methodname>
<methodparam>
<funcparams>string $id, string $status,integer $timeout=30</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Wait the status change of an instance for a maximum time of <emphasis>n</emphasis> seconds.
Return <emphasis>true</emphasis> if the status changes as expected, <emphasis>false</emphasis> if not.
</para>
<para>
<emphasis>$id</emphasis> is the ID of the instance;
</para>
<para>
<emphasis>$status</emphasis> is the status to wait for;
</para>
<para>
<emphasis>$timeout</emphasis> is the maximum time, in seconds, to wait for the status change. This parametr is optional and the default value is 30 seconds.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.cloud.infrastructure.methods.zones-instance">
<term>
<methodsynopsis>
<methodname>zonesInstance</methodname>
<methodparam>
<funcparams/>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Return all the available zones for an instance.
The return value is an array.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2 id="zend.cloud.infrastructure.examples">
<title>Examples</title>
<example id="zend.cloud.infrastructure.examples.authenticate">
<title>Get the datetime system information of an instance</title>
<para>Get the result of the <emphasis>date</emphasis> command line.</para>
<programlisting language="php"><![CDATA[
$param = array (
Instance::SSH_USERNAME => 'username',
Instance::SSH_PASSWORD => 'password',
);
$cmd = 'date';
$output = $infrastructure->deployInstance('instance-id', $param, $cmd);
echo $output;
]]></programlisting>
</example>
<example id="zend.cloud.infrastructure.examples.get-datetime">
<title>Get the datetime system information of an instance</title>
<para>Get the result of the <emphasis>date</emphasis> command line.</para>
<programlisting language="php"><![CDATA[
$param = array (
Instance::SSH_USERNAME => 'username',
Instance::SSH_PASSWORD => 'password',
);
$cmd = 'date';
$output = $infrastructure->deployInstance('instance-id', $param, $cmd);
echo $output;
]]></programlisting>
</example>
<example id="zend.cloud.infrastructure.examples.reboot">
<title>Reboot an instance and wait for the running status</title>
<para>Reboot an instance and wait 60 seconds for the running status.</para>
<programlisting language="php"><![CDATA[
if (!$infrastructure->rebootInstance('instance-id')) {
die ('Error in the execution of the reboot command');
}
echo 'Reboot command executed successfully';
if ($rackspace->waitStatusInstance('instance-id', Zend_Cloud_Infrastructure_Instance::STATUS_RUNNING, 60)) {
echo 'The instance is ready';
} else {
echo 'The instance is not ready yet';
}
]]></programlisting>
</example>
</sect2>
</sect1>
|