Search in sources :

Example 6 with SnapshotPolicyVO

use of com.cloud.storage.SnapshotPolicyVO in project cloudstack by apache.

the class SnapshotManagerImpl method createPolicy.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_CREATE, eventDescription = "creating snapshot policy")
public SnapshotPolicyVO createPolicy(CreateSnapshotPolicyCmd cmd, Account policyOwner) {
    Long volumeId = cmd.getVolumeId();
    boolean display = cmd.isDisplay();
    VolumeVO volume = _volsDao.findById(cmd.getVolumeId());
    if (volume == null) {
        throw new InvalidParameterValueException("Failed to create snapshot policy, unable to find a volume with id " + volumeId);
    }
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
    // If display is false we don't actually schedule snapshots.
    if (volume.getState() != Volume.State.Ready && display) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
    }
    if (volume.getTemplateId() != null) {
        VMTemplateVO template = _templateDao.findById(volume.getTemplateId());
        if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM) {
            throw new InvalidParameterValueException("VolumeId: " + volumeId + " is for System VM , Creating snapshot against System VM volumes is not supported");
        }
    }
    AccountVO owner = _accountDao.findById(volume.getAccountId());
    Long instanceId = volume.getInstanceId();
    if (instanceId != null) {
        // It is not detached, but attached to a VM
        if (_vmDao.findById(instanceId) == null) {
            // It is not a UserVM but a SystemVM or DomR
            throw new InvalidParameterValueException("Failed to create snapshot policy, snapshots of volumes attached to System or router VM are not allowed");
        }
    }
    IntervalType intvType = DateUtil.IntervalType.getIntervalType(cmd.getIntervalType());
    if (intvType == null) {
        throw new InvalidParameterValueException("Unsupported interval type " + cmd.getIntervalType());
    }
    Type type = getSnapshotType(intvType);
    TimeZone timeZone = TimeZone.getTimeZone(cmd.getTimezone());
    String timezoneId = timeZone.getID();
    if (!timezoneId.equals(cmd.getTimezone())) {
        s_logger.warn("Using timezone: " + timezoneId + " for running this snapshot policy as an equivalent of " + cmd.getTimezone());
    }
    try {
        DateUtil.getNextRunTime(intvType, cmd.getSchedule(), timezoneId, null);
    } catch (Exception e) {
        throw new InvalidParameterValueException("Invalid schedule: " + cmd.getSchedule() + " for interval type: " + cmd.getIntervalType());
    }
    if (cmd.getMaxSnaps() <= 0) {
        throw new InvalidParameterValueException("maxSnaps should be greater than 0");
    }
    int intervalMaxSnaps = type.getMax();
    if (cmd.getMaxSnaps() > intervalMaxSnaps) {
        throw new InvalidParameterValueException("maxSnaps exceeds limit: " + intervalMaxSnaps + " for interval type: " + cmd.getIntervalType());
    }
    // Verify that max doesn't exceed domain and account snapshot limits in case display is on
    if (display) {
        long accountLimit = _resourceLimitMgr.findCorrectResourceLimitForAccount(owner, ResourceType.snapshot);
        long domainLimit = _resourceLimitMgr.findCorrectResourceLimitForDomain(_domainMgr.getDomain(owner.getDomainId()), ResourceType.snapshot);
        int max = cmd.getMaxSnaps().intValue();
        if (!_accountMgr.isRootAdmin(owner.getId()) && ((accountLimit != -1 && max > accountLimit) || (domainLimit != -1 && max > domainLimit))) {
            String message = "domain/account";
            if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
                message = "domain/project";
            }
            throw new InvalidParameterValueException("Max number of snapshots shouldn't exceed the " + message + " level snapshot limit");
        }
    }
    SnapshotPolicyVO policy = _snapshotPolicyDao.findOneByVolumeInterval(volumeId, intvType);
    if (policy == null) {
        policy = new SnapshotPolicyVO(volumeId, cmd.getSchedule(), timezoneId, intvType, cmd.getMaxSnaps(), display);
        policy = _snapshotPolicyDao.persist(policy);
        _snapSchedMgr.scheduleNextSnapshotJob(policy);
    } else {
        try {
            boolean previousDisplay = policy.isDisplay();
            policy = _snapshotPolicyDao.acquireInLockTable(policy.getId());
            policy.setSchedule(cmd.getSchedule());
            policy.setTimezone(timezoneId);
            policy.setInterval((short) intvType.ordinal());
            policy.setMaxSnaps(cmd.getMaxSnaps());
            policy.setActive(true);
            policy.setDisplay(display);
            _snapshotPolicyDao.update(policy.getId(), policy);
            _snapSchedMgr.scheduleOrCancelNextSnapshotJobOnDisplayChange(policy, previousDisplay);
        } finally {
            if (policy != null) {
                _snapshotPolicyDao.releaseFromLockTable(policy.getId());
            }
        }
    }
    // TODO - Make createSnapshotPolicy - BaseAsyncCreate and remove this.
    CallContext.current().putContextParameter(SnapshotPolicy.class, policy.getUuid());
    return policy;
}
Also used : IntervalType(com.cloud.utils.DateUtil.IntervalType) VMTemplateVO(com.cloud.storage.VMTemplateVO) AccountVO(com.cloud.user.AccountVO) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) ScopeType(com.cloud.storage.ScopeType) Type(com.cloud.storage.Snapshot.Type) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ResourceType(com.cloud.configuration.Resource.ResourceType) IntervalType(com.cloud.utils.DateUtil.IntervalType) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) TimeZone(java.util.TimeZone) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 7 with SnapshotPolicyVO

