Examples

0

Customize a Virtual Machine

For John’s question in the VMware Communities about customizing a VM here:
http://communities.vmware.com/thread/311899
I took the challenge and created a small example how to customize a Virtual Machine using the CustomizeVM_Task().

The network settings, licensing, domain stuff and so on should come from a sysprep.inf text file.

Creating the proper CustomizationSpecification is really a hard job, especially since a couple of settings are mandatory even if the used information is already given via the SysprepText.

I got help using the API Reference, and the UML charts in Steve Jin’s indispensable book (http://www.amazon.com/VMware-VI-vSphere-SDK-Infrastructure/dp/0137153635 , I cannot mention it often enough… and no, that’s not an affiliate-link…yet… 😀 ), page 242.

When testing it, I got a lot of not helpful errors like “Cannot complete customization”. If you increase the loglevel in vCenter, in the vCenter logs there are a bit more information. Just don’t forget to reset it afterwards to avoid “logflooding”.

At the end I ended up with this code:


var sysprepMime = sysprepFile.getContentAsMimeAttachment();
 var sysprepText = sysprepMime.content
 System.debug("sysprep: " + sysprepText);

//create the custSpec objects
 var custSpec = new VcCustomizationSpec();
 System.debug("custSpec: " + custSpec);
 var myVcCustomizationSysprepText = new VcCustomizationSysprepText() ;
 System.debug("myVcCustomizationSysprepText: " + myVcCustomizationSysprepText);
 myVcCustomizationSysprepText.value = sysprepText //from Resource element above
 custSpec.identity = myVcCustomizationSysprepText;
 System.debug("custSpec.identity: " + custSpec.identity);

//fill the mandatory GlobalIPSettings
 var myVcCustomizationGlobalIPSettings = new VcCustomizationGlobalIPSettings() ;
 custSpec.globalIPSettings = myVcCustomizationGlobalIPSettings;
 System.debug("custSpec.globalIPSettings: " + custSpec.globalIPSettings);

//fill in the NIC-settings-map for one nic with unknownIPGenerator (IP-spec is in sysprep-text):
 var mappingMap = new Array();
 var myVcCustomizationAdapterMapping = new VcCustomizationAdapterMapping() ;
 var myVcCustomizationIPSettings = new VcCustomizationIPSettings() ;
 var myIpGenerator;
 //myIpGenerator = new VcCustomizationUnknownIpGenerator() ;
 //myIpGenerator = new VcCustomizationFixedIp();
 //myIpGenerator.ipAddress = "1.2.3.4";
 myIpGenerator = new VcCustomizationDhcpIpGenerator() ;

myVcCustomizationIPSettings.ip = myIpGenerator;
 myVcCustomizationAdapterMapping.adapter = myVcCustomizationIPSettings;
 System.debug("myVcCustomizationIPSettings.ip:" + myVcCustomizationIPSettings.ip);
 System.debug("myVcCustomizationAdapterMapping.adapter: " + myVcCustomizationAdapterMapping.adapter);
 mappingMap.push(myVcCustomizationAdapterMapping);
 System.debug("mappingMap: " + mappingMap);

//adding map to custSpec
 custSpec.nicSettingMap = mappingMap;

vmToCustomize.customizeVM_Task(custSpec);

Download the full workflow example here: http://www.vcoportal.de/download/workflow/customizeVM.workflow

Customize VM Workflow
Customize VM Workflow
customizeVM.workflow
2.1 KiB
Details...
0

XML-handling, the E4X-way

The (easier?) alternative to the XML-plugin:
Create variables with plain XML-syntax like
</div>
//create new xml-variable with E4X-syntax
 var xmlcontent = <vms></vms>;
 //E4X: create XML-Child Element: VM
 xmlcontent.vm.@uuid = uuid;
 xmlcontent.vm.name = vmname;
 xmlcontent.vm.cpus = numCpus;
 xmlcontent.vm.memory = memSize;
 xmlcontent.vm.ip = guestIpAddress;
 xmlcontent.vm.hostname = guestHostname;
 //log for sure : - )
 System.debug(xmlcontent);

Available through the Rhino JavaScript engine in vCO, which includes E4X.