Search in sources :

Example 21 with TemplateObjectTO

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);
        }
    }
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Example 22 with TemplateObjectTO

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);
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) Script(com.cloud.utils.script.Script) VhdProcessor(com.cloud.storage.template.VhdProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) TARProcessor(com.cloud.storage.template.TARProcessor) Processor(com.cloud.storage.template.Processor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) HashMap(java.util.HashMap) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) VhdProcessor(com.cloud.storage.template.VhdProcessor) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 23 with TemplateObjectTO

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");
}
Also used : VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) DownloadCommand(com.cloud.storage.command.DownloadCommand) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) Test(org.junit.Test)

Example 24 with TemplateObjectTO

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

TemplateObjectTO (com.cloud.storage.to.TemplateObjectTO)24 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 NfsTO (com.cloud.agent.api.to.NfsTO)15 InternalErrorException (com.cloud.exception.InternalErrorException)14 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)10 DataTO (com.cloud.agent.api.to.DataTO)10 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)8 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)7 DiskTO (com.cloud.agent.api.to.DiskTO)6 URISyntaxException (java.net.URISyntaxException)6 ConfigurationException (javax.naming.ConfigurationException)6 IOException (java.io.IOException)5 Answer (com.cloud.agent.api.Answer)4