use of com.cloud.storage.SnapshotPolicyVO in project cloudstack by apache.

the class SnapshotManagerImpl method updateSnapshotPolicy.

@Override
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_UPDATE, eventDescription = "updating snapshot policy", async = true)
public SnapshotPolicy updateSnapshotPolicy(UpdateSnapshotPolicyCmd cmd) {
    Long id = cmd.getId();
    String customUUID = cmd.getCustomId();
    Boolean display = cmd.getDisplay();
    SnapshotPolicyVO policyVO = _snapshotPolicyDao.findById(id);
    if (display != null) {
        boolean previousDisplay = policyVO.isDisplay();
        policyVO.setDisplay(display);
        _snapSchedMgr.scheduleOrCancelNextSnapshotJobOnDisplayChange(policyVO, previousDisplay);
    }
    if (customUUID != null)
        policyVO.setUuid(customUUID);
    _snapshotPolicyDao.update(id, policyVO);
    return policyVO;
}
Also used : SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) ActionEvent(com.cloud.event.ActionEvent)

Example 8 with SnapshotPolicyVO

use of com.cloud.storage.SnapshotPolicyVO in project cloudstack by apache.

the class SnapshotManagerImpl method deleteSnapshotPolicies.

@Override
public boolean deleteSnapshotPolicies(DeleteSnapshotPoliciesCmd cmd) {
    Long policyId = cmd.getId();
    List<Long> policyIds = cmd.getIds();
    Long userId = getSnapshotUserId();
    if ((policyId == null) && (policyIds == null)) {
        throw new InvalidParameterValueException("No policy id (or list of ids) specified.");
    }
    if (policyIds == null) {
        policyIds = new ArrayList<Long>();
        policyIds.add(policyId);
    } else if (policyIds.size() <= 0) {
        // Not even sure how this is even possible
        throw new InvalidParameterValueException("There are no policy ids");
    }
    for (Long policy : policyIds) {
        SnapshotPolicyVO snapshotPolicyVO = _snapshotPolicyDao.findById(policy);
        if (snapshotPolicyVO == null) {
            throw new InvalidParameterValueException("Policy id given: " + policy + " does not exist");
        }
        VolumeVO volume = _volsDao.findById(snapshotPolicyVO.getVolumeId());
        if (volume == null) {
            throw new InvalidParameterValueException("Policy id given: " + policy + " does not belong to a valid volume");
        }
        _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
    }
    boolean success = true;
    if (policyIds.contains(Snapshot.MANUAL_POLICY_ID)) {
        throw new InvalidParameterValueException("Invalid Policy id given: " + Snapshot.MANUAL_POLICY_ID);
    }
    for (Long pId : policyIds) {
        if (!deletePolicy(userId, pId)) {
            success = false;
            s_logger.warn("Failed to delete snapshot policy with Id: " + policyId);
            return success;
        }
    }
    return success;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 9 with SnapshotPolicyVO

use of com.cloud.storage.SnapshotPolicyVO in project cloudstack by apache.

the class SnapshotManagerImpl method listPoliciesforVolume.

@Override
public Pair<List<? extends SnapshotPolicy>, Integer> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) {
    Long volumeId = cmd.getVolumeId();
    boolean display = cmd.isDisplay();
    Long id = cmd.getId();
    Pair<List<SnapshotPolicyVO>, Integer> result = null;
    // TODO - Have a better way of doing this.
    if (id != null) {
        result = _snapshotPolicyDao.listAndCountById(id, display, null);
        if (result != null && result.first() != null && !result.first().isEmpty()) {
            SnapshotPolicyVO snapshotPolicy = result.first().get(0);
            volumeId = snapshotPolicy.getVolumeId();
        }
    }
    VolumeVO volume = _volsDao.findById(volumeId);
    if (volume == null) {
        throw new InvalidParameterValueException("Unable to find a volume with id " + volumeId);
    }
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
    if (result != null)
        return new Pair<List<? extends SnapshotPolicy>, Integer>(result.first(), result.second());
    result = _snapshotPolicyDao.listAndCountByVolumeId(volumeId, display);
    return new Pair<List<? extends SnapshotPolicy>, Integer>(result.first(), result.second());
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) Pair(com.cloud.utils.Pair)

