Search This Blog

Showing posts with label novaclient. Show all posts
Showing posts with label novaclient. Show all posts

Saturday, April 20, 2013

How to develop and debug programs that use pyrax or python-novaclient libraries

Python belongs to a category of high-level, dynamically typed programming languages. This has enormous impact on the way how you write the code and how it is interpreted and executed.

As an example let's take a look at Python Rackspace pyrax module. It was built and made available in a form of Python SDK for Rackspace cloud. Under the hood it is a wrapper and integrator of various python libraries that exists to interact with Openstack systems(cloud servers, files, networks, databases ...). As an example it uses python-novaclient library communicate and interact with Nova (Openstack Compute).

From developer point of view you can write a Python program to interact with Rackspace cloud using any of them. Both should work fine.

Working with any of these libraries you are going to face these challenges very soon:
  • where is the latest API documentation 
  • how well are function documented
  • for a function what arguments it accepts and what it returns
In comparison to other statically typed languages like Java you quickly realize that Python definition of a function doesn't' fully answer the above questions. Below are couple of tricks to help you start with to play with them although.

Problem 1

How to start interactive Python session with loaded pyrax module for testing

We are going to use bpython that has a nice feature allowing it to show the available functions and variables a Python objects implements (for these who are interested how this works you can read about introspection here and there)
  1. Create a file with credentials
  2.  
    root@server:~# cat > rackspace_cloud_credentials
    [rackspace_cloud]
    username = user
    api_key = key
    

  3. Create an auxiliary file for bpython
  4.  
    # https://github.com/rtomaszewski/api-challenge/blob/master/bpython-pyrax.py
    cat > bpython-pyrax.py
    import os
    import sys
    import json
    import re
    import time
    import datetime, time
    
    from pprint import pprint
    from pprint import pformat
    
    import pyrax
    
    creds_file = os.path.expanduser("~/rackspace_cloud_credentials")
    pyrax.set_credential_file(creds_file, "LON")
    cs = pyrax.cloudservers
    
    help_str="""
    the pyrax module has been loaded, try typing one of the commands below to see if it works.
    
    #example 1: whow all cloud servers under you cloud account 
    l=cs.servers.list()
    print(l)
    
    # example 2: show list of images 
    pprint(cs.images.list())
    """
    
    print(help_str)
    

  5. Open the bpython interpreter
  6.  
    root@server:~# bpython -i  bpython-pyrax.py
    
    the pyrax module has been loaded, try typing one of the commands below to see if it works.
    
    #example 1: whow all cloud servers under you cloud account
    l=cs.servers.list()
    print(l)
    
    # example 2: show list of images
    pprint(cs.images.list())
    
    >>>
    

  7. Test how it works

Problem 2

How to start interactive Python session with loaded python-novaclient for testing
  1. Create bash file with variables
  2.  
    cat > nova.vars.sh
    export OS_AUTH_SYSTEM="rackspace_uk"
    export OS_AUTH_URL="https://lon.identity.api.rackspacecloud.com/v2.0/"
    export OS_NO_CACHE="1"
    export OS_PASSWORD="pass"
    export OS_REGION_NAME="LON"
    export OS_TENANT_NAME="user"
    export OS_USERNAME="user"
    export NOVA_RAX_AUTH=1
    export OS_PROJECT_ID="user"
    

  3. Create an auxiliary file for bpython
  4.  
    # https://github.com/rtomaszewski/api-challenge/blob/master/bpython-novaclient.py
    cat > bpython-novaclient.py
    import os
    import sys
    import json
    import re
    import time
    import datetime, time
    
    from pprint import pprint
    from pprint import pformat
    
    from novaclient.v1_1 import client
    
    os.environ["OS_USERNAME"]
    os.environ["OS_PASSWORD"]
    os.environ["OS_NO_CACHE"]
    os.environ["OS_TENANT_NAME"]
    os.environ["OS_AUTH_URL"]
    os.environ["OS_REGION_NAME"]
    os.environ["OS_AUTH_SYSTEM"]
    os.environ["NOVA_RAX_AUTH"]
    os.environ["OS_PROJECT_ID"]
    
    from novaclient import auth_plugin as _cs_auth_plugin
    _cs_auth_plugin.discover_auth_systems()
    auth_plugin = _cs_auth_plugin.load_plugin("rackspace_uk")
    
    cs = client.Client(os.environ["OS_USERNAME"], os.environ["OS_PASSWORD"], os.environ["OS_TENANT_NAME"], auth_url=os.environ["OS_AUTH_URL"], auth_system="rackspace", region_name="LON",  service_type="compute", auth_plugin=auth_plugin)
    
    help_str="""
    the novaclient module has been loaded and INITIATED, try typing one of the commands below to see if it works.
    
    #example 1: whow all cloud servers under you cloud account 
    l=cs.servers.list()
    print(l)
    
    # example 2: show list of images 
    pprint(cs.images.list())
    """
    
    print(help_str)
    

  5. Open the bpython interpreter
  6.  
    root@manage2:~# bpython -i bpython-novaclient.py
    
    the novaclient module has been loaded and INITIATED, try typing one of the commands below to see if it works.
    
    #example 1: whow all cloud servers under you cloud account
    l=cs.servers.list()
    print(l)
    
    # example 2: show list of images
    pprint(cs.images.list())
    >>>
    

  7. Test how it works

