introduction to vsphere apis using pyvmomi

28
Introduction to vSphere APIs Using pyVmomi pyVmomi Michael Rice @VirtDevNinja http://errr-online.com/ https://github.com/michaelrice

Upload: michael-rice

Post on 15-Jul-2015

1.207 views

Category:

Software


14 download

TRANSCRIPT

Introduction to vSphere APIs Using pyVmomi

pyVmomiMichael Rice

@VirtDevNinja

http://errr-online.com/

https://github.com/michaelrice

About Michael Rice

❖ Software Developer @ Rackspace

❖ Trust me, I have a Linux beard.

❖ http://linuxbeard.com/image/114385301362

❖ RPM Package Maintainer for pyVmomi

❖ Leading community contributor to pyVmomi & community-

samples

❖ Creator of YAVIJAVA the VIJAVA fork

❖ StackOverflow pyVmomi & VIJAVA

About pyVmomi

❖ Open Source python SDK for vSphere

❖ Released Dec 2013

❖ License: Apache 2.0

❖ Python VMWare Managed Object Management Interface

❖ Other Vmomi projects include GoVmomi & rbVmomi

❖ Wraps vSphere WebServices SOAP API very tightly

❖ Its been around a long time!! Look on your 4.0 HostSystems!

❖ Multiple versions. Whats on HostSystems != whats on GitHub

Why Use Python & pyVmomi

❖ Python is easy for beginners to learn

❖ vSphere 6.0 VMWare released VMWare vCloud Suite SDK for Python

❖ http://developercenter.vmware.com/web/sdk/60/vcloudsuite-python

❖ Python is in the top 10 languages you should be learning RIGHT NOW!

❖ If you work in a mixed environment of admins running Windows, Mac and Linux then

the same Python code works for everyone! (mostly)

❖ Integrate with ConfigManagement like Ansible & SaltStack

❖ pySphere and pSphere are much slower because they use SUDS

❖ pyVmomi is FAST! 360 milliseconds to inventory 576 VMs

❖ Do tasks you can normally only do from the UI — Reset Alarms from Red to Green

Setting Up a Development Environment

❖ Setup a Development vCenter using Simulator

❖ Install Python and VirtualEnv on our desktop

❖ Install Python IDE — pyCharm/IntelliJ, pyDev, etc..

❖ No Intelli-Sense due to dynamic nature of library

❖ Install pyVmomi

Installing vCenter SA

❖ Download vCenter Server Appliance 5.5 ova

❖ 6.0 has a bug and Simulator DOES NOT WORK!

❖ Deploy the OVA to Fusion or Workstation

❖ Use simple shell script to configure VCSA Simulator

❖ https://gist.github.com/michaelrice/

#!/bin/bash

# See the original work from William Lam at http://www.virtuallyghetto.com/

echo "Accepting EULA ..."

/usr/sbin/vpxd_servicecfg eula accept

echo "Setting default ports ..."

/usr/sbin/vpxd_servicecfg 'ports' 'defaults'

echo "Configuring Embedded DB ..."

/usr/sbin/vpxd_servicecfg 'db' 'write' 'embedded'

echo "Configuring SSO..."

/usr/sbin/vpxd_servicecfg 'sso' 'write' 'embedded' 'password'

echo "Starting VCSIM ..."

/usr/bin/vmware-vcsim-start default

echo "Starting VCSA ..."

/usr/sbin/vpxd_servicecfg service start

Install Python & VirtualEnv

❖ Please See one of the 1000’s of guides online

❖ I use 2.7 but pyVmomi supports the following versions

of Python: 2.6, 2.7, 3.3, 3.4

❖ On Mac consider brew install of python

Install an IDE

❖ pyCharm/IntelliJ

❖ pyDev

❖ Other

Install pyVmomi

❖ Universal/Recommended

❖ pip install -U pyvmomi

❖ RHEL/Fedora/CentOS

❖ yum install pyvmomi

❖ FreeBSD

❖ pkg install pyvmomi

Not So Fast!!

vSphere Object Model

❖ Managed Objects

❖ HostSystem, VirtualMachine, Folder, Network, etc..

❖ Managed Object Reference

❖ Used in MO to point to other associated managed objects

