01-30-2018 04:42 AM - edited 03-01-2019 06:47 AM
Task Name | Toggle the VMware tools automatic upgrade on a VM at power on |
Description | |
Prerequisites |
|
Category | Workflow |
Components | |
User Inputs | VM name vCenter Cluster name/vCenter DC Name vCenter name Boolean (true / false) |
Output |
Instructions for Regular Workflow Use:
Note:
This task can work in conjunction with this posting:
UCSD - Patch ESXi Servers by vCenter Cluster (Firmware Upgrade coming in UCSD 6.6)
The Workflow:
User Input to toggle the flag on:
Result:
User Input to toggle the flag off:
Result:
Workflow execution:
The code of the custom task for the VMware tools toggle:
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(com.cloupia.service.cIM.inframgr.vmware);
importPackage(com.cloupia.model.cIM);
importPackage(com.cloupia.lib.cIaaS.vmware);
importPackage(com.vmware.vim25.mo);
importPackage(com.vmware.vim25);
importPackage(java.lang);
importPackage(com.cloupia.service.cIM.inframgr);
importPackage(com.cloupia.feature.vmwareController.wftasks.configs);
var clusterName=input.VCenterCluster;
var account=InfraPersistenceUtil.getAccount(input.VCenterAccount);
var datacenterName = input.VCenterDC;
var serviceInstance = new VCenterConnectionManager(account).getServiceInstance();
var dataCenterEntity = null;
var vmName = input.VMName;
if (datacenterName !== null && !datacenterName.isEmpty()) {
//If datacenter name is present, get the entity from root folder.
logger.addInfo("Datacenter name is " + datacenterName);
var folder = serviceInstance.getRootFolder();
dataCenterEntity = new InventoryNavigator(folder).searchManagedEntity("Datacenter", datacenterName);
}
var clusterEntity = null;
var folder = null;
if (dataCenterEntity !== null) {
logger.addInfo("Datacenter entity is " + dataCenterEntity);
if (clusterName !== null && !clusterName.isEmpty()) {
logger.addInfo("Cluster name is " + clusterName);
//Once we have datacenter and cluster, filter at cluster level
clusterEntity = new InventoryNavigator(dataCenterEntity).searchManagedEntity("ClusterComputeResource", clusterName);
}
} else {
logger.addInfo("Cluster is only present " + clusterName);
if (clusterName !== null && !clusterName.isEmpty()) {
folder = serviceInstance.getRootFolder();
clusterEntity = new InventoryNavigator(folder).searchManagedEntity("ClusterComputeResource", clusterName);
}
}
var vmME = null;
if (clusterEntity !== null) {
logger.addInfo("Cluster entity is: " + clusterEntity);
logger.addInfo("VM name is " + vmName);
vmME = new InventoryNavigator(clusterEntity).searchManagedEntity("VirtualMachine", vmName);
logger.addInfo("VM Entity is " + vmME);
if (vmME !== null) {
// logger.addInfo("Tools running status " + vmME.getGuest().getToolsRunningStatus());
// logger.addInfo("Tools status " + vmME.getGuest().getToolsStatus());
// logger.addInfo("Tools version " + vmME.getGuest().getToolsVersion());
// logger.addInfo("Tools version status " + vmME.getGuest().getToolsVersionStatus());
// logger.addInfo("Tools version status2 " + vmME.getGuest().getToolsVersionStatus2());
// logger.addInfo("Tool policy " + vmME.getConfig().getTools().getToolsUpgradePolicy());
var powerState = vmME.getRuntime().getPowerState().toString();
if (powerState.equals("poweredOff")) {
var toolsInfo = vmME.getConfig().getTools();
// toolsInfo.setAfterPowerOn(true);
if (input.UpgradeYesNo === false){
toolsInfo.setAfterPowerOn(false);
toolsInfo.setToolsUpgradePolicy(UpgradePolicy.manual.toString());
}
if (input.UpgradeYesNo === true){
toolsInfo.setAfterPowerOn(true);
toolsInfo.setToolsUpgradePolicy(UpgradePolicy.upgradeAtPowerCycle.toString());
}
// toolsInfo.setAfterPowerOn(false);
//For manual setting
//toolsInfo.setToolsUpgradePolicy(UpgradePolicy.manual.toString());
//For upgrade at power recycle settings
//toolsInfo.setToolsUpgradePolicy(UpgradePolicy.upgradeAtPowerCycle.toString());
//vmME.getConfig().setTools(toolsInfo);
var spec = new VirtualMachineConfigSpec();
spec.setTools(toolsInfo);
logger.addInfo("Tool policy2 " + vmME.getConfig().getTools().getToolsUpgradePolicy());
var aTask = vmME.reconfigVM_Task(spec);
var aStatus = aTask.waitForTask();
if (!Task.SUCCESS.equals(aStatus)) {
var ti = task.getTaskInfo();
if (ti !== null) {
if (ti.getError() !== null) {
aStatus = aStatus + ": " + ti.getError().getLocalizedMessage();
}
}
logger.addInfo(status);
} else {
logger.addInfo("Successfully reconfigured the vm with name "+ vmName);
}
}
}
}
serviceInstance.getServerConnection().logout();
VOD:
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: