Search in sources :

Example 11 with ZoneScope

use of org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope in project cloudstack by apache.

the class HypervisorTemplateAdapter method create.

@Override
public VMTemplateVO create(TemplateProfile profile) {
    // persist entry in vm_template, vm_template_details and template_zone_ref tables, not that entry at template_store_ref is not created here, and created in createTemplateAsync.
    VMTemplateVO template = persistTemplate(profile, State.Active);
    if (template == null) {
        throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate());
    }
    // find all eligible image stores for this zone scope
    List<DataStore> imageStores = storeMgr.getImageStoresByScope(new ZoneScope(profile.getZoneId()));
    if (imageStores == null || imageStores.size() == 0) {
        throw new CloudRuntimeException("Unable to find image store to download template " + profile.getTemplate());
    }
    Set<Long> zoneSet = new HashSet<Long>();
    // For private templates choose a random store. TODO - Have a better algorithm based on size, no. of objects, load etc.
    Collections.shuffle(imageStores);
    for (DataStore imageStore : imageStores) {
        // skip data stores for a disabled zone
        Long zoneId = imageStore.getScope().getScopeId();
        if (zoneId != null) {
            DataCenterVO zone = _dcDao.findById(zoneId);
            if (zone == null) {
                s_logger.warn("Unable to find zone by id " + zoneId + ", so skip downloading template to its image store " + imageStore.getId());
                continue;
            }
            // Check if zone is disabled
            if (Grouping.AllocationState.Disabled == zone.getAllocationState()) {
                s_logger.info("Zone " + zoneId + " is disabled, so skip downloading template to its image store " + imageStore.getId());
                continue;
            }
            // Check if image store has enough capacity for template
            if (!_statsCollector.imageStoreHasEnoughCapacity(imageStore)) {
                s_logger.info("Image store doesn't has enough capacity, so skip downloading template to this image store " + imageStore.getId());
                continue;
            }
            // We want to download private template to one of the image store in a zone
            if (isPrivateTemplate(template) && zoneSet.contains(zoneId)) {
                continue;
            } else {
                zoneSet.add(zoneId);
            }
        }
        TemplateInfo tmpl = imageFactory.getTemplate(template.getId(), imageStore);
        CreateTemplateContext<TemplateApiResult> context = new CreateTemplateContext<TemplateApiResult>(null, tmpl);
        AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createTemplateAsyncCallBack(null, null));
        caller.setContext(context);
        imageService.createTemplateAsync(tmpl, imageStore, caller);
    }
    _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
    return template;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) HashSet(java.util.HashSet)

Example 12 with ZoneScope

use of org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope in project cloudstack by apache.

the class StorageManagerImpl method createPool.

@Override
public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws ResourceInUseException, IllegalArgumentException, UnknownHostException, ResourceUnavailableException {
    String providerName = cmd.getStorageProviderName();
    DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(providerName);
    if (storeProvider == null) {
        storeProvider = _dataStoreProviderMgr.getDefaultPrimaryDataStoreProvider();
        if (storeProvider == null) {
            throw new InvalidParameterValueException("can't find storage provider: " + providerName);
        }
    }
    Long clusterId = cmd.getClusterId();
    Long podId = cmd.getPodId();
    Long zoneId = cmd.getZoneId();
    ScopeType scopeType = ScopeType.CLUSTER;
    String scope = cmd.getScope();
    if (scope != null) {
        try {
            scopeType = Enum.valueOf(ScopeType.class, scope.toUpperCase());
        } catch (Exception e) {
            throw new InvalidParameterValueException("invalid scope for pool " + scope);
        }
    }
    if (scopeType == ScopeType.CLUSTER && clusterId == null) {
        throw new InvalidParameterValueException("cluster id can't be null, if scope is cluster");
    } else if (scopeType == ScopeType.ZONE && zoneId == null) {
        throw new InvalidParameterValueException("zone id can't be null, if scope is zone");
    }
    HypervisorType hypervisorType = HypervisorType.KVM;
    if (scopeType == ScopeType.ZONE) {
        // ignore passed clusterId and podId
        clusterId = null;
        podId = null;
        String hypervisor = cmd.getHypervisor();
        if (hypervisor != null) {
            try {
                hypervisorType = HypervisorType.getType(hypervisor);
            } catch (Exception e) {
                throw new InvalidParameterValueException("invalid hypervisor type " + hypervisor);
            }
        } else {
            throw new InvalidParameterValueException("Missing parameter hypervisor. Hypervisor type is required to create zone wide primary storage.");
        }
        if (hypervisorType != HypervisorType.KVM && hypervisorType != HypervisorType.VMware && hypervisorType != HypervisorType.Hyperv && hypervisorType != HypervisorType.LXC && hypervisorType != HypervisorType.Any) {
            throw new InvalidParameterValueException("zone wide storage pool is not supported for hypervisor type " + hypervisor);
        }
    }
    Map<String, String> details = extractApiParamAsMap(cmd.getDetails());
    DataCenterVO zone = _dcDao.findById(cmd.getZoneId());
    if (zone == null) {
        throw new InvalidParameterValueException("unable to find zone by id " + zoneId);
    }
    // Check if zone is disabled
    Account account = CallContext.current().getCallingAccount();
    if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getId())) {
        throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId);
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("zoneId", zone.getId());
    params.put("clusterId", clusterId);
    params.put("podId", podId);
    params.put("url", cmd.getUrl());
    params.put("tags", cmd.getTags());
    params.put("name", cmd.getStoragePoolName());
    params.put("details", details);
    params.put("providerName", storeProvider.getName());
    params.put("managed", cmd.isManaged());
    params.put("capacityBytes", cmd.getCapacityBytes());
    params.put("capacityIops", cmd.getCapacityIops());
    DataStoreLifeCycle lifeCycle = storeProvider.getDataStoreLifeCycle();
    DataStore store = null;
    try {
        store = lifeCycle.initialize(params);
        if (scopeType == ScopeType.CLUSTER) {
            ClusterScope clusterScope = new ClusterScope(clusterId, podId, zoneId);
            lifeCycle.attachCluster(store, clusterScope);
        } else if (scopeType == ScopeType.ZONE) {
            ZoneScope zoneScope = new ZoneScope(zoneId);
            lifeCycle.attachZone(store, zoneScope, hypervisorType);
        }
    } catch (Exception e) {
        s_logger.debug("Failed to add data store: " + e.getMessage(), e);
        try {
            // not deleting data store.
            if (store != null) {
                lifeCycle.deleteDataStore(store);
            }
        } catch (Exception ex) {
            s_logger.debug("Failed to clean up storage pool: " + ex.getMessage());
        }
        throw new CloudRuntimeException("Failed to add data store: " + e.getMessage(), e);
    }
    return (PrimaryDataStoreInfo) _dataStoreMgr.getDataStore(store.getId(), DataStoreRole.Primary);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PrimaryDataStoreInfo(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo) Account(com.cloud.user.Account) HashMap(java.util.HashMap) DataStoreProvider(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) StorageConflictException(com.cloud.exception.StorageConflictException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) DataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreLifeCycle) PrimaryDataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) ClusterScope(org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 13 with ZoneScope

use of org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope in project cloudstack by apache.

the class StorageManagerImpl method getSecondaryStorageUsedStats.

@Override
public CapacityVO getSecondaryStorageUsedStats(Long hostId, Long zoneId) {
    SearchCriteria<HostVO> sc = _hostDao.createSearchCriteria();
    if (zoneId != null) {
        sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
    }
    List<Long> hosts = new ArrayList<Long>();
    if (hostId != null) {
        hosts.add(hostId);
    } else {
        List<DataStore> stores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(zoneId));
        if (stores != null) {
            for (DataStore store : stores) {
                hosts.add(store.getId());
            }
        }
    }
    CapacityVO capacity = new CapacityVO(hostId, zoneId, null, null, 0, 0, Capacity.CAPACITY_TYPE_SECONDARY_STORAGE);
    for (Long id : hosts) {
        StorageStats stats = ApiDBUtils.getSecondaryStorageStatistics(id);
        if (stats == null) {
            continue;
        }
        capacity.setUsedCapacity(stats.getByteUsed() + capacity.getUsedCapacity());
        capacity.setTotalCapacity(stats.getCapacityBytes() + capacity.getTotalCapacity());
    }
    return capacity;
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) CapacityVO(com.cloud.capacity.CapacityVO) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO)

