Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zendcloud.infrastructure.html

Size:29734
Storage flags:no_autoload,compress/gzip (20%)

Zend\Cloud\Infrastructure — Zend Framework 2 2.4.2 documentation

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use Zend\Cloud\Infrastructure\Adapter\Ec2 as Ec2Adapter;
use Zend\Cloud\Infrastructure\Factory as FactoryInfrastructure;

$key    = 'key';
$secret = 'secret';
$region = 'region';

$infrastructure = FactoryInfrastructure::getAdapter(array(
    FactoryInfrastructure::INFRASTRUCTURE_ADAPTER_KEY => 'Zend\Cloud\Infrastructure\Adapter\Ec2',
    Ec2Adapter::AWS_ACCESS_KEY => $key,
    Ec2Adapter::AWS_SECRET_KEY => $secret,
    Ec2Adapter::AWS_REGION     => $region,
));

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
2
3
4
5
6
7
8
9
$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());

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

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

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

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

getAdapter()

Return the adapter object.

getAdapterResult

getAdapterResult()

Return the original adapter result.

getLastHttpRequest

getLastHttpRequest()

Return the last HTTP Request of the adapter.

getLastHttpResponse

getLastHttpResponse()

Return the last HTTP Response of the adapter.

imagesInstance

imagesInstance()

Return all the available images to use for an instance. The return value is an instance of Zend\Cloud\Infrastructure\ImageList

listInstances

listInstances()

Return the list of of the available instances. The return is an instance of Zend\Cloud\Infrastructure\InstanceList.

monitorInstance

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 constants 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

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

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

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

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

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

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

zonesInstance()

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

Examples

Get the datetime system information of an instance

Get the result of the date command line.

1
2
3
4
5
6
7
8
9
$param = array (
    Instance::SSH_USERNAME => 'username',
    Instance::SSH_PASSWORD => 'password',
);

$cmd    = 'date';
$output = $infrastructure->deployInstance('instance-id', $param, $cmd);

echo $output;

Get the datetime system information of an instance

Get the result of the date command line.

1
2
3
4
5
6
7
8
9
$param = array (
    Instance::SSH_USERNAME => 'username',
    Instance::SSH_PASSWORD => 'password',
);

$cmd    = 'date';
$output = $infrastructure->deployInstance('instance-id', $param, $cmd);

echo $output;

Reboot an instance and wait for the running status

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use Zend\Cloud\Infrastructure\Instance;

if (!$infrastructure->rebootInstance('instance-id')) {
    die ('Error in the execution of the reboot command');
}
echo 'Reboot command executed successfully';

if ($rackspace->waitStatusInstance('instance-id', Instance::STATUS_RUNNING, 60)) {
    echo 'The instance is ready';
} else {
    echo 'The instance is not ready yet';
}

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Zend\Cloud\Infrastructure on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net