Search in sources :

Example 6 with TemplateObject

use of org.apache.cloudstack.storage.image.store.TemplateObject in project cloudstack by apache.

the class TemplateServiceImpl method copyTemplate.

@Override
public AsyncCallFuture<TemplateApiResult> copyTemplate(TemplateInfo srcTemplate, DataStore destStore) {
    // Need to understand what is the background to use two different urls for copy and extract.
    if (srcTemplate.getFormat() == ImageFormat.OVA) {
        ImageStoreEntity tmpltStore = (ImageStoreEntity) srcTemplate.getDataStore();
        tmpltStore.createEntityExtractUrl(srcTemplate.getInstallPath(), srcTemplate.getFormat(), srcTemplate);
    }
    // generate a URL from source template ssvm to download to destination data store
    String url = generateCopyUrl(srcTemplate);
    if (url == null) {
        s_logger.warn("Unable to start/resume copy of template " + srcTemplate.getUniqueName() + " to " + destStore.getName() + ", no secondary storage vm in running state in source zone");
        throw new CloudRuntimeException("No secondary VM in running state in source template zone ");
    }
    TemplateObject tmplForCopy = (TemplateObject) _templateFactory.getTemplate(srcTemplate, destStore);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Setting source template url to " + url);
    }
    tmplForCopy.setUrl(url);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Mark template_store_ref entry as Creating");
    }
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    DataObject templateOnStore = destStore.create(tmplForCopy);
    templateOnStore.processEvent(Event.CreateOnlyRequested);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Invoke datastore driver createAsync to create template on destination store");
    }
    try {
        TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
        AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().copyTemplateCrossZoneCallBack(null, null)).setContext(context);
        destStore.getDriver().createAsync(destStore, templateOnStore, caller);
    } catch (CloudRuntimeException ex) {
        // clean up already persisted template_store_ref entry in case of createTemplateCallback is never called
        TemplateDataStoreVO templateStoreVO = _vmTemplateStoreDao.findByStoreTemplate(destStore.getId(), srcTemplate.getId());
        if (templateStoreVO != null) {
            TemplateInfo tmplObj = _templateFactory.getTemplate(srcTemplate, destStore);
            tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
        }
        TemplateApiResult res = new TemplateApiResult((TemplateObject) templateOnStore);
        res.setResult(ex.getMessage());
        future.complete(res);
    }
    return future;
}
Also used : CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity)

Example 7 with TemplateObject

use of org.apache.cloudstack.storage.image.store.TemplateObject in project cloudstack by apache.

the class TemplateServiceImpl method deleteTemplateAsync.

@Override
public AsyncCallFuture<TemplateApiResult> deleteTemplateAsync(TemplateInfo template) {
    TemplateObject to = (TemplateObject) template;
    // update template_store_ref status
    to.processEvent(ObjectInDataStoreStateMachine.Event.DestroyRequested);
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, to, future);
    AsyncCallbackDispatcher<TemplateServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().deleteTemplateCallback(null, null)).setContext(context);
    to.getDataStore().getDriver().deleteAsync(to.getDataStore(), to, caller);
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 8 with TemplateObject

use of org.apache.cloudstack.storage.image.store.TemplateObject in project cloudstack by apache.

the class TemplateDataFactoryImpl method getTemplate.

@Override
public TemplateInfo getTemplate(long templateId, DataStore store) {
    VMTemplateVO templ = imageDataDao.findById(templateId);
    if (store == null) {
        TemplateObject tmpl = TemplateObject.getTemplate(templ, null);
        return tmpl;
    }
    // verify if the given input parameters are consistent with our db data.
    boolean found = false;
    if (store.getRole() == DataStoreRole.Primary) {
        VMTemplateStoragePoolVO templatePoolVO = templatePoolDao.findByPoolTemplate(store.getId(), templateId);
        if (templatePoolVO != null) {
            found = true;
        }
    } else {
        TemplateDataStoreVO templateStoreVO = templateStoreDao.findByStoreTemplate(store.getId(), templateId);
        if (templateStoreVO != null) {
            found = true;
        }
    }
    if (s_logger.isDebugEnabled()) {
        if (!found) {
            s_logger.debug("template " + templateId + " is not in store:" + store.getId() + ", type:" + store.getRole());
        } else {
            s_logger.debug("template " + templateId + " is already in store:" + store.getId() + ", type:" + store.getRole());
        }
    }
    TemplateObject tmpl = TemplateObject.getTemplate(templ, store);
    return tmpl;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject)

Example 9 with TemplateObject

use of org.apache.cloudstack.storage.image.store.TemplateObject in project cloudstack by apache.

the class TemplateServiceImpl method syncToRegionStoreAsync.

private AsyncCallFuture<TemplateApiResult> syncToRegionStoreAsync(TemplateInfo template, DataStore store) {
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    // no need to create entry on template_store_ref here, since entries are already created when prepareSecondaryStorageForMigration is invoked.
    // But we need to set default install path so that sync can be done in the right s3 path
    TemplateInfo templateOnStore = _templateFactory.getTemplate(template, store);
    String installPath = TemplateConstants.DEFAULT_TMPLT_ROOT_DIR + "/" + TemplateConstants.DEFAULT_TMPLT_FIRST_LEVEL_DIR + template.getAccountId() + "/" + template.getId() + "/" + template.getUniqueName();
    ((TemplateObject) templateOnStore).setInstallPath(installPath);
    TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
    AsyncCallbackDispatcher<TemplateServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().syncTemplateCallBack(null, null)).setContext(context);
    _motionSrv.copyAsync(template, templateOnStore, caller);
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject)

Example 10 with TemplateObject

use of org.apache.cloudstack.storage.image.store.TemplateObject in project cloudstack by apache.

the class TemplateServiceImpl method createTemplateAsync.

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

Aggregations

TemplateObject (org.apache.cloudstack.storage.image.store.TemplateObject)10 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)4 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)4 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)3 CommandResult (org.apache.cloudstack.storage.command.CommandResult)3 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)2 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)2 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)2 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)1 ImageStoreEntity (org.apache.cloudstack.storage.image.datastore.ImageStoreEntity)1