User Tools

Site Tools


vrealize:general:deletingavcenter

Deleting a vCenter Connection

Deleting a vCenter Connection in vRA is a pain. I had this pleasure, and here is some notes on how to to it: (Hint: you can avoid some troubles if you actually “empty” the vCenter first (remove hosts and all VM's). AND let the vRA Agent update the database)

In my environment there is so many Reservations and vCenters it's hard to remember where everything is connected. You can access the VRA database to help identify these items.

To find out witch Reservations (Disk) a vCenter endpoint is connected to:

Check if there is any Reservations connected to the vCenter you want to delete. (Must be empty, before you continue.)

SELECT * FROM Host JOIN HostReservation AS HR ON Host.HostID=HR.HostID JOIN ManagementEndpoints AS ME ON ME.ManagementEndpointID = Host.ManagementEndpointID WHERE ME.ManagementEndpointName='vCenterName'

Check if there is any Hosts connected to the vCenter you want to delete. (Will not be empty, you will need to remove these..)

SELECT * FROM Host JOIN ManagementEndpoints AS ME ON ME.ManagementEndpointID = Host.ManagementEndpointID WHERE ME.ManagementEndpointName='vCenterName'

You can delete in the vRA database directly (seen several articles about this), but I prefer not to access the database directly, because DB layout might change in the next version of vRA)

Order of actions:

  1. Change the Reservations of all VM's belonging to the Reserverations you want to delete.
  2. VM assigned to a Host belonging to the vCenter.
  3. Delete All orphaned VM's in vRA(LINK) (only in the vRA database, not deleted on the vCenter itself)
  4. Delete All Resource Pools (only in the vRA database, not deleted on the vCenter itself)
  5. Delete All VM's left (Typically templates and VM's not managed in vRA) (only in the vRA database, not deleted on the vCenter itself)
  6. Delete All Hosts belonging to the vCenter connection. (only in the vRA database, not deleted on the vCenter itself)
  7. Stop/Delete the Agents (Windows Service)
  8. Delete Endpoint (vRA)
  9. Delete Endpoint (vRO)

