Search in sources :

Example 46 with VmTemplate

use of org.ovirt.engine.core.common.businessentities.VmTemplate in project ovirt-engine by oVirt.

the class ExportVmCommand method updateCopyVmInSpm.

private void updateCopyVmInSpm(Guid storagePoolId, VM vm, Guid storageDomainId) {
    HashMap<Guid, KeyValuePairCompat<String, List<Guid>>> vmsAndMetaDictionary = new HashMap<>();
    List<DiskImage> vmImages = new ArrayList<>();
    List<LunDisk> lunDisks = new ArrayList<>();
    List<VmNetworkInterface> interfaces = vm.getInterfaces();
    if (interfaces != null) {
        // TODO remove this when the API changes
        interfaces.clear();
        interfaces.addAll(vmNetworkInterfaceDao.getAllForVm(vm.getId()));
    }
    List<Guid> imageGroupIds = new ArrayList<>();
    for (Disk disk : getDisksBasedOnImage()) {
        DiskImage diskImage = (DiskImage) disk;
        diskImage.setParentId(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
        diskImage.setImageTemplateId(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
        diskImage.setStorageIds(new ArrayList<>(Collections.singletonList(storageDomainId)));
        DiskImage diskForVolumeInfo = getDiskForVolumeInfo(diskImage);
        diskImage.setVolumeFormat(diskForVolumeInfo.getVolumeFormat());
        diskImage.setVolumeType(diskForVolumeInfo.getVolumeType());
        VDSReturnValue vdsReturnValue = runVdsCommand(VDSCommandType.GetImageInfo, new GetImageInfoVDSCommandParameters(storagePoolId, storageDomainId, diskImage.getId(), diskImage.getImageId()));
        if (vdsReturnValue != null && vdsReturnValue.getSucceeded()) {
            DiskImage fromVdsm = (DiskImage) vdsReturnValue.getReturnValue();
            diskImage.setActualSizeInBytes(fromVdsm.getActualSizeInBytes());
        }
        vmImages.add(diskImage);
        imageGroupIds.add(disk.getId());
    }
    if (StringUtils.isEmpty(vm.getVmtName())) {
        VmTemplate t = vmTemplateDao.get(vm.getVmtGuid());
        vm.setVmtName(t.getName());
    }
    lunDisks.addAll(DisksFilter.filterLunDisks(getVm().getDiskMap().values(), ONLY_NOT_SHAREABLE));
    lunDisks.forEach(lun -> lun.getLun().setLunConnections(new ArrayList<>(storageServerConnectionDao.getAllForLun(lun.getLun().getId()))));
    getVm().setVmtGuid(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
    FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(vm);
    fullEntityOvfData.setClusterName(vm.getClusterName());
    fullEntityOvfData.setDiskImages(vmImages);
    fullEntityOvfData.setLunDisks(lunDisks);
    String vmMeta = ovfManager.exportVm(vm, fullEntityOvfData, clusterUtils.getCompatibilityVersion(vm));
    vmsAndMetaDictionary.put(vm.getId(), new KeyValuePairCompat<>(vmMeta, imageGroupIds));
    UpdateVMVDSCommandParameters tempVar = new UpdateVMVDSCommandParameters(storagePoolId, vmsAndMetaDictionary);
    tempVar.setStorageDomainId(storageDomainId);
    runVdsCommand(VDSCommandType.UpdateVM, tempVar);
}
Also used : GetImageInfoVDSCommandParameters(org.ovirt.engine.core.common.vdscommands.GetImageInfoVDSCommandParameters) KeyValuePairCompat(org.ovirt.engine.core.compat.KeyValuePairCompat) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UpdateVMVDSCommandParameters(org.ovirt.engine.core.common.vdscommands.UpdateVMVDSCommandParameters) Guid(org.ovirt.engine.core.compat.Guid) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) LunDisk(org.ovirt.engine.core.common.businessentities.storage.LunDisk) VDSReturnValue(org.ovirt.engine.core.common.vdscommands.VDSReturnValue) VmNetworkInterface(org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) LunDisk(org.ovirt.engine.core.common.businessentities.storage.LunDisk) Disk(org.ovirt.engine.core.common.businessentities.storage.Disk)

Example 47 with VmTemplate

use of org.ovirt.engine.core.common.businessentities.VmTemplate in project ovirt-engine by oVirt.

the class VmDeviceUtils method copyVmDevices.

/**
 * Copy devices from the given VmDevice list to the destination VM/VmBase.
 */
public void copyVmDevices(Guid srcId, Guid dstId, VmBase srcVmBase, VmBase dstVmBase, List<VmDevice> srcDevices, Map<Guid, Guid> srcDeviceIdToDstDeviceIdMapping, boolean isSoundEnabled, boolean isConsoleEnabled, Boolean isVirtioScsiEnabled, boolean isBalloonEnabled, Set<GraphicsType> graphicsToSkip, boolean copySnapshotDevices, boolean copyHostDevices, Version versionToUpdateRngDeviceWith) {
    if (graphicsToSkip == null) {
        graphicsToSkip = Collections.emptySet();
    }
    String dstCdPath = dstVmBase.getIsoPath();
    boolean dstIsVm = !(dstVmBase instanceof VmTemplate);
    boolean hasCd = hasCdDevice(dstVmBase.getId());
    boolean hasSound = false;
    boolean hasConsole = false;
    boolean hasVirtioScsi = false;
    boolean hasBalloon = false;
    boolean hasRng = hasRngDevice(dstId);
    Cluster cluster = null;
    if (dstVmBase.getClusterId() != null) {
        cluster = clusterDao.get(dstVmBase.getClusterId());
    }
    for (VmDevice device : srcDevices) {
        if (device.getSnapshotId() != null && !copySnapshotDevices) {
            continue;
        }
        Guid deviceId = Guid.newGuid();
        Map<String, Object> specParams = new HashMap<>();
        switch(device.getType()) {
            case DISK:
                if (VmDeviceType.DISK.getName().equals(device.getDevice())) {
                    if (srcDeviceIdToDstDeviceIdMapping.containsKey(device.getDeviceId())) {
                        deviceId = srcDeviceIdToDstDeviceIdMapping.get(device.getDeviceId());
                    }
                } else if (VmDeviceType.CDROM.getName().equals(device.getDevice())) {
                    if (!hasCd) {
                        hasCd = true;
                        // check here is source VM had CD (VM from snapshot)
                        String srcCdPath = (String) device.getSpecParams().get(VdsProperties.Path);
                        specParams.putAll(getCdDeviceSpecParams(srcCdPath, dstCdPath));
                    } else {
                        // CD already exists
                        continue;
                    }
                }
                break;
            case INTERFACE:
                if (srcDeviceIdToDstDeviceIdMapping.containsKey(device.getDeviceId())) {
                    deviceId = srcDeviceIdToDstDeviceIdMapping.get(device.getDeviceId());
                }
                break;
            case CONTROLLER:
                if (VmDeviceType.USB.getName().equals(device.getDevice())) {
                    specParams = device.getSpecParams();
                } else if (VmDeviceType.VIRTIOSCSI.getName().equals(device.getDevice())) {
                    hasVirtioScsi = true;
                    if (Boolean.FALSE.equals(isVirtioScsiEnabled)) {
                        continue;
                    }
                }
                break;
            case VIDEO:
                if (dstIsVm) {
                    // to the new Vm params.
                    continue;
                }
                specParams.putAll(getVideoDeviceSpecParams(dstVmBase));
                break;
            case BALLOON:
                if (!isBalloonEnabled) {
                    continue;
                }
                hasBalloon = true;
                specParams.putAll(getMemoryBalloonSpecParams());
                break;
            case SMARTCARD:
                specParams.putAll(getSmartcardDeviceSpecParams());
                break;
            case WATCHDOG:
                specParams.putAll(device.getSpecParams());
                break;
            case RNG:
                if (hasRng) {
                    continue;
                }
                if (!new VirtIoRngValidator().canAddRngDevice(cluster, new VmRngDevice(device)).isValid()) {
                    continue;
                }
                final VmRngDevice rngDevice = new VmRngDevice(device);
                if (versionToUpdateRngDeviceWith != null) {
                    rngDevice.updateSourceByVersion(versionToUpdateRngDeviceWith);
                }
                specParams.putAll(rngDevice.getSpecParams());
                break;
            case CONSOLE:
                if (!isConsoleEnabled) {
                    continue;
                }
                specParams.putAll(device.getSpecParams());
                hasConsole = true;
                break;
            case SOUND:
                if (!isSoundEnabled) {
                    continue;
                }
                hasSound = true;
                break;
            case GRAPHICS:
                GraphicsType type = GraphicsType.fromVmDeviceType(VmDeviceType.getByName(device.getDevice()));
                // OR if we already have it
                if (graphicsToSkip.contains(type) || hasGraphicsDevice(dstId, GraphicsType.fromString(device.getDevice()))) {
                    continue;
                }
                break;
            case HOSTDEV:
                if (!copyHostDevices) {
                    continue;
                }
                specParams.putAll(device.getSpecParams());
                break;
            default:
                break;
        }
        device.setId(new VmDeviceId(deviceId, dstId));
        device.setSpecParams(specParams);
        vmDeviceDao.save(device);
    }
    if (!hasCd) {
        addCdDevice(dstId, dstCdPath);
    }
    updateUsbSlots(srcVmBase, dstVmBase, () -> clusterDao.get(dstVmBase.getClusterId()));
    if (isSoundEnabled && !hasSound) {
        if (dstIsVm) {
            addSoundDevice(dstVmBase);
        } else {
            addSoundDevice(dstVmBase.getId(), dstVmBase.getOsId(), cluster != null ? cluster.getCompatibilityVersion() : null);
        }
    }
    if (isConsoleEnabled && !hasConsole) {
        addConsoleDevice(dstId);
    }
    if (Boolean.TRUE.equals(isVirtioScsiEnabled) && !hasVirtioScsi) {
        addVirtioScsiController(dstVmBase, getVmCompatibilityVersion(dstVmBase));
    }
    if (isBalloonEnabled && !hasBalloon) {
        addMemoryBalloon(dstId);
    }
    if (dstIsVm) {
        addVideoDevices(dstVmBase, getNeededNumberOfVideoDevices(dstVmBase));
    }
}
Also used : VirtIoRngValidator(org.ovirt.engine.core.bll.validator.VirtIoRngValidator) HashMap(java.util.HashMap) MacPoolPerCluster(org.ovirt.engine.core.bll.network.macpool.MacPoolPerCluster) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) Guid(org.ovirt.engine.core.compat.Guid) VmDeviceId(org.ovirt.engine.core.common.businessentities.VmDeviceId) GraphicsType(org.ovirt.engine.core.common.businessentities.GraphicsType) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) VmRngDevice(org.ovirt.engine.core.common.businessentities.VmRngDevice)

