Search in sources :

Example 1 with CopyVolumeAnswer

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

the class LibvirtCopyVolumeCommandWrapper method execute.

@Override
public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final boolean copyToSecondary = command.toSecondaryStorage();
    String volumePath = command.getVolumePath();
    final StorageFilerTO pool = command.getPool();
    final String secondaryStorageUrl = command.getSecondaryStorageURL();
    KvmStoragePool secondaryStoragePool = null;
    KvmStoragePool primaryPool = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        try {
            primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primaryPool = storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType());
            } else {
                return new CopyVolumeAnswer(command, false, e.getMessage(), null, null);
            }
        }
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final String volumeName = libvirtUtilitiesHelper.generateUuidName();
        if (copyToSecondary) {
            final String destVolumeName = volumeName + ".qcow2";
            final KvmPhysicalDisk volume = primaryPool.getPhysicalDisk(command.getVolumePath());
            final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
            secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
            secondaryStoragePool.createFolder(volumeDestPath);
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
            secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + volumeDestPath);
            storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, 0);
            return new CopyVolumeAnswer(command, true, null, null, volumeName);
        } else {
            volumePath = "/volumes/" + command.getVolumeId() + File.separator;
            secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + volumePath);
            final KvmPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(command.getVolumePath() + ".qcow2");
            storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, 0);
            return new CopyVolumeAnswer(command, true, null, null, volumeName);
        }
    } catch (final CloudRuntimeException e) {
        return new CopyVolumeAnswer(command, false, e.toString(), null, null);
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) CopyVolumeAnswer(com.cloud.legacymodel.communication.answer.CopyVolumeAnswer) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO)

Aggregations

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 CopyVolumeAnswer (com.cloud.legacymodel.communication.answer.CopyVolumeAnswer)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 StorageFilerTO (com.cloud.legacymodel.to.StorageFilerTO)1