Tuesday, March 8, 2016

Using the Python OpenStack API to access P2C

OpenStack has a Python API that can be used to develop services around it. In this post I describe how to use it for P2C.

1. After starting an Ubuntu 14.04 instance in P2C, log in to the instance using its floating IP. First create/edit /etc/apt/apt.conf.d/43proxy.

Acquire::http::Proxy "http://10.0.3.201:3142";

Run the following commands to install dependencies.
  • sudo apt-get install build-essential autoconf libtool pkg-config python-dev
  • wget https://bootstrap.pypa.io/get-pip.py
  • python get-pip.py
  • sudo pip install python-keystoneclient
  • sudo pip install python-glanceclient
  • sudo pip install python-novaclient

2. Edit /etc/hosts to add an entry for the frontend node:

10.0.3.101  cinterlabs-frontend

3. Create an rc file(guest-openrc.sh) file containing the following information(Don't forget to replace the values with your own credentials):

  export OS_TENANT_ID=028d55fc448046c6832db6527da13bf9
  export OS_TENANT_NAME=guest
  export OS_USERNAME=guest
  export OS_PASSWORD=guest_password

  export OS_AUTH_URL=http://cinterlabs-frontend:35357/v2.0

4. Create credentials.py containing the following:

#!/usr/bin/env python
import os

def get_keystone_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    return d

def get_nova_creds():
    d = {}
    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


5. Create list-instances.py containing the following:

#!/usr/bin/env python

from novaclient import client as novaclient
from credentials import get_nova_creds
creds = get_nova_creds()
nova = novaclient.Client("2", **creds)
print nova.servers.list()


6. Test the code by executing the commands below. You should see a list of instances.
  • source guest-openrc.sh
  • chmod 755 list-instances.py
  • ./list-instances.py
7. Sample code to start an instance using the Python API. Save as start-instance.py

#!/usr/bin/env python
import os
import time
from novaclient import client as novaclient
from credentials import get_nova_creds

creds = get_nova_creds()
nova = novaclient.Client("2",**creds)
image = nova.images.find(name="Ubuntu-14.04-server-amd64")
flavor = nova.flavors.find(name="p2c.1_512_5_1_1")
instance = nova.servers.create(name="frompython", image=image, flavor=flavor, key_name="jachermocilla-p2c")

# Poll until the status is no longer 'BUILD'
status = instance.status
while status == 'BUILD':
    time.sleep(5)
    # Retrieve the instance again so the status field updates
    instance = nova.servers.get(instance.id)
    status = instance.status
print "status: %s" % status


To test:
  • source guest-openrc.sh
  • chmod 755 start-instance.py
  • ./start-instance.py

References:
  • http://www.ibm.com/developerworks/cloud/library/cl-openstack-pythonapis/
  • http://docs.openstack.org/developer/python-novaclient/