public void endElement(String uri, String name, String qName) { if (equalsOrSuffix(qName, "Children")) { inChildren = false; Vm vm = vmHandler.getResult(); if (vm != null) this.children.add(vmHandler.getResult()); } else if (equalsOrSuffix(qName, "Tasks")) { inTasks = false; this.tasks.add(taskHandler.getResult()); } else if (equalsOrSuffix(qName, "NetworkSection")) { inNetworkSection = false; this.networkSection = networkSectionHandler.getResult(); } if (inChildren) { vmHandler.endElement(uri, name, qName); } else if (inTasks) { taskHandler.endElement(uri, name, qName); } else if (inNetworkSection) { networkSectionHandler.endElement(uri, name, qName); } else if (equalsOrSuffix(qName, "Description")) { description = currentOrNull(); } else if (equalsOrSuffix(qName, "VAppScopedLocalId")) { vAppScopedLocalId = currentOrNull(); } else if (equalsOrSuffix(qName, "ovfDescriptorUploaded")) { ovfDescriptorUploaded = Boolean.parseBoolean(currentOrNull()); } currentText = new StringBuilder(); }
@Override protected void executeVmCommand() { if (!isOperationPerformedOnDiskSnapshot()) { getVmStaticDao().incrementDbGeneration(getVm().getId()); } final VmDevice vmDevice = createVmDevice(); getVmDeviceDao().save(vmDevice); // update cached image List<Disk> imageList = new ArrayList<>(); imageList.add(disk); VmHandler.updateDisksForVm(getVm(), imageList); if (!isOperationPerformedOnDiskSnapshot()) { if (disk.isAllowSnapshot()) { updateDiskVmSnapshotId(); } } // update vm device boot order updateBootOrderInVmDevice(); if (getParameters().isPlugUnPlug() && getVm().getStatus() != VMStatus.Down) { performPlugCommand(VDSCommandType.HotPlugDisk, disk, vmDevice); } setSucceeded(true); }
protected boolean initVdss() { setVdsIdRef(getVm().getRunOnVds()); Guid vdsToRunOn = schedulingManager.schedule( getVdsGroup(), getVm(), getVdsBlackList(), getVdsWhiteList(), getDestinationHostList(), new ArrayList<String>(), new VdsFreeMemoryChecker(this), getCorrelationId()); setDestinationVdsId(vdsToRunOn); if (vdsToRunOn != null && !Guid.Empty.equals(vdsToRunOn)) { getRunVdssList().add(vdsToRunOn); } VmHandler.updateVmGuestAgentVersion(getVm()); if (vdsToRunOn != null && vdsToRunOn.equals(Guid.Empty)) { return false; } if (getDestinationVds() == null || getVds() == null) { return false; } return true; }
@Override protected void ExecuteVmCommand() { VmHandler.LockVm(getVm().getDynamicData(), getCompensationContext()); TransactionSupport.executeInNewTransaction( new TransactionMethod<Void>() { @Override public Void runInTransaction() { for (Guid imageId : getParameters().getImageIds()) { RemoveImageParameters tempVar = new RemoveImageParameters(imageId, null, getVmId()); tempVar.setParentCommand(VdcActionType.RemoveDisksFromVm); tempVar.setEntityId(getParameters().getEntityId()); RemoveImageParameters p = tempVar; p.setParentParemeters(getParameters()); VdcReturnValueBase vdcReturnValue = Backend.getInstance().runInternalAction(VdcActionType.RemoveImage, p); getParameters().getImagesParameters().add(p); getReturnValue().getTaskIdList().addAll(vdcReturnValue.getInternalTaskIdList()); if (!vdcReturnValue.getSucceeded()) { setSucceeded(false); break; } setSucceeded(vdcReturnValue.getSucceeded()); } return null; } }); }
@Override public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); if (equalsOrSuffix(qName, "Children")) { inChildren = true; } else if (equalsOrSuffix(qName, "Tasks")) { inTasks = true; } else if (equalsOrSuffix(qName, "NetworkSection")) { inNetworkSection = true; } if (inChildren) { vmHandler.startElement(uri, localName, qName, attrs); } else if (inTasks) { taskHandler.startElement(uri, localName, qName, attrs); } else if (inNetworkSection) { networkSectionHandler.startElement(uri, localName, qName, attrs); } else if (equalsOrSuffix(qName, "VAppTemplate")) { template = newReferenceType(attributes); if (attributes.containsKey("status")) this.status = Status.fromValue(Integer.parseInt(attributes.get("status"))); } else if (equalsOrSuffix(qName, "Link") && "up".equals(attributes.get("rel"))) { vdc = newReferenceType(attributes); } }
protected boolean verifyAddVM() { final List<String> reasons = getReturnValue().getCanDoActionMessages(); final int nicsCount = getParameters().getVmsCount() * getVmNicDao().getAllForTemplate(getVmTemplateId()).size(); final int priority = getParameters().getVmStaticData().getPriority(); return VmHandler.verifyAddVm(reasons, nicsCount, priority, getMacPool()); }
private String generateUniqueVmName() { String currentVmName; do { currentVmName = nameForVmInPoolGenerator.generateVmName(); } while (VmHandler.isVmWithSameNameExistStatic(currentVmName, getStoragePoolId())); return currentVmName; }
private void updateVmInitPassword() { // We are not passing the VmInit password to the UI, // so we need to update the VmInit password from its template. if (getParameters().getVmStaticData().getVmInit() != null && getParameters().getVmStaticData().getVmInit().isPasswordAlreadyStored()) { VmBase temp = new VmBase(); temp.setId(getParameters().getVmStaticData().getVmtGuid()); VmHandler.updateVmInitFromDB(temp, false); getParameters() .getVmStaticData() .getVmInit() .setRootPassword(temp.getVmInit().getRootPassword()); } }
private boolean checkCanPerformRegularUpdate() { boolean retValue = true; if (_oldDisk.getDiskInterface() != getParameters().getDiskInfo().getDiskInterface()) { List<VmNetworkInterface> allVmInterfaces = DbFacade.getInstance().getVmNetworkInterfaceDao().getAllForVm(getVmId()); List<Disk> allVmDisks = getDiskDao().getAllForVm(getVmId()); allVmDisks.removeAll( LinqUtils.filter( allVmDisks, new Predicate<Disk>() { @Override public boolean eval(Disk o) { return o.getId().equals(_oldDisk.getId()); } })); allVmDisks.add(getParameters().getDiskInfo()); if (!checkPciAndIdeLimit( getVm().getnum_of_monitors(), allVmInterfaces, allVmDisks, getReturnValue().getCanDoActionMessages())) { retValue = false; } } // Validate update boot disk. if (retValue && getParameters().getDiskInfo().isBoot()) { VmHandler.updateDisksFromDb(getVm()); for (Disk disk : getVm().getDiskMap().values()) { if (disk.isBoot() && !disk.getId().equals(_oldDisk.getId())) { retValue = false; addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_DISK_BOOT_IN_USE); getReturnValue() .getCanDoActionMessages() .add(String.format("$DiskName %1$s", disk.getDiskAlias())); break; } } } // Set disk alias name in the disk retrieved from the parameters. ImagesHandler.setDiskAlias(getParameters().getDiskInfo(), getVm()); return retValue && validateShareableDisk(); }
public String generateOvfConfigurationForVm(VM vm) { if (VMStatus.ImageLocked != vm.getStatus()) { VmHandler.updateDisksFromDb(vm); DiskImagesValidator validator = new DiskImagesValidator(vm.getDiskList()); if (validator.diskImagesNotLocked().isValid()) { loadVmData(vm); Long currentDbGeneration = getDbFacade().getVmStaticDao().getDbGeneration(vm.getId()); // currentDbGeneration can be null in case that the vm was deleted during the run of // OvfDataUpdater. if (currentDbGeneration != null && vm.getStaticData().getDbGeneration() == currentDbGeneration) { return buildMetadataDictionaryForVm(vm); } } } return null; }
@Override public List<PermissionSubject> getPermissionCheckSubjects() { List<PermissionSubject> permissionList = super.getPermissionCheckSubjects(); // this runs before canDoAction so the getVm() can be null - instead of failing on NPE here we // pass the parent permissions and let the canDoAction to return proper error if (getVm() == null) { return permissionList; } if (getParameters().getTargetVdsGroupId() != null && !getParameters().getTargetVdsGroupId().equals(getVm().getVdsGroupId())) { // additional permissions needed since changing the cluster permissionList.addAll( VmHandler.getPermissionsNeededToChangeCluster( getParameters().getVmId(), getParameters().getTargetVdsGroupId())); } return permissionList; }
/** * This operation may take much time so the inner commands have fine-grained TX handling which * means they aim to make all calls to Vds commands (i.e VDSM calls) out of TX. */ @Override protected void executeCommand() { updateVmInitPassword(); VmHandler.warnMemorySizeLegal( getParameters().getVmStaticData(), getVdsGroup().getCompatibilityVersion()); // Free exclusive VM_POOL lock, if taken. Further AddVmAndAttachToPool commands // require shared VM_POOL locks only. freeLock(); Guid poolId = getPoolId(); setActionReturnValue(poolId); VmTemplateHandler.lockVmTemplateInTransaction( getParameters().getVmStaticData().getVmtGuid(), getCompensationContext()); addVmsToPool(poolId); getReturnValue().setCanDoAction(isAddVmsSucceded()); setSucceeded(isAddVmsSucceded()); VmTemplateHandler.unlockVmTemplate(getParameters().getVmStaticData().getVmtGuid()); if (!isVmsAdded()) onNoVmsAdded(poolId); getCompensationContext().resetCompensation(); }
protected boolean validate() { VM vm = parentCommand.getVm(); if (vm == null) { parentCommand.addCanDoActionMessage(EngineMessage.ACTION_TYPE_FAILED_VM_NOT_FOUND); return false; } else { targetCluster = DbFacade.getInstance().getVdsGroupDao().get(targetClusterId); if (targetCluster == null) { parentCommand.addCanDoActionMessage(EngineMessage.VM_CLUSTER_IS_NOT_VALID); return false; } // Check that the target cluster is in the same data center. if (!targetCluster.getStoragePoolId().equals(vm.getStoragePoolId())) { parentCommand.addCanDoActionMessage( EngineMessage.VM_CANNOT_MOVE_TO_CLUSTER_IN_OTHER_STORAGE_POOL); return false; } List<VmNic> interfaces = DbFacade.getInstance().getVmNicDao().getAllForVm(vm.getId()); Version clusterCompatibilityVersion = targetCluster.getCompatibilityVersion(); if (!validateDestinationClusterContainsNetworks(interfaces) || !validateNics(interfaces, clusterCompatibilityVersion)) { return false; } // Check if VM static parameters are compatible for new cluster. boolean isCpuSocketsValid = AddVmCommand.checkCpuSockets( vm.getStaticData().getNumOfSockets(), vm.getStaticData().getCpuPerSocket(), vm.getStaticData().getThreadsPerCpu(), clusterCompatibilityVersion.getValue(), parentCommand.getReturnValue().getCanDoActionMessages()); if (!isCpuSocketsValid) { return false; } // Check that the USB policy is legal if (!VmHandler.isUsbPolicyLegal( vm.getUsbPolicy(), vm.getOs(), targetCluster, parentCommand.getReturnValue().getCanDoActionMessages())) { return false; } // Check if the display type is supported if (!VmHandler.isGraphicsAndDisplaySupported( vm.getOs(), VmDeviceUtils.getGraphicsTypesOfEntity(vm.getId()), vm.getDefaultDisplayType(), parentCommand.getReturnValue().getCanDoActionMessages(), clusterCompatibilityVersion)) { return false; } if (VmDeviceUtils.hasVirtioScsiController(vm.getId())) { // Verify cluster compatibility if (!FeatureSupported.virtIoScsi(targetCluster.getCompatibilityVersion())) { return parentCommand.failCanDoAction( EngineMessage.VIRTIO_SCSI_INTERFACE_IS_NOT_AVAILABLE_FOR_CLUSTER_LEVEL); } // Verify OS compatibility if (!VmHandler.isOsTypeSupportedForVirtioScsi( vm.getOs(), targetCluster.getCompatibilityVersion(), parentCommand.getReturnValue().getCanDoActionMessages())) { return false; } } // A existing VM cannot be changed into a cluster without a defined architecture if (targetCluster.getArchitecture() == ArchitectureType.undefined) { return parentCommand.failCanDoAction( EngineMessage.ACTION_TYPE_FAILED_CLUSTER_UNDEFINED_ARCHITECTURE); } else if (targetCluster.getArchitecture() != vm.getClusterArch()) { return parentCommand.failCanDoAction( EngineMessage.ACTION_TYPE_FAILED_VM_CLUSTER_DIFFERENT_ARCHITECTURES); } } return true; }
public void characters(char ch[], int start, int length) { if (inTasks) taskHandler.characters(ch, start, length); else if (inChildren) vmHandler.characters(ch, start, length); else if (inNetworkSection) networkSectionHandler.characters(ch, start, length); else currentText.append(ch, start, length); }
protected void mockOsRepository() { SimpleDependencyInjector.getInstance().bind(OsRepository.class, osRepository); VmHandler.init(); when(osRepository.isWindows(0)).thenReturn(true); when(osRepository.isCpuSupported(anyInt(), any(Version.class), anyString())).thenReturn(true); }