Facebook Twitter E-mail RSS
magnify
formats

Rackspace Cloud Server API basics

Published on June 26, 2012

So you have made the decision to wean off the GUI and learn something about using a command line API tool. The Rackspace Cloud Servers API basics starts with using a simple tool in this case the openstack-compute command line tool. I will begin by showing you the main uses of the most common command line tool to manage Rackspace cloud servers. This article is the first in a series of ‘basic’ articles that will show you various command line tools to use to customize your Rackspace experience. This tool is a simple python command line interface used to issue direct API calls to Rackspace cloud servers. It provides all the server controls you would find within the control panel in a single command line interface.

Installation:  The program is a simple python command line script which should be able to be installed on any OS with python installed. I wont even bother going over how to install it, you can figure it out, If you can not install software on your workstation you might want to stay away from a server- sorry.

Basic configuration:  The tool is written to allow you to pass the account username and API key via the command line or add it to your system as an environment variable. On a Unix system this is just a matter of adding the following lines to your profile.

export OPENSTACK_COMPUTE_USERNAME=frankjones
export OPENSTACK_COMPUTE_APIKEY=4f3wO9ssdfaer2kw21sdf0

Before we dig any deeper you may to want to look over the documentation for this tool located here.  Next you may want to review the Rackspace developers documentation to better understand the terminology used with this tool. So lets get to the meat of this article -

How to use this great little tool

The list command:  First lets start with a example of listing all the servers on your account. A basic command as long as the username and APIkey are passed via the environment variables if not you will need to add: –username <rackspace username> –apikey <account API key> to every command listed in this article.

$ openstack-compute list
+————+—————–+———+—————+—————+
|    ID           |       Name           | Status  | Public IP      |   Private IP   |
+————+—————–+———+—————+—————+
| 20151110 | webtest2           | ACTIVE | 50.56.22.1 | 10.177.192.8   |
| 20121115 | logserver           | ACTIVE | 50.56.23.5 | 10.177.192.4   |
| 20121113 | webtest1            | ACTIVE | 50.56.23.2| 10.177.194.1    |
+————+——————+——–+—————+—————+

This gives us a quick view of all the servers on the account. It provides the server ID, name, and server status which are required to dig deeper into the server details.

The show command:  Now lets look at the details on a specific server (viewed by ID). The format is: openstack-compute show <server ID>

$ openstack-compute show 20121113
+————+———————————+
|  Property  |              Value                            |
+————+———————————+
| flavor       | 1G server                                    |
| hostId     | 3fc8serbc4ea8sef916c08         |
| id             | 20121113                                      |
| image      | CentOS 6.2                                 |
| metadata | {}                                                 |
| name        | webtest1                                     |
| private ip | 10.177.194.1                               |
| progress   | 100                                              |
| public ip  | 50.56.23.2                                  |
| status       | ACTIVE                                       |
+————+———————————-+

This provides a list of details about the specific server. Such as image and flavor type which are details that are needed to create new servers.

The flavor-List command:  Flavor is the term for the server size which includes RAM and disk size available. The flavors are listed in RAM size and start at the size of 256MB and include 512MB,1GB, 2GB, 4GB, 8GB, 15GB, and 30GB. Below is a list of the flavors available on Rackspace cloud servers with the RAM and Disks sizes listed.

$ openstack-compute flavor-list
+—-+—————+——-+——+
| ID |      Name     |  RAM  | Disk |
+—-+—————+——-+——+
| 1  | 256 server    | 256   | 10      |
| 2  | 512 server    | 512   | 20      |
| 3  | 1GB server    | 1024  | 40    |
| 4  | 2GB server    | 2048  | 80   |
| 5  | 4GB server    | 4096  | 160  |
| 6  | 8GB server    | 8192  | 320  |
| 7  | 15.5GB server | 15872 | 620  |
| 8  | 30GB server   | 30720 | 1200 |
+—-+—————+——-+——+

The image-list command:  Next are images, they are the OS builds that are available. Images fall into two categories, the basic models provided by Rackspace and any custom images you have created within your account. Notice in the example below the last image has a much higher ID number, that is a custom image the rest are the current Rackspace default OS choices. Unlike Amazon there is no shared image pool so you can only build a default system provided by Rackspace or your own custom built image.

