Merge pull request #57 from Microsoft/users/subraman/toolsversionfix

Using ToolVersionStatus and ToolsRunningStatus for finding out VM pow…
This commit is contained in:
Subrahmanyam Mandavilli 2016-05-04 10:12:40 +05:30
Родитель 403ef9fe47 ed08530d49
Коммит beef5601a6
5 изменённых файлов: 42 добавлений и 28 удалений

Просмотреть файл

@ -59,11 +59,12 @@ public interface IVMWare {
/**
* @param vmName name of the virtual machine
* @param defaultValue this value is returned when not able to determine vm power on status
* @param connData connection information for vCenter
* @return true if powered on, otherwise false
* @throws Exception
*/
boolean isVMPoweredOn(String vmName, ConnectionData connData) throws Exception;
boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception;
/**
* @param templateName name of the virtual machine template to be cloned

Просмотреть файл

@ -21,7 +21,8 @@ public class VMWareImpl implements IVMWare {
private final String SNAPSHOT = "snapshot";
private final String CONFIG = "config";
private final String NAME = "name";
private final String GUEST_TOOLS_STATUS = "guest.toolsStatus";
private final String GUEST_TOOLS_RUNNING_STATUS = "guest.toolsRunningStatus";
private final String GUEST_TOOLS_VERSION_STATUS = "guest.toolsVersionStatus";
private final String GUEST_HEART_BEAT_STATUS = "guestHeartbeatStatus";
private final String INFO_STATE = "info.state";
@ -133,7 +134,7 @@ public class VMWareImpl implements IVMWare {
public void powerOnVM(String vmName, ConnectionData connData) throws Exception {
connect(connData);
if (!isVMPoweredOn(vmName, connData)) {
if (!isVMPoweredOn(vmName, false, connData)) {
ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false);
ManagedObjectReference task = vimPort.powerOnVMTask(vmMor, null);
@ -142,8 +143,8 @@ public class VMWareImpl implements IVMWare {
String.format("Failed to power on virtual machine [ %s ].", vmName));
}
System.out.println(String.format("Waiting for virtual machine [ %s ] to start.", vmName));
waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_STATUS}, new String[]{GUEST_TOOLS_STATUS},
new Object[][]{new Object[]{VirtualMachineToolsStatus.TOOLS_OK, VirtualMachineToolsStatus.TOOLS_OLD}},
waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_RUNNING_STATUS}, new String[]{GUEST_TOOLS_RUNNING_STATUS},
new Object[][]{new Object[]{VirtualMachineToolsRunningStatus.GUEST_TOOLS_RUNNING.value()}},
Constants.START_STOP_MAX_WAIT_IN_MINUTES);
waitOnMorProperties(vmMor, new String[]{GUEST_HEART_BEAT_STATUS}, new String[]{GUEST_HEART_BEAT_STATUS},
new Object[][]{new Object[]{ManagedEntityStatus.GREEN}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES);
@ -155,12 +156,13 @@ public class VMWareImpl implements IVMWare {
public void shutdownVM(String vmName, ConnectionData connData) throws Exception {
connect(connData);
if (isVMPoweredOn(vmName, connData)) {
if (isVMPoweredOn(vmName, true, connData)) {
ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false);
vimPort.shutdownGuest(vmMor);
System.out.println(String.format("Waiting for virtual machine [ %s ] to shutdown.", vmName));
waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_STATUS}, new String[]{GUEST_TOOLS_STATUS},
new Object[][]{new Object[]{VirtualMachineToolsStatus.TOOLS_NOT_RUNNING}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES);
waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_RUNNING_STATUS}, new String[]{GUEST_TOOLS_RUNNING_STATUS},
new Object[][]{new Object[]{VirtualMachineToolsRunningStatus.GUEST_TOOLS_NOT_RUNNING.value()}},
Constants.START_STOP_MAX_WAIT_IN_MINUTES);
waitOnMorProperties(vmMor, new String[]{GUEST_HEART_BEAT_STATUS}, new String[]{GUEST_HEART_BEAT_STATUS},
new Object[][]{new Object[]{ManagedEntityStatus.GRAY}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES);
System.out.println(String.format("Successfully shutdowned the virtual machine [ %s ].", vmName));
@ -170,7 +172,7 @@ public class VMWareImpl implements IVMWare {
public void powerOffVM(String vmName, ConnectionData connData) throws Exception {
connect(connData);
if (isVMPoweredOn(vmName, connData)) {
if (isVMPoweredOn(vmName, true, connData)) {
ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false);
ManagedObjectReference powerOffTask = vimPort.powerOffVMTask(vmMor);
System.out.println(String.format("Waiting for virtual machine [ %s ] to power off.", vmName));
@ -179,6 +181,7 @@ public class VMWareImpl implements IVMWare {
String.format("Failed to power off virtual machine [ %s ].", vmName));
}
System.out.println(String.format("Successfully powered off the virtual machine [ %s ].", vmName));
return;
}
System.out.println(String.format("Virtual machine [ %s ] is already powered off.", vmName));
}
@ -228,12 +231,22 @@ public class VMWareImpl implements IVMWare {
return true;
}
public boolean isVMPoweredOn(String vmName, ConnectionData connData) throws Exception {
public boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception {
connect(connData);
System.out.println("Checking virtual machine [ " + vmName + " ] power status.");
ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false);
VirtualMachineToolsStatus vmToolsStatus = (VirtualMachineToolsStatus) getMorProperties(vmMor, new String[]{GUEST_TOOLS_STATUS}).get(GUEST_TOOLS_STATUS);
return (vmToolsStatus != null) && (vmToolsStatus == VirtualMachineToolsStatus.TOOLS_OK);
VirtualMachineToolsVersionStatus toolsVersionStatus = VirtualMachineToolsVersionStatus.fromValue(
(String) getMorProperties(vmMor, new String[]{GUEST_TOOLS_VERSION_STATUS}).get(GUEST_TOOLS_VERSION_STATUS));
if (toolsVersionStatus.equals(VirtualMachineToolsVersionStatus.GUEST_TOOLS_NOT_INSTALLED)) {
System.out.println("VMware tools are not installed, proceeding without checking poweron status");
return defaultValue;
}
VirtualMachineToolsRunningStatus toolsRunningStatus = VirtualMachineToolsRunningStatus.fromValue(
(String) getMorProperties(vmMor, new String[]{GUEST_TOOLS_RUNNING_STATUS}).get(GUEST_TOOLS_RUNNING_STATUS));
return !(toolsRunningStatus.equals(VirtualMachineToolsRunningStatus.GUEST_TOOLS_NOT_RUNNING));
}
private ManagedObjectReference getMorByName(ManagedObjectReference rootContainer, String mobName, String morefType,

Просмотреть файл

@ -155,7 +155,7 @@ public class InMemoryVMWareImpl implements IVMWare {
return vmSnapshotInfo.containsKey(vmName);
}
public boolean isVMPoweredOn(String vmName, ConnectionData connData) throws Exception {
public boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception {
vmName = vmName.toLowerCase();
if (vmStateInformation.containsKey(vmName)) {
return vmStateInformation.get(vmName).equals(started);

Просмотреть файл

@ -258,9 +258,9 @@ public abstract class VMWarePlatformTests {
connData.setTargetDC(targetDC);
vmWareImpl.powerOnVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true);
vmWareImpl.shutdownVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false);
}
@Test
@ -270,14 +270,14 @@ public abstract class VMWarePlatformTests {
connData.setTargetDC(targetDC);
vmWareImpl.powerOnVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true);
vmWareImpl.powerOnVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true);
vmWareImpl.shutdownVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false);
vmWareImpl.shutdownVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false);
}
@Test
@ -287,10 +287,10 @@ public abstract class VMWarePlatformTests {
connData.setTargetDC(targetDC);
vmWareImpl.powerOnVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true);
vmWareImpl.powerOffVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false);
}
// Common for restore/delete snapshot operations

Просмотреть файл

@ -68,20 +68,20 @@ public class VmOpsToolUnitTests {
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn("vm1", true, connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn("vm2", true, connData)).isEqualTo(true);
cmdArgs = getCmdArgs("vm1", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION);
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn("vm1", false, connData)).isEqualTo(false);
cmdArgs = getCmdArgs("vm2", Constants.POWER_OPS, Constants.POWER_OFF_VM_ACTION);
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn("vm2", false, connData)).isEqualTo(false);
}
@Test
@ -97,7 +97,7 @@ public class VmOpsToolUnitTests {
}
assertThat(exp).isNotNull();
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn("vm1", true, connData)).isEqualTo(true);
cmdArgs = getCmdArgs("vm1, VmThatFailsInShutdown", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION);
@ -109,7 +109,7 @@ public class VmOpsToolUnitTests {
}
assertThat(exp).isNotNull();
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn("vm1", false, connData)).isEqualTo(false);
cmdArgs = getCmdArgs("vm2, VmThatFailsInPowerOff", Constants.POWER_OPS, Constants.POWER_OFF_VM_ACTION);
@ -121,7 +121,7 @@ public class VmOpsToolUnitTests {
}
assertThat(exp).isNotNull();
assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(false);
assertThat(vmWareImpl.isVMPoweredOn("vm2", false, connData)).isEqualTo(false);
}
@Test