Import a VM in Lab Manager using Orchestrator
Posted by: hbr in vCenter, server virtualization, script, Orchestrator, Labmanager, esx, bash on
Nov 3, 2010
There are a lot of things you can automate in a virtual infrastructure. One of the tools that can help automate the more complex workflows is Orchestrator. It’s free when you have a vCenter and it’s very powerful.
Another great tool is Lab Manager. It’s designed for Test and Dev environments but it can also help to improve uptime of your datacenter by regulating changes in your production environment.
This is a Proof of Concept combining the two tools as they overlap on some areas, complement each other on others but also have a few gaps between them. Luckily there’s a 3rd ‘tool’ that helps fill that gap: PowerCLI.
This blog is a global guide to getting things done. It doesn’t describe every step in detail and assumes basic scripting and workflow knowledge. However, if things are unclear or you need help, don’t hesitate and post a comment at the bottom of this post!
ESX host scripting
At a certain point in the chain, we’ll have the ESX host move a VM from a VMFS to a windows file share. To do this, we’ll use a small piece of bash scripting. Create a file ‘/root/mv-vm’ with the following content:
#!/bin/bash
[ "$1" == "" ] && exit 1
vmx=`vmware-cmd -l | grep -w $1`
[ "$vmx" == "" ] && exit 2
dir=`dirname $vmx`
mnt=/vmfs/volumes/VMFS_Storage01/_clone
vmware-cmd -s unregister $vmx
mount -t cifs –o username=user,password=pwd,workgroup=mydom //server/share/dir/to/put/vm $mnt || exit 3
mv $dir $mnt || exit 4
umount //server/share/dir/to/put/vm
Open the smb port in the esx firewall: ‘esxcfg-firewall –e smbClient’ and that’s it for now. There should really be more error checking in there but for now this is good enough. If the script fails somehow a quick debug option is to put ‘set –x’ on the second line and run it manually.
Orchestrator
First thing we’ll do is have Orchestrator clone a VM to a share from which Lab Manager will import it into the Lab environment.
To do this, we log in to the Orchestrator client and create a new Workflow. We could build all steps ourselves but luckily VMware already provided most common functions in a library of workflows. So, we right click our workflow and choose ‘Edit’.
In the Input tab, we create 2 parameters: ‘vm’ with type VC:VirtualMachine and ‘name’ with type ‘string’. Hit the ‘Show in Presentation’ button and we have two parameters that we can provide when the workflow starts.
Now go to the Presentation tab, select each parameter and below on the Properties tab, hit the ‘Add property’ and add ‘Mandatory input’ to both.
Workflow building
Next, in the Schema tab we go to ‘Action & Workflow’ and drag a Workflow element to the drawing screen. In the search bar that appears, type ‘Clone’ and choose ‘Clone virtual machine, no customization’. Next we add a ‘Run SSH command’, a ‘Send notification to mailing list’, 4 scriptable tasks (from ‘Generic’) and an ‘End workflow’ (‘Generic’). Then we draw the connections (hit the ‘connections’ button first). Draw a line from the start to the ‘Clone’, then one from there to the first scriptable task, next to the ‘SSH’ and then to another scriptable task and finaly to the ‘mail’ workflow element. Then draw one from the right side of the clone and SSH element (which means ‘in case things go wrong’) to 2 more 'scriptable task’ elements and from each of those to the ‘Send notification’ element. This should make the workflow look like this:
Now we define all inputs and outputs from the elements. Start with the ‘Mail’ element and hit the ‘IN’ tab. Click every ‘not set’ link in the Source parameter list and click ‘Create new parameter’. Fill in the values where appropriate except the ‘content’. We’ll fill that in later. Once the ‘Mail’ element is done, it should look something like this:
Now to configure the scriptable parts. We’ll use those to bind the specific elements together. The output for the Clone element is a new VM object but for the SSH element we need a command line as input with the specific new VM name. That’s where the first scriptable element comes in.
As input for this element we need the parameter ‘name’ we ask from the user that starts the workflow. Just add it from the known parameters in the In tab. Now we add two OUT parameters called ‘cmd’ and ‘hostname’, make them a ‘string’ and then at the ‘Scripting’ tab, add this script:
hostname = host.name
cmd = '/root/mv-vm '+name
The next scriptable elements will handle error and messages from the other elements to the Mail element. As input they’ll take the errorcodes and result texts. No output is needed, just some script on the Scripting tab to fill the ‘content’ parameter:
content = "Unable to clone VM '"+vm.name+"';
Server.error( content, "Exception found: "+errorCode );content = “SSH command execution failed with error: “ + errorText;
Server.error( content, "Exception found: "+errorCode1 );content = “VM moved to fileshare succesfully.”;
Now we configure the SSH element. Like the mail element, basically fill in all the ‘not set’ parameters except from the hostNameOrIP and cmd. They will be connected to the OUT parameters of the script element above it.
Then, last but not least, the Clone element. Fill in the parameters again, connecting the ‘vm’ and ‘name’ to the global parameters and creating new ones for the rest, filling them in as you go. Later on you can create more global presented parameters from some of them but for now the two is enough to get going.
So, that was easy enough, right? Well, it can be quite daunting the first time. A good help may be a more detailed step by step example that’s available at http://www.vmware.com/pdf/vco_410_developers_guide.pdf from page 66 and up.
Once everything is set up, it’s time to hit ‘validate’, save and close and run the workflow. If everything’s configured correctly, you’ll end up with a copy of the selected VM on a file share.
In the next part we’ll import the VM into LabManager using some internal LabManager API’s and connect that to this workflow: http://virtuall.eu/blog/hbr/import-a-vm-into-lab-manager-using-orchestrator-part-2
