VMware Cloud Community
claritas
Contributor
Contributor

Orchestrator customized workflow - delete old snapshots for VM's in specific resource pool

Hi,

Hoping someone may be able to help me out with something that I've been struggling with recently.

We currently have a number of tasks set up on our orchestrator, mainly tasks that create snapshots and remove old one.

The creation of snapshots is specific to a resource pool, so we do not create snapshots for machines that don't require them.

Basically, we are in a situation where we need to retain old snapshots for a couple of VM's, currently the only way we can do this is by pausing the deletion task.. After 30 days this causes issues with creating new snapshots as we have hit the maximum number allowed.

I need assistance editing the task that removes snapshots so that specific machines can be excluded (by moving them into a different resource pool). The problem at the minute is, this task applies to every single VM within VCenter, but i want to be able to make it work in a similar way to the snapshot creation task, where we are able to select a resource pool.

I've been playing around with the workflow and no matter what it always starts to remove snapshots from all VM's instead of just the test one I've moved into a new test resource pool.

Does anyone have any advice?

Thanks in advance!

Reply
0 Kudos
5 Replies
iiliev
VMware Employee
VMware Employee

Hi,

Are you using the out-of-the-box workflow 'Remove old snapshots' available under Library > vCenter > Virtual Machine management > Snapshot ? It works this way; if you take a look at scripting action com.vmware.library.vc.vm.snapshot/getPropertiesForAllSnapshotTask you'll see that it enumerates all VMs that are connected and not a template.

So basically you need to modify this workflow and this action (eg. duplicate them so you will be able to make changes) and for each VM being enumerated, decide whether to skip it or process with its snapshots. To make such decision, you need to check whether the VM' resource pool is your 'special' resource pool.

To get the resource pool a VM belongs to is easy.

var rp = vm.resourcePool;

Then you can compare this resource pool with your 'special' resource pool, ie. by comparing their .id properties.

Note that each resource pool can contain other resource pools. vm.resourcePool will return the direct resource pool where the VM is; to get also its parent resource pools, you should navigate up in object parent hierarchy. Something like the following will enumerate all VM's parent pools:

var rp = vm.resourcePool;

while (rp != null && rp instanceof VcResourcePool) {

  rp = rp.parent;

}

Reply
0 Kudos
claritas
Contributor
Contributor

Hi there,

Yes that's correct, I have duplicated the existing "remove old snapshots" workflow and attempted to make changes so that it uses the resource pool instead of the entire vcenter instance.

When I tried to do it myself before, I copied the initialize script from the "create a snapshot of all virtual machines in a resource pool" which meant that it asks for a resource pool when setting up the job to run, but it then still looks at the entire vcenter.

Are you able to advise where i would insert the lines you have advised me to add? Does it overwrite the getpropertiesforall task?Alternatively, are you able to edit the workflow and send me the edited version of it?

Thanks

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

You need to make your changes in the scripting action com.vmware.library.vc.vm.snapshot/getPropertiesForAllSnapshotTask which is the place where all VMs are processed and snapshot info about each of them is put into a property map data structure.

So you need to make a copy of this action, insert some code in the main for loop to check whether to skip he currently processed VM or not, and then modify your custom copy of Remove old snapshots workflow to use the modified action instead of the original one.

Reply
0 Kudos
claritas
Contributor
Contributor

Hi,

I have added the line var rp = vm.resourcepool; into the action, so it's now as below:

var vms = VcPlugin.getAllVirtualMachines();

var snapshotProperties = new Properties();

var rp = vm.resourcePool;

for(i in vms){

var vm = vms[i];

if(vm.runtime.connectionState.value=="connected" && !vm.config.template){

var vmLayout = vm.layoutEx;

if(vmLayout!=null){

var layoutFiles = vmLayout.file;

var layoutSnapshots = vmLayout.snapshot;

for(l in layoutSnapshots){

var layoutSnapshot = layoutSnapshots[l];

var dataKey = layoutSnapshot.dataKey;

for(m in layoutFiles){

if(layoutFiles[m].key==dataKey){

snapshotProperties.put(layoutFiles[m].name,VcPlugin.convertToVimManagedObject(vm,layoutSnapshot.key));

//System.log("Name : " +layoutFiles[m].name + " - Size : " + layoutFiles[m].size);

}

}

}

}

}

}

return snapshotProperties;

Where do i reference the particular resource pool that i want to use in the task?

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

To reference the resource pool, you can add an input parameter to the action, and then pass the resource pool instance when you call the action.

BTW, you added the code at a wrong location. At that point, vm is not yet defined (it gets defined inside the loop), so your code will not work. It should be something like:

var vms = VcPlugin.getAllVirtualMachines();

var snapshotProperties = new Properties();

for(i in vms){

     var vm = vms[i];

     var rp = vm.resourcePool; // 'vm' is valid at this point

     if(vm.runtime.connectionState.value=="connected" && !vm.config.template){

          // add your code here to check whether the vm belongs or not to your special pool

          // eg. by comparing rp.id and yourpool.id, or more complex check like that in reply# 1

          // and then either continue with the current loop iteration code below

          // or skip this iteration and proceed with the next vm

          var vmLayout = vm.layoutEx;

          if(vmLayout!=null){

               var layoutFiles = vmLayout.file;

               var layoutSnapshots = vmLayout.snapshot;

               for(l in layoutSnapshots){

                    var layoutSnapshot = layoutSnapshots[l];

                    var dataKey = layoutSnapshot.dataKey;

                    for(m in layoutFiles){

                         if(layoutFiles[m].key==dataKey){

                              snapshotProperties.put(layoutFiles[m].name,VcPlugin.convertToVimManagedObject(vm,layoutSnapshot.key));

                              //System.log("Name : " +layoutFiles[m].name + " - Size : " + layoutFiles[m].size);

                         }

                    }

               }

          }

     }

}

return snapshotProperties;

Reply
0 Kudos