@Override public VirtualMachine.Type findSystemVMTypeById(final long instanceId) { final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(instanceId, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm == null) { final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a system vm of specified instanceId"); ex.addProxyObject(String.valueOf(instanceId), "instanceId"); throw ex; } return systemVm.getType(); }
protected void handleVmStopped(VMInstanceVO vm) { if (vm.getType() != VirtualMachine.Type.User || !isVmSecurityGroupEnabled(vm.getId())) { return; } List<Long> affectedVms = getAffectedVmsForVmStop(vm); scheduleRulesetUpdateToHosts(affectedVms, true, null); }
public void updateMissingRootDiskController(final VMInstanceVO vm, final String rootVolChainInfo) { if (vm == null || !VirtualMachine.Type.User.equals(vm.getType()) || Strings.isNullOrEmpty(rootVolChainInfo)) { return; } String rootDiskController = null; try { final VirtualMachineDiskInfo infoInChain = _gson.fromJson(rootVolChainInfo, VirtualMachineDiskInfo.class); if (infoInChain != null) { rootDiskController = infoInChain.getControllerFromDeviceBusName(); } final UserVmVO userVmVo = _userVmDao.findById(vm.getId()); if ((rootDiskController != null) && (!rootDiskController.isEmpty())) { _userVmDao.loadDetails(userVmVo); _userVmMgr.persistDeviceBusInfo(userVmVo, rootDiskController); } } catch (JsonParseException e) { s_logger.debug("Error parsing chain info json: " + e.getMessage()); } }
@Override public void scheduleStop(VMInstanceVO vm, long hostId, WorkType type) { assert (type == WorkType.CheckStop || type == WorkType.ForceStop || type == WorkType.Stop); if (_haDao.hasBeenScheduled(vm.getId(), type)) { s_logger.info("There's already a job scheduled to stop " + vm); return; } HaWorkVO work = new HaWorkVO(vm.getId(), vm.getType(), type, Step.Scheduled, hostId, vm.getState(), 0, vm.getUpdated()); _haDao.persist(work); if (s_logger.isDebugEnabled()) { s_logger.debug("Scheduled " + work); } wakeupWorkers(); }
public void handleVmStarted(VMInstanceVO vm) { if (vm.getType() != VirtualMachine.Type.User || !isVmSecurityGroupEnabled(vm.getId())) { return; } List<Long> affectedVms = getAffectedVmsForVmStart(vm); scheduleRulesetUpdateToHosts(affectedVms, true, null); }
@Override public void scheduleDestroy(VMInstanceVO vm, long hostId) { final HaWorkVO work = new HaWorkVO(vm.getId(), vm.getType(), WorkType.Destroy, Step.Scheduled, hostId, vm.getState(), 0, vm.getUpdated()); _haDao.persist(work); if (s_logger.isDebugEnabled()) { s_logger.debug("Scheduled " + work.toString()); } wakeupWorkers(); }
protected void handleVmMigrated(VMInstanceVO vm) { if (!isVmSecurityGroupEnabled(vm.getId())) { return; } if (vm.getType() != VirtualMachine.Type.User) { Commands cmds = null; NetworkRulesSystemVmCommand nrc = new NetworkRulesSystemVmCommand(vm.getInstanceName(), vm.getType()); cmds = new Commands(nrc); try { _agentMgr.send(vm.getHostId(), cmds); } catch (AgentUnavailableException e) { s_logger.debug(e.toString()); } catch (OperationTimedoutException e) { s_logger.debug(e.toString()); } } else { List<Long> affectedVms = new ArrayList<Long>(); affectedVms.add(vm.getId()); scheduleRulesetUpdateToHosts(affectedVms, true, null); } }
if(vm != null && vm.getType() == VirtualMachine.Type.User) { updateVmMetaData(vm.getId(), platform);
private boolean skipCalculation(VMInstanceVO vm) { if (vm.getState() == State.Expunging) { if (s_logger.isDebugEnabled()) { s_logger.debug("Skip counting capacity for Expunging VM : " + vm.getInstanceName()); } return true; } if (vm.getState() == State.Destroyed && vm.getType() != VirtualMachine.Type.User) { return true; } if (vm.getState() == State.Stopped || vm.getState() == State.Destroyed) { // for Stopped/Destroyed VMs, we will skip counting it if it hasn't // been used for a while int secondsToSkipVMs = _secondsToSkipStoppedVMs; if (vm.getState() == State.Destroyed) { secondsToSkipVMs = _secondsToSkipDestroyedVMs; } long millisecondsSinceLastUpdate = DateUtil.currentGMTTime().getTime() - vm.getUpdateTime().getTime(); if (millisecondsSinceLastUpdate > secondsToSkipVMs * 1000L) { if (s_logger.isDebugEnabled()) { s_logger.debug("Skip counting " + vm.getState().toString() + " vm " + vm.getInstanceName() + " in capacity allocation as it has been " + vm.getState().toString().toLowerCase() + " for " + millisecondsSinceLastUpdate / 60000 + " minutes"); } return true; } } return false; }
@Override @ActionEvent(eventType = "", eventDescription = "", async = true) public VirtualMachine startSystemVM(final long vmId) { final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(vmId, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm == null) { final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a system vm with specified vmId"); ex.addProxyObject(String.valueOf(vmId), "vmId"); throw ex; } if (systemVm.getType() == VirtualMachine.Type.ConsoleProxy) { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_PROXY_START, "starting console proxy Vm"); return startConsoleProxy(vmId); } else if (systemVm.getType() == VirtualMachine.Type.SecondaryStorageVm) { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_SSVM_START, "starting secondary storage Vm"); return startSecondaryStorageVm(vmId); } else { final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a system vm with specified vmId"); ex.addProxyObject(systemVm.getUuid(), "vmId"); throw ex; } }
@Override @DB public boolean remove(Long id) { TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VMInstanceVO vm = findById(id); if (vm != null && vm.getType() == Type.User) { _tagsDao.removeByIdAndType(id, ResourceObjectType.UserVm); } boolean result = super.remove(id); txn.commit(); return result; }
@Override @ActionEvent(eventType = "", eventDescription = "", async = true) public VMInstanceVO stopSystemVM(final StopSystemVmCmd cmd) throws ResourceUnavailableException, ConcurrentOperationException { final Long id = cmd.getId(); // verify parameters final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(id, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm == null) { final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a system vm with specified vmId"); ex.addProxyObject(id.toString(), "vmId"); throw ex; } try { if (systemVm.getType() == VirtualMachine.Type.ConsoleProxy) { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_PROXY_STOP, "stopping console proxy Vm"); return stopConsoleProxy(systemVm, cmd.isForced()); } else if (systemVm.getType() == VirtualMachine.Type.SecondaryStorageVm) { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_SSVM_STOP, "stopping secondary storage Vm"); return stopSecondaryStorageVm(systemVm, cmd.isForced()); } return null; } catch (final OperationTimedoutException e) { throw new CloudRuntimeException("Unable to stop " + systemVm, e); } }
@Override public boolean scheduleMigration(final VMInstanceVO vm) { if (vm.getHostId() != null) { final HaWorkVO work = new HaWorkVO(vm.getId(), vm.getType(), WorkType.Migration, Step.Scheduled, vm.getHostId(), vm.getState(), 0, vm.getUpdated()); _haDao.persist(work); wakeupWorkers(); } return true; }
@Override public VMInstanceVO rebootSystemVM(final RebootSystemVmCmd cmd) { final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(cmd.getId(), VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm == null) { final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a system vm with specified vmId"); ex.addProxyObject(cmd.getId().toString(), "vmId"); throw ex; } if (systemVm.getType().equals(VirtualMachine.Type.ConsoleProxy)) { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_PROXY_REBOOT, "rebooting console proxy Vm"); return rebootConsoleProxy(cmd.getId()); } else { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_SSVM_REBOOT, "rebooting secondary storage Vm"); return rebootSecondaryStorageVm(cmd.getId()); } }
@Override @ActionEvent(eventType = "", eventDescription = "", async = true) public VMInstanceVO destroySystemVM(final DestroySystemVmCmd cmd) { final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(cmd.getId(), VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm == null) { final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a system vm with specified vmId"); ex.addProxyObject(cmd.getId().toString(), "vmId"); throw ex; } if (systemVm.getType().equals(VirtualMachine.Type.ConsoleProxy)) { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_PROXY_DESTROY, "destroying console proxy Vm"); return destroyConsoleProxy(cmd.getId()); } else { ActionEventUtils.startNestedActionEvent(EventTypes.EVENT_SSVM_DESTROY, "destroying secondary storage Vm"); return destroySecondaryStorageVm(cmd.getId()); } }
if (vm.getType() != VirtualMachine.Type.User) {
vm.getType(), vm.getId(), VmWorkStop.class.getName());
@Override public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, Volume vol, boolean status, Object opaque) { pubishOnEventBus(transition.getEvent().name(), "postStateTransitionEvent", vol, transition.getCurrentState(), transition.getToState()); if(transition.isImpacted(StateMachine2.Transition.Impact.USAGE)) { Long instanceId = vol.getInstanceId(); VMInstanceVO vmInstanceVO = null; if(instanceId != null) { vmInstanceVO = _vmInstanceDao.findById(instanceId); } if(instanceId == null || vmInstanceVO.getType() == VirtualMachine.Type.User) { if (transition.getToState() == State.Ready) { if (transition.getCurrentState() == State.Resizing) { // Log usage event for volumes belonging user VM's only UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_RESIZE, vol.getAccountId(), vol.getDataCenterId(), vol.getId(), vol.getName(), vol.getDiskOfferingId(), vol.getTemplateId(), vol.getSize(), Volume.class.getName(), vol.getUuid()); } else { UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, vol.getAccountId(), vol.getDataCenterId(), vol.getId(), vol.getName(), vol.getDiskOfferingId(), null, vol.getSize(), Volume.class.getName(), vol.getUuid(), vol.isDisplayVolume()); } } else if (transition.getToState() == State.Destroy && vol.getVolumeType() != Volume.Type.ROOT) { //Do not Publish Usage Event for ROOT Disk as it would have been published already while destroying a VM UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_DELETE, vol.getAccountId(), vol.getDataCenterId(), vol.getId(), vol.getName(), Volume.class.getName(), vol.getUuid(), vol.isDisplayVolume()); } else if (transition.getToState() == State.Uploaded) { //Currently we are not capturing Usage for Secondary Storage so Usage for this operation will be captured when it is moved to primary storage } } } return true; }
@Override public List<VMInstanceVO> listNonRemovedVmsByTypeAndNetwork(long networkId, VirtualMachine.Type... types) { if (NetworkTypeSearch == null) { SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder(); nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); NetworkTypeSearch = createSearchBuilder(); NetworkTypeSearch.and("types", NetworkTypeSearch.entity().getType(), SearchCriteria.Op.IN); NetworkTypeSearch.and("removed", NetworkTypeSearch.entity().getRemoved(), SearchCriteria.Op.NULL); NetworkTypeSearch.join("nicSearch", nicSearch, NetworkTypeSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); NetworkTypeSearch.done(); } SearchCriteria<VMInstanceVO> sc = NetworkTypeSearch.create(); if (types != null && types.length != 0) { sc.setParameters("types", (Object[])types); } sc.setJoinParameters("nicSearch", "networkId", networkId); return listBy(sc); }
/** * Lists all pods given a Data Center Id, a {@link VirtualMachine.Type} and * a list of {@link VirtualMachine.State} * @param id * @param type * @param states * @return */ protected List<HostPodVO> listByDataCenterIdVMTypeAndStates(final long id, final VirtualMachine.Type type, final VirtualMachine.State... states) { final SearchBuilder<VMInstanceVO> vmInstanceSearch = vmDao.createSearchBuilder(); vmInstanceSearch.and("type", vmInstanceSearch.entity().getType(), SearchCriteria.Op.EQ); vmInstanceSearch.and("states", vmInstanceSearch.entity().getState(), SearchCriteria.Op.IN); final SearchBuilder<HostPodVO> podIdSearch = podDao.createSearchBuilder(); podIdSearch.and("dc", podIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); podIdSearch.select(null, SearchCriteria.Func.DISTINCT, podIdSearch.entity().getId()); podIdSearch.join("vmInstanceSearch", vmInstanceSearch, podIdSearch.entity().getId(), vmInstanceSearch.entity().getPodIdToDeployIn(), JoinBuilder.JoinType.INNER); podIdSearch.done(); final SearchCriteria<HostPodVO> sc = podIdSearch.create(); sc.setParameters("dc", id); sc.setJoinParameters("vmInstanceSearch", "type", type); sc.setJoinParameters("vmInstanceSearch", "states", (Object[]) states); return podDao.search(sc, null); }