Search in sources :

Example 46 with TemplateInfo

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

the class TemplateManagerImpl method prepareIso.

// for ISO, we need to consider whether to copy to cache storage or not if it is not on NFS, since our hypervisor resource always assumes that they are in NFS
@Override
public TemplateInfo prepareIso(long isoId, long dcId) {
    TemplateInfo tmplt = _tmplFactory.getTemplate(isoId, DataStoreRole.Image, dcId);
    if (tmplt == null || tmplt.getFormat() != ImageFormat.ISO) {
        s_logger.warn("ISO: " + isoId + " does not exist in vm_template table");
        return null;
    }
    if (tmplt.getDataStore() != null && !(tmplt.getDataStore().getTO() instanceof NfsTO)) {
        // if it is s3, need to download into cache storage first
        Scope destScope = new ZoneScope(dcId);
        TemplateInfo cacheData = (TemplateInfo) cacheMgr.createCacheObject(tmplt, destScope);
        if (cacheData == null) {
            s_logger.error("Failed in copy iso from S3 to cache storage");
            return null;
        }
        return cacheData;
    } else {
        return tmplt;
    }
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) Scope(org.apache.cloudstack.engine.subsystem.api.storage.Scope) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) PublishScope(org.apache.cloudstack.framework.messagebus.PublishScope) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 47 with TemplateInfo

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

the class TemplateManagerImpl method prepareTemplateForCreate.

@Override
@DB
public VMTemplateStoragePoolVO prepareTemplateForCreate(VMTemplateVO templ, StoragePool pool) {
    VMTemplateVO template = _tmpltDao.findById(templ.getId(), true);
    long poolId = pool.getId();
    long templateId = template.getId();
    VMTemplateStoragePoolVO templateStoragePoolRef = null;
    TemplateDataStoreVO templateStoreRef = null;
    templateStoragePoolRef = _tmpltPoolDao.findByPoolTemplate(poolId, templateId);
    if (templateStoragePoolRef != null) {
        templateStoragePoolRef.setMarkedForGC(false);
        _tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef);
        if (templateStoragePoolRef.getDownloadState() == Status.DOWNLOADED) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId);
            }
            return templateStoragePoolRef;
        }
    }
    templateStoreRef = _tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, pool.getDataCenterId(), VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
    if (templateStoreRef == null) {
        s_logger.error("Unable to find a secondary storage host who has completely downloaded the template.");
        return null;
    }
    List<StoragePoolHostVO> vos = _poolHostDao.listByHostStatus(poolId, com.cloud.host.Status.Up);
    if (vos == null || vos.isEmpty()) {
        throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool");
    }
    if (templateStoragePoolRef == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Downloading template " + templateId + " to pool " + poolId);
        }
        DataStore srcSecStore = _dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image);
        TemplateInfo srcTemplate = _tmplFactory.getTemplate(templateId, srcSecStore);
        AsyncCallFuture<TemplateApiResult> future = _tmpltSvr.prepareTemplateOnPrimary(srcTemplate, pool);
        try {
            TemplateApiResult result = future.get();
            if (result.isFailed()) {
                s_logger.debug("prepare template failed:" + result.getResult());
                return null;
            }
            return _tmpltPoolDao.findByPoolTemplate(poolId, templateId);
        } catch (Exception ex) {
            s_logger.debug("failed to copy template from image store:" + srcSecStore.getName() + " to primary storage");
        }
    }
    return null;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) DB(com.cloud.utils.db.DB)

Example 48 with TemplateInfo

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

the class TemplateManagerImpl method copy.

