Documentation

Zend_Cloud_Infrastructure - SimpleCloud API: Zend_Cloud

Zend_Cloud_Infrastructure

Overview

The Zend_Cloud_Infrastructure is a class to manage different cloud computing infrastructures using a common API.

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:

  • create a new instance;

  • delete a new instance;

  • start/stop an instance;

  • reboot an instance;

  • list of the available instances;

  • get the status of an instance;

  • wait for a status change of an instance;

  • get the public IP or DNS name of the instance;

  • list all the available images for new instances;

  • list all the available geographical zones for new instances;

  • monitor an instance getting the systems information (CPU%, RAM%, DISK%, NETWORK% usage);

  • deploy of an instance (run arbitrary shell script on an instance);

Note: Deployment of an instance
For the deploy operations we used the » SSH2 PHP extension (ext/ssh2) to connect on an instance and execute shell script. The SSH2 extensions can be used to connect only to Gnu/Linux instances (servers).

This class is managed by a factory to initialize specific cloud computing adapters.

Quick Start

To use this class you have to initialize the factory with a specific adapters. You can check the supported apadters in the specific Chapter Zend_Cloud_Infrastructure_Adapter. We are planning to support other cloud computing vendors very soon.

For instance, to work with the AMAZON EC2 adapter you have to initialize the class with following parameters:

  1. $key    = 'key';
  2. $secret = 'secret';
  3. $region = 'region';
  4.  
  5. $infrastructure = Zend_Cloud_Infrastructure_Factory::getAdapter(array(
  6.     Zend_Cloud_Infrastructure_Factory::INFRASTRUCTURE_ADAPTER_KEY => 'Zend_Cloud_Infrastructure_Adapter_Ec2',
  7.     Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_ACCESS_KEY => $key,
  8.     Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_SECRET_KEY => $secret,
  9.     Zend_Cloud_Infrastructure_Adapter_Ec2::AWS_REGION     => $region,
  10. ));

Zend_Cloud_Infrastructure 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:

  1. $param= array (
  2.     'imageId'      => 'your-image-id',
  3.     'instanceType' => 'your-instance-type',
  4. );
  5.  
  6. $instance= $infrastructure->createInstance('name of the instance', $param);
  7.  
  8. printf ("Name of the instance: %s\n", $instance->getName());
  9. printf ("ID of the instance  : %s\n", $instance->getId());

The interface of the createInstance is always the same, only the content of $param is specific to the adapter. for more information about the adapter supported by Zend_Cloud_Infrastructure go to the specific page of the manual.

The Zend_Cloud_Infrastructure uses the classes Zend_Cloud_Infrastructure_Instance and Zend_Cloud_Infrastructure_Image to manage the instances (servers) and the images of an instance.

Available Methods

createInstance ( string $name, array $options )

Create an instance. The return value is an instance of Zend_Cloud_Infrastructure_Instance. In case of error the return is false.

$name is the name of the instance to create

$options is the array contains the specific parameter for the cloud adapter. For more info read the Chapter of Zend_Cloud_Infrastructure_Adapter.

deployInstance ( string $id, array $param, string|array $cmd )

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: Requirement
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, » click here.

$id is the ID of the instance

$param 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 Zend_Cloud_Infrastructure_Instance: SSH_USERNAME, SSH_PASSWORD.

$cmd is a string (or an array) contains the commands line to be executed in the instance.

destroyInstance ( string $id )

Destroy an instance. Return true in case of success, false in case of error.

$id is the ID of the instance

getAdapter ( )

Return the adapter object.

getAdapterResult ( )

Return the original adapter result.

getLastHttpRequest ( )

Return the last HTTP Request of the adapter.

getLastHttpResponse ( )

Return the last HTTP Response of the adapter.

imagesInstance ( )

Return all the available images to use for an instance. The return value is an instance of Zend_Cloud_Infrastructure_ImageList

listInstances ( )

Return the list of of the available instances. The return is an instance of Zend_Cloud_Infrastructure_InstanceList.

monitorInstance ( string $id,string $metric,array $options=null )

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.

$id is the ID of the instance;

$metric is the metric to be monitored. The allowed metrics are reported as contants of the Zend_Cloud_Infrastructure_Instance class: MONITOR_CPU, MONITOR_RAM, MONITOR_NETWORK_IN, MONITOR_NETWORK_OUT, MONITOR_DISK, MONITOR_DISK_WRITE, MONITOR_DISK_READ.

$options is the optional array contains the adapter specific options.

publicDnsInstance ( string $id )

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 false.

$id is the ID of the instance

rebootInstance ( string $id )

Reboot an instance. Return true in case of success, false in case of error.

$id is the ID of the instance

startInstance ( string $id )

Start an instance. Return true in case of success, false in case of error.

$id is the ID of the instance

statusInstance ( string $id )

Get the status of an instance. The return value is a string. The available status are reported in the following constants of the class Zend_Cloud_Infrastructure_Instance: STATUS_STOPPED, STATUS_RUNNING, STATUS_SHUTTING_DOWN, STATUS_REBOOTING, STATUS_TERMINATED, STATUS_PENDING, STATUS_REBUILD. In case of error the return is false.

$id is the ID of the instance

stopInstance ( string $id )

Stop an instance. Return true in case of success, false in case of error.

$id is the ID of the instance

waitStatusInstance ( string $id, string $status,integer $timeout=30 )

Wait the status change of an instance for a maximum time of n seconds. Return true if the status changes as expected, false if not.

$id is the ID of the instance;

$status is the status to wait for;

$timeout is the maximum time, in seconds, to wait for the status change. This parametr is optional and the default value is 30 seconds.

zonesInstance ( )

Return all the available zones for an instance. The return value is an array.

Examples

Example #1 Get the datetime system information of an instance

Get the result of the date command line.

  1. $param = array (
  2.     Instance::SSH_USERNAME => 'username',
  3.     Instance::SSH_PASSWORD => 'password',
  4. );
  5.  
  6. $cmd    = 'date';
  7. $output = $infrastructure->deployInstance('instance-id', $param, $cmd);
  8.  
  9. echo $output;

Example #2 Get the datetime system information of an instance

Get the result of the date command line.

  1. $param = array (
  2.     Instance::SSH_USERNAME => 'username',
  3.     Instance::SSH_PASSWORD => 'password',
  4. );
  5.  
  6. $cmd    = 'date';
  7. $output = $infrastructure->deployInstance('instance-id', $param, $cmd);
  8.  
  9. echo $output;

Example #3 Reboot an instance and wait for the running status

Reboot an instance and wait 60 seconds for the running status.

  1. if (!$infrastructure->rebootInstance('instance-id')) {
  2.     die ('Error in the execution of the reboot command');
  3. }
  4. echo 'Reboot command executed successfully';
  5.  
  6. if ($rackspace->waitStatusInstance('instance-id', Zend_Cloud_Infrastructure_Instance::STATUS_RUNNING, 60)) {
  7.     echo 'The instance is ready';
  8. } else {
  9.     echo 'The instance is not ready yet';
  10. }

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts