Month: October 2013

Using VIRTUALENV in python

virtualenv

I wanted to blog about virtualenv which is really an important tool to use when installing Python 2.x or 3.x. Centos, for exmaple, comes with its own version of Python. Install a newer Python version and you have broken yum. This is not good, but this is where virtualenv comes in. In essence, virtualenv allows you to run multiple python environments on one server which in turn allows you to run different libraries that will not conflict with each python environment. Say I have a mysql library that needs to be at a specific version – I have installed the mysql python package. If I use virtualenv, that mysql python package will only apply to the virtualenv python environment. It therefore is a way to keep your libraries separate. Now you can still use your main python library for common libraries, but virtualenv just helps separate each python environment – good also when mulitple developers are working on the one server.

Here is how to set up a virtualenv for Python (pre-requisite is that you have your version of python installed)

1 – Make sure you have virtualenv installed.

yum search python-virtualenv

2 – Create a directory to install your python environment

mkdir ~/my_python_app/

3 -Run the virtualenv –<directory from step 2>

virtualenv ~/my_python_app/

Here’s the output for  steps 2 & 3

[root@centos ~]# mkdir my_python_app
[root@centos ~]# cd my_python_app/
[root@centos my_python_app]# virtualenv ~/my_python_app/
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /root/my_python_app/bin/python2.7
Also creating executable in /root/my_python_app/bin/python
Installing Setuptools............
Installing Pip...................
done.

And there you have it – an independent python environment. There is more that you should do to really customise the environment, but that’s all you need to do to initially get yourself up and running.

I do however want to highlight a useful option, which can be found using virtualenv –help:

-p PYTHON_EXE, --python=PYTHON_EXE
        The Python interpreter to use, e.g.,
        --python=python2.5 will use the python2.5 interpreter
        to create the new environment.  The default is the
        interpreter that virtualenv was installed with
        (/usr/local/bin/python2.7)

This option allows you to create a python environment using a different python version. This is really good if you want your python environment to be independent but use different version of python, 2.6, 3.x etc.

Activating your environment.

So once you have configured your python environment, you need to activate it so you can work on it. Here is how to do this:

[root@centos my_python_app]# source /root/my_python_app/bin/activate

Once you have done this, you will see the prompt change as follows:

(my_python_app)[root@v28388581sl0001 my_python_app]#

The (my_python_app) just shows you what virtualenv is activated.

Once you are activated, you can start using pip to install your libraries, code in python, etc. Once you are finished, you just need to deactivate the virtualenv, as follows:

(my_python_app)[root@centos my_python_app]# deactivate
[root@centos my_python_app]#

Now you can see your prompt has gone back to normal and the active virtual environment has been removed.

So, how do you run a python script using a specific python environment? Well, you do not need to activate the python virtualenv, you just specify the full path to the python interpreter in the bin folder within the virtualenv.

We installed a virtualenv in /root/my_python_app, so the python exe or interpreter (which ever you fancy calling it) will be found in the bin folder, as follows:

[root@centos /]# /root/my_python_app/bin/python
Python 2.7.5 (default, Oct 24 2013, 15:46:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Notice that the version is Python 2.7.5 – if I had specified a different python version, that would reflect when calling the python interpreter in the bin folder. All you need to do is specify the full path followed by the script you wish to run.

TIP:  However, I can give you a easy tip here. Rather than specify the full path, update your bash profile or better still, /etc/bashrc as follows:

alias python_v1=/root/my_python_app/bin/python

Happy Python coding – and please get to use virtualenv. It will save you hassle.

Advertisements

Deploying a VM to vCloud Director using API and Python

pyton_api_1

Further to my other post about getting started with vCloud Director API using Python, I thought I would write down how I deployed a VM to a vApp.

Once you have got a login function working by obtaining the x-vcloud-authorization token, you need to use the recomposeVApp REST API call, which as outlined by the documentation is done by using appending this URI, /vApp/{id}/action/recomposeVApp, to your endpoint. NOte, your endpoint is your API URL.

Here is my example code, which can be found on my github account, https://github.com/oliverleach/vcloud-autodeploy/blob/master/vCloudDeployVm.py


def recompose_vapp(self):

    post_headers = self.headers
    post_headers['Content-Type']='application/vnd.vmware.vcloud.recomposeVAppParams+xml'

    xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
             <RecomposeVAppParams
              xmlns="http://www.vmware.com/vcloud/v1.5"
              xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
              xmlns:environment_1="http://schemas.dmtf.org/ovf/environment/1">
              <Description> "api deployed vm" </Description>
                  <SourcedItem sourceDelete="false">
                      <Source href="%s"/>
                  </SourcedItem>
              <AllEULAsAccepted>true</AllEULAsAccepted>
              </RecomposeVAppParams>""" % (self.vapp_template)

    post = requests.post(self.vapp + '/action/recomposeVApp', data=xml, headers=post_headers)
    result = ET.fromstring(post.text)

Firstly, you can see I am setting the post_headers value to self.headers – these headers include the x-vcloud-authorization token. You also need to add the content type which is outlined in the documentation. It just tells the API what XML to expect, which in this case is recomposeVAppParams+xml.

Next, I have specified the XML file and I’ve cut down what I have specified and kept it simple. The only thing I have provided is the vapp_template value, which is something I have obtain earlier in my API script.

Finally, I then post this request as shown again below:

post = requests.post(self.vapp + '/action/recomposeVApp', data=xml, headers=post_headers)

Here I am using the python requests library and adding the required parameers, which in cludes the self.vapp URL, again obtained earlier in my API script, which appends /action/recomposeVApp. I then provide the XML which is contained in the function and add the post_headers.

You need to look at the whole API script to understand how I have built up the post request, including how I have obtained the various requirements such as vapp href and the vapp_template href.

Please do post a comment if you would like more info or use the contact me link on my blog.

Happy scripting. 🙂