Integrating VMware vDatacenter with python (the docker method) – Part 1

This how-to shows how to write a simple python code to retreive informations about a vDatacenter resources consumption. Python could use 2 method:

  1. hard using vSphere rest api
  2. easy by using pyvmomi from vmware github repositoy ( http://vmware.github.io/pyvmomi-community-samples/ )

Choosing the second (so tired to write overheads), I’ll show how to develope your integration running from a docker container. Remeber that alternatively you could choose to develope it with traditional methods following the guide http://vmware.github.io/pyvmomi-community-samples/ .

Let’s start creating a docker image to develope your code with the following Dockerfile:

Note: The purpose for this image is creating a develoment environment to develop and test the code. After coding a new image must rebuild  without developement features (lik ssh). Now it’s time build the image and create a container:

Then start your new container linking an engine directory to this container (change /path/to/hostdir with yours):

 

Now it’s time to code: create a new file with your preferred editor (I suggest the use of Eclipse with pydev plugin)

This example code (forked from https://github.com/vmware/pyvmomi-community-samples ) shows how to connect and retrive contents from vCenter; in this case the retrived abjects are all virtual machines.

Save file with this name: allVMs.py and test it interacting with the container:

then run the file(under the container directory from /opt/pythoncode):

If all goes well, and no errors is returned, all vm in your vCenter are shown. After test your developement is complete, now it’s time to package your code.

 

Packaging this app in docker means creating another image. This operation should be easy, but you need to modify Dockerfile from dev image removing development statements and adding the project into the working directory.

Proceed with copy the project (in this case only the allvms.py file) under a new image construction directory (in my example linoproject_pyvomomi_allvms ), then create a new Dockerfile with the following content:

Build your image running:

To create and use this first container (aka microservice) you should run for the first time:

Once microservice is created then your microservice could relaunch it with this command:

That’s all!

WEB:

http://vmware.github.io/pyvmomi-community-samples/

HOL-SDC-1422 – VMware Development Tools and SDKs

   Send article as PDF