Example 48 with VmTemplate

use of org.ovirt.engine.core.common.businessentities.VmTemplate in project ovirt-engine by oVirt.

the class BackendVmsResource method add.

@Override
public Response add(Vm vm) {
    validateParameters(vm, "cluster.id|name");
    validateIconParameters(vm);
    Response response = null;
    if (vm.isSetInitialization() && vm.getInitialization().isSetConfiguration()) {
        validateParameters(vm, "initialization.configuration.type", "initialization.configuration.data");
        response = importVmFromConfiguration(vm);
    } else {
        validateParameters(vm, "name");
        if (isCreateFromSnapshot(vm)) {
            validateSnapshotExistence(vm);
            response = createVmFromSnapshot(vm);
        } else {
            validateParameters(vm, "template.id|name");
            Cluster cluster = getCluster(vm);
            VmTemplate template = lookupTemplate(vm.getTemplate(), cluster.getStoragePoolId());
            VmStatic builtFromTemplate = VmMapper.map(template, null, cluster.getCompatibilityVersion());
            // since the template cpu_profile doesn't match cluster.
            if (!vm.isSetCpuProfile() && vm.isSetCluster() && !Objects.equals(Objects.toString(template.getClusterId(), null), vm.getCluster().getId())) {
                builtFromTemplate.setCpuProfileId(null);
            }
            VmStatic builtFromInstanceType = null;
            InstanceType instanceTypeEntity = null;
            if (vm.isSetInstanceType() && (vm.getInstanceType().isSetId() || vm.getInstanceType().isSetName())) {
                instanceTypeEntity = lookupInstance(vm.getInstanceType());
                builtFromInstanceType = VmMapper.map(instanceTypeEntity, builtFromTemplate, cluster.getCompatibilityVersion());
                builtFromInstanceType.setInstanceTypeId(instanceTypeEntity.getId());
            }
            VmStatic staticVm = getMapper(Vm.class, VmStatic.class).map(vm, builtFromInstanceType != null ? builtFromInstanceType : builtFromTemplate);
            if (namedCluster(vm)) {
                staticVm.setClusterId(cluster.getId());
            }
            updateMaxMemoryIfUnspecified(vm, staticVm);
            if (Guid.Empty.equals(template.getId()) && !vm.isSetOs()) {
                staticVm.setOsId(OsRepository.AUTO_SELECT_OS);
            }
            staticVm.setUsbPolicy(VmMapper.getUsbPolicyOnCreate(vm.getUsb()));
            if (!isFiltered() && vm.isSetPlacementPolicy()) {
                Set<Guid> hostGuidsSet = validateAndUpdateHostsInPlacementPolicy(vm.getPlacementPolicy());
                staticVm.setDedicatedVmForVdsList(new LinkedList<>(hostGuidsSet));
            } else {
                vm.setPlacementPolicy(null);
            }
            // migration support (disabling it in architectures that do not support this feature)
            if (!vm.isSetPlacementPolicy() && template.getId().equals(Guid.Empty)) {
                staticVm.setMigrationSupport(null);
            }
            Guid storageDomainId = (vm.isSetStorageDomain() && vm.getStorageDomain().isSetId()) ? asGuid(vm.getStorageDomain().getId()) : Guid.Empty;
            boolean clone = ParametersHelper.getBooleanParameter(httpHeaders, uriInfo, CLONE, true, false);
            if (clone) {
                response = cloneVmFromTemplate(staticVm, vm, template, instanceTypeEntity, cluster);
            } else if (Guid.Empty.equals(template.getId())) {
                response = addVmFromScratch(staticVm, vm, instanceTypeEntity, cluster);
            } else {
                response = addVm(staticVm, vm, storageDomainId, template, instanceTypeEntity, cluster);
            }
        }
    }
    Vm result = (Vm) response.getEntity();
    if (result != null) {
        DisplayHelper.adjustDisplayData(this, result, false);
        removeRestrictedInfo(result);
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) Vm(org.ovirt.engine.api.model.Vm) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic) Guid(org.ovirt.engine.core.compat.Guid) InstanceType(org.ovirt.engine.core.common.businessentities.InstanceType)