This is a vRO workflow that can do the cleanup(Delete ResourcePools,Delete VM's (only from the vRA database, not actually delete it) and Delete all hosts, these are normally the “trouble” some when you want to delete a vCenter and there is always something stuck.

// World 
var Certificate ="";
var ManagementEndpointID ="";
var CredID ="";
var HostArray = new Array();	// Hosts to Delete
var ResourcePoolArray = new Array();	// Resource Pools to delete
var VMArray = new Array();	// VM's to Delete
 
var DestoryVM = true;		// Set to TRUE to delete VM's left.. 
 
function AnylyzeEndPoint(EndPointName)
{
	var entitySetName = "ManagementEndpoints";
	var host = Server.findAllForType("vCAC:vCACHost")[0];
	var locs = [];  
	var model = "ManagementModelEntities.svc";  
	var property = new Properties();  
	property.put("ManagementEndpointName",EndPointName);
	var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);  
 
	var entityKeyId = "";
	if ( computeResources.length == 0)
	{	
		System.warn ("No ManagmentEndpoint found with Name: "+EndPointName+" in vRA");
		return false;
	}
	entityKeyId = 		computeResources[0].keyString;
	var Props = computeResources[0].getProperties();
	var Links = computeResources[0].getLinks(host);	 // Links = Array of Properties
 
	Certificate = Props.get("Certificate");
	ManagementEndpointID = Props.get("ManagementEndpointID");
	System.log(Props.get("ManagementEndpointDescription)"));
 
	var CredLink = computeResources[0].getLink(host,"Credential");
	var HostLink = computeResources[0].getLink(host,"Hosts");
	CredProp = CredLink[0].getProperties();
	CredID = CredProp.get("CredentialID");
 
 
	System.log("Certificate to Delete: "+Certificate);
	System.log("ManangmentEndpointID "+ManagementEndpointID);
	System.log("Credentials to delete: "+CredID);
 
	// Get a list of all the hosts
	for each (H in HostLink)
	{
		var HostProp = H.getProperties();
		System.log("Gonna delete host: "+HostProp.get("HostName")+ " HostUniqueID: "+HostProp.get("HostUniqueID"));
//		System.log("Gonna delete host: "+HostProp.get("HostID"));
 
		// We will put ALL hosts to an array.
		// However, it's important that Cluster Hosts comes AFTER ESXi hosts
		var IsESX = HostProp.get("HostUuid");
		if (IsESX == null || IsESX == "")
		{
			HostArray.push(HostProp.get("HostID")); // Add To End
		}
		else
		{
			HostArray.unshift(HostProp.get("HostID")); // Add To Start
		}
	}
 
	// Now Check if Hosts contains any VM's
	var AllOK = true;
	for each (H in HostArray)
	{
		if (GetListOfVMsRunningOnAHost(H) == false)
		{
			AllOK = false;
		}	
		if (GetRemainingVMs(H) == false)
		{
			AllOK = false;
		}	
 
	}
	return AllOK;
 
}
// VM's not handled by VRA
function GetRemainingVMs(HostID)
{
	var entitySetName = "VirtualMachines";
	var host = Server.findAllForType("vCAC:vCACHost")[0];
	var locs = [];  
	var model = "ManagementModelEntities.svc";  
	var property = new Properties();  
	property.put("HostID",HostID);
	var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);  
 
	var entityKeyId = "";
	if ( computeResources.length == 0)
	{
		// No VM's left. Great!	
		return true;
	}
	var Result = true;
	for each (VM in	computeResources)
	{
		var Props = VM.getProperties();
		var MachineName = Props.get("VirtualMachineName");
		var Template =  Props.get("IsTemplate");
		var IsManaged = Props.get("IsManaged");
		if (DestoryVM == true)
		{
			System.log("Gonna delete VM: "+MachineName+" With ID: "+Props.get("VirtualMachineID"));
		}
		else
		{
			System.warn("VM left in host("+HostID+") Name: "+MachineName+" IsTemplate: "+Template+" IsManaged: "+IsManaged);
			Result = false;
		}
		VMArray.push(Props.get("VirtualMachineID"));
	}
	return Result;
 
 
}
function GetListOfVMsRunningOnAHost(HostID)
{
	var entitySetName = "Hosts";
	var host = Server.findAllForType("vCAC:vCACHost")[0];
	var locs = [];  
	var model = "ManagementModelEntities.svc";  
	var property = new Properties();  
	property.put("HostID",HostID);
	var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);  
 
	var entityKeyId = "";
	if ( computeResources.length == 0)
	{	
		System.warn("No Host found with HostID: "+HostID+" in vRA");
		return false;
	}
	// entity key is needed for the delete action
	entityKeyId = 		computeResources[0].keyString;
	var Props = computeResources[0].getProperties();
	var Links = computeResources[0].getLinks(host);	 // Links = Array of Properties
	var HostStatsLink = computeResources[0].getLink(host,"HostStats");
	var HostStatsProps = HostStatsLink[0].getProperties();
	var ResourcePoolLink = computeResources[0].getLink(host,"ResourcePools");
 
 
	var Res = HostStatsProps.get("Reservations");
	var Mac = HostStatsProps.get("MachinesTotal");
	var Alloc = HostStatsProps.get("MachinesAllocated");
	if (Res !=0 || Mac != 0 || Alloc != 0)
	{
		System.warn("Host: "+H+" Not Empty.  Reservations: "+Res+". Machines Total: "+Mac+". Allocated Machines: "+Alloc);
		return false;	
	}	
	// Get All Resource Pools, 
	for each (R in ResourcePoolLink)
	{
		RProps = R.getProperties();
		System.log("Going to delete ResourcePool: "+RProps.get("ResourcePoolName")+"("+RProps.get("ResourcePoolUniqueID")+") ID: "+RProps.get("ResourcePoolID"));
		ResourcePoolArray.push(RProps.get("ResourcePoolID"));
	}
	return true;
}
function FindAndDestroyResourcePool(ResourcePoolID)
{
	var entitySetName = "ResourcePools";
	var host = Server.findAllForType("vCAC:vCACHost")[0];
	var locs = [];  
	var model = "ManagementModelEntities.svc";  
	var property = new Properties();  
	property.put("ResourcePoolID",ResourcePoolID);
	var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);  
 
	var entityKeyId = "";
	if ( computeResources.length == 0)
	{	
		System.warn ("No ResourcePool found with ResourcePoolID: "+ResourcePoolID+" in vRA");
		return false;
	}	
	// entity key is needed for the delete action
	entityKeyId = 		computeResources[0].keyString;
	var Props = computeResources[0].getProperties();
	var Links = computeResources[0].getLinks(host);	 // Links = Array of Properties
 
 
	System.log("Removing: "+entityKeyId + " (ResourcePool)");
 
 
	try 
	{
		vCACEntityManager.deleteModelEntityBySerializedKey(host.id , model , entitySetName , entityKeyId, null);
	} 
	catch(e) 
	{
		System.warn(e);
		return false;
	}	
	System.log("Deleted OK.");
	return true;
}
 
