Search in sources :

Example 21 with TemplateObjectTO

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

the class Xenserver625StorageProcessor method createTemplateFromSnapshot.

@Override
public Answer createTemplateFromSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof NfsTO) {
        return createTemplateFromSnapshot2(cmd);
    }
    final int wait = cmd.getWait();
    final SnapshotObjectTO srcObj = (SnapshotObjectTO) srcData;
    final TemplateObjectTO destObj = (TemplateObjectTO) destData;
    final NfsTO srcStore = (NfsTO) srcObj.getDataStore();
    final NfsTO destStore = (NfsTO) destObj.getDataStore();
    URI srcUri = null;
    URI destUri = null;
    try {
        srcUri = new URI(srcStore.getUrl());
        destUri = new URI(destStore.getUrl());
    } catch (final Exception e) {
        s_logger.debug("incorrect url", e);
        return new CopyCmdAnswer("incorrect url" + e.toString());
    }
    final String srcPath = srcObj.getPath();
    final int index = srcPath.lastIndexOf("/");
    final String srcDir = srcPath.substring(0, index);
    final String destDir = destObj.getPath();
    SR srcSr = null;
    SR destSr = null;
    VDI destVdi = null;
    boolean result = false;
    try {
        srcSr = createFileSr(conn, srcUri.getHost() + ":" + srcUri.getPath(), srcDir);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(destNfsPath.getBytes());
        mountNfs(conn, destUri.getHost() + ":" + destUri.getPath(), localDir);
        makeDirectory(conn, localDir + "/" + destDir);
        destSr = createFileSR(conn, localDir + "/" + destDir);
        final String nameLabel = "cloud-" + UUID.randomUUID().toString();
        final String[] parents = srcObj.getParents();
        final List<VDI> snapshotChains = new ArrayList<>();
        if (parents != null) {
            for (int i = 0; i < parents.length; i++) {
                final String snChainPath = parents[i];
                final String uuid = getSnapshotUuid(snChainPath);
                final VDI chain = VDI.getByUuid(conn, uuid);
                snapshotChains.add(chain);
            }
        }
        final String snapshotUuid = getSnapshotUuid(srcPath);
        final VDI snapshotVdi = VDI.getByUuid(conn, snapshotUuid);
        snapshotChains.add(snapshotVdi);
        final long templateVirtualSize = snapshotChains.get(0).getVirtualSize(conn);
        destVdi = createVdi(conn, nameLabel, destSr, templateVirtualSize);
        final String destVdiUuid = destVdi.getUuid(conn);
        for (final VDI snapChain : snapshotChains) {
            final Task task = snapChain.copyAsync(conn, null, null, destVdi);
            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);
            task.destroy(conn);
        }
        destVdi = VDI.getByUuid(conn, destVdiUuid);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        final String templateUuid = destVdi.getUuid(conn);
        final String templateFilename = templateUuid + ".vhd";
        final long virtualSize = destVdi.getVirtualSize(conn);
        final long physicalSize = destVdi.getPhysicalUtilisation(conn);
        String templatePath = destNfsPath + "/" + destDir;
        templatePath = templatePath.replaceAll("//", "/");
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, nameLabel, null, physicalSize, virtualSize, destObj.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(ImageFormat.VHD);
        newTemplate.setSize(destVdi.getVirtualSize(conn));
        newTemplate.setPhysicalSize(destVdi.getPhysicalUtilisation(conn));
        newTemplate.setName(destVdiUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    } catch (final Exception e) {
        s_logger.error("Failed create template from snapshot", e);
        return new CopyCmdAnswer("Failed create template from snapshot " + e.toString());
    } finally {
        if (!result) {
            if (destVdi != null) {
                try {
                    destVdi.destroy(conn);
                } catch (final Exception e) {
                    s_logger.debug("Clean up left over on dest storage failed: ", e);
                }
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 22 with TemplateObjectTO

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

the class Xenserver625StorageProcessor method createTemplateFromSnapshot2.

public Answer createTemplateFromSnapshot2(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final SnapshotObjectTO snapshotObjTO = (SnapshotObjectTO) cmd.getSrcTO();
    final TemplateObjectTO templateObjTO = (TemplateObjectTO) cmd.getDestTO();
    if (!(snapshotObjTO.getDataStore() instanceof PrimaryDataStoreTO) || !(templateObjTO.getDataStore() instanceof NfsTO)) {
        return null;
    }
    NfsTO destStore = null;
    URI destUri = null;
    try {
        destStore = (NfsTO) templateObjTO.getDataStore();
        destUri = new URI(destStore.getUrl());
    } catch (final Exception ex) {
        s_logger.debug("Invalid URI", ex);
        return new CopyCmdAnswer("Invalid URI: " + ex.toString());
    }
    SR srcSr = null;
    SR destSr = null;
    final String destDir = templateObjTO.getPath();
    VDI destVdi = null;
    boolean result = false;
    try {
        final Map<String, String> srcDetails = cmd.getOptions();
        final String iScsiName = srcDetails.get(DiskTO.IQN);
        final String storageHost = srcDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = srcDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = srcDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        srcSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(destNfsPath.getBytes());
        mountNfs(conn, destNfsPath, localDir);
        makeDirectory(conn, localDir + "/" + destDir);
        destSr = createFileSR(conn, localDir + "/" + destDir);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        destVdi = srcVdi.copy(conn, destSr);
        final String nameLabel = "cloud-" + UUID.randomUUID().toString();
        destVdi.setNameLabel(conn, nameLabel);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        final String templateUuid = destVdi.getUuid(conn);
        final String templateFilename = templateUuid + ".vhd";
        final long virtualSize = destVdi.getVirtualSize(conn);
        final long physicalSize = destVdi.getPhysicalUtilisation(conn);
        // create the template.properties file
        String templatePath = destNfsPath + "/" + destDir;
        templatePath = templatePath.replaceAll("//", "/");
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, nameLabel, null, physicalSize, virtualSize, templateObjTO.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(ImageFormat.VHD);
        newTemplate.setHypervisorType(HypervisorType.XenServer);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(templateUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    // } catch (Exception ex) {
    // s_logger.error("Failed to create a template from a snapshot",
    // ex);
    // 
    // return new
    // CopyCmdAnswer("Failed to create a template from a snapshot: " +
    // ex.toString());
    } catch (final BadServerResponse e) {
        s_logger.error("Failed to create a template from a snapshot due to incomprehensible server response", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } catch (final XenAPIException e) {
        s_logger.error("Failed to create a template from a snapshot due to xenapi error", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } catch (final XmlRpcException e) {
        s_logger.error("Failed to create a template from a snapshot due to rpc error", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } finally {
        if (!result) {
            if (destVdi != null) {
                try {
                    destVdi.destroy(conn);
                } catch (final Exception e) {
                    s_logger.debug("Cleaned up leftover VDI on destination storage due to failure: ", e);
                }
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
}
Also used : SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 23 with TemplateObjectTO

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

the class VolumeServiceImpl method managedCopyBaseImageCallback.

protected Void managedCopyBaseImageCallback(final AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, final ManagedCreateBaseImageContext<VolumeApiResult> context) {
    final CopyCommandResult result = callback.getResult();
    final VolumeInfo volumeInfo = context.getVolumeInfo();
    final VolumeApiResult res = new VolumeApiResult(volumeInfo);
    if (result.isSuccess()) {
        // volumeInfo.processEvent(Event.OperationSuccessed, result.getAnswer());
        final VolumeVO volume = volDao.findById(volumeInfo.getId());
        final CopyCmdAnswer answer = (CopyCmdAnswer) result.getAnswer();
        final TemplateObjectTO templateObjectTo = (TemplateObjectTO) answer.getNewData();
        volume.setPath(templateObjectTo.getPath());
        if (templateObjectTo.getFormat() != null) {
            volume.setFormat(templateObjectTo.getFormat());
        }
        volDao.update(volume.getId(), volume);
    } else {
        volumeInfo.processEvent(Event.DestroyRequested);
        res.setResult(result.getResult());
    }
    final AsyncCallFuture<VolumeApiResult> future = context.getFuture();
    future.complete(res);
    return null;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCommandResult(com.cloud.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 24 with TemplateObjectTO

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

the class TemplateManagerImpl method prepareIsoForVmProfile.

@Override
public void prepareIsoForVmProfile(final VirtualMachineProfile profile) {
    final UserVmVO vm = this._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 = this._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, VolumeType.ISO);
        profile.addDisk(disk);
    } else {
        final TemplateObjectTO iso = new TemplateObjectTO();
        iso.setFormat(ImageFormat.ISO);
        final DiskTO disk = new DiskTO(iso, 3L, null, VolumeType.ISO);
        profile.addDisk(disk);
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) GuestOSVO(com.cloud.storage.GuestOSVO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) DiskTO(com.cloud.legacymodel.to.DiskTO)

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