Now let’s show some examples. Before we start, you will need access to vSphere and have access with privileges to create/modify/delete machines on infrastructure. Also i recommend to see http://vijava.sourceforge.net/doc/getstarted/tutorial.htm for a vijava setup. Or if you are using maven, you can add following dependency into your pom file
<dependency>
<groupId>com.vmware</groupId>
<artifactId>vijava</artifactId>
<version>5.1</version>
</dependency>
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
/**
* Sample code for creating customized Linux/Windows VM clone from template using VIJAVA
*/
public class VMManager {
public static void main(String args[]) throws MalformedURLException,
RemoteException, InterruptedException {
//Define needed vars
String vSphereUrl = "https://vsphere.example.com:1234/sdk";
String vSpherePassword = "password";
String vSphereUsername = "username";
String templateVMName = "template-machine1";
String cloneName = "vm-1";
//Connect to vSphere server using VIJAVA
ServiceInstance si = new ServiceInstance(new URL(vSphereUrl),vSphereUsername,vSpherePassword);
//Find the template machine in the inventory
InventoryNavigator inventoryNavigator = new InventoryNavigator(si.getRootFolder());
VirtualMachine vmTemplate = (VirtualMachine) inventoryNavigator.
searchManagedEntity("VirtualMachine", templateVMName);
//Create customization for cloning process(Uncomment the one you need)
VirtualMachineCloneSpec cloneSpec = createLinuxCustomization();
//VirtualMachineCloneSpec cloneSpec = createWindowsCustomization();
//Do the cloning - providing the clone specification
Task cloneTask = vmTemplate.cloneVM_Task((Folder) vmTemplate.getParent(),cloneName,cloneSpec);
cloneTask.waitForTask();
//Here is our new customized virtual machine ready
VirtualMachine vm = (VirtualMachine) inventoryNavigator.searchManagedEntity("VirtualMachine", cloneName);
vm.powerOnVM_Task(null).waitForTask();
}
public static VirtualMachineCloneSpec createLinuxCustomization(){
VirtualMachineCloneSpec vmCloneSpec = new VirtualMachineCloneSpec();
//Set location of clone to be the same as template (Datastore)
vmCloneSpec.setLocation(new VirtualMachineRelocateSpec());
//Clone is not powered on, not a template.
vmCloneSpec.setPowerOn(false);
vmCloneSpec.setTemplate(false);
//Create customization specs/linux specific options
CustomizationSpec customSpec = new CustomizationSpec();
CustomizationLinuxOptions linuxOptions = new CustomizationLinuxOptions();
customSpec.setOptions(linuxOptions);
CustomizationLinuxPrep linuxPrep = new CustomizationLinuxPrep();
linuxPrep.setDomain("example.domain.com");
linuxPrep.setHwClockUTC(true);
linuxPrep.setTimeZone("Europe/London");
CustomizationFixedName fixedName = new CustomizationFixedName();
fixedName.setName("cloned-machine-hostname");
linuxPrep.setHostName(fixedName);
customSpec.setIdentity(linuxPrep);
//Network related settings
CustomizationGlobalIPSettings globalIPSettings = new CustomizationGlobalIPSettings();
globalIPSettings.setDnsServerList(new String[]{"8.8.8.8", "8.8.4.4"});
globalIPSettings.setDnsSuffixList(new String[]{"search.com","my.search.com"});
customSpec.setGlobalIPSettings(globalIPSettings);
CustomizationFixedIp fixedIp = new CustomizationFixedIp();
fixedIp.setIpAddress("192.168.10.1");
CustomizationIPSettings customizationIPSettings = new CustomizationIPSettings();
customizationIPSettings.setIp(fixedIp);
customizationIPSettings.setGateway(new String[]{"192.168.1.1"});
customizationIPSettings.setSubnetMask("255.255.0.0");
CustomizationAdapterMapping adapterMapping = new CustomizationAdapterMapping();
adapterMapping.setAdapter(customizationIPSettings);
CustomizationAdapterMapping[] adapterMappings = new CustomizationAdapterMapping[]{adapterMapping};
customSpec.setNicSettingMap(adapterMappings);
//Set all customization to clone specs
vmCloneSpec.setCustomization(customSpec);
return vmCloneSpec;
}
public static VirtualMachineCloneSpec createWindowsCustomization(){
//Windows needs valid product key in order to create fully working clone. Otherwise you will get error message
//when machine is cloned
String productID="XXXXX-XXXXX-XXXXXX-XXXXX";
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
//Set location of clone to be the same as template (Datastore)
cloneSpec.setLocation(new VirtualMachineRelocateSpec());
//Clone is not powered on, not a template.
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
//Create customization specs/win specific options
//Windows are using SYSPREP for these kind of stuff
CustomizationSpec customSpec = new CustomizationSpec();
CustomizationWinOptions winOptions = new CustomizationWinOptions();
winOptions.setChangeSID(true);
//We don't want our preconfigured users to be deleted
winOptions.setDeleteAccounts(false);
customSpec.setOptions(winOptions);
CustomizationSysprep sprep = new CustomizationSysprep();
CustomizationGuiUnattended guiUnattended = new CustomizationGuiUnattended();
guiUnattended.setAutoLogon(false);
guiUnattended.setAutoLogonCount(0);
guiUnattended.setTimeZone(4);
sprep.setGuiUnattended(guiUnattended);
CustomizationIdentification custIdent = new CustomizationIdentification();
custIdent.setJoinWorkgroup("WORKGROUP");
sprep.setIdentification(custIdent);
CustomizationUserData custUserData = new CustomizationUserData();
CustomizationFixedName fixedName = new CustomizationFixedName();
fixedName.setName("windows-clone");
//set from cloned machine
custUserData.setProductId(productID); // REQUIRED FOR Windows
custUserData.setComputerName(fixedName);
custUserData.setFullName("windows-clone.example.com");
custUserData.setOrgName("example.com");
sprep.setUserData(custUserData);
customSpec.setIdentity(sprep);
//Network related settings
CustomizationGlobalIPSettings globalIPSettings = new CustomizationGlobalIPSettings();
globalIPSettings.setDnsServerList(new String[]{"8.8.8.8","8.8.4.4"});
globalIPSettings.setDnsSuffixList(new String[]{"example.com"});
customSpec.setGlobalIPSettings(globalIPSettings);
CustomizationFixedIp fixedIp = new CustomizationFixedIp();
fixedIp.setIpAddress("192.168.10.2");
CustomizationIPSettings customizationIPSettings = new CustomizationIPSettings();
customizationIPSettings.setIp(fixedIp);
customizationIPSettings.setGateway(new String[]{"192.168.1.1"});
customizationIPSettings.setSubnetMask("255.255.0.0");
//Disabling netBIOS
customizationIPSettings.setNetBIOS(CustomizationNetBIOSMode.disableNetBIOS);
customizationIPSettings.setDnsDomain("example.com");
CustomizationAdapterMapping adapterMapping = new CustomizationAdapterMapping();
adapterMapping.setAdapter(customizationIPSettings);
CustomizationAdapterMapping[] adapterMappings = new CustomizationAdapterMapping[]{adapterMapping};
customSpec.setNicSettingMap(adapterMappings);
//Set all customization to clone specs
cloneSpec.setCustomization(customSpec);
return cloneSpec;
}
}