VMware Cloud Community
FreddyFredFred
Hot Shot
Hot Shot

Problem finding datastores attached to hosts

Lately I’ve been running into random failures in my deployment workflow when it comes to finding what datastores are attached to what hosts. My workflow selects a host to use, then loops through all datastores, looking to see what hosts are connected and builds a list of available datastores I can pick from.

The relevant section of code is where my problem is:

var allDatastores = System.getModule("com.vmware.library.vc.datastore").getAllDatastores();

var vmStorage = new Array();

var storageName;

if (allDatastores)

{

for (i = 0; i < allDatastores.length; i++)

                {

                                for (j=0; j < allDatastores[i].host.length; j++)

Everything works fine but every now and then, I get the following error: TypeError: Cannot read property "length" from undefined

I believe the error is coming from the host.length call (I’ve adding a little more logging to try and confirm the next time it happens). I added a try/catch block to loop through the same section of the code in the event of a failure but after 10 attempts (with 15 seconds between), it still fails. Another call to the workflow right after works just fine.

Anyone know what might be the cause of if there’s a better way to get a list of datastores and/or datastore clusters attached to a given host? I've seen other random failures (usually with running stuff within the guest OS via vmware tools) and retrying seems to work but in this case, not so much.

Thanks

3 Replies
austinb324
Enthusiast
Enthusiast

Im not sure where the error is in your logic. Maybe you can look at restructuring your method. Below is an excerpt from one of my workflows where I am selecting a datastore for a VM to go onto. The difference is that I am looking at the cluster level. Hope this helps:

var allStorage = vmCluster.datastore;

var vmStorage = new Array();

var storageName;

var byteToGB = 1073741824; //1073741824 = 1024*1024*1024 = 1GB

//Determine size of VM here

var storageSum = calcDisks(currentVM);

System.log("VM Size: " + storageSum);

storageSum += 50; //50GB of headroom on datastore required.

for (ii in allStorage)

{

    storageName = allStorage[ii].name

    if (storageName.search("-DS") >= 0)  //This is specific to my company. I wanted to make sure to weed out any datastores that dont have our std naming scheme

    {

        vmStorage.push(allStorage[ii]);

    }

}

var ii = 0;var vmStorageInfo;

var vmStorageLength = vmStorage.length;

while(vmDatastore == null && ii < vmStorageLength)

{

    //extract storageinfo and compare free space to needed space

    vmStorageInfo = vmStorage[ii].info;

    if ((vmStorageInfo.freeSpace / byteToGB) >= storageSum)

    {

        vmDatastore = vmStorage[ii];

    }

    ii++;

}

System.log("Selected VM Datastore: " + vmDatastore.name);

function calcDisks(VM)

{

  var total = 0;

  var devices = VM.config.hardware.device;

  var h = 0;

 

  for (var i in devices) {

     if (devices[i] instanceof VcVirtualDisk) {

         total += devices[i].capacityInKB / 1024 / 1024;  //Produces VM size in GB

       }

  }

 

  return total;

}

0 Kudos
FreddyFredFred
Hot Shot
Hot Shot

Thanks for the code. It gives me an idea on how to get the datastores in a much more efficient manner.

Interestingly enough, I just did a quick test and run into my problem.

This line of code failed and returned no data.

allDatastores = hostToUse.datastore;

where hostToUse is a hard coded attribute in a test workflow I made. I reran the workflow and it worked the second time around.

Is there any logs on the server which might tell me why the line randomly failed?

0 Kudos
austinb324
Enthusiast
Enthusiast

Honestly, when I'm debugging in orchestrator I just put: System.log("Variable: " + Variable); everywhere that I can so I can watch what the values are as my workflow runs. Even log the things your 'know' are correct. You may find that you did miss something that you have been assuming was right. Happens to me all the time Smiley Happy.