Search This Blog

Showing posts with label bpython. Show all posts
Showing posts with label bpython. 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

bpython supports virtualenv

With the help of virtualenv you can create different virtual environments for Python. Each environment can have installed different modules or have the same module but in different version.

Problem

Does bpython support virtualenv by default.

Solution

Yes, bpython supports virtualenv environments by default. Below is how you can verify it.
 
root@manage2:~# python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7']
>>>

Virtualenv local configuration (see the (novaclinet) string representing the name of the environment at the beginning)
 
root@manage2:~/virtualenv.d/novaclient/bin# source  activate
(novaclient)root@manage2:~/virtualenv.d/novaclient/bin# python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7', '/root/virtualenv.d/novaclient/lib/python2.7/plat-linux2', '/root/virtualenv.d/novaclient/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/lib/python2.7/lib-old', '/root/virtualenv.d/novaclient/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages', '/root/virtualenv.d/novaclient/lib/python2.7/site-packages']

Global, system wide configuration
 
root@manage2:~# bpython
>>> import sys
>>> sys.path
['', '/usr/bin', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7']
>>>

Virtualenv local configuration
 
(novaclient)root@manage2:~/virtualenv.d/novaclient/bin# bpython
>>> import sys
>>> sys.path
['', '/root/virtualenv.d/novaclient/bin', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/root/virtualenv.d/novaclient/lib/python2.7', '/root/virtualenv.d/novaclient/lib/python2.7/plat-linux2', '/root/virtualenv.d/novaclient/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/lib/python2.7/lib-old', '/root/virtualenv.d/novaclient/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/root/virtualenv.d/novaclient/local/lib/python2.7/site-packages']
>>>