Search in sources :

Example 96 with VMTemplateVO

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

the class TemplateManagerImpl method copyTemplate.

@Override
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true)
public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException {
    Long templateId = cmd.getId();
    Long userId = CallContext.current().getCallingUserId();
    Long sourceZoneId = cmd.getSourceZoneId();
    Long destZoneId = cmd.getDestinationZoneId();
    Account caller = CallContext.current().getCallingAccount();
    // Verify parameters
    VMTemplateVO template = _tmpltDao.findById(templateId);
    if (template == null || template.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find template with id");
    }
    DataStore srcSecStore = null;
    if (sourceZoneId != null) {
        // template is on zone-wide secondary storage
        srcSecStore = getImageStore(sourceZoneId, templateId);
    } else {
        // template is on region store
        srcSecStore = getImageStore(templateId);
    }
    if (srcSecStore == null) {
        throw new InvalidParameterValueException("There is no template " + templateId + " ready on image store.");
    }
    if (template.isCrossZones()) {
        // sync template from cache store to region store if it is not there, for cases where we are going to migrate existing NFS to S3.
        _tmpltSvr.syncTemplateToRegionStore(templateId, srcSecStore);
        s_logger.debug("Template " + templateId + " is cross-zone, don't need to copy");
        return template;
    }
    if (sourceZoneId != null) {
        if (sourceZoneId.equals(destZoneId)) {
            throw new InvalidParameterValueException("Please specify different source and destination zones.");
        }
        DataCenterVO sourceZone = _dcDao.findById(sourceZoneId);
        if (sourceZone == null) {
            throw new InvalidParameterValueException("Please specify a valid source zone.");
        }
    }
    DataCenterVO dstZone = _dcDao.findById(destZoneId);
    if (dstZone == null) {
        throw new InvalidParameterValueException("Please specify a valid destination zone.");
    }
    DataStore dstSecStore = getImageStore(destZoneId, templateId);
    if (dstSecStore != null) {
        s_logger.debug("There is template " + templateId + " in secondary storage " + dstSecStore.getName() + " in zone " + destZoneId + " , don't need to copy");
        return template;
    }
    _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template);
    boolean success = copy(userId, template, srcSecStore, dstZone);
    if (success) {
        // increase resource count
        long accountId = template.getAccountId();
        if (template.getSize() != null) {
            _resourceLimitMgr.incrementResourceCount(accountId, ResourceType.secondary_storage, template.getSize());
        }
        return template;
    } else {
        throw new CloudRuntimeException("Failed to copy template");
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) 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) VMTemplateVO(com.cloud.storage.VMTemplateVO) ActionEvent(com.cloud.event.ActionEvent)

Example 97 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO 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 98 with VMTemplateVO

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

the class HypervisorTemplateAdapter method prepareDelete.

@Override
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
    TemplateProfile profile = super.prepareDelete(cmd);
    VMTemplateVO template = profile.getTemplate();
    Long zoneId = profile.getZoneId();
    if (template.getTemplateType() == TemplateType.SYSTEM) {
        throw new InvalidParameterValueException("The DomR template cannot be deleted.");
    }
    if (zoneId != null && (storeMgr.getImageStore(zoneId) == null)) {
        throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
    }
    return profile;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateProfile(com.cloud.storage.TemplateProfile)

Example 99 with VMTemplateVO

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

the class BareMetalTemplateAdapter method delete.

@Override
@DB
public boolean delete(TemplateProfile profile) {
    VMTemplateVO template = profile.getTemplate();
    Long templateId = template.getId();
    boolean success = true;
    String zoneName;
    if (!template.isCrossZones() && profile.getZoneId() != null) {
        zoneName = profile.getZoneId().toString();
    } else {
        zoneName = "all zones";
    }
    s_logger.debug("Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
    Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
    String eventType = EventTypes.EVENT_TEMPLATE_DELETE;
    List<TemplateDataStoreVO> templateHostVOs = this._tmpltStoreDao.listByTemplate(templateId);
    for (TemplateDataStoreVO vo : templateHostVOs) {
        TemplateDataStoreVO lock = null;
        try {
            lock = _tmpltStoreDao.acquireInLockTable(vo.getId());
            if (lock == null) {
                s_logger.debug("Failed to acquire lock when deleting templateDataStoreVO with ID: " + vo.getId());
                success = false;
                break;
            }
            vo.setDestroyed(true);
            _tmpltStoreDao.update(vo.getId(), vo);
        } finally {
            if (lock != null) {
                _tmpltStoreDao.releaseFromLockTable(lock.getId());
            }
        }
    }
    if (profile.getZoneId() != null) {
        UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), profile.getZoneId(), templateId, null);
        _usageEventDao.persist(usageEvent);
    } else {
        List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
        for (DataCenterVO dc : dcs) {
            UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), dc.getId(), templateId, null);
            _usageEventDao.persist(usageEvent);
        }
    }
    VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(profile.getZoneId(), templateId);
    if (templateZone != null) {
        _tmpltZoneDao.remove(templateZone.getId());
    }
    s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
    // If there are no more non-destroyed template host entries for this template, delete it
    if (success && (_tmpltStoreDao.listByTemplate(templateId).size() == 0)) {
        long accountId = template.getAccountId();
        VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId);
        try {
            if (lock == null) {
                s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId);
                success = false;
            } else if (_tmpltDao.remove(templateId)) {
                // Decrement the number of templates and total secondary storage space used by the account.
                _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                _resourceLimitMgr.recalculateResourceCount(accountId, template.getDomainId(), ResourceType.secondary_storage.getOrdinal());
            }
        } finally {
            if (lock != null) {
                _tmpltDao.releaseFromLockTable(lock.getId());
            }
        }
        s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed.");
    }
    return success;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) UsageEventVO(com.cloud.event.UsageEventVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) DB(com.cloud.utils.db.DB)

Example 100 with VMTemplateVO

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

the class SimulatorDiscoverer method associateTemplatesToZone.

private void associateTemplatesToZone(long hostId, long dcId) {
    VMTemplateZoneVO tmpltZone;
    List<VMTemplateVO> allTemplates = _vmTemplateDao.listAll();
    for (VMTemplateVO vt : allTemplates) {
        if (vt.isCrossZones()) {
            tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
            if (tmpltZone == null) {
                VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
                _vmTemplateZoneDao.persist(vmTemplateZone);
            }
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.util.Date)

Aggregations

VMTemplateVO (com.cloud.storage.VMTemplateVO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)27 Account (com.cloud.user.Account)27 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)24 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)22 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)17 Date (java.util.Date)16 ArrayList (java.util.ArrayList)15 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)13 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 DataCenterVO (com.cloud.dc.DataCenterVO)12 ActionEvent (com.cloud.event.ActionEvent)11 TemplateProfile (com.cloud.storage.TemplateProfile)11 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)11 VolumeVO (com.cloud.storage.VolumeVO)11 Test (org.junit.Test)11 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)10 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)9 DB (com.cloud.utils.db.DB)9