References
  1. http://bpython-interpreter.org/screenshots/
  2. http://stackoverflow.com/questions/546337/how-do-i-perform-introspection-on-an-object-in-python-2-x
  3. http://docs.rackspace.com/servers/api/v2/cs-gettingstarted/content/section_gs_install_nova.html
  4. https://github.com/openstack/python-novaclient
  5. https://github.com/rackspace/pyrax/blob/master/samples/cloudservers/create_server.py

Friday, April 5, 2013

How to create two isolated Python environment to develop with Openstack nova

This is a natural choice to relay on the package mechanism that is available for you in Linux. You don't have to worry about dependencies, compilation, some basic configuration etc. You simply run rpm -i or apt-get install or pip install to install a package.

Problem

How to create two separate Python environment to to develop and test Python Openstack nova python-novaclient

Solution

We can use a Python package called virtualenv.

To create our two separate and isolated Python environments we need to execute these commands below (the logs are from powershell).
 
PS C:\Users\radoslaw\workspace> virtualenv.exe nova-git
New python executable in nova-git\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

PS C:\Users\radoslaw\workspace> virtualenv.exe nova-stable
New python executable in nova-stable\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

Once the env are created we have to enabled and switch to them. It is important to enable an environment before trying to run any pip commands. If you forget the requested changes are going to be performed on your default system configuration.

In every env we will install different version of the python-novaclient package. In first we are going to use the version from official Python PyPI repository. In seconds we use the latest source code from github.
 
PS C:\Users\radoslaw\workspace> cd .\nova-stable
PS C:\Users\radoslaw\workspace\nova-stable> cd .\Scripts
PS C:\Users\radoslaw\workspace\nova-stable\Scripts> .\activate.ps1
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable\Scripts>
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable\Scripts>
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable\Scripts> pip install  python-novaclient
Downloading/unpacking python-novaclient

To install the version directly from github under the second environment.
 
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable> deactivate
PS C:\Users\radoslaw\workspace> cd .\nova-git
PS C:\Users\radoslaw\workspace\nova-git> .\Scripts\activate.ps1
(nova-git) PS C:\Users\radoslaw\workspace\nova-git> pip install -e git+https://https://github.com/openstack/python-novaclient.git#egg=
python-novaclient

To list installed versions
 
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable> pip list
iso8601 (0.1.4)
prettytable (0.7.1)
python-novaclient (2.13.0)
requests (1.2.0)
simplejson (3.1.2)

(nova-git) PS C:\Users\radoslaw\workspace\nova-git> pip list
iso8601 (0.1.4)
prettytable (0.7.1)
python-novaclient (2.13.0.4.g0fc4e12, c:\users\radoslaw\workspace\nova-git\src\python-novaclient)
requests (1.2.0)
simplejson (3.1.2)

To upgrade the packages in the environments
 
(nova-stable) PS C:\Users\radoslaw\workspace\nova-stable> pip install -U python-novaclient
(nova-git) PS C:\Users\radoslaw\workspace\nova-git> pip install -U python-novaclient

References
  1. http://www.rackspace.com/knowledge_center/article/using-python-novaclient-with-the-rackspace-cloud
  2. https://github.com/openstack/python-novaclient
  3. http://www.virtualenv.org/en/latest/
  4. http://www.pip-installer.org/en/latest/usage.html#pip-install