Table of Contents - VMwaredocs.hol.vmware.com/HOL-2014/hol-sdc-1422_pdf_en.pdf · ·...
Transcript of Table of Contents - VMwaredocs.hol.vmware.com/HOL-2014/hol-sdc-1422_pdf_en.pdf · ·...
Table of ContentsHOL-SDC-1422 - VMware Development Tools and SDKs ................................................... 2
Lab Overview .......................................................................................................... 3Module 1 - Developer Center, Workbench IS, and the vSphere Management SDK (30min) .................................................................................................................................. 4
Introduction............................................................................................................. 5Using Workbench IS and the vSphere Management SDK ........................................6Creating a new vSphere management sample application ...................................12Next steps ............................................................................................................. 21Make it yours (optional) ........................................................................................ 22
Module 2 - pyvmomi - The Python SDK for VMware vSphere API (30 min)......................23Introduction........................................................................................................... 24Using PyDev and the pyvmomi SDK...................................................................... 25Inspecting the vCenter inventory from the Python interpreter..............................30Putting it all together ............................................................................................ 39Next Steps............................................................................................................. 40
Module 3 - Workbench IS Tools (15 min) ......................................................................... 41Introduction........................................................................................................... 42Using the Remote System Explorer (RSE) ............................................................. 43Using the Log Browser .......................................................................................... 54
HOL-SDC-1422
Page 1HOL-SDC-1422
Lab OverviewThis introductory lab will get you up and running building cloud infrastructure solutionswith VMware development tools and SDKs. These are developer focused modules andsome familiarity with programming in Java or Python is expected. But don't worry, nocoding is required. Complete sample code has been provided.
The lab is broken into Modules and the Modules can be taken in any order. Use the"Table of Contents" button in the menu bar for quick access to the module content. Youhave 90 minutes per seating to work in the lab. This will be enough time to completeone or more modules. Each module may be completed individually. In some Modules,the Lessons build off of each other, so once you pick a module, you will need to followeach Lesson in order:
• Module 1 - Developer Center, Workbench IS and the vSphereManagement SDK (30 Minutes) - You will build a Java application to manageresources on vCenter server and ESXi server. There is a Java sample alreadyincluded that manages VMs in a vCenter cluster.
• Module 2 - pyvmomi - The Python SDK for VMware vSphere API (30Minutes) - You will build a Python application to manage resources on vCenterserver and ESXi server. There is a Python sample already included that lists theVMs in a vCenter cluster.
• Module 3 - Workbench IS Tools (15 Minutes) - This module is an introductionto tools included in Workbench IS including: RSE (Remote System Explorer) - Atool to inspect and manage resources on a remote ESXi or vCenter system andLog Browser - A tool to collect, analyze, and correlate log files from a remotesystem.
NOTE: If you are using a device with non-US keyboard layout, you might find itdifficult to enter CLI commands, user names and passwords throughout themodules in this lab. Refer to the file README.txt on the desktop for additionalinformation on resolving any keyboard input issues.
Lab Captain: Bob Fraser
HOL-SDC-1422
Page 3HOL-SDC-1422
Module 1 - DeveloperCenter, Workbench IS,
and the vSphereManagement SDK (30
min)
HOL-SDC-1422
Page 4HOL-SDC-1422
IntroductionThis module will show you how to build a Java application to manage resources onvCenter server and ESXi server. There is a Java sample already included that managesVMs in a vCenter cluster. This module is expected to take approximately 30 minutes.
At the end there is an optional section that will cover installation of the Eclipse-basedWorkbench IS tooling and the SDK on your own system.
HOL-SDC-1422
Page 5HOL-SDC-1422
Using Workbench IS and the vSphereManagement SDKThis lesson shows you how to use Workbench IS to run the sample and view the results.This also takes you through the code to understand how the SDK may be used toperform operations on the virtual infrastructure. For more detailed explanation refer tothe SDK documentation at http://developercenter.vmware.com>> SDKs >> vSphereManagement SDK.
First you will run the sample application from the command line via a batch script. Thenyou will debug the sample application using Workbench IS and the Eclipse debugger.
HOL-SDC-1422
Page 6HOL-SDC-1422
Run sample application
The sample can be run by double clicking PowerOnVM.bat on the desktop. Just hit"enter" four times to accept the defaults and connect to the vCenter instance in the lab,then follow the menu prompts to navigate the vCenter hierarchy. The image aboveshows an example run of the sample application.
Press "0" to return to previous menu or press a number to select an item from themenu.
HOL-SDC-1422
Page 7HOL-SDC-1422
Debug sample application
To debug the sample application, first launch Eclipse by double clicking the icon on thedesktop.
Press the triangle next to the debug icon and select Debug Configurations ...
HOL-SDC-1422
Page 8HOL-SDC-1422
Debug
Select Java Application >> PowerOnVM.
The image above shows how the debug configuration is set up. The Main class iscom.vmware.samples.poweronvm.Main
Click on the Arguments tab and see the three program arguments:
vcsa-01a.corp.local root VMware1!
These arguments correspond to <vCenter IP> <vCenter User> <vCenter Password>respectively.
Click Debug to run the sample application in the Workbench IS debugger.
You can use the Eclipse debugger features to set breakpoints in the code, step throughcode, inspect variables, etc.
HOL-SDC-1422
Page 9HOL-SDC-1422
Sample application outline
Navigate back to the Java perspective and open the PowerOnVM project in thepackage explorer.
The image above shows the outline of the sample application. A more detailedexplanation can be found in the code comments.
Connection.java: This class has methods for connecting and disconnecting from thevCenter Server.
HelloSDK.java: This is the main program entry point and it drives the console based UIfor the sample.
Hosts.java: This class has methods for getting the ESXi host properties and ManagedObject References (Morefs). Morefs are "handles" to remote objects in ESX or vCenter.When you manipulate the Moref, corresponding actions occur on the remote system.
HOL-SDC-1422
Page 10HOL-SDC-1422
SimpleUI.java: This class has helper methods used for rendering the menu based UIand capturing the users input. The user interface has intentionally been made simpleand loosely coupled to allow easy reuse of this sample in a Web application or stand-alone Java application.
SSLCert.java: This class is used by the Connection class to handle the SSL Certificatesfor the vCenter connection.
VirtualMachine.java: This class has all the methods that interact with virtual machinessuch as getting VM properties, VM Power operations, getting VM name, getting VMMorefs etc
VMTask.java: This class has some helper methods used by the VirtualMachine class.
HOL-SDC-1422
Page 11HOL-SDC-1422
Creating a new vSphere managementsample applicationThis lesson shows how to setup a vSphere Web Services Sample Application andconfigure it to run a sample.
Create new Eclipse project
Start the Eclipse IDE by double clicking on the desktop icon.
From the "File" Menu select "New" then "Other" as shown in the image above.
HOL-SDC-1422
Page 12HOL-SDC-1422
Select a wizard
Scroll down to the "VMware" folder and select the "Development Kit Projects" wizard andclick Next.
HOL-SDC-1422
Page 13HOL-SDC-1422
VMware Development Kit Project
Select the "vSphere Web Services Sample Application" and click Next.
HOL-SDC-1422
Page 14HOL-SDC-1422
vSphere Management SDK Sample
Enter a project name. Leave Project location at default and click Finish.
Run a sample SDK application
Before we run any sample SDK application we need to configure the connection detailsused by the samples.
You may cut and paste helpful text from the README.txt file on the desktop.
• Return to the Java perspective and in the newly created project, open theconnection.properties file for editing.
• Since we are in a development environment we will use basic authentication.Comment out the line com.vmware.connection.Connection =com.vmware.connection.SSOConnection
• Uncomment the line com.vmware.connection.Connection =com.vmware.connection.BasicConnection
HOL-SDC-1422
Page 15HOL-SDC-1422
• Uncomment the line and set the vimService.url property tohttps://vcsa-01a.corp.local/sdk
• Uncomment the line and set the vCenter Server Username to root• Uncomment the line and set the vCenter Server Password to VMware1!• Save the connection.properties file after finishing the edits.
Please refer to the highlighted code lines in the image above.
HOL-SDC-1422
Page 16HOL-SDC-1422
Create a new Debug (or Run) configuration
1. In the Package Explorer select the newly created samples project (e.g.vspheresamples).
2. Press the triangle next to the debug icon.3. Select Debug Configurations ...
HOL-SDC-1422
Page 17HOL-SDC-1422
Create a Java launch configuration
1. Select Java Application2. Press the New button to create a new launch configuration3. Set the Name (e.g. vspheresamples)4. Set the Main class to com.vmware.common.Main
Set the Program arguments and debug
To run a sample specify the sample class name in the arguments section of the launchconfiguration. You will run the SimpleClient sample. This sample will connect to avCenter server and print out an inventory of datacenters, hosts, and virtual machines.
1. Select the Arguments tab2. Set the Program arguments to com.vmware.general.SimpleClient3. Press Apply to save the configuration
HOL-SDC-1422
Page 18HOL-SDC-1422
Sample output
The image above shows an example run of the SimpleClient code sample
HOL-SDC-1422
Page 20HOL-SDC-1422
Next stepsYou now know how to build a Java application to manage resources on vCenter server orESXi server.
For further study, try some of the following:
• Set a breakpoint in the PowerOnVM application and debug. Step through the codeand inspect the returned data structures.
• Extend the PowerOnVM sample. Inspect and copy code fragments from thesample created in "Creating a new vSphere management sample application" andadd them to PowerOnVM. For example "clone" the VM (see class VMclone.java).
HOL-SDC-1422
Page 21HOL-SDC-1422
Make it yours (optional)Go to http://developercenter.vmware.com/web/workbench/3.0/workbench-is to find outmore about Workbench IS, additional SDKs, and to download the sample code used inthis hands-on lab.
HOL-SDC-1422
Page 22HOL-SDC-1422
IntroductionThis module will show you how to build a Python script or application to manageresources on vCenter server and ESXi server. There is a Python sample already includedthat lists the VMs in a vCenter cluster. This module is expected to take approximately 30minutes.
At the end there is an optional section that will cover installation of the pyvmomi SDK onyour own system.
HOL-SDC-1422
Page 24HOL-SDC-1422
Using PyDev and the pyvmomi SDKThis lesson shows you how to use Workbench IS and the PyDev plugin to run the sampleand view the results. This also takes you through the code to understand how the SDKmay be used to perform operations on the virtual infrastructure in a vSphere or vCenterenvironment.
First you will run the sample application from the command line via a Python script.Then you will debug the sample application using Workbench IS with the PyDev pluginand the Eclipse debugger.
Run sample application
The sample can be run by double clicking GetAllVMs.bat on the desktop. The imageabove shows an example run of the sample application.
Edit sample application
Start Workbench IS by double clicking on the Eclipse icon on the desktop
1. Navigate to the PyDev perspective.
HOL-SDC-1422
Page 25HOL-SDC-1422
2. Open the GetAllVMs project in the PyDev Package Explorer.3. Open getallvms.py.
The image above shows the sample application. A more detailed explanation can befound in the code comments.
Debug sample application
In this lesson you will learn how to debug a pyvmomi Python application.
HOL-SDC-1422
Page 26HOL-SDC-1422
Set a breakpoint
In getallvms.py scroll down into the main function.
At the line vm_folder = datacenter.vmFolder right click in the left margin to get thecontext menu as shown in the image above.
Select Add Breakpoint
Verify breakpoint
You will know that the breakpoint is set when you see the green debug icon to the left ofthe line of code.
Select Debug Configurations
Press the triangle next to the debug icon and select Debug Configurations ...
HOL-SDC-1422
Page 27HOL-SDC-1422
Debug
Select Python Run >> GetAllVMs getallvms.py
The image above shows how the debug configuration is set up.
Click on the Arguments tab and see the three program arguments:
-s vcsa-01a.corp.local -u root -p VMware1!
These arguments correspond to <vCenter IP> <vCenter User> <vCenter Password>respectively.
Click Debug to run the sample application in the Workbench IS debugger.
HOL-SDC-1422
Page 28HOL-SDC-1422
Inspect the results
The image above shows the Eclipse graphical debugger. Try inspecting the datacentervariable or stepping through the code.
HOL-SDC-1422
Page 29HOL-SDC-1422
Inspecting the vCenter inventory fromthe Python interpreter.One of the benefits of the Python programming language is its interpretive nature andpowerful interactive shell. In this lesson you will learn how to use these tools to inspectthe vCenter hierarchy of objects (Datacenter, Host, VM, etc.).
While you can always get to the Python interpreter by typing python in a terminalwindow, using a special purpose Python shell has advantages such as commandcompletion.
If you prefer using command line and no IDE, then try IDLE, the shell that comes withyour Python installation. You may run it by double clicking the icon on the desktop.
The examples below will use the console shell that comes with PyDev.
You may cut and paste helpful text from the README.txt file on the desktop.
HOL-SDC-1422
Page 30HOL-SDC-1422
Understanding the vCenter Inventory Hierarchy
VMware vCenter allows you to manage physical and virtual infrastructure in adatacenter. The pyvmomi SDK provides Python bindings that allow you toprogrammatically manage the inventory of that same infrastructure.
The inventory is managed as a collection of virtual datacenters. Each datacentercontains compute, data storage, and networking resources. Virtual machines and virtualapplications can be created in these datacenters that consume these resources. Folderscan be used to hierarchically group objects of the same type. The nested structureallows you to organize the objects in the datacenter into an easily manageablestructure. Possible reasons include aligning with various functions and/or organizationalstructure.
The image above shows the hierarchy of objects in the vCenter inventory.
For many pyvmomi applications, the flow of applications is similar. You connect to avCenter server, get a service instance, and starting with the root folder, navigate thehierarchy. You can then perform various operations on these managed objects.
HOL-SDC-1422
Page 31HOL-SDC-1422
Launching the PyDev console
Navigate to the PyDev perspective and make sure a console is visible. If no console isvisible, you can launch one with menu Window >> Show View >> Console.
In the console view, press the triangle next to the Open Console icon and select PyDevConsole.
HOL-SDC-1422
Page 32HOL-SDC-1422
Configure the console
Select Python console and press OK.
The Python interpreter shell will start. Double click the console tab to expand or restorethe console view. Press the stop icon to exit the console.
Initial setup and useful commands
In the console type
from pyVim import connect
Nothing is returned but this loads parts of the pyvmomi library into the interpreter.
Useful commands
Use the up and down arrows to navigate the command history. Use <tab> or <control>-<space> for command completion.
Try the import again as
from pyVi<tab> import con<tab>
help(connect)
will print out module information.
HOL-SDC-1422
Page 33HOL-SDC-1422
Connect to vCenter
Connect to vCenter and create a service instance:
si = connect.SmartConnect(host='vcsa-01a.corp.local', user='root', pwd='VMware1!')
Two useful commands are type and dir.
type(si)
returns the type of the object. The output is:
<class 'pyVmomi.VmomiSupport.vim.ServiceInstance'>
dir(si)
will print the directory of the object, showing properties and methods.
Navigate to a VM
Type the following commands to get to a VM. Remember to use <tab> or <control>-<space> for command completion.
Get the inventory:
inv = si.RetrieveContent()
Get the (first) datacenter:
dc1 = inv.rootFolder.childEntity[0]
Make sure we really have a datacenter:
type(dc1)
returns:
<class 'pyVmomi.VmomiSupport.vim.Datacenter'>
The type command is useful in the interpreter, but when writing scripts, there is aanother way to check types at runtime. First import the vim module:
from pyVmomi import vim
This imports the vim and vmodl symbol tables and allows you to use shorter classnames.
isinstance(dc1, vim.Datacenter) == True
HOL-SDC-1422
Page 34HOL-SDC-1422
can now be used to ensure that we really have a datacenter object.
Now get a list of VMs:
vmList = dc1.vmFolder.childEntity
Get the first VM
vm = vmList[0]
Verify the type.
type(vm)
But wait, this is a folder not a VM. Remember the vmfolder may be just a collection ofVMs, but it really is a hierarchy of VMs grouped using subfolders. The same is true forother folders (datacenter, datastore, network, etc.) Try again seting vm = vmList[1].Use the up arrow to go up the command history and edit the 0 into a 1 to save typing.
This time type(vm) returns:
<class 'pyVmomi.VmomiSupport.vim.VirtualMachine'>
Get the vm name:
vm.name
returns
'w12-core1'
vm.summary
will return a wealth of information about this VM, including runtime, configuration, andguest properties.
Power on VM
Now you will power on the VM.
First check the power state of the VM:
vm.runtime.powerState
returns
'poweredOff'
HOL-SDC-1422
Page 35HOL-SDC-1422
Power on the VM with the following command:
t = vm.PowerOn()
Code completion can be used to quickly get you to relevant properties and functions.
Type t = vm.p and wait for the pop up completion menu. If the menu doesn't appear,use <control>-<space>. Use the arrow key to select PowerOn().
The image above shows how code completion works.
Press <enter> twice to execute the power on command.
A task is launched on the remote server.
t.info
returns status of the task.
Specifically:
t.info.state
will return
'success'
when the PowerOn task has successfully completed on the server.
Verify the state of the VM:
vm.runtime.powerState
returns
'poweredOn'
HOL-SDC-1422
Page 36HOL-SDC-1422
Use vCenter to check VM power state
You can verify the VM is really powered on in vCenter.
Launch the firefox browser and go to the vSphere Web Client page.
Log in as user root password VMware1!.
Navigate the hierarchy and get the summary for VM w12-core.
The summary shows that the VM is up and running.
HOL-SDC-1422
Page 38HOL-SDC-1422
Putting it all togetherThe project powercyclevm contains a Python script to select a VM by name, power it offand power it back on. It optionally answers any VM questions on power on.
Edit powercyclevm
Edit the file virtual_machine_power_cycle_and_question.py
This script uses Tasks and you can see it check the state of the tasks to ensure previoussteps have completed.
Try debugging this application and stepping through the code. Use the powercyclevmdebug configuration to launch the script.
HOL-SDC-1422
Page 39HOL-SDC-1422
Next StepsYou now know how to build a Python application to manage resources on vCenter serveror ESXi server.
You can find the pyvmomi libraryon GitHub at: https://github.com/vmware/pyvmomi
You can install pyvmomi (on your own system) with Pip by running the followingcommand:
pip install pyvmomi
On Windows systems, you may need to be an administrator. On Unix based systems,you may need to use the sudo command.
You can find community contributed pyvmomi samples at: http://vmware.github.io/pyvmomi-community-samples/
HOL-SDC-1422
Page 40HOL-SDC-1422
IntroductionModule 3 - Workbench IS Tools
This module is an introduction to two tools included in Workbench IS:
• RSE - Remote System Explorer - This tool allows you to inspect and manageresources on a remote ESXi or vCenter system including files, the datacenterinventory, virtual machines and ssh sessions.
• Log Browser - This tool allows you to collect, analyze, and correlate log filesfrom a remote system.
This module is expected to take approximately 15 minutes.
HOL-SDC-1422
Page 42HOL-SDC-1422
Using the Remote System Explorer(RSE)In this lesson you will use the Remote System Explorer to connect to an ESX host orvCenter server, explore the inventory of resources, power on a virtual machine, andlaunch an SSH terminal window on an ESX host.
Think of the Remote System Explorer as a mini vSphere Client (albeit with limitedfunctionality) embedded in the Eclipse IDE. This can be useful if you need to connect toan ESX host directly or prefer an experience, integrated with your developmentenvironment, for inspecting and manipulating the vCenter infrastructure. RSE is crossplatform and runs on Windows, Linux, and Mac OSX.
Switch to the Remote System Explorer perspective
Launch Eclipse by double clicking the icon on the desktop.
Switch to the Remote System Explorer (RSE) perspective by clicking the icon on theupper right.
HOL-SDC-1422
Page 43HOL-SDC-1422
Connect to a vCenter server
Right click in the Remote System panel and select New >> Connection …
HOL-SDC-1422
Page 44HOL-SDC-1422
Select the connection type
Select Virtual Center for Linux and click Next
HOL-SDC-1422
Page 45HOL-SDC-1422
New Connection wizard
In the New Connection wizard set the Host name to vcsa-01a.corp.local, the Username to root, and the Password to VMware1! and click Finish.
HOL-SDC-1422
Page 46HOL-SDC-1422
Explore vCenter resources
You will now see a new connection. Expand the hierarchy. You can see the inventory ofresources such as datacenters, clusters, and hosts, as well as a list of VMs.
Click on a VM and, in the Properties panel, you will see a summary of properties for theVM such as memory, cpus, etc.
Power on a Virtual Machine
In the Remote System panel, find a VM in the vCenter inventory and right click to get acontext menu. Then select Power On.
HOL-SDC-1422
Page 47HOL-SDC-1422
If the VM is already powered on, you will see options to power off, suspend or reset theVM.
HOL-SDC-1422
Page 48HOL-SDC-1422
Connect to an ESX host
Open the Remote System Explorer perspective.
Right click in the Remote System panel and select New >> Connection …
HOL-SDC-1422
Page 49HOL-SDC-1422
New Connection wizard
In the New Connection wizard set the Host name to esx-01a.corp.local, the User nameto root, and the Password to VMware1!
Click Finish
HOL-SDC-1422
Page 51HOL-SDC-1422
Explore ESX resources
You will now see a new connection. Expand the hierarchy. You can see resources such asdatastores and network devices, as well as a list of VMs.
Click on a VM and, in the Properties panel, you will see a summary of properties for theVM such as memory, cpus, etc.
Open an SSH terminal on an ESX host
In the Remote Systems panel, right click on Ssh Terminals to get a context menu andselect Launch Terminal.
HOL-SDC-1422
Page 52HOL-SDC-1422
If this is the first time you are connecting to this host, you may see a message aboutunknown SSH hosts. Click Yes to allow the connection to proceed.
SSH terminal
A Terminals panel appears with a remote connection to the ESX host. You may nowperform administrative tasks such as running ESXCLI commands.
HOL-SDC-1422
Page 53HOL-SDC-1422
Using the Log BrowserLog Browser allows you to collect, analyze, and correlate log files from a remote ESX orvCenter system. In normal development, you may never need Log Browser. However, ifsomething goes wrong, this can be a powerful diagnostic tool.
In this lesson you will use Log Browser to connect to an ESX host, collect system logfiles, and analyze one of them to find errors and specific events such as the power on ofa virtual machine.
Open the resource perspective
Click the Open Perspective button and select Resource.
HOL-SDC-1422
Page 54HOL-SDC-1422
Create a new Log Analysis
In the menu select File >> New >> Log Analysis.
HOL-SDC-1422
Page 55HOL-SDC-1422
New Log Analysis wizard
Set the Source host by selecting esx-01.corp.local from the pull down menu. Thisconnection should already exist from the lesson Using the Remote System Explorer >>Connect to an ESX host. If it does not exist you may create it by clicking the Add …button.
Set the Log format by selecting [Standard Log Collection] from the pull down menu.
Set the Project container with any project name such as esxlogs (the project will becreated if it doesn’t exist).
Click Next.
HOL-SDC-1422
Page 56HOL-SDC-1422
Open the Log Browser views
Double click on esx-01a.corp.local_hostd.logx to open the Log Viewer with that log file.
Double click on the log entry "Current working directory ..." to open the Log EntryDetails view with that log entry.
HOL-SDC-1422
Page 58HOL-SDC-1422
Analyzing a log file
You can use filtering to find patterns or events in log files.
Type error in the Filter input field and click the Filter button (the button label will changeto Unfilter). You will now see all log entries with the string error.
You can use this same technique to find warnings as well. You can also use the filter withany set of keywords to find specific events.
HOL-SDC-1422
Page 59HOL-SDC-1422
Find PowerOn events
Type PowerOn in the Filter input field and also check the Case sensitive checkbox.
Click the Filter button (the button label will change to Unfilter).
You will now see all of the PowerOn events. If you double click on the log entry you canfind detailed information down to the path to the virtual machine VMX files.
You can use this technique to help diagnose various development tasks. If you werewriting vSphere programs or scripts to add a datastore, you could filter the logs usingthe datastore keyword to find all log messages related to datastores on this ESX host.
HOL-SDC-1422
Page 60HOL-SDC-1422
ConclusionThank you for participating in the VMware Hands-on Labs. Be sure to visithttp://hol.vmware.com/ to continue your lab experience online.
Lab SKU: HOL-SDC-1422
Version: 20150227-060930
HOL-SDC-1422
Page 61HOL-SDC-1422