function FindAndDestroyVM(VirtualMachineID)
{
	var entitySetName = "VirtualMachines";
	var host = Server.findAllForType("vCAC:vCACHost")[0];
	var locs = [];  
	var model = "ManagementModelEntities.svc";  
	var property = new Properties();  
	property.put("VirtualMachineID",VirtualMachineID);
	var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);  
 
	var entityKeyId = "";
	if ( computeResources.length == 0)
	{	
		System.warn ("No VM found with VirtualMachineID: "+VirtualMachineID+" in vRA");
		return false;
	}	
	// entity key is needed for the delete action
	entityKeyId = 		computeResources[0].keyString;
	var Props = computeResources[0].getProperties();
	var Links = computeResources[0].getLinks(host);	 // Links = Array of Properties
 
 
	System.log("Removing: "+entityKeyId + " (VM)");
 
 
	try 
	{
		vCACEntityManager.deleteModelEntityBySerializedKey(host.id , model , entitySetName , entityKeyId, null);
	} 
	catch(e) 
	{
		System.warn(e);
		return false;
	}	
	System.log("Deleted OK.");
	return true;
}
 
 
function FindAndDestroyHost(HostID)
{
	var entitySetName = "Hosts";
	var host = Server.findAllForType("vCAC:vCACHost")[0];
	var locs = [];  
	var model = "ManagementModelEntities.svc";  
	var property = new Properties();  
	property.put("HostID",HostID);
	var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);  
 
	var entityKeyId = "";
	if ( computeResources.length == 0)
	{	
		System.warn("No Host found with HostID: "+HostID+" in vRA");
		return false;
	}
	// entity key is needed for the delete action
	entityKeyId = 		computeResources[0].keyString;
	var Props = computeResources[0].getProperties();
	var Links = computeResources[0].getLinks(host);	 // Links = Array of Properties
 
	System.log("Removing: "+entityKeyId+" (Host)");
 
 
	try 
	{
		vCACEntityManager.deleteModelEntityBySerializedKey(host.id , model , entitySetName , entityKeyId, null);
	} 
	catch(e) 
	{
		System.warn(e);
		return false;
	}	
	System.log("Removed OK");
	return true;
}
 
if (AnylyzeEndPoint("vCenterName") == true)
{
	System.log("------------ ACTION START -----------");
 
	System.log("Starting ResourcePool Cleanup...");
	for each (R in ResourcePoolArray)
	{
		if (FindAndDestroyResourcePool(R) == false)
		{
			System.warn("***HALT***");
			break;
		}
	}
 
	System.log("Starting VM Cleanup...");
	if (DestoryVM == true)
	{
		for each (VM in VMArray)
		{
			if (FindAndDestroyVM(VM) == false)
			{
				System.warn("***HALT***");
				break;
			}
		}
	}	
 
	System.log("Starting Hosts Cleanup...");
	for each (H in HostArray)
	{
		if (FindAndDestroyHost(H) == false)
		{
			System.warn("***HALT***");
			break;
		}
	}
 
}
else
{
	System.log("..... BREAK ON ERROR .....");
}
vrealize/general/deletingavcenter.txt · Last modified: 2018/12/04 22:49 by admin