❖ Data Objects

❖ Information about a Managed Object. Properties of the

MO

Object Diagram

Basic Usage with Actual Code!!

>>> from pyVim.connect import SmartConnect

>>> si = SmartConnect(host="vcsa", user="admin", pwd="password")

>>> host = si.content.searchIndex.FindByDnsName(None, “DC0_C0_H0”, False)

>>> print host.name

DC0_C0_H0

>>> print host.summary.hardware.model

ProLiant DL380 G5

3 Lines of Code to get a HostSystem!

List All HostSystems in vCenter

service_instance = SmartConnect(User, Password, HostName)

Hosts = GetAllHosts(service_instance)

for host in Hosts:

PrintHostInfo(host)

def PrintHostInfo(host):

# print various property info for a given host

def GetAllHosts(service_instance):

# do work needed to get host related info here…

Solving Business Problems

❖ SAN Team has a “Non Impacting No Downtime Maintenance” to do on the SAN

Switches

❖ All customers have redundant connections

❖ Maint will require them to bring down A chan restore it then bring down B

chan and restore it

❖ Such a low risk VirtSupport not informed only customer is notified.

❖ vCenter Alarms monitor the SAN connections

❖ They see redundancy lost & trigger SNMP trap.

❖ Ticket is generated for each device!!

❖ 30 mins later its 3am & 100’s of tickets have hit the queue fire drill begins!

Solving Business Problems

❖ Already had SOP in place for how to manually work the Redundancy lost tickets

❖ Use python to connect to various APIs in company to gather data from ticket to

automate the SOP

❖ Use pyVmomi to connect vCenter to check for actual problem.

❖ Rescan HBA etc..

❖ Use pyVmomi to ack alarm & reset from red to green

❖ Use python to hit ticket API to close ticket if fixed or flag as real issue for

VirtSupport to go look at

❖ While this code was in production it worked 1000’s of tickets saving Rackers 100’s

of hours in NVA work.

Valuable Tools

❖ Onyx

❖ https://labs.vmware.com/flings/onyx

❖ Latest Version: 2.2.5

❖ Capture all traffic happening between client and server

❖ DoubleCloud Proxy

❖ Created by: Steve Jin

❖ http://www.doublecloud.org/doublecloud-proxy/

❖ Capture all traffic happening between client and server

Onyx & DoubleCloud Proxy

❖ Start the app and point them at your target vSphere

server

❖ Point your script or VIClient etc.. at proxy

❖ Profit

Things Ive Learned

❖ There are several “hidden” folders

❖ These can all be seen using the MOB

❖ HostSystems do not always have a unique UUID

❖ On Dell if Service Tag not set in BIOS it uses a generic default UUID

❖ When searching for them use the DNS Name or InventoryPath +

name

❖ InventoryPath can be tricky. Use MOB for help

❖ Using the UUID will return the first one it finds

❖ http://kb.vmware.com/kb/1006250

Things Ive Learned

❖ VirtualMachine BIOS UUID is shared by VMS in a vApp

❖ The only thing unique for use in searching for a VM is

the instance UUID so use it or the InventoryPath +

name

❖ Property Collectors are complicated but worth the extra

work

❖ You CAN NOT SCALE with out them!!

Must Have Links

❖ pyVmomi docs: https://github.com/vmware/pyvmomi/tree/master/docs

❖ community-samples: https://github.com/vmware/pyvmomi-community-samples/

❖ ~30 samples and growing!! Contribute today!!

❖ Help us: https://github.com/vmware/pyvmomi-community-

samples/labels/help%20wanted

❖ My Blog: http://www.errr-online.com/

❖ VMWare developer resources: http://developercenter.vmware.com/sdks

❖ Shawn Hartsock: http://hartsock.blogspot.com/

❖ VMWare employee currently maintaing pyVmomi

❖ youtube: https://www.youtube.com/playlist?list=PLO7-

YtwexdVIttJejhsBjo0TZPq0BSFfw

Getting Help

❖ pyVmomi: https://github.com/vmware/pyvmomi

❖ IRC: Freenode —> #pyvmomi

❖ Mailing List: http://pyvmomi.2338814.n4.nabble.com/

❖ StackOver Flow: tag post with pyvmomi