Search This Blog

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

No comments:

Post a Comment