Top Banner
Controlling multiple VMs with the power of Python Using pysphere and some of his friends By Yurii Vasylenko
23

Controlling multiple VMs with the power of Python

Jun 19, 2015

Download

Technology

Yurii Vasylenko

Just a brief introduction on how Python could be used for controlling VMWare Workstation machines.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Controlling multiple VMs with the power of Python

Controlling multiple VMs with the power of PythonUsing pysphere and some of his friends

By Yurii Vasylenko

Page 2: Controlling multiple VMs with the power of Python

Goals Have ability to control big number of VMs Manage parallel and serial operations on

these VMs Have remote access to power control (i.e.

you don’t have to start and suspend VMs manually)

Have remote snapshot management (when new build comes, just create a new snapshots, do not copy all VM for backup)

Page 3: Controlling multiple VMs with the power of Python

What will you need?Mandatory

Python 2.7.6 – strictly this one, 3.x unfortunately not supported.

VMWare Professional (for having all nice snapshot features etc.). ESX is fine too.

Optional

Pycharm (just for a bit easier start and a best way to work with python for beginning)

Psexec from Sysinternals tools

Page 4: Controlling multiple VMs with the power of Python

Why Python?Why not… Because…

COM-based VIX implementations

No need to compile, can edit scripts in-place

Powershell Powershell debug is a kind of hell, Powershell extensibility is kind of none.

Perl No specific advantages, I just know more about Python

Page 5: Controlling multiple VMs with the power of Python

Environment setup

Page 6: Controlling multiple VMs with the power of Python

First actions – Pycharm and Python. http://www.jetbrains.com/pycharm/ - get

Pycharm community edition, it’s free and easy to install.

http://www.python.org/download/releases/2.7.6/ - get Python 2.7.6.

Page 7: Controlling multiple VMs with the power of Python

Pycharm configuration Go to “File” – “Settings” – “Project

interpreter” – “Python interpreters” Click “Plus” icon and select installed

Python 2.7.6 interpreter (python.exe on Windows)

Page 8: Controlling multiple VMs with the power of Python

Pycharm configuration, pt.2 You have to install following packages:

pip and setuptools are just tools, related to module management in Python, so they will be proposed to install first.

Page 9: Controlling multiple VMs with the power of Python

VMWare configuration All machines, you want to control,

should be put in “Shared” section, otherwise it won’t be accessible.

Go to “Edit” – “Preferences” – “Shared VMs” and check port number (“Port used by VMWare Workstation Server”)

Page 11: Controlling multiple VMs with the power of Python

Scripting

Page 12: Controlling multiple VMs with the power of Python

Your first script in Pycharm Click “File” – “New…” and select “Python

file”. Name it any way you like – f.e. “test.py”

Go to “Run” – “Edit configurations”, create new one in “Python” section (let the name be “test”).

Select test.py from step 1 as “Script” and Python 2.7.6 as interpreter.

Click “OK” Click “Run” - “Run ‘test’”

Page 13: Controlling multiple VMs with the power of Python

First connection to VM#Importing class from pysphere modulefrom pysphere import VIServer

#Creating instance of VIServer classserver=VIServer()

#Opening connection to VMWare#127.0.1.1 – VMWare Server address (localhost or any other)#443 – port, specified in VMWare for Server#TESTDOMAIN\hohoho – credentials for accessing VMWare Server machine (domain login in this example)#“hohoho“ – domain password (yeah, we have to put it plain-textserver.connect("127.0.1.1:443",“TESTDOMAIN\hohoho",“hohoho")

#Getting handler of our VM, using it’s name

machine=server.get_vm_by_name("[Shared] Windows 8 x64")

#Powering machine on!machine.power_on()

Page 14: Controlling multiple VMs with the power of Python

Connect is not enoughWhat you can do next?- Manage files (send_file method)- Get VM properties (get_properties method)- Get environment variables (get_environment_variables

method)- Manage snapshots

(create_snapshot/revert_to_snapshot methods)

Page 15: Controlling multiple VMs with the power of Python

Long-running tasksSometimes task can run long enough. But asynchronous task running is your true friend.

For specific methods you can use sync_run=False option, which will allow task work in background:

task = machine.create_snapshot(sync_run=False)

After it, you may request state of task running, by task.get_state()expression.

Page 16: Controlling multiple VMs with the power of Python

Code execution on client VMFor running a program on target machine, you need to do a few simple steps:

Login to machine

machine.login_in_guest(login,password)

And run the process

machine.start_process(“cmd.exe", args=["/c", "query user > C:\\installs\\log.txt"],cwd="C:\\installs")

Page 17: Controlling multiple VMs with the power of Python

Code execution on client VM - IssuesCommon issues for running a process on VM are:- Not enough rights for task execution - unfortunately, we

can’t access UAC alert windows- We could run only .exe, not .bat – but we can run

cmd.exe and pass .bat file to it. - Outcome of executed program located in the middle

of nowhere – always use cwd argument of start_process() method, it sets up working directory for called program

- And the last, but not least…- We want some GUI, not just process/batch execution!

Page 18: Controlling multiple VMs with the power of Python

Running program with GUI (Windows only solution)

So, we need to run a GUI application. For this, we’ll have to use additional utility, called psexec from Sysinternals command-line tools.

If we’ll just use psexec, we won’t get a GUI of program, we’re running. Therefore, we have to use –i parameter of psexec and specify id of GUI session

Page 19: Controlling multiple VMs with the power of Python

Running program with GUI (Windows only solution – Code example)#logging into machinemachine.login_in_guest(login,password)

#getting session IDs on systemmachine.start_process("cmd.exe", args=["/c", "query user > C:\\installs\\log.txt"],cwd="C:\\installs")

#fetching file, containing list of user sessionsmachine.get_file("C:\\installs\\log.txt","E:\\log.txt",overwrite=True)

#reading itf=open("E:\\log.txt","r")

#and parsing it#session ID is the third field in rowfor line in f: if line.strip().startswith(login): session_id=(line.strip().split()[2])

#running calc.exe in gui formos.system(str.format("E:\PSTools\PsExec.exe \\\\{0} -u {1} -p {2} -i {3} -d calc.exe",ip_addr,login,password,session_id))

Page 20: Controlling multiple VMs with the power of Python

Constraints and warnings

Page 21: Controlling multiple VMs with the power of Python

You should always keep in mind, that: All your logins-passwords are stored in plain-text

(there’s no easy way to implement domain authentication)

On ESX extended VM statuses (all, except POWERED ON, POWERED OFF and SUSPENDED might be unavailable)

VM properties might be cached, therefore, use from_cache=False argument, if you want to see them updated

Ip_address property returns only one IP address, use net property to get info about all adapters

And don’t forget to disconnect from server

Page 22: Controlling multiple VMs with the power of Python

What to look on next? Explore methods of VIServer and

VIMachine classes. There are lots of useful instruments for VM management automation available.

You can refer to Getting started document: https://code.google.com/p/pysphere/wiki/GettingStarted

Page 23: Controlling multiple VMs with the power of Python

Questions?