Search in sources :

Example 46 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class Ovm3StorageProcessor method createTemplateFromSnapshot.

/**
     * Copies from secondary to secondary
     */
@Override
public Answer createTemplateFromSnapshot(CopyCommand cmd) {
    LOGGER.debug("execute createTemplateFromSnapshot: " + cmd.getClass());
    try {
        // src.getPath contains the uuid of the snapshot.
        DataTO srcData = cmd.getSrcTO();
        SnapshotObjectTO srcSnap = (SnapshotObjectTO) srcData;
        String secPoolUuid = pool.setupSecondaryStorage(srcData.getDataStore().getUrl());
        String srcFile = config.getAgentSecStoragePath() + "/" + secPoolUuid + "/" + srcSnap.getPath();
        // dest
        DataTO destData = cmd.getDestTO();
        TemplateObjectTO destTemplate = (TemplateObjectTO) destData;
        String secPoolUuidTemplate = pool.setupSecondaryStorage(destData.getDataStore().getUrl());
        String destDir = config.getAgentSecStoragePath() + "/" + secPoolUuidTemplate + "/" + destTemplate.getPath();
        String destFile = destDir + "/" + destTemplate.getUuid() + ".raw";
        CloudstackPlugin csp = new CloudstackPlugin(c);
        csp.ovsMkdirs(destDir);
        Linux host = new Linux(c);
        host.copyFile(srcFile, destFile);
        TemplateObjectTO newVol = new TemplateObjectTO();
        newVol.setUuid(destTemplate.getUuid());
        newVol.setPath(destTemplate.getUuid());
        newVol.setFormat(ImageFormat.RAW);
        return new CopyCmdAnswer(newVol);
    } catch (Ovm3ResourceException e) {
        String msg = "Error backupSnapshot: " + e.getMessage();
        LOGGER.info(msg);
        return new CopyCmdAnswer(msg);
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataTO(com.cloud.agent.api.to.DataTO) Linux(com.cloud.hypervisor.ovm3.objects.Linux) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CloudstackPlugin(com.cloud.hypervisor.ovm3.objects.CloudstackPlugin) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 47 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class Ovm3StorageProcessor method getIsoPath.

/**
     * Iso specific path return.
     * @param disk
     * @return
     * @throws Ovm3ResourceException
     */
private String getIsoPath(DiskTO disk) throws Ovm3ResourceException {
    TemplateObjectTO isoTO = (TemplateObjectTO) disk.getData();
    DataStoreTO store = isoTO.getDataStore();
    NfsTO nfsStore = (NfsTO) store;
    String secPoolUuid = pool.setupSecondaryStorage(nfsStore.getUrl());
    return config.getAgentSecStoragePath() + "/" + secPoolUuid + "/" + isoTO.getPath();
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 48 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class XenServerStorageProcessor method createTemplateFromSnapshot.

@Override
public Answer createTemplateFromSnapshot(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;
    }
    final String userSpecifiedTemplateName = templateObjTO.getName();
    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();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, destNfsPath, destDir)) {
            final String details = " Failed to create folder " + destDir + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final URI templateUri = new URI(destStore.getUrl() + "/" + destDir);
        destSr = hypervisorResource.createNfsSRbyURI(conn, templateUri, false);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        destVdi = srcVdi.copy(conn, destSr);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        if (userSpecifiedTemplateName != null) {
            destVdi.setNameLabel(conn, userSpecifiedTemplateName);
        }
        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, userSpecifiedTemplateName, null, physicalSize, virtualSize, templateObjTO.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + templateUri);
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(Storage.ImageFormat.VHD);
        newTemplate.setHypervisorType(HypervisorType.XenServer);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(templateUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    } catch (final 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());
    } 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(org.apache.cloudstack.storage.to.SnapshotObjectTO) Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 49 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class XenServerStorageProcessor method attachIso.

@Override
public AttachAnswer attachIso(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    final DataStoreTO store = data.getDataStore();
    String isoURL = null;
    if (store == null) {
        final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        isoURL = iso.getName();
    } else {
        if (!(store instanceof NfsTO)) {
            s_logger.debug("Can't attach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
        }
        final NfsTO nfsStore = (NfsTO) store;
        isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
    }
    final String vmName = cmd.getVmName();
    try {
        final Connection conn = hypervisorResource.getConnection();
        VBD isoVBD = null;
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, vmName);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
        // Find the VM's CD-ROM VBD
        final Set<VBD> vbds = vm.getVBDs(conn);
        for (final VBD vbd : vbds) {
            final String userDevice = vbd.getUserdevice(conn);
            final Types.VbdType type = vbd.getType(conn);
            if (userDevice.equals("3") && type == Types.VbdType.CD) {
                isoVBD = vbd;
                break;
            }
        }
        if (isoVBD == null) {
            throw new CloudRuntimeException("Unable to find CD-ROM VBD for VM: " + vmName);
        } else {
            // If an ISO is already inserted, eject it
            if (!isoVBD.getEmpty(conn)) {
                isoVBD.eject(conn);
            }
            // Insert the new ISO
            isoVBD.insert(conn, isoVDI);
        }
        return new AttachAnswer(disk);
    } catch (final XenAPIException e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    } catch (final Exception e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    }
}
Also used : Types(com.xensource.xenapi.Types) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 50 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

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();
    final Connection conn = hypervisorResource.getConnection();
    SR sr = null;
    boolean removeSrAfterCopy = false;
    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);
                        removeSrAfterCopy = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.REMOVE_AFTER_COPY));
                    }
                }
            }
            if (managed) {
                final Map<String, String> details = new HashMap<String, String>();
                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<String, String>());
                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);
    } finally {
        if (removeSrAfterCopy && sr != null) {
            hypervisorResource.removeSR(conn, sr);
        }
    }
    return new CopyCmdAnswer("not implemented yet");
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Aggregations

TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)57 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)40 NfsTO (com.cloud.agent.api.to.NfsTO)34 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)28 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)26 DataTO (com.cloud.agent.api.to.DataTO)22 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)20 InternalErrorException (com.cloud.exception.InternalErrorException)19 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)19 DiskTO (com.cloud.agent.api.to.DiskTO)13 XmlRpcException (org.apache.xmlrpc.XmlRpcException)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)11 Answer (com.cloud.agent.api.Answer)10 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)9 ConfigurationException (javax.naming.ConfigurationException)9 IOException (java.io.IOException)8