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;
}
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;
}
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;
}
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;
}
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);
}
}
}
Aggregations