Example 14 with ZoneScope

use of org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope in project cloudstack by apache.

the class StorageSystemDataMotionStrategy method getZoneScope.

private Scope getZoneScope(Scope scope) {
    ZoneScope zoneScope;
    if (scope instanceof ClusterScope) {
        ClusterScope clusterScope = (ClusterScope) scope;
        zoneScope = new ZoneScope(clusterScope.getZoneId());
    } else if (scope instanceof HostScope) {
        HostScope hostScope = (HostScope) scope;
        zoneScope = new ZoneScope(hostScope.getZoneId());
    } else {
        zoneScope = (ZoneScope) scope;
    }
    return zoneScope;
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) ClusterScope(org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope) HostScope(org.apache.cloudstack.engine.subsystem.api.storage.HostScope)

Example 15 with ZoneScope

use of org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope in project cloudstack by apache.

the class StorageSystemDataMotionStrategy method performCopyOfVdi.

/**
     * Copies data from secondary storage to a primary volume
     * @param volumeInfo The primary volume
     * @param snapshotInfo  destination of the copy
     * @param hostVO the host used to copy the data
     * @return result of the copy
     */
private CopyCmdAnswer performCopyOfVdi(VolumeInfo volumeInfo, SnapshotInfo snapshotInfo, HostVO hostVO) {
    Snapshot.LocationType locationType = snapshotInfo.getLocationType();
    String value = _configDao.getValue(Config.PrimaryStorageDownloadWait.toString());
    int primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue()));
    DataObject srcData = snapshotInfo;
    CopyCmdAnswer copyCmdAnswer = null;
    DataObject cacheData = null;
    boolean needCacheStorage = needCacheStorage(snapshotInfo, volumeInfo);
    if (needCacheStorage) {
        cacheData = cacheSnapshotChain(snapshotInfo, new ZoneScope(volumeInfo.getDataCenterId()));
        srcData = cacheData;
    }
    CopyCommand copyCommand = new CopyCommand(srcData.getTO(), volumeInfo.getTO(), primaryStorageDownloadWait, VirtualMachineManager.ExecuteInSequence.value());
    try {
        if (Snapshot.LocationType.PRIMARY.equals(locationType)) {
            _volumeService.grantAccess(snapshotInfo, hostVO, snapshotInfo.getDataStore());
            Map<String, String> srcDetails = getSnapshotDetails(snapshotInfo);
            copyCommand.setOptions(srcDetails);
        }
        _volumeService.grantAccess(volumeInfo, hostVO, volumeInfo.getDataStore());
        Map<String, String> destDetails = getVolumeDetails(volumeInfo);
        copyCommand.setOptions2(destDetails);
        copyCmdAnswer = (CopyCmdAnswer) _agentMgr.send(hostVO.getId(), copyCommand);
    } catch (CloudRuntimeException | AgentUnavailableException | OperationTimedoutException ex) {
        String msg = "Failed to perform VDI copy : ";
        LOGGER.warn(msg, ex);
        throw new CloudRuntimeException(msg + ex.getMessage());
    } finally {
        if (Snapshot.LocationType.PRIMARY.equals(locationType)) {
            _volumeService.revokeAccess(snapshotInfo, hostVO, snapshotInfo.getDataStore());
        }
        _volumeService.revokeAccess(volumeInfo, hostVO, volumeInfo.getDataStore());
        if (needCacheStorage && copyCmdAnswer != null && copyCmdAnswer.getResult()) {
            cacheMgr.deleteCacheObject(cacheData);
        }
    }
    return copyCmdAnswer;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) Snapshot(com.cloud.storage.Snapshot) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Aggregations

ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)23 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)6 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)6 ClusterScope (org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)4 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)4 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)4 Answer (com.cloud.agent.api.Answer)3 NfsTO (com.cloud.agent.api.to.NfsTO)3 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 ConnectionException (com.cloud.exception.ConnectionException)3 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)3 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3 HostVO (com.cloud.host.HostVO)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3