Create vCO/vRO scheduled workflow automatically

With vCAC/vRA, when you “destroy” a machine it deletes the vm immediately. There is a setting you can disable that will not delete it but instead will power if off and send it to a default “destroyed” folder in vCenter. However, after that it just sits there until it’s manually deleted.

Due to a business need, I wrote a workflow that automatically deletes the vm after a given number of days. In my example, we delete vm’s 14 days after they’ve been shut off. You can edit the code to make it however many days you’d like though.

First, if you haven’t changed the destroy option yet, check out this blog on how to do that. Once done, go on to the next step. http://dailyhypervisor.com/vcloud-automation-center-vcac-5-1-preventing-vcac-from-deleting-destroyed-virtual-machines/

Once you have disabled the immediate delete feature, you’ll want to do the following in your vCO/vRO console:

In your Decommisson workflow, drag and drop a scriptable task element on the page. You’ll probably want to make it one of the very last elements. Here are the properties:

and the script is:

workflowScheduleDate = new Date();
var day = workflowScheduleDate.getDay();
workflowScheduleDate.setDate(workflowScheduleDate.getDate()+ (day == 0 ? 14 : day == 1 ? 14 : day == 2 ? 14 : day == 3 ? 14 : day == 4 ? 14 : day == 5 ? 14 : day == 6 ? 14 : 0 ));

//This workflow ID is Delete virtual machine — copy and change to suit other workflows
var workflowToLaunch = Server.getWorkflowWithId(“BD80808080808080808080808080808003C180800122528313869552e41805bb1”);
if (workflowToLaunch == null) {
throw “Workflow not found”;
}

var workflowParameters = new Properties();
workflowParameters.put(“vm”,vcVirtualmachine);
var scheduledTask = workflowToLaunch.schedule(workflowParameters,workflowScheduleDate);

System.log(workflowScheduleDate);
System.log(scheduledTask);

The most important part is understanding how the days are calculated. If you see where it says “day ==0 ? 14”, this is javascript saying if day = Sunday, then schedule the deletion 14 days later. You could work the numbers to be business days only. You’d have to take into account the number of days and weekends etc. Our policy is 14 days from the time of shut down – business or not. Nevertheless, if your policy is 30 days, then just replace the 14 with 30.

Update1: If you want to give the user the ability to choose the number of days, all you have to do is create an input parameter type number called something like “numDays” and replace all of the “14’s” with the numDays variable. Then, when the workflow is run, you can put in any numeral and the workflow will be kicked off that number of days later.

Lastly, be sure to change the long character string that is the delete vm workflow. In essence, you could schedule any workflow with this script – you just have to change the workflow ID. To get the workflow ID, click and highlight the given workflow, then hit ctrl-c, then open notepad and ctrl-v. This will paste the workflow ID!

Now when you “destroy” a machine in vRA, it will be shut off, moved to the default folder, and deleted 14 (or however many) days later!

Update2: If for whatever reason you want to kill the deletion of vm, all you have to do is the following: Open vCO/vRO client -> From the main “Run” page and on the home tabe, click on “Tasks scheduled in the sytem.” Find the scheduled task created for your vm and delete it. That’s it!

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Create vCO/vRO scheduled workflow automatically

  1. jfrmilner says:

    hanks for this, just what I was looking for.

    A simplified (IMO) way of calculating the date could be wrote like this:
    workflowScheduleDate = new Date();
    workflowScheduleDate.setDate(workflowScheduleDate.getDate() + 14);

    Good post, keep them coming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s