Search in sources :

Example 16 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class TemplateObject method getTO.

@Override
public DataTO getTO() {
    DataTO to = null;
    DataStoreTO dataStoreTO = null;
    if (this.getDataStore() != null) {
        dataStoreTO = this.getDataStore().getTO();
    }
    if (dataStore == null) {
        to = new TemplateObjectTO(this.getInstallPath(), this.getUrl(), this.getUuid(), this.getId(), this.getFormat(), this.getAccountId(), this.getChecksum(), this.getDisplayText(), dataStoreTO, this.getName(), this.getUniqueName(), null, null, null, this.getHypervisorType());
    } else {
        to = dataStore.getDriver().getTO(this);
        if (to == null) {
            to = new TemplateObjectTO(this.getInstallPath(), this.getUrl(), this.getUuid(), this.getId(), this.getFormat(), this.getAccountId(), this.getChecksum(), this.getDisplayText(), dataStoreTO, this.getName(), this.getUniqueName(), null, null, null, this.getHypervisorType());
        }
    }
    return to;
}
Also used : DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO)

Example 17 with TemplateObjectTO

use of com.cloud.legacymodel.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(VMTemplateStatus.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(VMTemplateStatus.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.legacymodel.exceptions.CloudRuntimeException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException)

Example 18 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method createVmFromTemplate.

public VM createVmFromTemplate(final Connection conn, final VirtualMachineTO vmSpec, final Host host) throws XenAPIException, XmlRpcException {
    final String guestOsTypeName = getGuestOsType(vmSpec.getOs(), vmSpec.getPlatformEmulator(), vmSpec.getBootloader() == BootloaderType.CD);
    final Set<VM> templates = VM.getByNameLabel(conn, guestOsTypeName);
    if (templates == null || templates.isEmpty()) {
        throw new CloudRuntimeException("Cannot find template " + guestOsTypeName + " on XenServer host");
    }
    assert templates.size() == 1 : "Should only have 1 template but found " + templates.size();
    final VM template = templates.iterator().next();
    final VM.Record vmr = template.getRecord(conn);
    vmr.affinity = host;
    vmr.otherConfig.remove("disks");
    vmr.otherConfig.remove("default_template");
    vmr.otherConfig.remove("mac_seed");
    vmr.isATemplate = false;
    vmr.nameLabel = vmSpec.getName();
    vmr.actionsAfterCrash = Types.OnCrashBehaviour.DESTROY;
    vmr.actionsAfterShutdown = Types.OnNormalExit.DESTROY;
    vmr.otherConfig.put("vm_uuid", vmSpec.getUuid());
    // FIX ME: In case of dynamic
    vmr.VCPUsMax = (long) vmSpec.getCpus();
    if (isDmcEnabled(conn, host) && vmSpec.isEnableDynamicallyScaleVm()) {
        // scaling is allowed
        vmr.memoryStaticMin = getStaticMin(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
        vmr.memoryStaticMax = getStaticMax(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
        vmr.memoryDynamicMin = vmSpec.getMinRam();
        vmr.memoryDynamicMax = vmSpec.getMaxRam();
        if (guestOsTypeName.toLowerCase().contains("windows")) {
            vmr.VCPUsMax = (long) vmSpec.getCpus();
        } else {
            if (vmSpec.getVcpuMaxLimit() != null) {
                vmr.VCPUsMax = (long) vmSpec.getVcpuMaxLimit();
            }
        }
    } else {
        // scaling disallowed, set static memory target
        if (vmSpec.isEnableDynamicallyScaleVm() && !isDmcEnabled(conn, host)) {
            s_logger.warn("Host " + host.getHostname(conn) + " does not support dynamic scaling, so the vm " + vmSpec.getName() + " is not dynamically scalable");
        }
        vmr.memoryStaticMin = vmSpec.getMinRam();
        vmr.memoryStaticMax = vmSpec.getMaxRam();
        vmr.memoryDynamicMin = vmSpec.getMinRam();
        vmr.memoryDynamicMax = vmSpec.getMaxRam();
        vmr.VCPUsMax = (long) vmSpec.getCpus();
    }
    vmr.VCPUsAtStartup = (long) vmSpec.getCpus();
    vmr.consoles.clear();
    final VM vm = VM.create(conn, vmr);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Created VM " + vm.getUuid(conn) + " for " + vmSpec.getName());
    }
    final String bootArgs = vmSpec.getBootArgs();
    if (bootArgs != null && bootArgs.length() > 0) {
        String pvargs = vm.getPVArgs(conn);
        pvargs = pvargs + vmSpec.getBootArgs().replaceAll(" ", "%");
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("PV args are " + pvargs);
        }
        vm.setPVArgs(conn, pvargs);
    }
    if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) {
        if (vmSpec.getBootloader() == BootloaderType.CD) {
            final DiskTO[] disks = vmSpec.getDisks();
            for (final DiskTO disk : disks) {
                if (disk.getType() == VolumeType.ISO) {
                    final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
                    final String osType = iso.getGuestOsType();
                    if (osType != null) {
                        final String isoGuestOsName = getGuestOsType(osType, vmSpec.getPlatformEmulator(), vmSpec.getBootloader() == BootloaderType.CD);
                        if (!isoGuestOsName.equals(guestOsTypeName)) {
                            vmSpec.setBootloader(BootloaderType.PyGrub);
                        }
                    }
                }
            }
        }
        if (vmSpec.getBootloader() == BootloaderType.CD) {
            vm.setPVBootloader(conn, "eliloader");
            if (!vm.getOtherConfig(conn).containsKey("install-repository")) {
                vm.addToOtherConfig(conn, "install-repository", "cdrom");
            }
        } else if (vmSpec.getBootloader() == BootloaderType.PyGrub) {
            vm.setPVBootloader(conn, "pygrub");
            vm.setPVBootloaderArgs(conn, CitrixHelper.getPVbootloaderArgs(guestOsTypeName));
        } else {
            vm.destroy(conn);
            throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
        }
    }
    try {
        finalizeVmMetaData(vm, conn, vmSpec);
    } catch (final Exception e) {
        throw new CloudRuntimeException("Unable to finalize VM MetaData: " + vmSpec);
    }
    return vm;
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VM(com.xensource.xenapi.VM) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 19 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcDataTo = cmd.getSrcTO();
    final DataTO destDataTo = cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO srcDataStoreTo = srcDataTo.getDataStore();
    try {
        if (srcDataStoreTo instanceof NfsTO && srcDataTo.getObjectType() == DataObjectType.TEMPLATE) {
            final NfsTO srcImageStore = (NfsTO) srcDataStoreTo;
            final TemplateObjectTO srcTemplateObjectTo = (TemplateObjectTO) srcDataTo;
            final String storeUrl = srcImageStore.getUrl();
            final URI uri = new URI(storeUrl);
            final String tmplPath = uri.getHost() + ":" + uri.getPath() + "/" + srcDataTo.getPath();
            final DataStoreTO destDataStoreTo = destDataTo.getDataStore();
            boolean managed = false;
            String storageHost = null;
            String managedStoragePoolName = null;
            String managedStoragePoolRootVolumeName = null;
            String managedStoragePoolRootVolumeSize = null;
            String chapInitiatorUsername = null;
            String chapInitiatorSecret = null;
            if (destDataStoreTo instanceof PrimaryDataStoreTO) {
                final PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO) destDataStoreTo;
                final Map<String, String> details = destPrimaryDataStoreTo.getDetails();
                if (details != null) {
                    managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
                    if (managed) {
                        storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                        managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                        managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                        managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
                        chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                        chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                    }
                }
            }
            final Connection conn = hypervisorResource.getConnection();
            final SR sr;
            if (managed) {
                final Map<String, String> details = new HashMap<>();
                details.put(DiskTO.STORAGE_HOST, storageHost);
                details.put(DiskTO.IQN, managedStoragePoolName);
                details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
                details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
                details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);
                sr = hypervisorResource.prepareManagedSr(conn, details);
            } else {
                final String srName = destDataStoreTo.getUuid();
                final Set<SR> srs = SR.getByNameLabel(conn, srName);
                if (srs.size() != 1) {
                    final String msg = "There are " + srs.size() + " SRs with same name: " + srName;
                    s_logger.warn(msg);
                    return new CopyCmdAnswer(msg);
                } else {
                    sr = srs.iterator().next();
                }
            }
            final String srUuid = sr.getUuid(conn);
            final String tmplUuid = copy_vhd_from_secondarystorage(conn, tmplPath, srUuid, wait);
            final VDI tmplVdi = getVDIbyUuid(conn, tmplUuid);
            final String uuidToReturn;
            final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
            if (managed) {
                uuidToReturn = tmplUuid;
                tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
            } else {
                final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<>());
                uuidToReturn = snapshotVdi.getUuid(conn);
                snapshotVdi.setNameLabel(conn, "Template " + srcTemplateObjectTo.getName());
                tmplVdi.destroy(conn);
            }
            sr.scan(conn);
            try {
                Thread.sleep(5000);
            } catch (final InterruptedException e) {
            }
            final TemplateObjectTO newVol = new TemplateObjectTO();
            newVol.setUuid(uuidToReturn);
            newVol.setPath(uuidToReturn);
            if (physicalSize != null) {
                newVol.setSize(physicalSize);
            }
            newVol.setFormat(ImageFormat.VHD);
            return new CopyCmdAnswer(newVol);
        }
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " for template + " + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new CopyCmdAnswer(msg);
    }
    return new CopyCmdAnswer("not implemented yet");
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 20 with TemplateObjectTO

