Search in sources :

Example 6 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method createPrivateTemplate.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command) throws CloudRuntimeException {
    final long templateId = command.getEntityId();
    Long volumeId = command.getVolumeId();
    final Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    OptimiseFor optimiseFor = command.getOptimiseFor();
    String manufacturerString = command.getManufacturerString();
    String cpuFlags = command.getCpuFlags();
    Boolean macLearning = command.getMacLearning();
    MaintenancePolicy maintenancePolicy = command.getMaintenancePolicy();
    try {
        final TemplateInfo tmplInfo = this._tmplFactory.getTemplate(templateId, DataStoreRole.Image);
        long zoneId = 0;
        if (snapshotId != null) {
            snapshot = this._snapshotDao.findById(snapshotId);
            zoneId = snapshot.getDataCenterId();
        } else if (volumeId != null) {
            volume = this._volumeDao.findById(volumeId);
            zoneId = volume.getDataCenterId();
        }
        DataStore store = this._dataStoreMgr.getImageStore(zoneId);
        if (store == null) {
            throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
        }
        final AsyncCallFuture<TemplateApiResult> future;
        if (snapshotId != null) {
            final DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, this._snapshotStoreDao, this._dataStoreMgr);
            SnapshotInfo snapInfo = this._snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
            if (dataStoreRole == DataStoreRole.Image) {
                if (snapInfo == null) {
                    snapInfo = this._snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
                    }
                    if (volumeId == null) {
                        volumeId = snapInfo.getVolumeId();
                    }
                    // We need to copy the snapshot onto secondary.
                    final SnapshotStrategy snapshotStrategy = this._storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
                    snapshotStrategy.backupSnapshot(snapInfo);
                    // Attempt to grab it again.
                    snapInfo = this._snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
                    }
                }
                final DataStore snapStore = snapInfo.getDataStore();
                if (snapStore != null) {
                    // pick snapshot image store to create template
                    store = snapStore;
                }
            }
            future = this._tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
        } else if (volumeId != null) {
            final VolumeInfo volInfo = this._volFactory.getVolume(volumeId);
            future = this._tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        if (volume != null) {
            final VMInstanceVO vm = this._vmInstanceDao.findById(volume.getInstanceId());
            if (vm != null) {
                if (optimiseFor == null) {
                    optimiseFor = vm.getOptimiseFor();
                }
                if (manufacturerString == null) {
                    manufacturerString = vm.getManufacturerString();
                }
                if (cpuFlags == null) {
                    cpuFlags = vm.getCpuFlags();
                }
                if (macLearning == null) {
                    macLearning = vm.getMacLearning();
                }
                if (maintenancePolicy == null) {
                    maintenancePolicy = vm.getMaintenancePolicy();
                }
            }
        }
        final CommandResult result;
        try {
            result = future.get();
            if (result.isFailed()) {
                s_logger.debug("Failed to create template" + result.getResult());
                throw new CloudRuntimeException("Failed to create template" + result.getResult());
            }
            // create entries in template_zone_ref table
            if (this._dataStoreMgr.isRegionStore(store)) {
                // template created on region store
                this._tmpltSvr.associateTemplateToZone(templateId, null);
            } else {
                final VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                this._tmpltZoneDao.persist(templateZone);
            }
            if (optimiseFor == null) {
                optimiseFor = OptimiseFor.Generic;
            }
            if (maintenancePolicy == null) {
                maintenancePolicy = MaintenancePolicy.LiveMigrate;
            }
            privateTemplate = this._tmpltDao.findById(templateId);
            privateTemplate.setCpuFlags(cpuFlags);
            privateTemplate.setMacLearning(macLearning);
            privateTemplate.setManufacturerString(manufacturerString);
            privateTemplate.setOptimiseFor(optimiseFor);
            privateTemplate.setMaintenancePolicy(maintenancePolicy);
            this._tmpltDao.persist(privateTemplate);
        } catch (final InterruptedException | ExecutionException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        }
    } finally {
        if (privateTemplate == null) {
            final VolumeVO volumeFinal = volume;
            final SnapshotVO snapshotFinal = snapshot;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    // template_store_ref entries should have been removed using our
                    // DataObject.processEvent command in case of failure, but clean
                    // it up here to avoid
                    // some leftovers which will cause removing template from
                    // vm_template table fail.
                    TemplateManagerImpl.this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template_zone_ref record
                    TemplateManagerImpl.this._tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template record
                    TemplateManagerImpl.this._tmpltDao.expunge(templateId);
                    // decrement resource count
                    if (accountId != null) {
                        TemplateManagerImpl.this._resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                        TemplateManagerImpl.this._resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
                    }
                }
            });
        }
    }
    if (privateTemplate != null) {
        return privateTemplate;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) OptimiseFor(com.cloud.model.enumeration.OptimiseFor) DataStoreRole(com.cloud.model.enumeration.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) MaintenancePolicy(com.cloud.model.enumeration.MaintenancePolicy) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) VMInstanceVO(com.cloud.vm.VMInstanceVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(com.cloud.storage.command.CommandResult) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 7 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createSnapshotResponse.

@Override
public SnapshotResponse createSnapshotResponse(final Snapshot snapshot) {
    final SnapshotResponse snapshotResponse = new SnapshotResponse();
    snapshotResponse.setId(snapshot.getUuid());
    populateOwner(snapshotResponse, snapshot);
    final VolumeVO volume = findVolumeById(snapshot.getVolumeId());
    final String snapshotTypeStr = snapshot.getRecurringType().name();
    snapshotResponse.setSnapshotType(snapshotTypeStr);
    if (volume != null) {
        snapshotResponse.setVolumeId(volume.getUuid());
        snapshotResponse.setVolumeName(volume.getName());
        snapshotResponse.setVolumeType(volume.getVolumeType().name());
        final DataCenter zone = ApiDBUtils.findZoneById(volume.getDataCenterId());
        if (zone != null) {
            snapshotResponse.setZoneId(zone.getUuid());
        }
    }
    snapshotResponse.setCreated(snapshot.getCreated());
    snapshotResponse.setName(snapshot.getName());
    snapshotResponse.setIntervalType(ApiDBUtils.getSnapshotIntervalTypes(snapshot.getId()));
    snapshotResponse.setState(snapshot.getState());
    final SnapshotInfo snapshotInfo;
    if (snapshot instanceof SnapshotInfo) {
        snapshotInfo = (SnapshotInfo) snapshot;
    } else {
        final DataStoreRole dataStoreRole = getDataStoreRole(snapshot, this._snapshotStoreDao, this._dataStoreMgr);
        snapshotInfo = this.snapshotfactory.getSnapshot(snapshot.getId(), dataStoreRole);
    }
    if (snapshotInfo == null) {
        s_logger.debug("Unable to find info for image store snapshot with uuid " + snapshot.getUuid());
        snapshotResponse.setRevertable(false);
    } else {
        snapshotResponse.setRevertable(snapshotInfo.isRevertable());
        snapshotResponse.setPhysicaSize(snapshotInfo.getPhysicalSize());
    }
    // set tag information
    final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Snapshot, snapshot.getId());
    final List<ResourceTagResponse> tagResponses = new ArrayList<>();
    for (final ResourceTag tag : tags) {
        final ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        if (tagResponse != null) {
            tagResponses.add(tagResponse);
        }
    }
    snapshotResponse.setTags(tagResponses);
    snapshotResponse.setObjectName("snapshot");
    return snapshotResponse;
}
Also used : DataStoreRole(com.cloud.model.enumeration.DataStoreRole) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.legacymodel.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) VolumeVO(com.cloud.storage.VolumeVO) VMSnapshotResponse(com.cloud.api.response.VMSnapshotResponse) SnapshotResponse(com.cloud.api.response.SnapshotResponse) ResourceTagResponse(com.cloud.api.response.ResourceTagResponse) ArrayList(java.util.ArrayList)

Aggregations

DataStoreRole (com.cloud.model.enumeration.DataStoreRole)7 SnapshotInfo (com.cloud.engine.subsystem.api.storage.SnapshotInfo)3 VolumeVO (com.cloud.storage.VolumeVO)3 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)2 SnapshotStrategy (com.cloud.engine.subsystem.api.storage.SnapshotStrategy)2 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)2 DataCenter (com.cloud.legacymodel.dc.DataCenter)2 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)2 DB (com.cloud.utils.db.DB)2 ExecutionException (java.util.concurrent.ExecutionException)2 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)1 SnapshotResponse (com.cloud.api.response.SnapshotResponse)1 VMSnapshotResponse (com.cloud.api.response.VMSnapshotResponse)1 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)1 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)1 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)1 VolumeService (com.cloud.engine.subsystem.api.storage.VolumeService)1 ActionEvent (com.cloud.event.ActionEvent)1 Host (com.cloud.legacymodel.dc.Host)1