Example 10 with SnapshotPolicyVO

use of com.cloud.storage.SnapshotPolicyVO in project cloudstack by apache.

the class SnapshotManagerImpl method postCreateRecurringSnapshotForPolicy.

private void postCreateRecurringSnapshotForPolicy(long userId, long volumeId, long snapshotId, long policyId) {
    // Use count query
    SnapshotVO spstVO = _snapshotDao.findById(snapshotId);
    Type type = spstVO.getRecurringType();
    int maxSnaps = type.getMax();
    List<SnapshotVO> snaps = listSnapsforVolumeTypeNotDestroyed(volumeId, type);
    SnapshotPolicyVO policy = _snapshotPolicyDao.findById(policyId);
    if (policy != null && policy.getMaxSnaps() < maxSnaps) {
        maxSnaps = policy.getMaxSnaps();
    }
    while (snaps.size() > maxSnaps && snaps.size() > 1) {
        SnapshotVO oldestSnapshot = snaps.get(0);
        long oldSnapId = oldestSnapshot.getId();
        if (policy != null) {
            s_logger.debug("Max snaps: " + policy.getMaxSnaps() + " exceeded for snapshot policy with Id: " + policyId + ". Deleting oldest snapshot: " + oldSnapId);
        }
        if (deleteSnapshot(oldSnapId)) {
            //log Snapshot delete event
            ActionEventUtils.onCompletedActionEvent(User.UID_SYSTEM, oldestSnapshot.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_SNAPSHOT_DELETE, "Successfully deleted oldest snapshot: " + oldSnapId, 0);
        }
        snaps.remove(oldestSnapshot);
    }
}
Also used : ScopeType(com.cloud.storage.ScopeType) Type(com.cloud.storage.Snapshot.Type) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ResourceType(com.cloud.configuration.Resource.ResourceType) IntervalType(com.cloud.utils.DateUtil.IntervalType) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)

Aggregations

SnapshotPolicyVO (com.cloud.storage.SnapshotPolicyVO)13 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 VolumeVO (com.cloud.storage.VolumeVO)4 IntervalType (com.cloud.utils.DateUtil.IntervalType)4 ResourceType (com.cloud.configuration.Resource.ResourceType)2 ActionEvent (com.cloud.event.ActionEvent)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)2 ResourceObjectType (com.cloud.server.ResourceTag.ResourceObjectType)2 ScopeType (com.cloud.storage.ScopeType)2 Type (com.cloud.storage.Snapshot.Type)2 SnapshotScheduleVO (com.cloud.storage.SnapshotScheduleVO)2 DB (com.cloud.utils.db.DB)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)2 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)1 SnapshotVO (com.cloud.storage.SnapshotVO)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1