Search in sources :

Example 16 with CommandResult

use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.

the class LinstorPrimaryDataStoreDriverImpl method deleteAsync.

@Override
public void deleteAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
    s_logger.debug("deleteAsync: " + dataObject.getType() + ";" + dataObject.getUuid());
    String errMsg = null;
    final long storagePoolId = dataStore.getId();
    final StoragePoolVO storagePool = _storagePoolDao.findById(storagePoolId);
    switch(dataObject.getType()) {
        case VOLUME:
            {
                final VolumeInfo volumeInfo = (VolumeInfo) dataObject;
                final String rscName = LinstorUtil.RSC_PREFIX + volumeInfo.getPath();
                deleteResourceDefinition(storagePool, rscName);
                long usedBytes = storagePool.getUsedBytes();
                long capacityIops = storagePool.getCapacityIops();
                usedBytes -= volumeInfo.getSize();
                if (volumeInfo.getMaxIops() != null)
                    capacityIops += volumeInfo.getMaxIops();
                storagePool.setUsedBytes(Math.max(0, usedBytes));
                storagePool.setCapacityIops(Math.max(0, capacityIops));
                _storagePoolDao.update(storagePoolId, storagePool);
            }
            break;
        case SNAPSHOT:
            final SnapshotInfo snapshotInfo = (SnapshotInfo) dataObject;
            final String rscName = LinstorUtil.RSC_PREFIX + snapshotInfo.getBaseVolume().getPath();
            deleteSnapshot(dataStore, rscName, getSnapshotName(snapshotInfo.getUuid()));
            long usedBytes = storagePool.getUsedBytes() - snapshotInfo.getSize();
            storagePool.setUsedBytes(Math.max(0, usedBytes));
            _storagePoolDao.update(storagePoolId, storagePool);
            break;
        default:
            errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
            s_logger.error(errMsg);
    }
    if (callback != null) {
        CommandResult result = new CommandResult();
        result.setResult(errMsg);
        callback.complete(result);
    }
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 17 with CommandResult

use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.

the class DateraPrimaryDataStoreDriver method deleteAsync.

/**
 * Entrypoint for delete operations.
 *
 * @param dataStore  Primary storage
 * @param dataObject object to delete
 * @param callback   used for async, complete the callback after the operation
 *                   is done.
 */
@Override
public void deleteAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
    String errMsg = null;
    try {
        if (dataObject.getType() == DataObjectType.VOLUME) {
            s_logger.debug("deleteAsync - deleting volume");
            deleteVolume((VolumeInfo) dataObject, dataStore.getId());
        } else if (dataObject.getType() == DataObjectType.SNAPSHOT) {
            s_logger.debug("deleteAsync - deleting snapshot");
            deleteSnapshot((SnapshotInfo) dataObject, dataStore.getId());
        } else if (dataObject.getType() == DataObjectType.TEMPLATE) {
            s_logger.debug("deleteAsync - deleting template");
            deleteTemplate((TemplateInfo) dataObject, dataStore.getId());
        } else {
            errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
        }
    } catch (Exception ex) {
        errMsg = ex.getMessage();
        s_logger.error(errMsg);
    }
    CommandResult result = new CommandResult();
    result.setResult(errMsg);
    callback.complete(result);
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 18 with CommandResult

use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.

the class LinstorPrimaryDataStoreDriverImpl method revertSnapshot.

@Override
public void revertSnapshot(SnapshotInfo snapshot, SnapshotInfo snapshotOnPrimaryStore, AsyncCompletionCallback<CommandResult> callback) {
    s_logger.debug("Linstor: revertSnapshot");
    final VolumeInfo volumeInfo = snapshot.getBaseVolume();
    VolumeVO volumeVO = _volumeDao.findById(volumeInfo.getId());
    if (volumeVO == null || volumeVO.getRemoved() != null) {
        CommandResult commandResult = new CommandResult();
        commandResult.setResult("The volume that the snapshot belongs to no longer exists.");
        callback.complete(commandResult);
        return;
    }
    String resultMsg;
    try {
        final StoragePool pool = (StoragePool) snapshot.getDataStore();
        final String rscName = LinstorUtil.RSC_PREFIX + volumeInfo.getUuid();
        final String snapName = LinstorUtil.RSC_PREFIX + snapshot.getUuid();
        final DevelopersApi linstorApi = LinstorUtil.getLinstorAPI(pool.getHostAddress());
        ApiCallRcList answers = linstorApi.resourceSnapshotRollback(rscName, snapName);
        resultMsg = checkLinstorAnswers(answers);
    } catch (ApiException apiEx) {
        s_logger.error("Linstor: ApiEx - " + apiEx.getMessage());
        resultMsg = apiEx.getBestMessage();
    }
    if (callback != null) {
        CommandResult result = new CommandResult();
        result.setResult(resultMsg);
        callback.complete(result);
    }
}
Also used : ApiCallRcList(com.linbit.linstor.api.model.ApiCallRcList) StoragePool(com.cloud.storage.StoragePool) VolumeVO(com.cloud.storage.VolumeVO) DevelopersApi(com.linbit.linstor.api.DevelopersApi) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) ApiException(com.linbit.linstor.api.ApiException)