Example 49 with VmTemplate

use of org.ovirt.engine.core.common.businessentities.VmTemplate in project ovirt-engine by oVirt.

the class BackendVmPoolsResource method mapToVM.

protected VM mapToVM(VmPool model, VmTemplate template, Cluster cluster) {
    // apply template
    VmStatic fromTemplate = getMapper(VmTemplate.class, VmStatic.class).map(template, null);
    VmStatic fromInstanceType = null;
    if (model.isSetInstanceType()) {
        org.ovirt.engine.core.common.businessentities.InstanceType instanceType = loadInstanceType(model);
        fromTemplate.setInstanceTypeId(instanceType.getId());
        fromInstanceType = VmMapper.map(instanceType, fromTemplate, cluster.getCompatibilityVersion());
        fromInstanceType.setInstanceTypeId(instanceType.getId());
    }
    // override with client-provided data
    VM vm = new VM(getMapper(VmPool.class, VmStatic.class).map(model, fromInstanceType != null ? fromInstanceType : fromTemplate), new VmDynamic(), new VmStatistics());
    return vm;
}
Also used : VmDynamic(org.ovirt.engine.core.common.businessentities.VmDynamic) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) VmStatistics(org.ovirt.engine.core.common.businessentities.VmStatistics) VM(org.ovirt.engine.core.common.businessentities.VM) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic)

