Search in sources :

Example 16 with MockVolumeVO

use of com.cloud.simulator.MockVolumeVO in project cloudstack by apache.

the class MockStorageManagerImpl method CopyVolume.

@Override
public CopyVolumeAnswer CopyVolume(CopyVolumeCommand cmd) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    boolean toSecondaryStorage = cmd.toSecondaryStorage();
    MockSecStorageVO sec = null;
    MockStoragePoolVO primaryStorage = null;
    try {
        txn.start();
        sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageURL());
        if (sec == null) {
            return new CopyVolumeAnswer(cmd, false, "can't find secondary storage", null, null);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when accessing secondary at " + cmd.getSecondaryStorageURL(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        primaryStorage = _mockStoragePoolDao.findByUuid(cmd.getPool().getUuid());
        if (primaryStorage == null) {
            return new CopyVolumeAnswer(cmd, false, "Can't find primary storage", null, null);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when accessing primary at " + cmd.getPool(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    MockVolumeVO volume = null;
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
        if (volume == null) {
            return new CopyVolumeAnswer(cmd, false, "cant' find volume" + cmd.getVolumePath(), null, null);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when accessing volume at " + cmd.getVolumePath(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    String name = UUID.randomUUID().toString();
    if (toSecondaryStorage) {
        MockVolumeVO vol = new MockVolumeVO();
        vol.setName(name);
        vol.setPath(sec.getMountPoint() + name);
        vol.setPoolId(sec.getId());
        vol.setSize(volume.getSize());
        vol.setStatus(Status.DOWNLOADED);
        vol.setType(MockVolumeType.VOLUME);
        txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
        try {
            txn.start();
            vol = _mockVolumeDao.persist(vol);
            txn.commit();
        } catch (Exception ex) {
            txn.rollback();
            throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when persisting volume " + vol.getName(), ex);
        } finally {
            txn.close();
            txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
            txn.close();
        }
        return new CopyVolumeAnswer(cmd, true, null, sec.getMountPoint(), vol.getPath());
    } else {
        MockVolumeVO vol = new MockVolumeVO();
        vol.setName(name);
        vol.setPath(primaryStorage.getMountPoint() + name);
        vol.setPoolId(primaryStorage.getId());
        vol.setSize(volume.getSize());
        vol.setStatus(Status.DOWNLOADED);
        vol.setType(MockVolumeType.VOLUME);
        txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
        try {
            txn.start();
            vol = _mockVolumeDao.persist(vol);
            txn.commit();
        } catch (Exception ex) {
            txn.rollback();
            throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when persisting volume " + vol.getName(), ex);
        } finally {
            txn.close();
            txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
            txn.close();
        }
        return new CopyVolumeAnswer(cmd, true, null, primaryStorage.getMountPoint(), vol.getPath());
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) MockStoragePoolVO(com.cloud.simulator.MockStoragePoolVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 17 with MockVolumeVO

use of com.cloud.simulator.MockVolumeVO in project cloudstack by apache.

the class MockStorageManagerImpl method CreateVolumeFromSnapshot.

@Override
public CreateVolumeFromSnapshotAnswer CreateVolumeFromSnapshot(CreateVolumeFromSnapshotCommand cmd) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    MockVolumeVO backSnapshot = null;
    MockStoragePoolVO primary = null;
    try {
        txn.start();
        backSnapshot = _mockVolumeDao.findByName(cmd.getSnapshotUuid());
        if (backSnapshot == null) {
            return new CreateVolumeFromSnapshotAnswer(cmd, false, "can't find the backupsnapshot: " + cmd.getSnapshotUuid(), null);
        }
        primary = _mockStoragePoolDao.findByUuid(cmd.getPrimaryStoragePoolNameLabel());
        if (primary == null) {
            return new CreateVolumeFromSnapshotAnswer(cmd, false, "can't find the primary storage: " + cmd.getPrimaryStoragePoolNameLabel(), null);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when creating volume from snapshot", ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    String uuid = UUID.randomUUID().toString();
    MockVolumeVO volume = new MockVolumeVO();
    volume.setName(uuid);
    volume.setPath(primary.getMountPoint() + uuid);
    volume.setPoolId(primary.getId());
    volume.setSize(backSnapshot.getSize());
    volume.setStatus(Status.DOWNLOADED);
    volume.setType(MockVolumeType.VOLUME);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        _mockVolumeDao.persist(volume);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when creating volume from snapshot " + volume, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new CreateVolumeFromSnapshotAnswer(cmd, true, null, volume.getPath());
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MockStoragePoolVO(com.cloud.simulator.MockStoragePoolVO) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)

Example 18 with MockVolumeVO

use of com.cloud.simulator.MockVolumeVO in project CloudStack-archive by CloudStack-extras.

the class MockStorageManagerImpl method ListTemplates.

@Override
public Answer ListTemplates(ListTemplateCommand cmd) {
    MockSecStorageVO storage = _mockSecStorageDao.findByUrl(cmd.getSecUrl());
    if (storage == null) {
        return new Answer(cmd, false, "Failed to get secondary storage");
    }
    List<MockVolumeVO> templates = _mockVolumeDao.findByStorageIdAndType(storage.getId(), MockVolumeType.TEMPLATE);
    Map<String, TemplateInfo> templateInfos = new HashMap<String, TemplateInfo>();
    for (MockVolumeVO template : templates) {
        templateInfos.put(template.getName(), new TemplateInfo(template.getName(), template.getPath().replaceAll(storage.getMountPoint(), ""), template.getSize(), template.getSize(), true, false));
    }
    return new ListTemplateAnswer(cmd.getSecUrl(), templateInfos);
}
Also used : DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) AttachVolumeAnswer(com.cloud.agent.api.AttachVolumeAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) TemplateInfo(com.cloud.storage.template.TemplateInfo) HashMap(java.util.HashMap) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) MockVolumeVO(com.cloud.simulator.MockVolumeVO)

Example 19 with MockVolumeVO

use of com.cloud.simulator.MockVolumeVO in project CloudStack-archive by CloudStack-extras.

the class MockStorageManagerImpl method preinstallTemplates.

@Override
public void preinstallTemplates(String url, long zoneId) {
    MockSecStorageVO storage = _mockSecStorageDao.findByUrl(url);
    if (storage == null) {
        storage = new MockSecStorageVO();
        URI uri;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            return;
        }
        String nfsHost = uri.getHost();
        String nfsPath = uri.getPath();
        String path = nfsHost + ":" + nfsPath;
        String dir = "/mnt/" + UUID.nameUUIDFromBytes(path.getBytes()).toString() + File.separator;
        storage.setUrl(url);
        storage.setCapacity(DEFAULT_HOST_STORAGE_SIZE);
        storage.setMountPoint(dir);
        storage = _mockSecStorageDao.persist(storage);
        //preinstall default templates into secondary storage
        long defaultTemplateSize = 2 * 1024 * 1024 * 1024L;
        MockVolumeVO template = new MockVolumeVO();
        template.setName("simulator-domR");
        template.setPath(storage.getMountPoint() + "template/tmpl/1/9/" + UUID.randomUUID().toString());
        template.setPoolId(storage.getId());
        template.setSize(defaultTemplateSize);
        template.setType(MockVolumeType.TEMPLATE);
        template.setStatus(Status.DOWNLOADED);
        _mockVolumeDao.persist(template);
        template = new MockVolumeVO();
        template.setName("simulator-Centos");
        template.setPath(storage.getMountPoint() + "template/tmpl/1/10/" + UUID.randomUUID().toString());
        template.setPoolId(storage.getId());
        template.setSize(defaultTemplateSize);
        template.setType(MockVolumeType.TEMPLATE);
        template.setStatus(Status.DOWNLOADED);
        _mockVolumeDao.persist(template);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URI(java.net.URI)

Example 20 with MockVolumeVO

use of com.cloud.simulator.MockVolumeVO in project CloudStack-archive by CloudStack-extras.

the class MockStorageManagerImpl method CreatePrivateTemplateFromVolume.

@Override
public CreatePrivateTemplateAnswer CreatePrivateTemplateFromVolume(CreatePrivateTemplateFromVolumeCommand cmd) {
    MockVolumeVO volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
    if (volume == null) {
        return new CreatePrivateTemplateAnswer(cmd, false, "cant' find volume" + cmd.getVolumePath());
    }
    MockSecStorageVO sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
    if (sec == null) {
        return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
    }
    MockVolumeVO template = new MockVolumeVO();
    String uuid = UUID.randomUUID().toString();
    template.setName(uuid);
    template.setPath(sec.getMountPoint() + uuid);
    template.setPoolId(sec.getId());
    template.setSize(volume.getSize());
    template.setStatus(Status.DOWNLOADED);
    template.setType(MockVolumeType.TEMPLATE);
    template = _mockVolumeDao.persist(template);
    return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(), template.getSize(), template.getName(), ImageFormat.QCOW2);
}
Also used : CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO)

Aggregations

MockVolumeVO (com.cloud.simulator.MockVolumeVO)37 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 PrimaryStorageDownloadAnswer (com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer)18 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)18 URISyntaxException (java.net.URISyntaxException)18 MockSecStorageVO (com.cloud.simulator.MockSecStorageVO)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 ConfigurationException (javax.naming.ConfigurationException)17 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)16 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)16 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)14 CreateVolumeFromSnapshotAnswer (com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)14 ManageSnapshotAnswer (com.cloud.agent.api.ManageSnapshotAnswer)14 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)14 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)14 Answer (com.cloud.agent.api.Answer)12 GetStorageStatsAnswer (com.cloud.agent.api.GetStorageStatsAnswer)12 ModifyStoragePoolAnswer (com.cloud.agent.api.ModifyStoragePoolAnswer)12 SecStorageSetupAnswer (com.cloud.agent.api.SecStorageSetupAnswer)12 ListTemplateAnswer (com.cloud.agent.api.storage.ListTemplateAnswer)12