VMware Cloud Community
GarTomlon
Enthusiast
Enthusiast
Jump to solution

Add either a reboot or a power on element to a workflow.

I have a workflow that has a ForEach element that mounts an ISO to CDROM to an array of VMs.    This works great.  I would then like to reboot this array of VMs.  I added another ForEach [reboot vm] element and set the array to be traversed to be my VMs.I set the source parameter to be the VM attribute.  I run it and it attempts to work fine, until it hits a VM that is powered off.  IT then stops and errors out.  You can not reboot a VM that is powered off.  I guess I need to check the power state against the array and if the VM is powered off, I woudl like to power it on; if it is powered on, I would like to reboot it.  How would I go about this?

Thanks 

Reply
0 Kudos
1 Solution

Accepted Solutions
jasnyder
Hot Shot
Hot Shot
Jump to solution

If you duplicate the /Library/vCenter/Virtual Machine management/Power Management/Reboot guest OS workflow and modify it, you can fix this behavior.  In the duplicated workflow, change the script content to the following (assuming you want to start the VM if it's off; if not - remove the "vm.powerOnVM_Task()" call from the else block):

if(vm.runtime.powerState.value == "poweredOn") {

    vm.rebootGuest();

}

else {

    vm.powerOnVM_Task();

}

If you're using that workflow, it calls the Initiate Guest OS Reboot (i.e. graceful shutdown) from vCenter:

pastedImage_2.png

vCenter is the one kicking the error back to vRO because it can't reboot a guest that isn't running.

Note that my script change does not account for the correct action for all possible power states, it just checks to see if the VM was already on before calling rebootGuest, otherwise, it calls powerOnVM_Task();  This is probably safe for most cases.

If you couple this with a wait workflow that reboots, sleeps for a minute or so , and then waits for VMware tools to report good to go, then you know the reboot operation finished.  The reason for sleeping is to give the vCenter time to realize VM Tools isn't running and update that status.  You may need to make it longer in case you think a shutdown on the VM takes a while as is the case for some complex applications to receive kill signals and shutdown gracefully.

The schema for the Reboot Guest OS and Wait workflow looks like this (I previously made this one for other projects; I can share if you like, but it's pretty simple and you can build it yourself in a couple of minutes):

pastedImage_4.png

View solution in original post

Reply
0 Kudos
4 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

How exactly do you reboot a given VM? There is out of the box workflow 'Reset virtual machine and wait' (under Library > vCenter > Virtual Machine management > Power Management) that should work no matter what's the current state of the VM as it uses resetVM_Task() virtual machine API to do the real job.

Reply
0 Kudos
GarTomlon
Enthusiast
Enthusiast
Jump to solution

Is Reset Virtual Machine and Wait as graceful as Reboot Virtual Machine and Wait?  And if a VM is powered off, a reset VM will remained powered off (correct?).  Ultimately, I need the array of VMs either powered on (if they are powered off) or reboot (if they are currently powered on.

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Check resetVM_Task documentation here - https://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc_50%2Fvim.VirtualMa...

The doc said if the VM is powered off, after resetVM_Task it will be powered on.

As for Reboot virtual machine and wait, where is this workflow coming from? I think the workflow coming from vCenter plugin is Reset ..., not Reboot ....

Reply
0 Kudos
jasnyder
Hot Shot
Hot Shot
Jump to solution

If you duplicate the /Library/vCenter/Virtual Machine management/Power Management/Reboot guest OS workflow and modify it, you can fix this behavior.  In the duplicated workflow, change the script content to the following (assuming you want to start the VM if it's off; if not - remove the "vm.powerOnVM_Task()" call from the else block):

if(vm.runtime.powerState.value == "poweredOn") {

    vm.rebootGuest();

}

else {

    vm.powerOnVM_Task();

}

If you're using that workflow, it calls the Initiate Guest OS Reboot (i.e. graceful shutdown) from vCenter:

pastedImage_2.png

vCenter is the one kicking the error back to vRO because it can't reboot a guest that isn't running.

Note that my script change does not account for the correct action for all possible power states, it just checks to see if the VM was already on before calling rebootGuest, otherwise, it calls powerOnVM_Task();  This is probably safe for most cases.

If you couple this with a wait workflow that reboots, sleeps for a minute or so , and then waits for VMware tools to report good to go, then you know the reboot operation finished.  The reason for sleeping is to give the vCenter time to realize VM Tools isn't running and update that status.  You may need to make it longer in case you think a shutdown on the VM takes a while as is the case for some complex applications to receive kill signals and shutdown gracefully.

The schema for the Reboot Guest OS and Wait workflow looks like this (I previously made this one for other projects; I can share if you like, but it's pretty simple and you can build it yourself in a couple of minutes):

pastedImage_4.png

Reply
0 Kudos