Fix pr44962 add check before hot add cpu or memory (#48289)

This commit is contained in:
Diane Wang 2018-11-21 09:27:35 -08:00 committed by ansibot
parent 91150423f4
commit 9b44109ba3

View file

@ -919,6 +919,14 @@ class PyVmomiHelper(PyVmomi):
num_cpus = int(self.params['hardware']['num_cpus']) num_cpus = int(self.params['hardware']['num_cpus'])
except ValueError as e: except ValueError as e:
self.module.fail_json(msg="hardware.num_cpus attribute should be an integer value.") self.module.fail_json(msg="hardware.num_cpus attribute should be an integer value.")
# check VM power state and cpu hot-add/hot-remove state before re-config VM
if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
if not vm_obj.config.cpuHotRemoveEnabled and num_cpus < vm_obj.config.hardware.numCPU:
self.module.fail_json(msg="Configured cpu number is less than the cpu number of the VM, "
"cpuHotRemove is not enabled")
if not vm_obj.config.cpuHotAddEnabled and num_cpus > vm_obj.config.hardware.numCPU:
self.module.fail_json(msg="Configured cpu number is more than the cpu number of the VM, "
"cpuHotAdd is not enabled")
if 'num_cpu_cores_per_socket' in self.params['hardware']: if 'num_cpu_cores_per_socket' in self.params['hardware']:
try: try:
@ -929,11 +937,9 @@ class PyVmomiHelper(PyVmomi):
if num_cpus % num_cpu_cores_per_socket != 0: if num_cpus % num_cpu_cores_per_socket != 0:
self.module.fail_json(msg="hardware.num_cpus attribute should be a multiple " self.module.fail_json(msg="hardware.num_cpus attribute should be a multiple "
"of hardware.num_cpu_cores_per_socket") "of hardware.num_cpu_cores_per_socket")
self.configspec.numCoresPerSocket = num_cpu_cores_per_socket self.configspec.numCoresPerSocket = num_cpu_cores_per_socket
if vm_obj is None or self.configspec.numCoresPerSocket != vm_obj.config.hardware.numCoresPerSocket: if vm_obj is None or self.configspec.numCoresPerSocket != vm_obj.config.hardware.numCoresPerSocket:
self.change_detected = True self.change_detected = True
self.configspec.numCPUs = num_cpus self.configspec.numCPUs = num_cpus
if vm_obj is None or self.configspec.numCPUs != vm_obj.config.hardware.numCPU: if vm_obj is None or self.configspec.numCPUs != vm_obj.config.hardware.numCPU:
self.change_detected = True self.change_detected = True
@ -943,11 +949,19 @@ class PyVmomiHelper(PyVmomi):
if 'memory_mb' in self.params['hardware']: if 'memory_mb' in self.params['hardware']:
try: try:
self.configspec.memoryMB = int(self.params['hardware']['memory_mb']) memory_mb = int(self.params['hardware']['memory_mb'])
except ValueError: except ValueError:
self.module.fail_json(msg="Failed to parse hardware.memory_mb value." self.module.fail_json(msg="Failed to parse hardware.memory_mb value."
" Please refer the documentation and provide" " Please refer the documentation and provide"
" correct value.") " correct value.")
# check VM power state and memory hotadd state before re-config VM
if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
if vm_obj.config.memoryHotAddEnabled and memory_mb < vm_obj.config.hardware.memoryMB:
self.module.fail_json(msg="Configured memory is less than memory size of the VM, "
"operation is not supported")
elif not vm_obj.config.memoryHotAddEnabled and memory_mb != vm_obj.config.hardware.memoryMB:
self.module.fail_json(msg="memoryHotAdd is not enabled")
self.configspec.memoryMB = memory_mb
if vm_obj is None or self.configspec.memoryMB != vm_obj.config.hardware.memoryMB: if vm_obj is None or self.configspec.memoryMB != vm_obj.config.hardware.memoryMB:
self.change_detected = True self.change_detected = True
# memory_mb is mandatory for VM creation # memory_mb is mandatory for VM creation
@ -955,16 +969,25 @@ class PyVmomiHelper(PyVmomi):
self.module.fail_json(msg="hardware.memory_mb attribute is mandatory for VM creation") self.module.fail_json(msg="hardware.memory_mb attribute is mandatory for VM creation")
if 'hotadd_memory' in self.params['hardware']: if 'hotadd_memory' in self.params['hardware']:
if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn and \
vm_obj.config.memoryHotAddEnabled != bool(self.params['hardware']['hotadd_memory']):
self.module.fail_json(msg="Configure hotadd memory operation is not supported when VM is power on")
self.configspec.memoryHotAddEnabled = bool(self.params['hardware']['hotadd_memory']) self.configspec.memoryHotAddEnabled = bool(self.params['hardware']['hotadd_memory'])
if vm_obj is None or self.configspec.memoryHotAddEnabled != vm_obj.config.memoryHotAddEnabled: if vm_obj is None or self.configspec.memoryHotAddEnabled != vm_obj.config.memoryHotAddEnabled:
self.change_detected = True self.change_detected = True
if 'hotadd_cpu' in self.params['hardware']: if 'hotadd_cpu' in self.params['hardware']:
if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn and \
vm_obj.config.cpuHotAddEnabled != bool(self.params['hardware']['hotadd_cpu']):
self.module.fail_json(msg="Configure hotadd cpu operation is not supported when VM is power on")
self.configspec.cpuHotAddEnabled = bool(self.params['hardware']['hotadd_cpu']) self.configspec.cpuHotAddEnabled = bool(self.params['hardware']['hotadd_cpu'])
if vm_obj is None or self.configspec.cpuHotAddEnabled != vm_obj.config.cpuHotAddEnabled: if vm_obj is None or self.configspec.cpuHotAddEnabled != vm_obj.config.cpuHotAddEnabled:
self.change_detected = True self.change_detected = True
if 'hotremove_cpu' in self.params['hardware']: if 'hotremove_cpu' in self.params['hardware']:
if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn and \
vm_obj.config.cpuHotRemoveEnabled != bool(self.params['hardware']['hotremove_cpu']):
self.module.fail_json(msg="Configure hotremove cpu operation is not supported when VM is power on")
self.configspec.cpuHotRemoveEnabled = bool(self.params['hardware']['hotremove_cpu']) self.configspec.cpuHotRemoveEnabled = bool(self.params['hardware']['hotremove_cpu'])
if vm_obj is None or self.configspec.cpuHotRemoveEnabled != vm_obj.config.cpuHotRemoveEnabled: if vm_obj is None or self.configspec.cpuHotRemoveEnabled != vm_obj.config.cpuHotRemoveEnabled:
self.change_detected = True self.change_detected = True
@ -989,12 +1012,14 @@ class PyVmomiHelper(PyVmomi):
self.change_detected = True self.change_detected = True
if 'boot_firmware' in self.params['hardware']: if 'boot_firmware' in self.params['hardware']:
# boot firmware re-config can cause boot issue
if vm_obj is not None:
return
boot_firmware = self.params['hardware']['boot_firmware'].lower() boot_firmware = self.params['hardware']['boot_firmware'].lower()
if boot_firmware not in ('bios', 'efi'): if boot_firmware not in ('bios', 'efi'):
self.module.fail_json(msg="hardware.boot_firmware value is invalid [%s]." self.module.fail_json(msg="hardware.boot_firmware value is invalid [%s]."
" Need one of ['bios', 'efi']." % boot_firmware) " Need one of ['bios', 'efi']." % boot_firmware)
self.configspec.firmware = boot_firmware self.configspec.firmware = boot_firmware
if vm_obj is None or self.configspec.firmware != vm_obj.config.firmware:
self.change_detected = True self.change_detected = True
def configure_cdrom(self, vm_obj): def configure_cdrom(self, vm_obj):