Search in sources :

Example 6 with SnapshotStrategy

use of com.cloud.engine.subsystem.api.storage.SnapshotStrategy 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();
    final Long volumeId = command.getVolumeId();
    final Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    try {
        final TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
        long zoneId = 0;
        if (snapshotId != null) {
            snapshot = _snapshotDao.findById(snapshotId);
            zoneId = snapshot.getDataCenterId();
        } else if (volumeId != null) {
            volume = _volumeDao.findById(volumeId);
            zoneId = volume.getDataCenterId();
        }
        DataStore store = _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, _snapshotStoreDao, _dataStoreMgr);
            SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
            if (dataStoreRole == DataStoreRole.Image) {
                if (snapInfo == null) {
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
                    }
                    // We need to copy the snapshot onto secondary.
                    final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
                    snapshotStrategy.backupSnapshot(snapInfo);
                    // Attempt to grab it again.
                    snapInfo = _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 = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
        } else if (volumeId != null) {
            final VolumeInfo volInfo = _volFactory.getVolume(volumeId);
            future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        final CommandResult result;
        try {
            result = future.get();
            if (result.isFailed()) {
                privateTemplate = null;
                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 (_dataStoreMgr.isRegionStore(store)) {
                // template created on region store
                _tmpltSvr.associateTemplateToZone(templateId, null);
            } else {
                final VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                _tmpltZoneDao.persist(templateZone);
            }
            privateTemplate = _tmpltDao.findById(templateId);
        } 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.
                    _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template_zone_ref record
                    _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template record
                    _tmpltDao.expunge(templateId);
                    // decrement resource count
                    if (accountId != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                        _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) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(com.cloud.storage.command.CommandResult) DataStoreRole(com.cloud.storage.DataStoreRole) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 7 with SnapshotStrategy

use of com.cloud.engine.subsystem.api.storage.SnapshotStrategy in project cosmic by MissionCriticalCloud.

the class SnapshotManagerImpl method deleteSnapshot.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_DELETE, eventDescription = "deleting snapshot", async = true)
public boolean deleteSnapshot(final long snapshotId) {
    final Account caller = CallContext.current().getCallingAccount();
    // Verify parameters
    final SnapshotVO snapshotCheck = _snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
        throw new InvalidParameterValueException("unable to find a snapshot with id " + snapshotId);
    }
    _accountMgr.checkAccess(caller, null, true, snapshotCheck);
    final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshotCheck, SnapshotOperation.DELETE);
    if (snapshotStrategy == null) {
        s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
        return false;
    }
    try {
        final boolean result = snapshotStrategy.deleteSnapshot(snapshotId);
        if (result) {
            if (snapshotCheck.getState() != Snapshot.State.Error && snapshotCheck.getState() != Snapshot.State.Destroyed) {
                _resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.snapshot);
            }
            if (snapshotCheck.getState() == Snapshot.State.BackedUp) {
                final SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Image);
                if (snapshotStoreRef != null) {
                    _resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
                }
            }
        }
        return result;
    } catch (final Exception e) {
        s_logger.debug("Failed to delete snapshot: " + snapshotCheck.getId() + ":" + e.toString());
        throw new CloudRuntimeException("Failed to delete snapshot:" + e.toString());
    }
}
Also used : Account(com.cloud.user.Account) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

SnapshotStrategy (com.cloud.engine.subsystem.api.storage.SnapshotStrategy)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 SnapshotInfo (com.cloud.engine.subsystem.api.storage.SnapshotInfo)5 SnapshotVO (com.cloud.storage.SnapshotVO)5 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)5 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)4 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)4 VolumeVO (com.cloud.storage.VolumeVO)4 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)4 Account (com.cloud.user.Account)4 DB (com.cloud.utils.db.DB)4 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)4 ConfigurationException (javax.naming.ConfigurationException)4 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 ActionEvent (com.cloud.event.ActionEvent)2 DataStoreRole (com.cloud.storage.DataStoreRole)2 ExecutionException (java.util.concurrent.ExecutionException)2 Answer (com.cloud.agent.api.Answer)1 DeleteSnapshotsDirCommand (com.cloud.agent.api.DeleteSnapshotsDirCommand)1