$ openstack-compute image-list
+———-+—————————————————+——–+
|    ID    |                        Name                                         | Status |
+———-+—————————————————+——–+
| 24       | Windows Server 2008 SP2 x64                       | ACTIVE |
| 31       | Windows Server 2008 SP2 x86                       | ACTIVE |
| 56       | Windows Server 2008 SP2 x86 – MSSQL2K8R2          | ACTIVE |
| 57       | Windows Server 2008 SP2 x64 – MSSQL2K8R2          | ACTIVE |
| 85       | Windows Server 2008 R2 x64                        | ACTIVE |
| 86       | Windows Server 2008 R2 x64 – MSSQL2K8R2           | ACTIVE |
| 89       | Windows Server 2008 R2 x64 – SQL2K8R2 Web         | ACTIVE |
| 91       | Windows Server 2008 R2 + SQL Server 2012 Standard | ACTIVE |
| 92       | Windows Server 2008 R2 + SQL Server 2012 Web      | ACTIVE |
| 100      | Arch 2011.10                                      | ACTIVE |
| 103      | Debian 5 (Lenny)                                  | ACTIVE |
| 104      | Debian 6 (Squeeze)                                | ACTIVE |
| 108      | Gentoo 11.0                                               | ACTIVE |
| 109      | openSUSE 12                                           | ACTIVE |
| 110      | Red Hat Enterprise Linux 5.5                   | ACTIVE |
| 111      | Red Hat Enterprise Linux 6                        | ACTIVE |
| 112      | Ubuntu 10.04 LTS                                  | ACTIVE |
| 114      | CentOS 5.6                                            | ACTIVE |
| 115      | Ubuntu 11.04                                           | ACTIVE |
| 116      | Fedora 15                                               | ACTIVE |
| 118      | CentOS 6.0                                           | ACTIVE |
| 119      | Ubuntu 11.10                                         | ACTIVE |
| 120      | Fedora 16                                               | ACTIVE |
| 121      | CentOS 5.8                                             | ACTIVE |
| 122      | CentOS 6.2                                             | ACTIVE |
| 125      | Ubuntu 12.04 LTS                                  | ACTIVE |
| 126      | Fedora 17                                                | ACTIVE |
| 17177128 | test-setup-122911                                 | ACTIVE |
+———-+—————————————————+——–+

To create a new server you need to understand these two options so you can add these variables to create a new system.

The boot command:  Here is an example of a create command for a new server named ‘webtest3′ with a size of 1GB (flavor 3) and the OS of CentOS 6.2 (image 122). The format is: openstack-compute boot <flavor #> <image #> <new server name>

$ openstack-compute boot –flavor 3 –image 122 webtest3
+———–+—————————————————————-+
|  Property |                             Value                              |
+———–+—————————————————————-+
| addresses | {u’public’: [u'50.56.118.48'], u’private’: [u'10.19.1.12']} |
| adminPass | v7lAC76webtest3                                                |
| flavorId  | 3                                                                                  |
| hostId    | 3fe7f91b4657cf0a098f5df1b27                             |
| id        | 20936230                                                                      |
| imageId   | 122                                                                            |
| metadata  | {}                                                                              |
| name      | webtest3                                                                    |
| progress  | 0                                                                                |
| status    | BUILD                                                                        |
+———–+—————————————————————-+

The output shows the command was accepted and the new server was created, it has setup the public and private IP’s and assigned a new root login. The status is ‘BUILD’ and will take 3-5 minutes to completed then the status will become ‘ACTIVE’ when the server is ready for your use. To check the status and ensure the build completes just run the ‘show’ command. The format is: openstack-compute show <server ID>

$ openstack-compute show 20936230
+————+———————————-+
|  Property  |              Value               |
+————+———————————-+
| flavor     | 1GB server                       |
| hostId     | 3fe7f91b4657cf0a098f5df1b27 |
| id         | 20936230                         |
| image      | CentOS 6.2                       |
| metadata   | {}                               |
| name       | webtest3                         |
| private ip | 10.19.1.12                     |
| progress   | 100                              |
| public ip  | 50.56.118.48                    |
| status     | ACTIVE                           |
+————+———————————-+

The reboot command:  Another useful command is the server reboot command this can be important if the server locks up or otherwise fails and you need to restore it. The command is pretty straight forward its : openstack-compute reboot <server ID>  This command will not have any output but you can use the ‘show’ command to see the status which will at first be ‘REBOOT’ then ‘ACTIVE’ once its up and running again.

