How to launch and delete OpenStack instances using Python and Apache Libcloud

What you need

This tutorial will guide you with step-by-step instructions on how to get a basic authentication token on DreamCompute and launch a virtual machine.

You will need to have your username, password and project ID available. This information can be easily retrieved from DreamCompute’s dashboard, in the openrc file.

You must also need to have

libcloud 0.15.1 or higher installed.

Authenticate

Add the following code to a file or copy and paste them in an interactive shell, when available. Make sure to use your username, password and project ID.

You can use an interactive Python shell by calling python -i or ipython.

from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

auth_username = 'your_auth_username'
auth_password = 'your_auth_password'
project_name = 'your_project_name_or_id'

auth_url = 'https://iad2.dream.io:5000/v2.0'
region_name = 'RegionOne'

provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
                auth_password,
                ex_force_auth_url=auth_url,
                ex_force_auth_version='2.0_password',
                ex_tenant_name=project_name,
                ex_force_service_region=region_name)

This code creates the conn object that you can use in the next steps.

Flavors and images

To launch an instance, which is just a virtual machine, you choose a flavor and an image. The flavor represents the size of the instance, including the number of CPUs and amount of RAM and disk space. An image is a prepared OS installation from which you clone your instance. When you boot instances in a public cloud, larger flavors can be more expensive than smaller ones in terms of monetary cost.

To list the images that are available in your cloud, run some API calls:

images = conn.list_images()
for image in images:
    print(image)

This code returns output like this:

<NodeImage: id=840b74a6-500a-45f0-92fc-250a4650cb2d, name=CoreOS, driver=OpenStack  ...>
<NodeImage: id=a8484bc7-f6fb-45fd-9dfb-409c7c6e9bc5, name=Debian-8.3, driver=OpenStack  ...>
<NodeImage: id=152f56d5-f5df-4d2f-8be0-70f79122c2af, name=Debian-7.9, driver=OpenStack  ...>
<NodeImage: id=f53d69f6-4b15-4ef6-af70-e0c37e314c64, name=Fedora-23, driver=OpenStack  ...>
<NodeImage: id=12f6a911-00a2-42eb-8712-d930da2da81f, name=Ubuntu-16.04, driver=OpenStack  ...>
<NodeImage: id=5091963c-6132-4590-a5c5-ec720a2afe88, name=test, driver=OpenStack  ...>
<NodeImage: id=e0139772-0aa3-43d5-a480-ba438bb6fc8e, name=volume-testing-snap, driver=OpenStack  ...>
<NodeImage: id=873e4bab-ed23-4096-83fb-ee8b0dd2f5a3, name=Ubuntu-15.10, driver=OpenStack  ...>
<NodeImage: id=03f89ff2-d66e-49f5-ae61-656a006bbbe9, name=Ubuntu-14.04, driver=OpenStack  ...>
<NodeImage: id=c1e8c5b5-bea6-45e9-8202-b8e769b661a4, name=CentOS-7, driver=OpenStack  ...>
<NodeImage: id=10ff94ea-18dc-4790-8ac8-84e6ac9f3132, name=CentOS-6, driver=OpenStack  ...>

Note

You may notice that the image IDs change often even if the names of the images remain the same as the images are updated over time.

You can also get information about available flavors using API calls:

flavors = conn.list_sizes()
for flavor in flavors:
    print(flavor)

This code returns output like this:

<OpenStackNodeSize: id=100, name=gp1.subsonic, ram=1024, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=1,  ...>
<OpenStackNodeSize: id=200, name=gp1.supersonic, ram=2048, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=1,  ...>
<OpenStackNodeSize: id=300, name=gp1.lightspeed, ram=4096, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=2,  ...>
<OpenStackNodeSize: id=400, name=gp1.warpspeed, ram=8192, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=4,  ...>
<OpenStackNodeSize: id=50, name=gp1.semisonic, ram=512, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=1,  ...>
<OpenStackNodeSize: id=500, name=gp1.hyperspeed, ram=16384, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=8,  ...>

Choose an image and flavor for your instance. For example, pick one of about 1GB RAM, 1 CPU, and a 1GB disk. This example uses the Ubuntu image with a small flavor, which is a safe choice.

First, tell the connection to get a specified image by using the ID of the image that you picked in the previous section:

image_id = '03f89ff2-d66e-49f5-ae61-656a006bbbe9'
image = conn.get_image(image_id)
print(image)

This code returns output like this:

<NodeImage: id=90d5e049-aaed-4abc-aa75-60c2b1ed6516, name=Ubuntu-14.04, driver=OpenStack  ...>

Next, choose which flavor you want to use:

flavor_id = '50'
flavor = conn.ex_get_size(flavor_id)
print(flavor)

This code returns output like this:

<OpenStackNodeSize: id=100, name=subsonic, ram=1024, disk=80, bandwidth=None, price=0.0, driver=OpenStack, vcpus=1,  ...>

Now, you can launch the instance.

Launch an instance

Now you can create the instance:

instance_name = 'testing'
testing_instance = conn.create_node(name=instance_name, image=image, size=flavor, networks=['public'])
print(testing_instance)

This code returns output like this:

<Node: uuid=3c8fd6fc02916c26d75b5e9bfa53d8b1315671be, name=testing, state=PENDING, public_ips=[], private_ips=[], provider=OpenStack ...>

If you list existing instances you’ll see the newly created one in there:

instances = conn.list_nodes()
for instance in instances:
    print(instance)

The newly created instance will look something like:

<Node: uuid=3c8fd6fc02916c26d75b5e9bfa53d8b1315671be, name=testing, state=RUNNING, public_ips=[], private_ips=[], provider=OpenStack ...>

Before you continue, you must do one more thing.

Destroy an instance

Cloud resources, such as running instances that you no longer use, can cost money. To avoid unexpected expenses, destroy cloud resources.

conn.destroy_node(testing_instance)

If you list the instances again, the instance disappears.

Did this article answer your questions?

Article last updated .