How to launch and delete OpenStack instances using Python and Shade

 

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

a recent version of shade library installed.

Note

Please observe the following when using shade:

  • Before proceeding, install the latest version of shade.
  • The output of your script can be formatted differently depending on your version of shade.

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.

Create a configuration file to store your user name, password, project_name in ~/.config/openstack/clouds.yml.

clouds:
    dreamcompute:
        auth:
            username: $YOUR_USERNAME
            password: $YOUR_PASSWORD
            project_name: $YOUR_PROJECT
            auth_url: 'https://iad2.dream.io:5000/v2.0'
        region_name: RegionOne

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

from shade import *

simple_logging(debug=True)
conn = openstack_cloud(cloud='dreamcompute')

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:

checksum: 750a56555d4ec7303f5dc33b007ff632
container_format: bare
created_at: '2014-07-14T19:02:15Z'
direct_url:
rbd://7e14670e-a6f8-445b-b632-4b79bafc4781/masseffect-images/b4efbc2a-6130-4f2e-b436-55a618c4de20/snap
disk_format: raw
file: /v2/images/b4efbc2a-6130-4f2e-b436-55a618c4de20/file
id: b4efbc2a-6130-4f2e-b436-55a618c4de20
min_disk: 10
min_ram: 1024
name: Debian-7.0-Wheezy
owner: 0bacd8121bb548698f340455b38bf561
protected: false
schema: /v2/schemas/image
size: 5242880000
status: active
tags: []
updated_at: '2014-10-15T22:42:52Z'
visibility: public

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_flavors()
for flavor in flavors:
    print(flavor)

This code returns output like this:

HUMAN_ID: true
NAME_ATTR: name
OS-FLV-DISABLED:disabled: false
OS-FLV-EXT-DATA:ephemeral: 0
disk: 80
ephemeral: 0
human_id: supersonic
id: '200'
is_public: true
links:
-   href:
    https://compute.dream.io:8774/v2/5d013ac5962749a49af7ff18c2fb228c/flavors/200
    rel: self
-   href:
    https://compute.dream.io:8774/5d013ac5962749a49af7ff18c2fb228c/flavors/200
    rel: bookmark
name: supersonic
os-flavor-access:is_public: true
ram: 2048
swap: ''
vcpus: 1

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_name = 'Ubuntu-14.04'
image = conn.get_image(image_name)
print(image)

This code returns output like this:

checksum: da578dd59289a35a0ac7744a0bd85cf5
container_format: bare
created_at: '2014-10-27T22:05:37Z'
direct_url:
rbd://7e14670e-a6f8-445b-b632-4b79bafc4781/masseffect-images/c55094e9-699c-4da9-95b4-2e2e75f4c66e/snap
disk_format: raw
file: /v2/images/c55094e9-699c-4da9-95b4-2e2e75f4c66e/file
id: c55094e9-699c-4da9-95b4-2e2e75f4c66e
min_disk: 0
min_ram: 0
name: Ubuntu-14.04
owner: 0bacd8121bb548698f340455b38bf561
protected: false
schema: /v2/schemas/image
size: 10737418240
status: active
tags: []
updated_at: '2014-10-27T22:08:55Z'
visibility: public

Next, choose which flavor you want to use:

Because shade accepts either the ID or name in most API calls, specify the name for the flavor:

flavor_name = 'gp1.semisonic'
flavor = conn.get_flavor(flavor_name)
print(flavor)

This code returns output like this:

HUMAN_ID: true
NAME_ATTR: name
OS-FLV-DISABLED:disabled: false
OS-FLV-EXT-DATA:ephemeral: 0
disk: 80
ephemeral: 0
human_id: subsonic
id: '100'
is_public: true
links:
-   href:
    https://compute.dream.io:8774/v2/5d013ac5962749a49af7ff18c2fb228c/flavors/100
    rel: self
-   href:
    https://compute.dream.io:8774/5d013ac5962749a49af7ff18c2fb228c/flavors/100
    rel: bookmark
name: subsonic
os-flavor-access:is_public: true
ram: 1024
swap: ''
vcpus: 1

Now, you can launch the instance.

Launch an instance

Now you can create the instance:

instance_name = 'testing'
testing_instance = conn.create_server(wait=True, auto_ip=True,
    name=instance_name,
    image=image['id'],
    flavor=flavor['id'],
    network='public'
    )
print(testing_instance)

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

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

The newly created instance will look something like:

HUMAN_ID: true
 NAME_ATTR: name
 OS-DCF:diskConfig: MANUAL
 OS-EXT-AZ:availability_zone: iad-1
 OS-EXT-STS:power_state: 1
 OS-EXT-STS:task_state: null
 OS-EXT-STS:vm_state: active
 OS-SRV-USG:launched_at: '2015-07-20T20:31:10.000000'
 OS-SRV-USG:terminated_at: null
 accessIPv4: ''
 accessIPv6: ''
 addresses:
     private-network:
     -   OS-EXT-IPS-MAC:mac_addr: fa:16:3e:60:f5:cd
         OS-EXT-IPS:type: fixed
         addr: 2607:f298:6050:4e14:f816:3eff:fe60:f5cd
         version: 6
     -   OS-EXT-IPS-MAC:mac_addr: fa:16:3e:60:f5:cd
         OS-EXT-IPS:type: fixed
         addr: 10.10.10.14
         version: 4
 config_drive: ''
 created: '2015-07-20T20:30:23Z'
 flavor:
     id: '100'
     links:
     -   href:
         https://compute.dream.io:8774/5d013ac5962749a49af7ff18c2fb228c/flavors/100
         rel: bookmark
 hostId: f71865b497e6fa71063e292b11846eb64b5a41cd5c00fbb7465b6a48
 human_id: testing
 id: 67ecebdc-daff-4d84-bd04-bc76c67b48ec
 image:
     id: c55094e9-699c-4da9-95b4-2e2e75f4c66e
     links:
     -   href:
         https://compute.dream.io:8774/5d013ac5962749a49af7ff18c2fb228c/images/c55094e9-699c-4da9-95b4-2e2e75f4c66e
         rel: bookmark
 key_name: null
 links:
 -   href:
     https://compute.dream.io:8774/v2/5d013ac5962749a49af7ff18c2fb228c/servers/67ecebdc-daff-4d84-bd04-bc76c67b48ec
     rel: self
 -   href:
     https://compute.dream.io:8774/5d013ac5962749a49af7ff18c2fb228c/servers/67ecebdc-daff-4d84-bd04-bc76c67b48ec
     rel: bookmark
 metadata: {}
 name: testing
 networks:
     private-network:
     - 2607:f298:6050:4e14:f816:3eff:fe60:f5cd
     - 10.10.10.14
 os-extended-volumes:volumes_attached: []
 progress: 0
 security_groups:
 -   name: default
 status: ACTIVE
 tenant_id: 5d013ac5962749a49af7ff18c2fb228c
 updated: '2015-07-20T20:31:10Z'
 user_id: bfd3dbf1c8a242cd90884408de547bb9

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.delete_server(name_or_id=instance_name)

If you list the instances again, the instance disappears.

 

Did this article answer your questions?

Article last updated .