Example 19 with CommandResult

use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.

the class ElastistorPrimaryDataStoreDriver method deleteAsync.

@Override
public void deleteAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
    String errMsg = null;
    StoragePoolVO storagePool = _storagePoolDao.findById(dataStore.getId());
    if (!(storagePool.isManaged())) {
        super.deleteAsync(dataStore, dataObject, callback);
        return;
    }
    if (dataObject.getType() == DataObjectType.VOLUME) {
        VolumeInfo volumeInfo = (VolumeInfo) dataObject;
        long storagePoolId = dataStore.getId();
        boolean result = false;
        try {
            result = ElastistorUtil.deleteElastistorVolume(volumeInfo.getUuid());
        } catch (Throwable e) {
            e.printStackTrace();
            CommandResult result2 = new CommandResult();
            result2.setResult(e.toString());
            callback.complete(result2);
        }
        if (result) {
            long usedBytes = storagePool.getUsedBytes();
            long capacityIops = storagePool.getCapacityIops();
            usedBytes -= volumeInfo.getSize();
            capacityIops += volumeInfo.getMaxIops();
            storagePool.setUsedBytes(usedBytes < 0 ? 0 : usedBytes);
            storagePool.setCapacityIops(capacityIops < 0 ? 0 : capacityIops);
            _storagePoolDao.update(storagePoolId, storagePool);
        } else {
            errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
        }
    } else {
        errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
    }
    CommandResult result = new CommandResult();
    result.setResult(errMsg);
    callback.complete(result);
}
Also used : StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 20 with CommandResult

use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.

the class TemplateManagerImpl method createPrivateTemplate.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
    final long templateId = command.getEntityId();
    Long volumeId = command.getVolumeId();
    Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    Account caller = CallContext.current().getCallingAccount();
    try {
        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.getImageStoreWithFreeCapacity(zoneId);
        if (store == null) {
            throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
        }
        AsyncCallFuture<TemplateApiResult> future = null;
        if (snapshotId != null) {
            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.
                    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");
                    }
                }
                _accountMgr.checkAccess(caller, null, true, snapInfo);
                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) {
            VolumeInfo volInfo = _volFactory.getVolume(volumeId);
            if (volInfo == null) {
                throw new InvalidParameterValueException("No such volume exist");
            }
            _accountMgr.checkAccess(caller, null, true, volInfo);
            future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        CommandResult result = null;
        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 {
                VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                _tmpltZoneDao.persist(templateZone);
            }
            privateTemplate = _tmpltDao.findById(templateId);
            TemplateDataStoreVO srcTmpltStore = _tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
            UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), zoneId, privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), srcTmpltStore.getPhysicalSize(), privateTemplate.getSize());
            _usageEventDao.persist(usageEvent);
        } catch (InterruptedException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        } catch (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(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 : Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) UsageEventVO(com.cloud.event.UsageEventVO) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DataStoreRole(com.cloud.storage.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(org.apache.cloudstack.storage.command.CommandResult) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

CommandResult (org.apache.cloudstack.storage.command.CommandResult)37 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)35 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)15 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)12 ExecutionException (java.util.concurrent.ExecutionException)11 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)9 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)9 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)8 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)7 VolumeVO (com.cloud.storage.VolumeVO)6 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)6 HashMap (java.util.HashMap)6 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)5 Answer (com.cloud.agent.api.Answer)4 MigrateWithStorageAnswer (com.cloud.agent.api.MigrateWithStorageAnswer)4 MigrateWithStorageCommand (com.cloud.agent.api.MigrateWithStorageCommand)4 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)4 Host (com.cloud.host.Host)4 VMInstanceVO (com.cloud.vm.VMInstanceVO)4