
Sergey Nivens - Fotolia
How to write application code for an OpenStack SDK
Though there is no official OpenStack SDK, there are plenty of SDKs that support OpenStack. Learn about SDKs as well as the requirements for using an SDK for OpenStack in this tip.
A software development kit enables you to write application codes. OpenStack recognizes a number of different software development kits that allow you to create OpenStack cloud applications, most notably Python -- however, there are a few other software development kits that support OpenStack, including Clojure, Erlang and Java. That said, at present, there is no formal determinate for what defines an OpenStack SDK.
Let's take a look at some software development kits (SDKs) you can use with OpenStack, as well as some sample Python and JavaScript programs.
In order to run any OpenStack SDK, you need a working OpenStack system; you can use DevStack or PackStack. If you opt to use PackStack, be sure to use CentOS 7. For the sake of these examples, I'm using OpenStack version Ocata. You also need a VM ISO image; OpenStack includes Cirros. For this example, I'm using ttylinux-i686-12.1.iso.
Test commands before using Python
Before using the Python command line, try using the OpenStack command line to test out commands you intend to code in order to make sure they work and that you fully understand them.
Start by sourcing your environment with the following commands:
source keystonerc_admin
cat keystonerc_admin
unset OS_SERVICE_TOKEN
export OS_USERNAME=admin
export OS_PASSWORD=(obfuscated)
export OS_AUTH_URL=http://10.0.2.15:5000/v2.0
export OS_TENANT_NAME=admin
export OS_REGION_NAME=RegionOne
To upload the Tiny image into OpenStack, enter the following commands:
glance image-create --name "tiny" --disk-format iso --file ttylinux-i686-12.1.iso --container-format bare
glance image-delete ebcd193a-e366-49df-9333-0444e0540b16
Once you've tested everything out with the OpenStack command line, delete the image so you can load it again using Python code. There should be no need to install the Python software development kit, as that gets installed with OpenStack.
Running the Python OpenStack SDK
The next step is to load an image with the code shown below. There are several ways to authenticate, including using Keystone. In this example, I authenticated using the connection object. Note that I used project_name instead of tenant_name -- OpenStack documentation isn't consistent on this point.
from openstack import connection
import os
def auth_args() :
d = {}
d['username'] = os.environ['OS_USERNAME']
d['password'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['project_name'] = os.environ['OS_TENANT_NAME']
return d
conn=connection.Connection(**auth_args())
image_attrs = {
'name': 'Tiny',
'disk_format': 'iso',
'container_format': 'bare',
'visibility': 'public',
'Location' : 'file://home/walkerDownloads/ttylinux-i686-12.1.iso'
}
conn.image.upload_image(**image_attrs)
Launch an instance
If you want to code launching an instance, here are the basic commands to do that from the Linux command line. If you create a volume larger than 10 GB, you will receive an error message. Use the list command after each create command to get the ID to feed into the next command. I used m1.small (ID=2).
openstack keypair create KEY_NAME > MY_KEY.pem
chmod 600 MY_KEY.pem
openstack keypair create --public-key ~/.ssh/id_rsa.pub KEY_NAME
openstack keypair list
openstack volume create --image 43242f6d-17fb-44eb-b8d5-eeda3ab412ae --size 9 bootable_volume
openstack volume list
openstack flavor list
openstack server create --flavor 2 --volume d4563f6c-6b61-4fd9-907b-ba1f0e43f058 --block-device source=volume,id=d4563f6c-6b61-4fd9-907b-ba1f0e43f058,dest=volume,size=10,shutdown=preserve,bootindex=0 simpleVM
It takes a little while to create a volume and instance, so use the list command over and over until it says available.
Running the JavaScript OpenStack SDK
There isn't much in the way of documentation or examples for the three JavaScript SDKs, so I've used the openstack-wrapper, which has the best documentation of the three.
Install Node.js and then install the SDK with this command:
npm install openstack-wrapper
Note that I used the v3 Keystone URL -- shown below -- and not v2, as was set in our environment:
export OS_AUTH_URL=http://10.0.2.15:5000/v2.0
I then created a file called OpenStack.js and ran it with this command:
node OpenStack.js
Here is the code, taken directly from the openstack-wrapper documentation. You need to insert your user ID and password and the URL for v3 of Keystone:
var OSWrap = require('openstack-wrapper');
var keystone = new OSWrap.Keystone('http://localhost:35357/v3');
keystone.getToken('admin', 'af83d7c502234d71', function(error, token){
if(error)
{
console.error('an error occured', error);
}
else
{
console.log('A general token object has been retrived', token);
//the token value (token.token) is required for project listing & getting project specific tokens
}
});
This returns a token, shown below, that you can use to work with other OpenStack services:
A general token object has been retrieved { issued_at: '2017-03-03T20:22:47.000000Z',
audit_ids: [ 'le_YlL5_SbW3FuvI3d8IDw' ],
methods: [ 'password' ],
expires_at: '2017-03-03T21:22:47.000000Z',
user:
{ domain: { id: 'default', name: 'Default' },
id: '0942598fed8645c08e571bc36a9437e5',
name: 'admin' },
token: '9b19eb7eb57a47d78f278192c714e7cc' }