Search in sources :

Example 1 with PrimaryStorageDownloadAnswer

use of com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtPrimaryStorageDownloadCommandWrapper method execute.

@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String tmplturl = command.getUrl();
    final int index = tmplturl.lastIndexOf("/");
    final String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KvmPhysicalDisk tmplVol = null;
    KvmStoragePool secondaryPool = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        secondaryPool = storagePoolMgr.getStoragePoolByUri(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (final KvmPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
            if (tmplVol == null) {
                return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        /* Copy volume to primary storage */
        final KvmStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid());
        final KvmPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0);
        return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
    } catch (final CloudRuntimeException e) {
        return new PrimaryStorageDownloadAnswer(e.toString());
    } finally {
        if (secondaryPool != null) {
            storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
    }
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

Example 2 with PrimaryStorageDownloadAnswer

use of com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final TemplateObjectTO template = (TemplateObjectTO) srcData;
    final DataStoreTO imageStore = template.getDataStore();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
    final int index = tmplturl.lastIndexOf("/");
    final String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KvmPhysicalDisk tmplVol = null;
    KvmStoragePool secondaryPool = null;
    try {
        secondaryPool = this.storagePoolMgr.getStoragePoolByUri(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (final KvmPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        if (tmplVol == null) {
            return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
        }
        /* Copy volume to primary storage */
        this.logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
        final KvmStoragePool primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KvmPhysicalDisk primaryVol;
        if (destData instanceof VolumeObjectTO) {
            final VolumeObjectTO volume = (VolumeObjectTO) destData;
            // rather than cloning on deploy
            if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
                this.logger.debug("Using configured size of " + volume.getSize());
                tmplVol.setSize(volume.getSize());
                tmplVol.setVirtualSize(volume.getSize());
            } else {
                this.logger.debug("Using template's size of " + tmplVol.getVirtualSize());
            }
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else if (destData instanceof TemplateObjectTO) {
            final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else {
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
        }
        DataTO data = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            final TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(primaryVol.getName());
            newTemplate.setSize(primaryVol.getSize());
            if (primaryPool.getType() == StoragePoolType.RBD) {
                newTemplate.setFormat(ImageFormat.RAW);
            } else {
                newTemplate.setFormat(ImageFormat.QCOW2);
            }
            data = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            final VolumeObjectTO volumeObjectTo = new VolumeObjectTO();
            volumeObjectTo.setPath(primaryVol.getName());
            volumeObjectTo.setSize(primaryVol.getSize());
            if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
                volumeObjectTo.setFormat(ImageFormat.RAW);
            } else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
                volumeObjectTo.setFormat(ImageFormat.QCOW2);
            }
            data = volumeObjectTo;
        }
        return new CopyCmdAnswer(data);
    } catch (final CloudRuntimeException e) {
        return new CopyCmdAnswer(e.toString());
    } finally {
        try {
            if (secondaryPool != null) {
                secondaryPool.delete();
            }
        } catch (final Exception e) {
            this.logger.debug("Failed to clean up secondary storage", e);
        }
    }
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) NfsTO(com.cloud.legacymodel.to.NfsTO) LibvirtException(org.libvirt.LibvirtException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 3 with PrimaryStorageDownloadAnswer

use of com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer in project cosmic by MissionCriticalCloud.

the class CitrixPrimaryStorageDownloadCommandWrapper method execute.

@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final CitrixResourceBase citrixResourceBase) {
    final String tmplturl = command.getUrl();
    final String poolName = command.getPoolUuid();
    final int wait = command.getWait();
    try {
        final URI uri = new URI(tmplturl);
        final String tmplpath = uri.getHost() + ":" + uri.getPath();
        final Connection conn = citrixResourceBase.getConnection();
        SR poolsr = null;
        final Set<SR> srs = SR.getByNameLabel(conn, poolName);
        if (srs.size() != 1) {
            final String msg = "There are " + srs.size() + " SRs with same name: " + poolName;
            s_logger.warn(msg);
            return new PrimaryStorageDownloadAnswer(msg);
        } else {
            poolsr = srs.iterator().next();
        }
        final String pUuid = poolsr.getUuid(conn);
        final boolean isISCSI = citrixResourceBase.IsISCSI(poolsr.getType(conn));
        final String uuid = citrixResourceBase.copyVhdFromSecondaryStorage(conn, tmplpath, pUuid, wait);
        final VDI tmpl = citrixResourceBase.getVDIbyUuid(conn, uuid);
        final VDI snapshotvdi = tmpl.snapshot(conn, new HashMap<>());
        final String snapshotUuid = snapshotvdi.getUuid(conn);
        snapshotvdi.setNameLabel(conn, "Template " + command.getName());
        final String parentuuid = citrixResourceBase.getVhdParent(conn, pUuid, snapshotUuid, isISCSI);
        final VDI parent = citrixResourceBase.getVDIbyUuid(conn, parentuuid);
        final Long phySize = parent.getPhysicalUtilisation(conn);
        tmpl.destroy(conn);
        poolsr.scan(conn);
        try {
            Thread.sleep(5000);
        } catch (final Exception e) {
        }
        return new PrimaryStorageDownloadAnswer(snapshotvdi.getUuid(conn), phySize);
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " on host:" + citrixResourceBase.getHost().getUuid() + " for template: " + tmplturl + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new PrimaryStorageDownloadAnswer(msg);
    }
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) URI(java.net.URI) SR(com.xensource.xenapi.SR)

Aggregations

PrimaryStorageDownloadAnswer (com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer)3 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 KvmPhysicalDisk (com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)1 KvmStoragePool (com.cloud.agent.resource.kvm.storage.KvmStoragePool)1 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)1 QemuImgException (com.cloud.agent.resource.kvm.storage.utils.QemuImgException)1 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)1 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)1 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)1 DataTO (com.cloud.legacymodel.to.DataTO)1 NfsTO (com.cloud.legacymodel.to.NfsTO)1 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)1 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)1 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)1 Connection (com.xensource.xenapi.Connection)1 SR (com.xensource.xenapi.SR)1 VDI (com.xensource.xenapi.VDI)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URI (java.net.URI)1