Sunday 7 June 2015

How to create a virtual machine using openstack nova python client


1) First create python nova client object

from novaclient.v2 import client
USER = "admin"
PASS = "cloud"
TENANT = "admin"
AUTH_URL = "http://192.168.56.101:5000/v2.0/"
nova_conn = client.Client(USER, PASS, TENANT, AUTH_URL, service_type="compute")

Explanation:

#import client from novaclient.v2, since the class "Client" defined in client.py.
#https://github.com/openstack/python-novaclient/blob/master/novaclient/v2/client.py#L52
from novaclient.v2 import client

#Declare username, password, tenant/project and auth_url
#AUTH_URL tells where the authentication service (keystone) is running.
USER = "admin"
PASS = "cloud"
TENANT = "admin"
AUTH_URL = "http://192.168.56.101:5000/v2.0/"


#Create a object of class Client (create a connection to nova).
nova_conn = client.Client(USER, PASS, TENANT, AUTH_URL, service_type="compute")

 2) Create a vm

a) Find image id:

 >>> [(x.name, x.id) for x in nova_conn.images.list()]

b) Find flavor id:

>>> [(x.name, x.id) for x in nova_conn.flavors.list()]

c) Create a vm named "myvm2"

>>> nova_conn.servers.create("myvm2", "b935b518-8bed-4947-91ae-a2ed78f591e6", "1")
  • b935b518-8bed-4947-91ae-a2ed78f591e6  --->  id of image "newimage"
  • 1 --->  id of flavor "m1.tiny"
d) List all vms

>>> [(x.name,x.id) for x in nova_conn.servers.list()]


3) How to find methods and attributes:

a) Find methods and attribute of "nova_conn".

>>> dir(nova_conn)

 b) Find methods and attribute of "nova_conn.servers".

>>> dir(nova_conn.servers)




 4) How to get help page.

a)find help of "nova_conn.servers.create".

>>> help(nova_conn.servers.create)




No comments:

Post a Comment