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:
- hard using vSphere rest api
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
FROM ubuntu:trusty MAINTAINER Lino Telera Linoproject.net <linotelera@gmail.com> # Install base packages RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -yq install \ python \ python-pip python-dev build-essential \ openssh-server RUN pip install --upgrade pip RUN pip install --upgrade virtualenv ## SSL python connectons RUN pip install requests[security] RUN pip install pyopenssl ndg-httpsclient pyasn1 ## SSH settings RUN mkdir /var/run/sshd RUN echo 'root:screencast' | chpasswd RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config # SSH login fix. Otherwise user is kicked off after login RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" >> /etc/profile ## Working directory RUN mkdir /opt/pythoncode EXPOSE 22 EXPOSE 80 EXPOSE 443 WORKDIR /opt/pythoncode |
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:
1 |
docker build -t="linoproject/pyvmomi" . |
Then start your new container linking an engine directory to this container (change /path/to/hostdir with yours):
1 |
docker run -d -v /path/to/hostdir:/opt/pythoncode --name=devpyvmom linoproject/pyvmomi |
1 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import argparse import atexit import urllib3.contrib.pyopenssl from pyVmomi import vim from pyVmomi import vmodl from pyVim import connect def get_args(): """ Supports the command-line arguments listed below. """ parser = argparse.ArgumentParser( description='Process args for retrieving all the Virtual Machines') parser.add_argument('-s', '--host', required=True, action='store', help='Remote host to connect to') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='Port to connect on') parser.add_argument('-u', '--user', required=True, action='store', help='User name to use when connecting to host') parser.add_argument('-p', '--password', required=True, action='store', help='Password to use when connecting to host') args = parser.parse_args() return args def main(): """ Simple command-line program for listing all VM in a vcenter """ import requests requests.packages.urllib3.disable_warnings() urllib3.contrib.pyopenssl.inject_into_urllib3() args = get_args() try: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) if not service_instance: print("Could not connect to the specified host using " "specified username and password") return -1 atexit.register(connect.Disconnect, service_instance) content = service_instance.RetrieveContent() # Search for all Datastore Clusters aka StoragePod obj_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True) vm_list = obj_view.view obj_view.Destroy() for singlevm in vm_list: print "VM: %s" % singlevm.name except vmodl.MethodFault as error: print "Caught vmodl fault : " + error.msg return -1 return 0 # Start program if __name__ == "__main__": main() |
Save file with this name: allVMs.py and test it interacting with the container:
1 |
docker exec -ti devpyvmom /bin/bash |
then run the file(under the container directory from /opt/pythoncode):
1 |
python allVMs.py -s <vcenter_fdqn_or_ip> -u <user> -p <password> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
FROM ubuntu:trusty MAINTAINER Lino Telera Linoproject.net <linotelera@gmail.com> # Install base packages RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -yq install \ python \ python-pip python-dev build-essential \ libffi-dev libssl-dev \ git RUN pip install --upgrade pip RUN pip install --upgrade virtualenv RUN pip install requests[security] RUN pip install pyopenssl ndg-httpsclient pyasn1 ## Install pyvmomi RUN pip install pyvmomi ## Working directory RUN mkdir /opt/services/ ADD allVMs.py /opt/services/allVMs.py WORKDIR /opt/pythoncode CMD ["python","/opt/services/allVMs.py"] |
Build your image running:
1 |
docker build -t="linoproject/pyvmomi-allvms" . |
To create and use this first container (aka microservice) you should run for the first time:
1 |
docker run --name="linoservice_allvm" linoproject/pyvmomi-allvms python /opt/services/allVMs.py -s <ip_fdqn_vcenter> -u <user> -p <password> |
Once microservice is created then your microservice could relaunch it with this command:
1 |
docker start -i linoservice_allvm |
That’s all!
WEB:
http://vmware.github.io/pyvmomi-community-samples/
HOL-SDC-1422 – VMware Development Tools and SDKs