Saturday, March 21, 2015

Writing OpenStack application - A simple example

Finally, after reading Brent Salisbury's post I got my OpenStack-ODL environment up and running. So, I thought of writing a small application on OpenStack using the novaclient.v1_1.client package.

Setup Environment:

  • As described in this post till the line "The state of OVS after the stack should be the following:" [Just grep this line and do everything done before this.]
  • 2 VM running instances namely test_vm1, test_vm
  • The script should run on the Controller VM.


If you have your own setup, then you can take down the below details

  • Python version 2.7.5
  • Below packages need to be installed
  • pip install python-keystoneclient
  • pip install python-novaclient
Lets look at the below sample script.

  1 #!/usr/bin/env python
  2 import novaclient.v1_1.client as nvclient
  3
  4 # Replace the values below  the ones from your local config,
  5 auth_url = "http://192.168.56.104:5000/v2.0"
  6 username = "admin"
  7 password = "admin"
  8 tenant_name = "demo"
  9 project_id = "demo"
 10
 11
 12 nova = nvclient.Client(auth_url=auth_url, username=username,
 13                            api_key=password, project_id=project_id)
 14
 15 # Get the list of VMs
 16 srv_list =  nova.servers.list()
 17 server = nova.servers.find(name="test_vm1")
 18 print server
 19 print 'Rebooting server '+'test_vm1 ...'
 20 server.reboot()
 21 print 'test_vm1 rebooted'


Line 2 is used for importing nova package with.
Line 5-9 describe your local settings. When you run the controller node using devstack, you'll get the below output:
Horizon is now available at http://192.168.56.104/
Keystone is serving at http://192.168.56.104:5000/v2.0/
The IP Address here is the IP on which horizon service is running.
On line 5, the port number 5000 is used. You may also use port number 35357. Port 5000, 35357 are keystone's port numbers of public and administrative end-points.
For more details on port numbers, check this.

Line 12 is used to create a nova client object against which nova API calls would be made.
Line 16 and 17 are used to obtain the list of VMs and get the object of instance with name "test_vm1".
API call on line 20 finally reboots the instance.
Refer this to see the list of Server module APIs.

*********************************OUTPUT************************************
[fedora@fedora-odl-1 ~]$ python os_sample.py
[, ]
Rebooting server test_vm1 ...
test_vm1 rebooted
[fedora@fedora-odl-1 ~]$


Dashboard state before running the script.

After running the script os_sample, you'll see the rebooting message in the Power state column.

Acknowledgements:
This post is incomplete without acknowledging this wonderful link.

No comments:

Post a Comment