creat template from volume/snapshot, and then create vm from template works now

This commit is contained in:
Edison Su 2013-05-07 20:15:35 -07:00
parent 5aeca646ae
commit ac7be218d5
5 changed files with 59 additions and 8 deletions

View File

@ -107,6 +107,7 @@ import com.cloud.host.Host.Type;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.resource.ServerResourceBase;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.StorageLayer;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.template.DownloadManager;
@ -298,9 +299,9 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
snapshotName = snapshotName + ".vhd";
}
snapshotPath = snapshotPath.substring(0, index);
snapshotPath = srcMountPoint + snapshotPath;
snapshotPath = srcMountPoint + File.separator + snapshotPath;
String destMountPoint = this.getRootDir(destDataStore.getUrl());
String destPath = destMountPoint + destData.getPath();
String destPath = destMountPoint + File.separator + destData.getPath();
String errMsg = null;
try {
@ -329,7 +330,8 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
loc.save();
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(destData.getPath() + File.separator + templateUuid);
newTemplate.setPath(destData.getPath() + File.separator + templateName);
newTemplate.setFormat(ImageFormat.VHD);
return new CopyCmdAnswer(newTemplate);
} catch (ConfigurationException e) {
s_logger.debug("Failed to create template from snapshot: " + e.toString());

View File

@ -396,7 +396,7 @@ public class TemplateServiceImpl implements TemplateService {
if (userVmUsingIso == null || userVmUsingIso.isEmpty()) {
//TODO: we cannot directly call deleteTemplateSync here to reuse delete logic since in this case, our db does not have this template at all.
VMTemplateVO template = _templateDao.findById(tInfo.getId());
DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(store.getTO(), tInfo.getInstallPath(), template.getId(), template.getAccountId());
DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(store.getTO(), tInfo.getInstallPath(), null, null);
EndPoint ep = _epSelector.select(store);
Answer answer = ep.sendMessage(dtCommand);
if (answer == null || !answer.getResult()) {
@ -574,7 +574,7 @@ public class TemplateServiceImpl implements TemplateService {
// remove entry from template_store_ref
destTemplate.getDataStore().delete(destTemplate);
} else {
destTemplate.processEvent(Event.OperationSuccessed);
destTemplate.processEvent(Event.OperationSuccessed, result.getAnswer());
}
future.complete(res);
} catch (Exception e) {

View File

@ -163,6 +163,7 @@ public class TemplateObject implements TemplateInfo {
}
public boolean stateTransit(TemplateEvent e) throws NoTransitionException {
this.imageVO = imageDao.findById(this.imageVO.getId());
boolean result= imageMgr.getStateMachine().transitTo(this.imageVO, e, null,
imageDao);
this.imageVO = imageDao.findById(this.imageVO.getId());
@ -172,6 +173,22 @@ public class TemplateObject implements TemplateInfo {
@Override
public void processEvent(Event event) {
try {
if (this.getDataStore().getRole() == DataStoreRole.Image ||
this.getDataStore().getRole() == DataStoreRole.ImageCache) {
TemplateEvent templEvent = null;
if (event == ObjectInDataStoreStateMachine.Event.CreateOnlyRequested) {
templEvent = TemplateEvent.CreateRequested;
} else if (event == ObjectInDataStoreStateMachine.Event.OperationSuccessed) {
templEvent = TemplateEvent.OperationSucceeded;
} else if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
templEvent = TemplateEvent.OperationFailed;
}
if (templEvent != null) {
this.stateTransit(templEvent);
}
}
ojbectInStoreMgr.update(this, event);
} catch (NoTransitionException e) {
s_logger.debug("failed to update state", e);
@ -201,7 +218,25 @@ public class TemplateObject implements TemplateInfo {
TemplateDataStoreVO templateStoreRef = this.templateStoreDao.findByStoreTemplate(this.getDataStore().getId(),
this.getId());
templateStoreRef.setInstallPath(newTemplate.getPath());
templateStoreRef.setDownloadPercent(100);
templateStoreRef.setDownloadState(Status.DOWNLOADED);
templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
VMTemplateVO templateVO = this.imageDao.findById(this.getId());
templateVO.setFormat(newTemplate.getFormat());
this.imageDao.update(templateVO.getId(), templateVO);
}
TemplateEvent templEvent = null;
if (event == ObjectInDataStoreStateMachine.Event.CreateOnlyRequested) {
templEvent = TemplateEvent.CreateRequested;
} else if (event == ObjectInDataStoreStateMachine.Event.OperationSuccessed) {
templEvent = TemplateEvent.OperationSucceeded;
} else if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
templEvent = TemplateEvent.OperationFailed;
}
if (templEvent != null) {
this.stateTransit(templEvent);
}
}
ojbectInStoreMgr.update(this, event);

View File

@ -1275,6 +1275,7 @@ public class XenServerStorageResource {
tmpltSR = null;
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(installPath);
newTemplate.setFormat(ImageFormat.VHD);
CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
return answer;
} catch (Exception e) {

View File

@ -22,6 +22,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -127,6 +128,7 @@ import com.cloud.storage.StoragePoolStatus;
import com.cloud.storage.TemplateProfile;
import com.cloud.storage.Upload;
import com.cloud.storage.Upload.Type;
import com.cloud.storage.VMTemplateZoneVO;
import com.cloud.storage.UploadVO;
import com.cloud.storage.VMTemplateHostVO;
@ -1347,8 +1349,16 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
try {
TemplateInfo tmplInfo = this._tmplFactory.getTemplate(templateId);
snapshot = _snapshotDao.findById(snapshotId);
ZoneScope scope = new ZoneScope(snapshot.getDataCenterId());
Long zoneId = null;
if (snapshotId != null) {
snapshot = _snapshotDao.findById(snapshotId);
zoneId = snapshot.getDataCenterId();
} else if (volumeId != null) {
volume = _volumeDao.findById(volumeId);
zoneId = volume.getDataCenterId();
}
ZoneScope scope = new ZoneScope(zoneId);
List<DataStore> store = this._dataStoreMgr.getImageStoresByScope(scope);
if (store.size() > 1) {
throw new CloudRuntimeException("muliple image data store, don't know which one to use");
@ -1374,12 +1384,15 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
s_logger.debug("Failed to create template" + result.getResult());
throw new CloudRuntimeException("Failed to create template" + result.getResult());
}
VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
this._tmpltZoneDao.persist(templateZone);
privateTemplate = this._tmpltDao.findById(templateId);
UsageEventVO usageEvent = new UsageEventVO(
EventTypes.EVENT_TEMPLATE_CREATE,
privateTemplate.getAccountId(),
snapshot.getDataCenterId(),
zoneId,
privateTemplate.getId(), privateTemplate.getName(),
null, privateTemplate.getSourceTemplateId(),
privateTemplate.getSize());