The root-password command:  Along that same line say you have forgot your root password (or administrator on Windows images) and need to reset it to get access to your server again. The control panel will let you reset it, but it generates a random new password for you. This program allows you to reset it to whatever you want in a single command: openstack-compute root-password <server ID> it will then prompt you for the new password twice to ensure its typed correctly. The server will then reboot and once back online the new password will be in effect.

The rename command:  One more useful command is the server rename command, all this does is change the server name as its listed in the API and the control panel it does not make any changes to the server such as changing the hostname. The command is : openstack-compute rename <server ID> <new server name>

The image-create command:  So now you have your new server all configured the way you want it. Before you go into production or make that next new code push you decide to make a on-demand backup of the server in its current state. So if needed you can ‘roll back’ and restore the system to this point in time. This is a full backup of the drive containing all data (configuration files etc). The format for this is:  openstack-compute <server ID> <backup name>

$ openstack-compute image-create 20936230 test3backup
+———-+—————————+
| Property |           Value           |
+———-+—————————+
| id       | 23478575                  |
| name     | test3backup               |
| serverId | 20936230                  |
| status   | QUEUED                    |
| updated  | 2012-06-23T00:44:35-05:00 |
+———-+—————————+

The creation of the server image can take a few minutes (depending on the number of inodes on the server), once it has completed it will be added to image listing for your account so you can use the command: openstack-compute image-list to see all your images and the new one should be listed at the bottom.

The image-delete command:  So now times goes by and you have created multiple backup images and there are several you no longer need. Even though they are pretty cheap to store you may want to remove some of the old ones. Lucky for you there is a command for just that purpose. First you would do a image-list and pick out the image ID of the image you want to remove then issue the following command: openstack-compute image-delete <image ID>  This command will run and you will not get any direct feedback but next time you run and image-list command you will see the image is no longer listed on your account.

The resize command:  The next issue I will show is how to resize a server via the command line tool. First off we need to determine what size the server is and what size you want it to become. Using the show command you can see the details of a specific server: openstack-compute show <server ID> using the example server above (webtest3) its a 1GB server and we can to resize it to a 256MB server. The command to resize using this example is: openstack-compute resize <server ID> <flavor #>

Now the resize command has no output so you have to use the show command to see if the resize started, if it is working it will show status of PREP_RESIZE as the example below shows.

$ openstack-compute show 20936230
+————+———————————-+
|  Property  |              Value               |
+————+———————————-+
| flavor     | 1GB server                       |
| hostId     | 3fe7f91b4657cf0a098f5df1b27ea1fa |
| id         | 20936230                         |
| image      | CentOS 6.2                       |
| metadata   | {}                               |
| name       | webtest3                         |
| private ip | 10.179.1.102                     |
| progress   | 0                                |
| public ip  | 50.56.188.248                    |
| status     | PREP_RESIZE                      |
+————+———————————-+

The resize can take some time depending on the size of the server and the number of inodes in use. Once it has completed it will reboot and come back online as the new size and be in the ‘verify resize’ state. From this point you need to check the status of the server then either confirm or revert the resize.

$ openstack-compute show 20936230
+————+———————————-+
|  Property  |              Value               |
+————+———————————-+
| flavor     | 256 server                       |
| hostId     | 4a9002b8e571ff57c1baf981844270b3 |
| id         | 20936230                         |
| image      | CentOS 6.2                       |
| metadata   | {}                               |
| name       | webtest1                         |
| private ip | 10.179.1.102                     |
| progress   | 0                                |
| public ip  | 50.56.188.248                    |
| status     | VERIFY_RESIZE                    |
+————+———————————-+

Then you would issue one of the following two commands to either verify or revert the resize.

$ openstack-compute resize-confirm 20936230

$ openstack-compute resize-revert 20936230

There is no output from these commands but after issued you can use the ‘show’ command to verify it has executed.

The delete command:  Now say this server has lived out its needed life and your ready to delete it from the account. The command to do that is pretty simple its: openstack-compute delete <server ID>  This command will not return any values but next time you do a list command the server will no longer be on the list.

In conclusion these commands will provide you the basic skills needed to use the openstack-compute command line tool.  In later articles I will go into additional details on how to do advanced curl commands and once live I will be getting deeper into the NOVA client which is the command line tool for the next generation Rackspace cloud platform that will be public within a few months. I’m current in the beta so I’m having a blast playing with Rackspace’s wonderful new toy, which I look forward to teaching you all about soon.

Enjoy

- Nomad