use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
    final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
    final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
    final int wait = cmd.getWait();
    final String secondaryStoragePoolURL = destStore.getUrl();
    final String volumeUUID = volume.getPath();
    final String userSpecifiedName = template.getName();
    String details = null;
    SR tmpltSR = null;
    boolean result = false;
    String secondaryStorageMountPath = null;
    String installPath = null;
    try {
        final URI uri = new URI(secondaryStoragePoolURL);
        secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
        installPath = template.getPath();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
            details = " Filed to create folder " + installPath + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final VDI vol = getVDIbyUuid(conn, volumeUUID);
        // create template SR
        final URI tmpltURI = new URI(secondaryStoragePoolURL + "/" + installPath);
        tmpltSR = hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
        // copy volume to template SR
        final VDI tmpltVDI = hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
        // scan makes XenServer pick up VDI physicalSize
        tmpltSR.scan(conn);
        if (userSpecifiedName != null) {
            tmpltVDI.setNameLabel(conn, userSpecifiedName);
        }
        final String tmpltUUID = tmpltVDI.getUuid(conn);
        final String tmpltFilename = tmpltUUID + ".vhd";
        final long virtualSize = tmpltVDI.getVirtualSize(conn);
        final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
        // create the template.properties file
        final String templatePath = secondaryStorageMountPath + "/" + installPath;
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + tmpltURI);
        }
        installPath = installPath + "/" + tmpltFilename;
        hypervisorResource.removeSR(conn, tmpltSR);
        tmpltSR = null;
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(installPath);
        newTemplate.setFormat(ImageFormat.VHD);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(tmpltUUID);
        final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
        return answer;
    } catch (final Exception e) {
        if (tmpltSR != null) {
            hypervisorResource.removeSR(conn, tmpltSR);
        }
        if (secondaryStorageMountPath != null) {
            hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
        }
        details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
        s_logger.error(details, e);
    }
    return new CopyCmdAnswer(details);
}
Also used : Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Aggregations

TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)24 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)17 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)17 NfsTO (com.cloud.legacymodel.to.NfsTO)14 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)12 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)12 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)11 DataTO (com.cloud.legacymodel.to.DataTO)11 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)8 DiskTO (com.cloud.legacymodel.to.DiskTO)6 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)6 ConfigurationException (javax.naming.ConfigurationException)6 IOException (java.io.IOException)5 Answer (com.cloud.legacymodel.communication.answer.Answer)4 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)4