Servers — php-opencloud 1.12.1 documentation

# Servers

## Setup

The first thing to do is pass in your credentials and instantiate a Rackspace client:

    <?php

    require 'vendor/autoload.php';

    use OpenCloud\Rackspace;

    $client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
      'username' => '{username}',
      'apiKey'   => '{apiKey}',
    ));

Now, set up the Auto Scale service:

    $service = $client->computeService('{catalogType}', '{region}', '{urlType}');

{catalogType} is the name of the service, as it appears in the service catalog. For Rackspace users, this will default to cloudServersOpenStack; for OpenStack users, you must set your own value since it can depend on your environment setup.

{region} is the Compute region the service will operate in. For Rackspace users, you can select one of the following from the supported regions page.

{urlType} is the type of URL to use, depending on what endpoints your catalog provides. For Rackspace, you may use either internalURL or publicURL. The former will execute HTTP transactions over the internal Rackspace network, reducing latency and the overall bandwidth cost - the caveat is that all of your resources must be in same region. publicURL, however, which is the default, will operate over the public Internet and is to be used for multi-region installations.

## Get server

The easiest way to retrieve a specific server is by its unique ID:

    $server = $service->server('{serverId}');

## List servers

You can list servers in two different ways:

- return an overview of each server (ID, name and links)
- return detailed information for each server

Knowing which option to use might help save unnecessary bandwidth and reduce latency.

    // overview
    $servers = $service->serverList();

    // detailed
    $servers = $service->serverList(true);

### URL parameters for filtering servers

Name | Description | Type
--- | --- | ---
image | The image ID | string
flavor | The flavor ID | string
name | The server name | string
status | The server status. Servers contain a status attribute that indicates the current server state. You can filter on the server status when you complete a list servers request, and the server status is returned in the response body. For a full list, please consult `OpenCloud\Compute\Constants\ServerState` | string
changes-since | Value for checking for changes since a previous request | A valid ISO 8601 dateTime (2011-01-24T17:08Z)
RAX-SI:image_schedule | If scheduled images enabled or not. If the value is TRUE, the list contains all servers that have an image schedule resource set on them. If the value is set to FALSE, the list contains all servers that do not have an image schedule. | bool

## Create server

### Using an image

Now we’re ready to create our instance:

    $server = $compute->server();

    $server->create(array(
        'name'     => 'My lovely server',
        'imageId'  => '{imageId}',
        'flavorId' => '{flavorId}',
    ));

It’s always best to be defensive when executing functionality over HTTP; you can achieve this best by wrapping calls in a try/catch block. It allows you to debug your failed operations in a graceful and efficient manner.

### Using a bootable volume

Firstly we need to find our volume using their IDs.

    $bootableVolume = $client->volumeService()->volume('{volumeId}');

Now we’re ready to create our instance:

    $server = $compute->server();

    $response = $server->create(array(
        'name'     => 'My lovely server',
        'volume'   => $bootableVolume,
        'flavorId' => '{flavorId}'
    ));

It’s always best to be defensive when executing functionality over HTTP; you can achieve this best by wrapping calls in a try/catch block. It allows you to debug your failed operations in a graceful and efficient manner.

### Create parameters

Name | Description | Type | Required
--- | --- | --- | ---
name | The server name. The name that you specify in a create request becomes the initial host name of the server. After the server is built, if you change the server name in the API or change the host name directly, the names are not kept in sync. | string | Yes
flavor | A populated `OpenCloud\Compute\Resource\Flavor` object representing your chosen flavor | object | Yes
image | A populated `OpenCloud\Compute\Resource\Image` object representing your chosen image | object | No, if volume is specified
volume | A populated `OpenCloud\Volume\Resource\Volume` object representing your chosen bootable volume | object | No, if image is specified
volumeDeleteOnTermination | `true` if the bootable volume should be deleted when the server is terminated; `false`, otherwise | boolean | No; default = `false`
OS-DCF:diskConfig | The disk configuration value. You can use two options: `AUTO` or `MANUAL`. `AUTO` means the server is built with a single partition the size of the target flavor disk. The file system is automatically adjusted to fit the entire partition. This keeps things simple and automated. `AUTO` is valid only for images and servers with a single partition that use the EXT3 file system. This is the default setting for applicable Rackspace base images.`MANUAL` means the server is built using whatever partition scheme and file system is in the source image. If the target flavor disk is larger, the remaining disk space is left unpartitioned. This enables images to have non-EXT3 file systems, multiple partitions, and so on, and enables you to manage the disk configuration. | string | No
networks | An array of populated `OpenCloud\Compute\Resource\Network` objects that indicate which networks your instance resides in. | array | No
metadata | An array of arbitrary data (key-value pairs) that adds additional meaning to your server. | array | No
keypair | You can install a registered keypair onto your newly created instance, thereby providing scope for keypair-based authentication. | array | No
personality | Files that you can upload to your newly created instance’s filesystem. | array | No

### Creating a server with keypairs

In order to provision an instance with a saved keypair (allowing you to SSH in without passwords), you create your server using the same operation as usual, with one extra parameter:

    $server = $compute->server();

    $server->create(array(
        'name'     => 'New server',
        'imageId'  => '{imageId}',
        'flavorId' => '{flavorId}',
        'keypair'  => 'main_key'
    ));

So, as you can see, you specify the name of an existing keypair that you previously created on the API.

### Creating a server with personality files

Before you execute the create operation, you can add “personality” files to your `OpenCloud\Compute\Resource\Server` object. These files are structured as a flat array.

    $server->addFile('/var/test_file', 'FILE CONTENT');

As you can see, the first parameter represents the filename, and the second is a string representation of its content. When the server is created these files will be created on its local filesystem. For more information about server personality files, please consult the official documentation.

## Update server

You can update certain attributes of an existing server instance. These attributes are detailed in the next section.

    $server->update(array(
       'name' => 'NEW SERVER NAME'
    ));

### Updatable attributes

name | description
--- | ---
name | The name of the server. If you edit the server name, the server host name does not change. Also, server names are not guaranteed to be unique.
accessIPv4 | The IP version 4 address.
accessIPv6 | The IP version 6 address.

## Delete server

    $server->delete();