Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Controlling multiple VMs with the power of Python

5.908 Aufrufe

Veröffentlicht am

Just a brief introduction on how Python could be used for controlling VMWare Workstation machines.

Veröffentlicht in: Technologie

Controlling multiple VMs with the power of Python

  1. 1. Controlling multiple VMs with the power of Python Using pysphere and some of his friends By Yurii Vasylenko
  2. 2. 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)
  3. 3. 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
  4. 4. 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 
  5. 5. Environment setup
  6. 6. 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/releas es/2.7.6/ - get Python 2.7.6.
  7. 7. 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)
  8. 8. 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.
  9. 9. 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”)
  10. 10. VMWare configuration, pt.2  You also need to install VMWare VIX SDK: https://my.vmware.com/web/vmware/fre e#desktop_end_user_computing/vmware _player/6_0|PLAYER-600-A|drivers_tools
  11. 11. Scripting
  12. 12. 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’”
  13. 13. First connection to VM #Importing class from pysphere module from pysphere import VIServer #Creating instance of VIServer class server=VIServer() #Opening connection to VMWare # – VMWare Server address (localhost or any other) #443 – port, specified in VMWare for Server #TESTDOMAINhohoho – credentials for accessing VMWare Server machine (domain login in this example) #“hohoho“ – domain password (yeah, we have to put it plain-text server.connect("",“TESTDOMAINhohoho",“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()
  14. 14. Connect is not enough What 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)
  15. 15. Long-running tasks Sometimes 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.
  16. 16. Code execution on client VM For 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:installslog.txt"],cwd="C:installs")
  17. 17. Code execution on client VM - Issues Common 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!
  18. 18. 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
  19. 19. Running program with GUI (Windows only solution – Code example) #logging into machine machine.login_in_guest(login,password) #getting session IDs on system machine.start_process("cmd.exe", args=["/c", "query user > C:installslog.txt"],cwd="C:installs") #fetching file, containing list of user sessions machine.get_file("C:installslog.txt","E:log.txt",overwrite=True) #reading it f=open("E:log.txt","r") #and parsing it #session ID is the third field in row for line in f: if line.strip().startswith(login): session_id=(line.strip().split()[2]) #running calc.exe in gui form os.system(str.format("E:PSToolsPsExec.exe {0} -u {1} -p {2} -i {3} -d calc.exe",ip_addr,login,password,session_id))
  20. 20. Constraints and warnings
  21. 21. 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 
  22. 22. 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/wik i/GettingStarted
  23. 23. Questions?