Long oldUpdated = vmi.getUpdated(); Date oldUpdateDate = vmi.getUpdateTime(); if (newState.equals(oldState) && newHostId != null && newHostId.equals(oldHostId)) {
private VMSnapshot takeHypervisorSnapshot(VolumeInfo volumeInfo) { VirtualMachine virtualMachine = volumeInfo.getAttachedVM(); if (virtualMachine != null && VirtualMachine.State.Running.equals(virtualMachine.getState())) { String vmSnapshotName = UUID.randomUUID().toString().replace("-", ""); VMSnapshotVO vmSnapshotVO = new VMSnapshotVO(virtualMachine.getAccountId(), virtualMachine.getDomainId(), virtualMachine.getId(), vmSnapshotName, vmSnapshotName, vmSnapshotName, virtualMachine.getServiceOfferingId(), VMSnapshot.Type.Disk, null); VMSnapshot vmSnapshot = vmSnapshotDao.persist(vmSnapshotVO); if (vmSnapshot == null) { throw new CloudRuntimeException("Unable to allocate a VM snapshot object"); } vmSnapshot = vmSnapshotService.createVMSnapshot(virtualMachine.getId(), vmSnapshot.getId(), true); if (vmSnapshot == null) { throw new CloudRuntimeException("Unable to create a hypervisor-side snapshot"); } try { Thread.sleep(60000); } catch (Exception ex) { s_logger.warn(ex.getMessage(), ex); } return vmSnapshot; } // We didn't need to take a hypervisor-side snapshot. Return 'null' to indicate this. return null; }
s_logger.debug("Added host " + groupVM.getHostId() + " to avoid set, since VM " + groupVM.getId() + " is present on the host"); } else if (VirtualMachine.State.Stopped.equals(groupVM.getState()) && groupVM.getLastHostId() != null) { long secondsSinceLastUpdate = (DateUtil.currentGMTTime().getTime() - groupVM.getUpdateTime().getTime()) / 1000; if (secondsSinceLastUpdate < _vmCapacityReleaseInterval) {
HostVO host = _hostDao.findById(hosts[0]); if (currentSize != newSize && host.getHypervisorType() == HypervisorType.XenServer && !userVm.getState().equals(State.Stopped)) { throw new InvalidParameterValueException(errorMsg); if (currentSize != newSize && _volsDao.getHypervisorType(volume.getId()) == HypervisorType.XenServer && !userVm.getState().equals(State.Stopped)) { throw new InvalidParameterValueException(errorMsg);
if (vmInstance == null) { throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId); } else if (!(vmInstance.getState().equals(State.Stopped))) { throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() + " " + " in state " + vmInstance.getState() + "; make sure the virtual machine is stopped");
UserVmVO userVm = _vmDao.findById(volume.getInstanceId()); if (userVm != null) { if (userVm.getState().equals(State.Destroyed) || userVm.getState().equals(State.Expunging)) { throw new CloudRuntimeException("Creating snapshot failed due to volume:" + volume.getId() + " is associated with vm:" + userVm.getInstanceName() + " is in " + userVm.getState().toString() + " state");
if (!(vmInstance.getState().equals(State.Stopped) || vmInstance.getState().equals(State.Running))) { s_logger.warn("Unable to upgrade virtual machine " + vmInstance.toString() + " in state " + vmInstance.getState()); throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() + " " + " in state " + vmInstance.getState() +
if (!vmInstance.getState().equals(State.Stopped)) { s_logger.warn("Unable to update affinity groups of the virtual machine " + vmInstance.toString() + " in state " + vmInstance.getState()); throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " +
if (!router.getState().equals(VirtualMachine.State.Stopped)) { s_logger.warn("Unable to upgrade router " + router.toString() + " in state " + router.getState()); throw new InvalidParameterValueException("Unable to upgrade router " + router.toString() + " in state " + router.getState()
if (!tmpVm.getState().equals(State.Running)) {
@Override @ActionEvent(eventType = EventTypes.EVENT_VM_UPGRADE, eventDescription = "Upgrading VM", async = true) public UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException { Long vmId = cmd.getId(); Long newServiceOfferingId = cmd.getServiceOfferingId(); VirtualMachine vm = (VirtualMachine) this._entityMgr.findById(VirtualMachine.class, vmId); if (vm == null) { throw new InvalidParameterValueException("Unable to find VM's UUID"); } CallContext.current().setEventDetails("Vm Id: " + vm.getUuid()); boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmd.getDetails()); if (result) { UserVmVO vmInstance = _vmDao.findById(vmId); if (vmInstance.getState().equals(State.Stopped)) { // Generate usage event for VM upgrade generateUsageEvent(vmInstance, vmInstance.isDisplayVm(), EventTypes.EVENT_VM_UPGRADE); } if (vmInstance.getState().equals(State.Running)) { // Generate usage event for Dynamic scaling of VM generateUsageEvent( vmInstance, vmInstance.isDisplayVm(), EventTypes.EVENT_VM_DYNAMIC_SCALE); } return vmInstance; } else { throw new CloudRuntimeException("Failed to scale the VM"); } }
private void updateVmStateForFailedVmCreation(Long vmId, Long hostId) { UserVmVO vm = _vmDao.findById(vmId); if (vm != null) { if (vm.getState().equals(State.Stopped)) { s_logger.debug("Destroying vm " + vm + " as it failed to create on Host with Id:" + hostId); try { _itMgr.stateTransitTo(vm, VirtualMachine.Event.OperationFailedToError, null); } catch (NoTransitionException e1) { s_logger.warn(e1.getMessage()); } // destroy associated volumes for vm in error state // get all volumes in non destroyed state List<VolumeVO> volumesForThisVm = _volsDao.findUsableVolumesForInstance(vm.getId()); for (VolumeVO volume : volumesForThisVm) { if (volume.getState() != Volume.State.Destroy) { volumeMgr.destroyVolume(volume); } } String msg = "Failed to deploy Vm with Id: " + vmId + ", on Host with Id: " + hostId; _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_USERVM, vm.getDataCenterId(), vm.getPodIdToDeployIn(), msg, msg); // Get serviceOffering for Virtual Machine ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId()); // Update Resource Count for the given account resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize())); } } }
public boolean connectPhysicalDisksViaVmSpec(VirtualMachineTO vmSpec) { boolean result = false; final String vmName = vmSpec.getName(); List<DiskTO> disks = Arrays.asList(vmSpec.getDisks()); for (DiskTO disk : disks) { if (disk.getType() == Volume.Type.ISO) { result = true; continue; } VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); PrimaryDataStoreTO store = (PrimaryDataStoreTO)vol.getDataStore(); if (!store.isManaged() && VirtualMachine.State.Migrating.equals(vmSpec.getState())) { result = true; continue; } KVMStoragePool pool = getStoragePool(store.getPoolType(), store.getUuid()); StorageAdaptor adaptor = getStorageAdaptor(pool.getType()); result = adaptor.connectPhysicalDisk(vol.getPath(), pool, disk.getDetails()); if (!result) { s_logger.error("Failed to connect disks via vm spec for vm: " + vmName + " volume:" + vol.toString()); return result; } } return result; }
@Override public boolean postStateTransitionEvent(StateMachine2.Transition<VirtualMachine.State, VirtualMachine.Event> transition, VirtualMachine vm, boolean status, Object opaque) { if (transition.getToState().equals(VirtualMachine.State.Expunging) && transition.getEvent().equals(VirtualMachine.Event.ExpungeOperation)) { Nic nic = _networkModel.getDefaultNic(vm.getId()); if (nic == null) { return true; } try { final Network network = _networkMgr.getNetwork(nic.getNetworkId()); final UserDataServiceProvider userDataUpdateProvider = _networkModel.getUserDataUpdateProvider(network); final Provider provider = userDataUpdateProvider.getProvider(); if (provider.equals(Provider.ConfigDrive)) { try { return deleteConfigDriveIso(vm); } catch (ResourceUnavailableException e) { LOG.error("Failed to delete config drive due to: ", e); return false; } } } catch (UnsupportedServiceException usse) {} } return true; }
@Override public boolean volumeInactive(Volume volume) { Long vmId = volume.getInstanceId(); if (vmId == null) { return true; } UserVm vm = _entityMgr.findById(UserVm.class, vmId); if (vm == null) { return true; } State state = vm.getState(); if (state.equals(State.Stopped) || state.equals(State.Destroyed)) { return true; } return false; }
@Override @ActionEvent(eventType = EventTypes.EVENT_VM_UPGRADE, eventDescription = "Upgrading system VM", async = true) public VirtualMachine upgradeSystemVM(final ScaleSystemVMCmd cmd) throws ResourceUnavailableException, ManagementServerException, VirtualMachineMigrationException, ConcurrentOperationException { final VMInstanceVO vmInstance = _vmInstanceDao.findById(cmd.getId()); if (vmInstance.getHypervisorType() == HypervisorType.XenServer && vmInstance.getState().equals(State.Running)) { throw new InvalidParameterValueException("Dynamic Scaling operation is not permitted for this hypervisor on system vm"); } final boolean result = _userVmMgr.upgradeVirtualMachine(cmd.getId(), cmd.getServiceOfferingId(), cmd.getDetails()); if (result) { final VirtualMachine vm = _vmInstanceDao.findById(cmd.getId()); return vm; } else { throw new CloudRuntimeException("Failed to upgrade System VM"); } }
@Override public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId, Map<String, String> customParameters) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException { // Verify input parameters VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId); if (vmInstance != null) { if (vmInstance.getState().equals(State.Stopped)) { upgradeStoppedVirtualMachine(vmId, newServiceOfferingId, customParameters); return true; } if (vmInstance.getState().equals(State.Running)) { return upgradeRunningVirtualMachine(vmId, newServiceOfferingId, customParameters); } } return false; }
@Override public boolean savePassword(final Network network, final NicProfile nic, final VirtualMachineProfile vm) throws ResourceUnavailableException { // savePassword is called by resetPasswordForVirtualMachine API which requires VM to be shutdown // Upper layers should save password in db, we do not need to update/create config drive iso at this point // Config drive will be created with updated password when VM starts in future if (vm != null && vm.getVirtualMachine().getState().equals(VirtualMachine.State.Running)) { throw new CloudRuntimeException("VM should to stopped to reset password"); } final boolean canHandle = canHandle(network.getTrafficType()); if (canHandle) { storePasswordInVmDetails(vm); } return canHandle; }
@Override public boolean saveSSHKey(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final String sshPublicKey) throws ResourceUnavailableException { // saveSSHKey is called by resetSSHKeyForVirtualMachine API which requires VM to be shutdown // Upper layers should save ssh public key in db, we do not need to update/create config drive iso at this point // Config drive will be created with updated password when VM starts in future if (vm != null && vm.getVirtualMachine().getState().equals(VirtualMachine.State.Running)) { throw new CloudRuntimeException("VM should to stopped to reset password"); } final boolean canHandle = canHandle(network.getTrafficType()); if (canHandle) { storePasswordInVmDetails(vm); } return canHandle; }
@Override public boolean saveUserData(final Network network, final NicProfile nic, final VirtualMachineProfile vm) throws ResourceUnavailableException { // saveUserData is called by updateVirtualMachine API which requires VM to be shutdown // Upper layers should save userdata in db, we do not need to update/create config drive iso at this point // Config drive will be created with updated password when VM starts in future if (vm != null && vm.getVirtualMachine().getState().equals(VirtualMachine.State.Running)) { throw new CloudRuntimeException("VM should to stopped to reset password"); } return canHandle(network.getTrafficType()); }