Search in sources :

Example 1 with DeleteSnapshotsDirCommand

use of com.cloud.agent.api.DeleteSnapshotsDirCommand in project cosmic by MissionCriticalCloud.

the class SnapshotManagerImpl method deleteSnapshotDirsForAccount.

@Override
public boolean deleteSnapshotDirsForAccount(final long accountId) {
    final List<VolumeVO> volumes = _volsDao.findIncludingRemovedByAccount(accountId);
    // The above call will list only non-destroyed volumes.
    // So call this method before marking the volumes as destroyed.
    // i.e Call them before the VMs for those volumes are destroyed.
    boolean success = true;
    for (final VolumeVO volume : volumes) {
        if (volume.getPoolId() == null) {
            continue;
        }
        final Long volumeId = volume.getId();
        final Long dcId = volume.getDataCenterId();
        if (_snapshotDao.listByVolumeIdIncludingRemoved(volumeId).isEmpty()) {
            // This volume doesn't have any snapshots. Nothing do delete.
            continue;
        }
        final List<DataStore> ssHosts = dataStoreMgr.getImageStoresByScope(new ZoneScope(dcId));
        for (final DataStore ssHost : ssHosts) {
            final String snapshotDir = TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR + "/" + accountId + "/" + volumeId;
            final DeleteSnapshotsDirCommand cmd = new DeleteSnapshotsDirCommand(ssHost.getTO(), snapshotDir);
            final EndPoint ep = _epSelector.select(ssHost);
            final Answer answer;
            if (ep == null) {
                final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                answer = new Answer(cmd, false, errMsg);
            } else {
                answer = ep.sendMessage(cmd);
            }
            if (answer != null && answer.getResult()) {
                s_logger.debug("Deleted all snapshots for volume: " + volumeId + " under account: " + accountId);
            } else {
                success = false;
                if (answer != null) {
                    s_logger.warn("Failed to delete all snapshot for volume " + volumeId + " on secondary storage " + ssHost.getUri());
                    s_logger.error(answer.getDetails());
                }
            }
        }
        // Either way delete the snapshots for this volume.
        final List<SnapshotVO> snapshots = listSnapsforVolume(volumeId);
        for (final SnapshotVO snapshot : snapshots) {
            final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.DELETE);
            if (snapshotStrategy == null) {
                s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshot.getId() + "'");
                continue;
            }
            final SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Image);
            if (snapshotStrategy.deleteSnapshot(snapshot.getId())) {
                if (Type.MANUAL == snapshot.getRecurringType()) {
                    _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.snapshot);
                    if (snapshotStoreRef != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
                    }
                }
            }
        }
    }
    // Returns true if snapshotsDir has been deleted for all volumes.
    return success;
}
Also used : SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) DeleteSnapshotsDirCommand(com.cloud.agent.api.DeleteSnapshotsDirCommand) Answer(com.cloud.agent.api.Answer) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy)

Example 2 with DeleteSnapshotsDirCommand

use of com.cloud.agent.api.DeleteSnapshotsDirCommand in project cloudstack by apache.

the class SnapshotManagerImpl method deleteSnapshotDirsForAccount.

@Override
public boolean deleteSnapshotDirsForAccount(long accountId) {
    List<VolumeVO> volumes = _volsDao.findIncludingRemovedByAccount(accountId);
    // The above call will list only non-destroyed volumes.
    // So call this method before marking the volumes as destroyed.
    // i.e Call them before the VMs for those volumes are destroyed.
    boolean success = true;
    for (VolumeVO volume : volumes) {
        if (volume.getPoolId() == null) {
            continue;
        }
        Long volumeId = volume.getId();
        Long dcId = volume.getDataCenterId();
        if (_snapshotDao.listByVolumeIdIncludingRemoved(volumeId).isEmpty()) {
            // This volume doesn't have any snapshots. Nothing do delete.
            continue;
        }
        List<DataStore> ssHosts = dataStoreMgr.getImageStoresByScope(new ZoneScope(dcId));
        for (DataStore ssHost : ssHosts) {
            String snapshotDir = TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR + "/" + accountId + "/" + volumeId;
            DeleteSnapshotsDirCommand cmd = new DeleteSnapshotsDirCommand(ssHost.getTO(), snapshotDir);
            EndPoint ep = _epSelector.select(ssHost);
            Answer answer = null;
            if (ep == null) {
                String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                answer = new Answer(cmd, false, errMsg);
            } else {
                answer = ep.sendMessage(cmd);
            }
            if ((answer != null) && answer.getResult()) {
                s_logger.debug("Deleted all snapshots for volume: " + volumeId + " under account: " + accountId);
            } else {
                success = false;
                if (answer != null) {
                    s_logger.warn("Failed to delete all snapshot for volume " + volumeId + " on secondary storage " + ssHost.getUri());
                    s_logger.error(answer.getDetails());
                }
            }
        }
        // Either way delete the snapshots for this volume.
        List<SnapshotVO> snapshots = listSnapsforVolume(volumeId);
        for (SnapshotVO snapshot : snapshots) {
            SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.DELETE);
            if (snapshotStrategy == null) {
                s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshot.getId() + "'");
                continue;
            }
            SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Image);
            if (snapshotStrategy.deleteSnapshot(snapshot.getId())) {
                if (Type.MANUAL == snapshot.getRecurringType()) {
                    _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.snapshot);
                    if (snapshotStoreRef != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
                    }
                }
                // Log event after successful deletion
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_DELETE, snapshot.getAccountId(), volume.getDataCenterId(), snapshot.getId(), snapshot.getName(), null, null, volume.getSize(), snapshot.getClass().getName(), snapshot.getUuid());
            }
        }
    }
    // Returns true if snapshotsDir has been deleted for all volumes.
    return success;
}
Also used : SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) DeleteSnapshotsDirCommand(com.cloud.agent.api.DeleteSnapshotsDirCommand) Answer(com.cloud.agent.api.Answer) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)

Aggregations

Answer (com.cloud.agent.api.Answer)2 DeleteSnapshotsDirCommand (com.cloud.agent.api.DeleteSnapshotsDirCommand)2 SnapshotVO (com.cloud.storage.SnapshotVO)2 VolumeVO (com.cloud.storage.VolumeVO)2 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)2 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)1 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)1 SnapshotStrategy (com.cloud.engine.subsystem.api.storage.SnapshotStrategy)1 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)1 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)1 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)1 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)1 SnapshotStrategy (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)1 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)1 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)1