use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class TemplateObject method processEvent.
@Override
public void processEvent(final ObjectInDataStoreStateMachine.Event event, final Answer answer) {
try {
if (getDataStore().getRole() == DataStoreRole.Primary) {
if (answer instanceof CopyCmdAnswer) {
final CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
final TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
final VMTemplateStoragePoolVO templatePoolRef = templatePoolDao.findByPoolTemplate(getDataStore().getId(), getId());
templatePoolRef.setDownloadPercent(100);
if (newTemplate.getSize() != null) {
templatePoolRef.setTemplateSize(newTemplate.getSize());
}
templatePoolRef.setDownloadState(Status.DOWNLOADED);
templatePoolRef.setLocalDownloadPath(newTemplate.getPath());
templatePoolRef.setInstallPath(newTemplate.getPath());
templatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
}
} else if (getDataStore().getRole() == DataStoreRole.Image || getDataStore().getRole() == DataStoreRole.ImageCache) {
if (answer instanceof CopyCmdAnswer) {
final CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
final TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
final TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), getId());
templateStoreRef.setInstallPath(newTemplate.getPath());
templateStoreRef.setDownloadPercent(100);
templateStoreRef.setDownloadState(Status.DOWNLOADED);
templateStoreRef.setSize(newTemplate.getSize());
if (newTemplate.getPhysicalSize() != null) {
templateStoreRef.setPhysicalSize(newTemplate.getPhysicalSize());
}
templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
if (getDataStore().getRole() == DataStoreRole.Image) {
final VMTemplateVO templateVO = imageDao.findById(getId());
if (newTemplate.getFormat() != null) {
templateVO.setFormat(newTemplate.getFormat());
}
if (newTemplate.getName() != null) {
// For template created from snapshot, template name is determine by resource code.
templateVO.setUniqueName(newTemplate.getName());
}
if (newTemplate.getHypervisorType() != null) {
templateVO.setHypervisorType(newTemplate.getHypervisorType());
}
templateVO.setSize(newTemplate.getSize());
imageDao.update(templateVO.getId(), templateVO);
}
}
}
objectInStoreMgr.update(this, event);
} catch (final NoTransitionException e) {
s_logger.debug("failed to update state", e);
throw new CloudRuntimeException("Failed to update state" + e.toString());
} catch (final Exception ex) {
s_logger.debug("failed to process event and answer", ex);
objectInStoreMgr.delete(this);
throw new CloudRuntimeException("Failed to process event", ex);
} finally {
// in case of OperationFailed, expunge the entry
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.deleteIfNotReady(this);
}
}
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method copySnapshotToTemplateFromNfsToNfsXenserver.
protected Answer copySnapshotToTemplateFromNfsToNfsXenserver(final CopyCommand cmd, final SnapshotObjectTO srcData, final NfsTO srcDataStore, final TemplateObjectTO destData, final NfsTO destDataStore) {
final String srcMountPoint = getRootDir(srcDataStore.getUrl());
String snapshotPath = srcData.getPath();
final int index = snapshotPath.lastIndexOf("/");
String snapshotName = snapshotPath.substring(index + 1);
if (!snapshotName.startsWith("VHD-") && !snapshotName.endsWith(".vhd")) {
snapshotName = snapshotName + ".vhd";
}
snapshotPath = snapshotPath.substring(0, index);
snapshotPath = srcMountPoint + File.separator + snapshotPath;
final String destMountPoint = getRootDir(destDataStore.getUrl());
final String destPath = destMountPoint + File.separator + destData.getPath();
String errMsg = null;
try {
_storage.mkdir(destPath);
final String templateUuid = UUID.randomUUID().toString();
final String templateName = templateUuid + ".vhd";
final Script command = new Script(createTemplateFromSnapshotXenScript, cmd.getWait() * 1000, s_logger);
command.add("-p", snapshotPath);
command.add("-s", snapshotName);
command.add("-n", templateName);
command.add("-t", destPath);
final String result = command.execute();
if (result != null && !result.equalsIgnoreCase("")) {
return new CopyCmdAnswer(result);
}
final Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, _storage);
final Processor processor = new VhdProcessor();
processor.configure("Vhd Processor", params);
final FormatInfo info = processor.process(destPath, null, templateUuid);
final TemplateLocation loc = new TemplateLocation(_storage, destPath);
loc.create(1, true, templateUuid);
loc.addFormat(info);
loc.save();
final TemplateProp prop = loc.getTemplateInfo();
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(destData.getPath() + File.separator + templateName);
newTemplate.setFormat(ImageFormat.VHD);
newTemplate.setSize(prop.getSize());
newTemplate.setPhysicalSize(prop.getPhysicalSize());
newTemplate.setName(templateUuid);
return new CopyCmdAnswer(newTemplate);
} catch (final ConfigurationException e) {
s_logger.debug("Failed to create template from snapshot: " + e.toString());
errMsg = e.toString();
} catch (final InternalErrorException e) {
s_logger.debug("Failed to create template from snapshot: " + e.toString());
errMsg = e.toString();
} catch (final IOException e) {
s_logger.debug("Failed to create template from snapshot: " + e.toString());
errMsg = e.toString();
}
return new CopyCmdAnswer(errMsg);
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class RequestTest method testDownload.
@Test
public void testDownload() {
s_logger.info("Testing Download answer");
final VirtualMachineTemplate template = Mockito.mock(VirtualMachineTemplate.class);
Mockito.when(template.getId()).thenReturn(1L);
Mockito.when(template.getFormat()).thenReturn(ImageFormat.QCOW2);
Mockito.when(template.getName()).thenReturn("templatename");
Mockito.when(template.getTemplateType()).thenReturn(TemplateType.USER);
Mockito.when(template.getDisplayText()).thenReturn("displayText");
Mockito.when(template.getHypervisorType()).thenReturn(HypervisorType.KVM);
Mockito.when(template.getUrl()).thenReturn("url");
final NfsTO nfs = new NfsTO("secUrl", DataStoreRole.Image);
final TemplateObjectTO to = new TemplateObjectTO(template);
to.setImageDataStore(nfs);
final DownloadCommand cmd = new DownloadCommand(to, 30000000l);
final Request req = new Request(1, 1, cmd, true);
req.logD("Debug for Download");
final DownloadAnswer answer = new DownloadAnswer("jobId", 50, "errorString", Status.ABANDONED, "filesystempath", "installpath", 10000000, 20000000, "chksum");
final Response resp = new Response(req, answer);
resp.logD("Debug for Download");
}
use of com.cloud.storage.to.TemplateObjectTO 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);
}
}
Aggregations