vCO Workflow Script to Load Balance Virtual Machine Deployments Across 2 vSphere Clusters in a Provider Virtual Data Center

The following script enables the load balanced deployment of virtual machines across 2 vSphere clusters in a Provider Virtual Data Center

 

 

 

var objVclQueryService;
	objVclQueryService = objVclHost.getQueryService();

var objVclExpression1;
	objVclExpression1 = new VclExpression(VclQueryProviderVdcResourcePoolRelationField.NAME, "DataCenter*Cluster01", VclExpressionType.EQUALS);

var objVclExpression2;
	objVclExpression2 = new VclExpression(VclQueryProviderVdcResourcePoolRelationField.ISENABLED, true, VclExpressionType.EQUALS);

var arrVclExpression;
	arrVclExpression = new Array();
	arrVclExpression.push(objVclExpression1);
	arrVclExpression.push(objVclExpression2);

var objVclFilter;
	objVclFilter = new VclFilter(arrVclExpression, VclFilterType.AND);

var objVclQueryParams;
	objVclQueryParams = new VclQueryParams();
	objVclQueryParams.setFilter(objVclFilter);

var objVclAbstractRecordResultSet;
	objVclAbstractRecordResultSet = objVclQueryService.queryRecords(VclQueryRecordType.PROVIDERVDCRESOURCEPOOLRELATION, objVclQueryParams);

var arrVclQueryResultProviderVdcResourcePoolRelationRecord;
	arrVclQueryResultProviderVdcResourcePoolRelationRecord = objVclAbstractRecordResultSet.getRecords(new VclQueryResultProviderVdcResourcePoolRelationRecord());

var intNumberOfVMsinDataCenter01;
var intUsageCPUinDataCenter01;
var intUsageMemoryInDataCenter01;

var intNumberOfVMsInDataCenter02;
var intUsageCPUinDataCenter02;
var intUsageMemoryInDataCenter02;

for each (var objVclQueryResultProviderVdcResourcePoolRelationRecord in arrVclQueryResultProviderVdcResourcePoolRelationRecord) 
{
	if (objVclQueryResultProviderVdcResourcePoolRelationRecord.name == "DataCenter01Cluster01")
	{
		intNumberOfVMsInDataCenter01 = objVclQueryResultProviderVdcResourcePoolRelationRecord.numberOfVMs;
		intUsageCPUinDataCenter01 = 100 * ( objVclQueryResultProviderVdcResourcePoolRelationRecord.cpuReservationAllocationMhz / objVclQueryResultProviderVdcResourcePoolRelationRecord.cpuReservationLimitMhz );
		intUsageMemoryInDataCenter01 = 100 * ( objVclQueryResultProviderVdcResourcePoolRelationRecord.memoryReservationAllocationMB / objVclQueryResultProviderVdcResourcePoolRelationRecord.memoryReservationLimitMB );
	}
	else if (objVclQueryResultProviderVdcResourcePoolRelationRecord.name == "DataCenter02Cluster01")
	{
		intNumberOfVMsInDataCenter02 = objVclQueryResultProviderVdcResourcePoolRelationRecord.numberOfVMs;
		intUsageCPUinDataCenter02 = 100 * ( objVclQueryResultProviderVdcResourcePoolRelationRecord.cpuReservationAllocationMhz / objVclQueryResultProviderVdcResourcePoolRelationRecord.cpuReservationLimitMhz );
		intUsageMemoryInDataCenter02 = 100 * ( objVclQueryResultProviderVdcResourcePoolRelationRecord.memoryReservationAllocationMB / objVclQueryResultProviderVdcResourcePoolRelationRecord.memoryReservationLimitMB );
	}
}

System.log("==================================================");
System.log("===== DataCenter01 ===== Usage VMs = " + intNumberOfVMsInDataCenter01);
System.log("===== DataCenter01 ===== Usage CPU = " + intUsageCPUinDataCenter01 + "%");
System.log("===== DataCenter01 ===== Usage Memory = " + intUsageMemoryInDataCenter01 + "%");
System.log("==================================================");
System.log("===== DataCenter02 ===== Usage VMs = " + intNumberOfVMsInDataCenter02);
System.log("===== DataCenter02 ===== Usage CPU = " + intUsageCPUinDataCenter02 + "%");
System.log("===== DataCenter02 ===== Usage Memory = " + intUsageMemoryInDataCenter02 + "%");
System.log("==================================================");

if ( intUsageMemoryInDataCenter01 > intUsageMemoryInDataCenter02 )
{
	strDeployDataCentre = "DataCenter02";
	strDeployStorageProfile = "DataCenter01-Storage-Profile-Name";
}
else
{
	strDeployDataCentre = "DataCenter01";
	strDeployStorageProfile = "DataCenter01-Storage-Profile-Name";
}

System.log("===== This vApp will be deployed into the " + strDeployStorageProfile + " storage profile in " + strDeployDataCentre);
System.log("==================================================");

Leave a Reply