Search in sources :

Example 16 with ImageFormat

use of com.cloud.storage.Storage.ImageFormat in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final VolumeObjectTO srcVol = (VolumeObjectTO) srcData;
    final VolumeObjectTO destVol = (VolumeObjectTO) destData;
    final ImageFormat srcFormat = srcVol.getFormat();
    final ImageFormat destFormat = destVol.getFormat();
    final DataStoreTO srcStore = srcData.getDataStore();
    final DataStoreTO destStore = destData.getDataStore();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) srcStore;
    if (!(destStore instanceof NfsTO)) {
        return new CopyCmdAnswer("can only handle nfs storage");
    }
    final NfsTO nfsStore = (NfsTO) destStore;
    final String srcVolumePath = srcData.getPath();
    final String destVolumePath = destData.getPath();
    final String secondaryStorageUrl = nfsStore.getUrl();
    KvmStoragePool secondaryStoragePool = null;
    try {
        final String volumeName = UUID.randomUUID().toString();
        final String destVolumeName = volumeName + "." + destFormat.getFileExtension();
        final KvmPhysicalDisk volume = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), srcVolumePath);
        volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
        secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
        secondaryStoragePool.createFolder(destVolumePath);
        storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + File.separator + destVolumePath);
        storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, cmd.getWaitInMillSeconds());
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(destVolumePath + File.separator + destVolumeName);
        newVol.setFormat(destFormat);
        return new CopyCmdAnswer(newVol);
    } catch (final CloudRuntimeException e) {
        logger.debug("Failed to copyVolumeFromPrimaryToSecondary: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) ImageFormat(com.cloud.storage.Storage.ImageFormat)

Example 17 with ImageFormat

use of com.cloud.storage.Storage.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandTemplateNoDisk.

@Test(expected = NullPointerException.class)
public void testPrimaryStorageDownloadCommandTemplateNoDisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final String name = "Test";
    final String url = "http://template/template.qcow2";
    final ImageFormat format = ImageFormat.VHD;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk tmplVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk primaryVol = Mockito.mock(KvmPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.getPhysicalDisk("template.qcow2")).thenReturn(tmplVol);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), command.getPoolUuid());
}
Also used : Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) NfsStoragePool(com.cloud.hypervisor.kvm.resource.KvmHaBase.NfsStoragePool) StoragePool(com.cloud.storage.StoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.agent.api.storage.PrimaryStorageDownloadCommand) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) ImageFormat(com.cloud.storage.Storage.ImageFormat) Test(org.junit.Test)

Example 18 with ImageFormat

use of com.cloud.storage.Storage.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNOQcow2.

@Test
public void testPrimaryStorageDownloadCommandNOTemplateNOQcow2() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KvmPhysicalDisk> disks = new ArrayList<>();
    final List<KvmPhysicalDisk> spiedDisks = Mockito.spy(disks);
    final String name = "Test";
    final String url = "http://template/";
    final ImageFormat format = ImageFormat.QCOW2;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk tmplVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk primaryVol = Mockito.mock(KvmPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(spiedDisks);
    when(spiedDisks.isEmpty()).thenReturn(false);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : NfsStoragePool(com.cloud.hypervisor.kvm.resource.KvmHaBase.NfsStoragePool) StoragePool(com.cloud.storage.StoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.agent.api.storage.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.storage.Storage.ImageFormat) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 19 with ImageFormat

use of com.cloud.storage.Storage.ImageFormat in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateDisk.

@Test(expected = NullPointerException.class)
public void testPrimaryStorageDownloadCommandNOTemplateDisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KvmPhysicalDisk> disks = new ArrayList<>();
    final String name = "Test";
    final String url = "http://template/";
    final ImageFormat format = ImageFormat.QCOW2;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk tmplVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk primaryVol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk disk = new KvmPhysicalDisk("/path", "disk.qcow2", primaryPool);
    disks.add(disk);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : NfsStoragePool(com.cloud.hypervisor.kvm.resource.KvmHaBase.NfsStoragePool) StoragePool(com.cloud.storage.StoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.agent.api.storage.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.storage.Storage.ImageFormat) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 20 with ImageFormat

use of com.cloud.storage.Storage.ImageFormat in project cosmic by MissionCriticalCloud.

the class LaunchPermissionDaoImpl method listPermittedTemplates.

@Override
public List<VMTemplateVO> listPermittedTemplates(final long accountId) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    final List<VMTemplateVO> permittedTemplates = new ArrayList<>();
    PreparedStatement pstmt = null;
    try {
        final String sql = LIST_PERMITTED_TEMPLATES;
        pstmt = txn.prepareAutoCloseStatement(sql);
        pstmt.setLong(1, accountId);
        final ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            final long id = rs.getLong(1);
            final String uniqueName = rs.getString(2);
            final String name = rs.getString(3);
            final boolean isPublic = rs.getBoolean(4);
            final String value = rs.getString(5);
            final ImageFormat format = ImageFormat.valueOf(value);
            final String tmpltType = rs.getString(6);
            final boolean requiresHVM = rs.getBoolean(7);
            final int bits = rs.getInt(8);
            final String url = rs.getString(9);
            final String createdTS = rs.getString(10);
            final long templateAccountId = rs.getLong(11);
            final String checksum = rs.getString(12);
            final String displayText = rs.getString(13);
            final boolean enablePassword = rs.getBoolean(14);
            final long guestOSId = rs.getLong(15);
            final boolean featured = rs.getBoolean(16);
            Date createdDate = null;
            if (createdTS != null) {
                createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
            }
            if (isPublic) {
                // if it's public already, skip adding it to
                continue;
            // permitted templates as this for private
            // templates only
            }
            final VMTemplateVO template = new VMTemplateVO(id, uniqueName, name, format, isPublic, featured, TemplateType.valueOf(tmpltType), url, createdDate, requiresHVM, bits, templateAccountId, checksum, displayText, enablePassword, guestOSId, true, null);
            permittedTemplates.add(template);
        }
    } catch (final Exception e) {
        s_logger.warn("Error listing permitted templates", e);
    }
    return permittedTemplates;
}
Also used : VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ImageFormat(com.cloud.storage.Storage.ImageFormat) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ResultSet(java.sql.ResultSet)

Aggregations

ImageFormat (com.cloud.storage.Storage.ImageFormat)30 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Test (org.junit.Test)9 Answer (com.cloud.agent.api.Answer)8 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)8 PrimaryStorageDownloadCommand (com.cloud.agent.api.storage.PrimaryStorageDownloadCommand)8 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)8 StoragePool (com.cloud.storage.StoragePool)8 ArrayList (java.util.ArrayList)8 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)7 DataTO (com.cloud.agent.api.to.DataTO)7 VMTemplateVO (com.cloud.storage.VMTemplateVO)7 NfsTO (com.cloud.agent.api.to.NfsTO)6 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)5 Account (com.cloud.user.Account)5 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)4 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool)4 IOException (java.io.IOException)4 Date (java.util.Date)4