Example 50 with VmTemplate

use of org.ovirt.engine.core.common.businessentities.VmTemplate in project ovirt-engine by oVirt.

the class BackendTemplatesResource method mapCollection.

protected Templates mapCollection(List<VmTemplate> entities) {
    Set<String> details = DetailHelper.getDetails(httpHeaders, uriInfo);
    boolean includeData = details.contains(DetailHelper.MAIN);
    boolean includeSize = details.contains("size");
    if (includeData) {
        // Fill VmInit for entities - the search query no join the VmInit to Templates
        IdsQueryParameters params = new IdsQueryParameters();
        List<Guid> ids = entities.stream().map(VmTemplate::getId).collect(Collectors.toList());
        params.setId(ids);
        QueryReturnValue queryReturnValue = runQuery(QueryType.GetVmsInit, params);
        if (queryReturnValue.getSucceeded() && queryReturnValue.getReturnValue() != null) {
            List<VmInit> vmInits = queryReturnValue.getReturnValue();
            Map<Guid, VmInit> initMap = Entities.businessEntitiesById(vmInits);
            for (VmTemplate template : entities) {
                template.setVmInit(initMap.get(template.getId()));
            }
        }
    }
    Templates collection = new Templates();
    if (includeData) {
        for (VmTemplate entity : entities) {
            Template template = map(entity);
            collection.getTemplates().add(addLinks(populate(template, entity)));
            DisplayHelper.adjustDisplayData(this, template);
        }
    }
    if (includeSize) {
        collection.setSize((long) entities.size());
    }
    return collection;
}
Also used : VmInit(org.ovirt.engine.core.common.businessentities.VmInit) IdsQueryParameters(org.ovirt.engine.core.common.queries.IdsQueryParameters) Templates(org.ovirt.engine.api.model.Templates) Guid(org.ovirt.engine.core.compat.Guid) Template(org.ovirt.engine.api.model.Template) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate)

Aggregations

VmTemplate (org.ovirt.engine.core.common.businessentities.VmTemplate)149 ArrayList (java.util.ArrayList)42 Guid (org.ovirt.engine.core.compat.Guid)40 Test (org.junit.Test)30 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)23 HashMap (java.util.HashMap)22 List (java.util.List)20 VM (org.ovirt.engine.core.common.businessentities.VM)19 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)16 Map (java.util.Map)13 QueryType (org.ovirt.engine.core.common.queries.QueryType)13 IdQueryParameters (org.ovirt.engine.core.common.queries.IdQueryParameters)12 Cluster (org.ovirt.engine.core.common.businessentities.Cluster)11 StoragePool (org.ovirt.engine.core.common.businessentities.StoragePool)11 ConstantsManager (org.ovirt.engine.ui.uicompat.ConstantsManager)11 Frontend (org.ovirt.engine.ui.frontend.Frontend)10 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)9 UICommand (org.ovirt.engine.ui.uicommonweb.UICommand)9 Collection (java.util.Collection)8 ActionParametersBase (org.ovirt.engine.core.common.action.ActionParametersBase)8