Get started Bring yourself up to speed with our introductory content.

Scripting the future: How to create an OpenStack VM with APIs

OpenStack supports a variety of application programming interface options for writing scripts. Learn how to create VMs through scripting with this guide to using the Python OpenStack API.

After installing the basic OpenStack platform, as we explained in a previous article, the system still does not...

do anything.  You still need to load VM images into the Glance repository and then create VMs.  Most people will want to do that with an application programming interface rather than the command line. There are application programming interfaces available in several languages. In this article, we'll show how to do that using the Python OpenStack application programming interface.

Prerequisites for creating an OpenStack VM

In order to write a program to create an OpenStack VM, you'll need to have the following OpenStack services installed and running:

  • Identity Service
  • Image Service
  • Compute Service
  • At least one ISO image loaded in the Image Service.

Install OpenStack Neutron networking

In the Compute Service setup, OpenStack installs Nova Network by default. This is the legacy way of networking, but on Linux this only works with physical network adapters, not virtual ones. Therefore, it will not work with a Linux VM, so you need to install OpenStack Neutron networking.

If you try to use Nova to create a network on a Linux VM, you will get stuck at this step:

nova network-create simple --bridge (bridge name) --multi-host T --fixed-range-v4 (ip range)

Without a network, you cannot create an OpenStack VM. In terms of the Python application programming interface (API), you will be missing a network that you can pass to server.create().

Before going any further, follow OpenStack's instructions on how to install Neutron, then create at least one network and come back here. Be mindful when installing OpenStack Neutron networking, as it can be a complicated process and requires an understanding of subnet, IP range and the basics of bridged networking. Also, when running my installation, I came across a bug in the Kilo version that prohibited me from creating any of the tables in the Neutron database. The error occurred in /var/log/neutron/neutron-server.log. To fix this, run the following command:

neutron-db-manage --config-file=/etc/neutron/neutron.conf upgrade head

Once you have installed OpenStack Neutron networking, you should have a network that you can see with this command:

nova net-list

Install Python API

The next step is to install Python and the pip package manager. Once this is done, run the command shown below:

pip install python-openstackclient

Create a key pair
In order to access the OpenStack VM with Secure Shell, you need a key pair. Run the following commands:

       ssh-keygen

nova keypair-add --pub-key ~/.ssh/id_rsa.pub demo-key

nova keypair-list

+----------+-------------------------------------------------+

| Name     | Fingerprint                                     |

+----------+-------------------------------------------------+

| demo-key | 59:af:5e:eb:a4:df:c1:11:83:41:02:29:5e:a9:87:de |

+----------+-------------------------------------------------+

Write the code
Source these environment variables:

export OS_TENANT_NAME=admin

export OS_USERNAME=admin

export OS_PASSWORD=ADMIN_PASS

export OS_AUTH_URL=http://controller:35357/v2.0

Create a script folder somewhere and create credentials.py with this exact content:

def get_nova_credentials_v2():

    d = {}

    d['version'] = '2'

    d['username'] = os.environ['OS_USERNAME']

    d['api_key'] = os.environ['OS_PASSWORD']

    d['auth_url'] = os.environ['OS_AUTH_URL']

    d['project_id'] = os.environ['OS_TENANT_NAME']

    return d

Import this into the Python program that you write to launch a VM.

List images
If you have loaded an ISO image into Glance, you can verify its location with the code shown below:

glance image-list

 +--------------------------------------+---------------------+-------------+------------------+----------+--------+

| ID                                   | Name                | Disk Format | Container Format | Size     | Status |

+--------------------------------------+---------------------+-------------+------------------+----------+--------+

| 99795654-5503-4dc5-b97f-a14d645a98a1 | cirros-0.3.3-x86_64 | qcow2       | bare             | 13200896 | active |

+--------------------------------------+---------------------+-------------+------------------+----------+--------+

List flavors

OpenStack loads flavors when you install it. These are different VM templates and are the same as what you would see on Amazon EC2 and elsewhere. These flavors are the combination of memory, virtual CPUs, disk size and so on. that define the size and capacity of the VM. To list flavors, enter this code:

nova flavor-list

+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+

| ID | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |

+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+

| 1  | m1.tiny   | 512       | 1    | 0         |      | 1     | 1.0         | True      |

| 2  | m1.small  | 2048      | 20   | 0         |      | 1     | 1.0         | True      |

| 3  | m1.medium | 4096      | 40   | 0         |      | 2     | 1.0         | True      |

| 4  | m1.large  | 8192      | 80   | 0         |      | 4     | 1.0         | True      |

| 5  | m1.xlarge | 16384     | 160  | 0         |      | 8     | 1.0         | True      |

+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+

This is the code we use to create an OpeStack VM, modified from its source. Below, I've bolded the parts you will have to change to make it work:

#!/usr/bin/env python

import time

import os

execfile('credentials.py')

 

from novaclient.client import Client

 

try:

        credentials = get_nova_credentials_v2()

        nova_client = Client(**credentials)

 

        image = nova_client.images.find(name="cirros-0.3.3-x86_64")

        flavor = nova_client.flavors.find(name="m1.tiny")

        net = nova_client.networks.find(label="demo-net")

        nics = [{'net-id': net.id}]

        instance = nova_client.servers.create(name="vm2", image=image,

                                          flavor=flavor, key_name="demo-key",nics=nics)

        print("Sleeping for 5s after create command")

        time.sleep(5)

        print("List of VMs")

        print(nova_client.servers.list())

finally:

        print("Execution Completed")

These sections will vary depending on what values you use when you set them up:

     flavor = nova_client.flavors.find(name="m1.tiny")

     net = nova_client.networks.find(label="demo-net")

key_name="demo-key"

Now you can run the Python code and see if it creates a VM. Obviously, if it doesn't, you'll have to work backward through the OpenStack installation to see what piece is missing. Based on personal experience, it can take a couple of tries to get it all working, as there are so many parts.

Next Steps

What's next for OpenStack?

Exploring OpenStack networking and orchestration

OpenStack creates a static code analysis tool for Python

Navigate the OpenStack services maze

This was last published in April 2016

Dig Deeper on Cloud computing architecture

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What other OpenStack APIs have you used for your private cloud?
Cancel

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close