@Override public String getVifNameByVmUuid(String vmUuid, Integer deviceId) { VMInstanceVO vm = _vmInstanceDao.findByUuid(vmUuid); if (vm != null) { return vm.getInstanceName() + "-" + deviceId.toString(); } return null; }
public VirtualMachineModel(VMInstanceVO vm, String uuid) { _uuid = uuid; if (vm != null) { _instanceId = vm.getId(); _instanceName = vm.getInstanceName(); } }
@Override public String toString() { if (toString == null) { toString = new StringBuilder("VM[").append(type.toString()).append("|").append(getInstanceName()).append("]").toString(); } return toString; }
@Override public String getAttachedVmName() { Long vmId = volumeVO.getInstanceId(); if (vmId != null) { VMInstanceVO vm = vmInstanceDao.findById(vmId); if (vm == null) { return null; } return vm.getInstanceName(); } return null; }
private void removeStaleVmFromSource(VMInstanceVO vm, HostVO srcHost) { s_logger.debug("Since VM's storage was successfully migrated across VMware Datacenters, unregistering VM: " + vm.getInstanceName() + " from source host: " + srcHost.getId()); final UnregisterVMCommand uvc = new UnregisterVMCommand(vm.getInstanceName()); uvc.setCleanupVmFiles(true); try { _agentMgr.send(srcHost.getId(), uvc); } catch (final Exception e) { throw new CloudRuntimeException("Failed to unregister VM: " + vm.getInstanceName() + " from source host: " + srcHost.getId() + " after successfully migrating VM's storage across VMware Datacenters"); } }
protected Map<String, HostVmStateReportEntry> getHostVmStateReport() { Map<String, HostVmStateReportEntry> states = new HashMap<String, HostVmStateReportEntry>(); if (hostId != null) { final List<? extends VMInstanceVO> vms = vmDao.listByHostId(hostId); for (VMInstanceVO vm : vms) { states.put( vm.getInstanceName(), new HostVmStateReportEntry( vm.getPowerState(), "host-" + hostId ) ); } } return states; }
private void setDestinationPoolAndReallocateNetwork(StoragePool destPool, VMInstanceVO vm) throws InsufficientCapacityException { //if the vm is migrated to different pod in basic mode, need to reallocate ip if (destPool.getPodId() != null && !destPool.getPodId().equals(vm.getPodIdToDeployIn())) { if (s_logger.isDebugEnabled()) { String msg = String.format("as the pod for vm %s has changed we are reallocating its network", vm.getInstanceName()); s_logger.debug(msg); } final DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterId(), destPool.getPodId(), null, null, null, null); final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, null, null, null, null); _networkMgr.reallocate(vmProfile, plan); } }
private void afterHypervisorMigrationCleanup(StoragePool destPool, VMInstanceVO vm, HostVO srcHost, Long srcClusterId, Answer[] hypervisorMigrationResults) throws InsufficientCapacityException { boolean isDebugEnabled = s_logger.isDebugEnabled(); if(isDebugEnabled) { String msg = String.format("cleaning up after hypervisor pool migration volumes for VM %s(%s) to pool %s(%s)", vm.getInstanceName(), vm.getUuid(), destPool.getName(), destPool.getUuid()); s_logger.debug(msg); } setDestinationPoolAndReallocateNetwork(destPool, vm); // OfflineVmwareMigration: don't set this to null or have another way to address the command; twice migrating will lead to an NPE Long destPodId = destPool.getPodId(); Long vmPodId = vm.getPodIdToDeployIn(); if (destPodId == null || ! destPodId.equals(vmPodId)) { if(isDebugEnabled) { String msg = String.format("resetting lasHost for VM %s(%s) as pod (%s) is no good.", vm.getInstanceName(), vm.getUuid(), destPodId); s_logger.debug(msg); } vm.setLastHostId(null); vm.setPodIdToDeployIn(destPodId); // OfflineVmwareMigration: a consecutive migration will fail probably (no host not pod) }// else keep last host set for this vm markVolumesInPool(vm,destPool, hypervisorMigrationResults); // OfflineVmwareMigration: deal with answers, if (hypervisorMigrationResults.length > 0) // OfflineVmwareMigration: iterate over the volumes for data updates }
private void orchestrateStorageMigration(final String vmUuid, final StoragePool destPool) { final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); preStorageMigrationStateCheck(destPool, vm); try { if(s_logger.isDebugEnabled()) { s_logger.debug(String.format("Offline migration of %s vm %s with volumes", vm.getHypervisorType().toString(), vm.getInstanceName())); } migrateThroughHypervisorOrStorage(destPool, vm); } catch (ConcurrentOperationException | InsufficientCapacityException // possibly InsufficientVirtualNetworkCapacityException or InsufficientAddressCapacityException | StorageUnavailableException e) { String msg = String.format("Failed to migrate VM: %s", vmUuid); s_logger.debug(msg); throw new CloudRuntimeException(msg, e); } finally { try { stateTransitTo(vm, Event.AgentReportStopped, null); } catch (final NoTransitionException e) { String anotherMEssage = String.format("failed to change vm state of VM: %s", vmUuid); s_logger.debug(anotherMEssage); throw new CloudRuntimeException(anotherMEssage, e); } } }
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; }
private void markVolumesInPool(VMInstanceVO vm, StoragePool destPool, Answer[] hypervisorMigrationResults) { MigrateVmToPoolAnswer relevantAnswer = null; for (Answer answer : hypervisorMigrationResults) { if (s_logger.isTraceEnabled()) { s_logger.trace(String.format("received an %s: %s", answer.getClass().getSimpleName(), answer)); } if (answer instanceof MigrateVmToPoolAnswer) { relevantAnswer = (MigrateVmToPoolAnswer) answer; } } if (relevantAnswer == null) { throw new CloudRuntimeException("no relevant migration results found"); } List<VolumeVO> volumes = _volsDao.findUsableVolumesForInstance(vm.getId()); if(s_logger.isDebugEnabled()) { String msg = String.format("found %d volumes for VM %s(uuid:%s, id:%d)", volumes.size(), vm.getInstanceName(), vm.getUuid(), vm.getId()); s_logger.debug(msg); } for (VolumeObjectTO result : relevantAnswer.getVolumeTos() ) { if(s_logger.isDebugEnabled()) { s_logger.debug(String.format("updating volume (%d) with path '%s' on pool '%d'", result.getId(), result.getPath(), destPool.getId())); } VolumeVO volume = _volsDao.findById(result.getId()); volume.setPath(result.getPath()); volume.setPoolId(destPool.getId()); _volsDao.update(volume.getId(), volume); } }
private void scanStalledVMInTransitionStateOnDisconnectedHosts() { final Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - VmOpWaitInterval.value() * 1000); final List<Long> stuckAndUncontrollableVMs = listStalledVMInTransitionStateOnDisconnectedHosts(cutTime); for (final Long vmId : stuckAndUncontrollableVMs) { final VMInstanceVO vm = _vmDao.findById(vmId); // We now only alert administrator about this situation _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_SYNC, vm.getDataCenterId(), vm.getPodIdToDeployIn(), VM_SYNC_ALERT_SUBJECT, "VM " + vm.getHostName() + "(" + vm.getInstanceName() + ") is stuck in " + vm.getState() + " state and its host is unreachable for too long"); } }
public void createVirtualMachine(VMInstanceVO dbVm, StringBuffer syncLogMesg) throws IOException { syncLogMesg.append("VM# DB: " + dbVm.getInstanceName() + "/" + dbVm.getUuid() + "; VNC: none; action: create\n"); VirtualMachineModel vmModel = new VirtualMachineModel(dbVm, dbVm.getUuid()); vmModel.build(_manager.getModelController(), dbVm); buildNicResources(vmModel, dbVm, syncLogMesg); if (_rwMode) { try { vmModel.update(_manager.getModelController()); } catch (InternalErrorException ex) { s_logger.warn("create virtual-machine", ex); return; } _manager.getDatabase().getVirtualMachines().add(vmModel); syncLogMesg.append("VM# VNC: " + dbVm.getUuid() + " created\n"); } }
/** * Initialize the object properties based on the DB object. * Common code between plugin calls and DBSync. */ public void setProperties(ModelController controller, VMInstanceVO instance) { ContrailManager manager = controller.getManager(); _instanceName = instance.getInstanceName(); _active = isActiveInstance(instance); try { _projectId = manager.getProjectId(instance.getDomainId(), instance.getAccountId()); } catch (IOException ex) { s_logger.warn("project read", ex); throw new CloudRuntimeException(ex); } _initialized = true; }
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); } }
public Boolean equalVirtualMachine(VMInstanceVO dbVm, VirtualMachine vncVm, StringBuffer syncLogMsg) { syncLogMsg.append("VM# DB: " + dbVm.getInstanceName() + "/" + dbVm.getUuid() + "; VNC: " + vncVm.getUuid() + "; action: equal; DB VM State: " + dbVm.getState() + "\n"); buildNicResources(vmModel, dbVm, syncLogMsg); } catch (IOException ex) { s_logger.warn("build nic information for " + dbVm.getInstanceName(), ex); syncLogMsg.append("VM # DB: " + dbVm.getInstanceName() + "; VNC: " + vncVm.getName() + "; attributes differ\n"); return false;
/** * Add VNC details as user VM details for each VM in 'vms' (KVM hosts only) */ protected void setKVMVncAccess(long hostId, List<VMInstanceVO> vms) { for (VMInstanceVO vm : vms) { GetVncPortAnswer vmVncPortAnswer = (GetVncPortAnswer) _agentMgr.easySend(hostId, new GetVncPortCommand(vm.getId(), vm.getInstanceName())); if (vmVncPortAnswer != null) { userVmDetailsDao.addDetail(vm.getId(), "kvm.vnc.address", vmVncPortAnswer.getAddress(), true); userVmDetailsDao.addDetail(vm.getId(), "kvm.vnc.port", String.valueOf(vmVncPortAnswer.getPort()), true); } } }
vmq.and(vmq.entity().getInstanceName(), SearchCriteria.Op.EQ, vmName); VMInstanceVO vm = vmq.find(); vmDao.update(vm.getId(), vm); s_logger.debug(String.format("received baremetal provision done notification for vm[id:%s name:%s] running on host[mac:%s, ip:%s]", vm.getId(), vm.getInstanceName(), host.getPrivateMacAddress(), host.getPrivateIpAddress()));
public void setProperties(ModelController controller, VMInstanceVO instance, NicVO nic) throws IOException { _vmName = instance.getInstanceName(); _deviceId = nic.getDeviceId(); Network network = controller.getNetworkDao().findById(nic.getNetworkId()); switch (nic.getState()) { case Allocated: case Reserved: _nicActive = true; break; default: _nicActive = false; break; } switch (network.getState()) { case Implemented: case Setup: _netActive = true; break; default: _netActive = false; break; } assert _vnModel != null; _networkId = _vnModel.getUuid(); }
s_logger.debug("Vm " + router.getInstanceName() + " is in " + router.getState() + ", so not sending unplug nic command to the backend"); } else { s_logger.warn("Unable to apply unplug nic, Vm " + router + " is not in the right state " + router.getState());