Search in sources :

Example 26 with TemplateInfo

use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.

the class TemplateServiceImpl method createTemplateAsync.

@Override
public void createTemplateAsync(final TemplateInfo template, final DataStore store, final AsyncCompletionCallback<TemplateApiResult> callback) {
    // persist template_store_ref entry
    final TemplateObject templateOnStore = (TemplateObject) store.create(template);
    // update template_store_ref and template state
    try {
        templateOnStore.processEvent(ObjectInDataStoreStateMachine.Event.CreateOnlyRequested);
    } catch (final Exception e) {
        final TemplateApiResult result = new TemplateApiResult(templateOnStore);
        result.setResult(e.toString());
        result.setSuccess(false);
        if (callback != null) {
            callback.complete(result);
        }
        return;
    }
    try {
        final TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<>(callback, templateOnStore, null);
        final AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createTemplateCallback(null, null)).setContext(context);
        store.getDriver().createAsync(store, templateOnStore, caller);
    } catch (final CloudRuntimeException ex) {
        // clean up already persisted template_store_ref entry in case of createTemplateCallback is never called
        final TemplateDataStoreVO templateStoreVO = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
        if (templateStoreVO != null) {
            final TemplateInfo tmplObj = _templateFactory.getTemplate(template, store);
            tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
        }
        final TemplateApiResult result = new TemplateApiResult(template);
        result.setResult(ex.getMessage());
        if (callback != null) {
            callback.complete(result);
        }
    }
}
Also used : TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateCmdResult(com.cloud.engine.subsystem.api.storage.CreateCmdResult) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateObject(com.cloud.storage.image.store.TemplateObject)

Example 27 with TemplateInfo

use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.

the class HypervisorTemplateAdapter method createTemplateAsyncCallBack.

protected Void createTemplateAsyncCallBack(final AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> callback, final CreateTemplateContext<TemplateApiResult> context) {
    final TemplateApiResult result = callback.getResult();
    final TemplateInfo template = context.template;
    if (result.isSuccess()) {
        final VMTemplateVO tmplt = _tmpltDao.findById(template.getId());
        // need to grant permission for public templates
        if (tmplt.isPublicTemplate()) {
            _messageBus.publish(_name, TemplateManager.MESSAGE_REGISTER_PUBLIC_TEMPLATE_EVENT, PublishScope.LOCAL, tmplt.getId());
        }
        final long accountId = tmplt.getAccountId();
        if (template.getSize() != null) {
            _resourceLimitMgr.incrementResourceCount(accountId, ResourceType.secondary_storage, template.getSize());
        }
    }
    return null;
}
Also used : TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)

Example 28 with TemplateInfo

use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.

the class HypervisorTemplateAdapter method create.

@Override
public VMTemplateVO create(final 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.
    final 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
    final 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());
    }
    final Set<Long> zoneSet = new HashSet<>();
    // For private templates choose a random store. TODO - Have a better algorithm based on size, no. of objects, load etc.
    Collections.shuffle(imageStores);
    for (final DataStore imageStore : imageStores) {
        // skip data stores for a disabled zone
        final Long zoneId = imageStore.getScope().getScopeId();
        if (zoneId != null) {
            final 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 (AllocationState.Disabled == zone.getAllocationState()) {
                s_logger.info("Zone " + zoneId + " is disabled, so skip downloading template to its 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);
            }
        }
        final TemplateInfo tmpl = imageFactory.getTemplate(template.getId(), imageStore);
        final CreateTemplateContext<TemplateApiResult> context = new CreateTemplateContext<>(null, tmpl);
        final 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(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) HashSet(java.util.HashSet)

Example 29 with TemplateInfo

use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.

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(final long isoId, final long dcId) {
    final 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
        final Scope destScope = new ZoneScope(dcId);
        final 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(com.cloud.engine.subsystem.api.storage.ZoneScope) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) Scope(com.cloud.engine.subsystem.api.storage.Scope) PublishScope(com.cloud.framework.messagebus.PublishScope) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 30 with TemplateInfo

use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method prepareIsoForVmProfile.

@Override
public void prepareIsoForVmProfile(final VirtualMachineProfile profile) {
    final UserVmVO vm = _userVmDao.findById(profile.getId());
    if (vm.getIsoId() != null) {
        final TemplateInfo template = prepareIso(vm.getIsoId(), vm.getDataCenterId());
        if (template == null) {
            s_logger.error("Failed to prepare ISO on secondary or cache storage");
            throw new CloudRuntimeException("Failed to prepare ISO on secondary or cache storage");
        }
        if (template.isBootable()) {
            profile.setBootLoaderType(BootloaderType.CD);
        }
        final GuestOSVO guestOS = _guestOSDao.findById(template.getGuestOSId());
        String displayName = null;
        if (guestOS != null) {
            displayName = guestOS.getDisplayName();
        }
        final TemplateObjectTO iso = (TemplateObjectTO) template.getTO();
        iso.setGuestOsType(displayName);
        final DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
        profile.addDisk(disk);
    } else {
        final TemplateObjectTO iso = new TemplateObjectTO();
        iso.setFormat(ImageFormat.ISO);
        final DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
        profile.addDisk(disk);
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) GuestOSVO(com.cloud.storage.GuestOSVO) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)30 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)15 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)12 TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)11 VMTemplateVO (com.cloud.storage.VMTemplateVO)10 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)8 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)7 ExecutionException (java.util.concurrent.ExecutionException)7 DB (com.cloud.utils.db.DB)6 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)6 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)5 HashSet (java.util.HashSet)5 CopyCommandResult (com.cloud.engine.subsystem.api.storage.CopyCommandResult)4 CreateCmdResult (com.cloud.engine.subsystem.api.storage.CreateCmdResult)4 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)4 AsyncCallFuture (com.cloud.framework.async.AsyncCallFuture)4 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)3 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3