@Override
@DB
public boolean copy(long userId, VMTemplateVO template, DataStore srcSecStore, DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException {
    long tmpltId = template.getId();
    long dstZoneId = dstZone.getId();
    // find all eligible image stores for the destination zone
    List<DataStore> dstSecStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(dstZoneId));
    if (dstSecStores == null || dstSecStores.isEmpty()) {
        throw new StorageUnavailableException("Destination zone is not ready, no image store associated", DataCenter.class, dstZone.getId());
    }
    AccountVO account = _accountDao.findById(template.getAccountId());
    // find the size of the template to be copied
    TemplateDataStoreVO srcTmpltStore = _tmplStoreDao.findByStoreTemplate(srcSecStore.getId(), tmpltId);
    _resourceLimitMgr.checkResourceLimit(account, ResourceType.template);
    _resourceLimitMgr.checkResourceLimit(account, ResourceType.secondary_storage, new Long(srcTmpltStore.getSize()).longValue());
    // Event details
    String copyEventType;
    if (template.getFormat().equals(ImageFormat.ISO)) {
        copyEventType = EventTypes.EVENT_ISO_COPY;
    } else {
        copyEventType = EventTypes.EVENT_TEMPLATE_COPY;
    }
    TemplateInfo srcTemplate = _tmplFactory.getTemplate(template.getId(), srcSecStore);
    // for that zone
    for (DataStore dstSecStore : dstSecStores) {
        TemplateDataStoreVO dstTmpltStore = _tmplStoreDao.findByStoreTemplate(dstSecStore.getId(), tmpltId);
        if (dstTmpltStore != null && dstTmpltStore.getDownloadState() == Status.DOWNLOADED) {
            // already downloaded on this image store
            return true;
        }
        if (dstTmpltStore != null && dstTmpltStore.getDownloadState() != Status.DOWNLOAD_IN_PROGRESS) {
            _tmplStoreDao.removeByTemplateStore(tmpltId, dstSecStore.getId());
        }
        AsyncCallFuture<TemplateApiResult> future = _tmpltSvr.copyTemplate(srcTemplate, dstSecStore);
        try {
            TemplateApiResult result = future.get();
            if (result.isFailed()) {
                s_logger.debug("copy template failed for image store " + dstSecStore.getName() + ":" + result.getResult());
                // try next image store
                continue;
            }
            _tmpltDao.addTemplateToZone(template, dstZoneId);
            if (account.getId() != Account.ACCOUNT_ID_SYSTEM) {
                UsageEventUtils.publishUsageEvent(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltStore.getPhysicalSize(), srcTmpltStore.getSize(), template.getClass().getName(), template.getUuid());
            }
            return true;
        } catch (Exception ex) {
            s_logger.debug("failed to copy template to image store:" + dstSecStore.getName() + " ,will try next one");
        }
    }
    return false;
}
Also used : TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) AccountVO(com.cloud.user.AccountVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) DB(com.cloud.utils.db.DB)

Example 49 with TemplateInfo

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

the class StorageManagerImpl method getBytesRequiredForTemplate.

private long getBytesRequiredForTemplate(VMTemplateVO tmpl, StoragePool pool) {
    DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(pool.getStorageProviderName());
    DataStoreDriver storeDriver = storeProvider.getDataStoreDriver();
    if (storeDriver instanceof PrimaryDataStoreDriver) {
        PrimaryDataStoreDriver primaryStoreDriver = (PrimaryDataStoreDriver) storeDriver;
        TemplateInfo templateInfo = tmplFactory.getReadyTemplateOnImageStore(tmpl.getId(), pool.getDataCenterId());
        return primaryStoreDriver.getBytesRequiredForTemplate(templateInfo, pool);
    }
    return tmpl.getSize();
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) PrimaryDataStoreDriver(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver) DataStoreProvider(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider) PrimaryDataStoreDriver(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver) DataStoreDriver(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreDriver)

Aggregations

TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)49 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)19 VMTemplateVO (com.cloud.storage.VMTemplateVO)17 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)15 ExecutionException (java.util.concurrent.ExecutionException)13 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)13 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)11 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)9 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)9 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)9 Test (org.testng.annotations.Test)9 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)8 DB (com.cloud.utils.db.DB)7 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)6 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)6 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)6 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)5 HashSet (java.util.HashSet)5 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)5