Search in sources :

Example 31 with MockVolumeVO

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

the class MockStorageManagerImpl method DownloadProcess.

@Override
public DownloadAnswer DownloadProcess(DownloadProgressCommand cmd) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        String volumeId = cmd.getJobId();
        MockVolumeVO volume = _mockVolumeDao.findById(Long.parseLong(volumeId));
        if (volume == null) {
            return new DownloadAnswer("Can't find the downloading volume", Status.ABANDONED);
        }
        long size = Math.min(volume.getSize() + DEFAULT_TEMPLATE_SIZE / 5, DEFAULT_TEMPLATE_SIZE);
        volume.setSize(size);
        double volumeSize = volume.getSize();
        double pct = volumeSize / DEFAULT_TEMPLATE_SIZE;
        if (pct >= 1.0) {
            volume.setStatus(Status.DOWNLOADED);
            _mockVolumeDao.update(volume.getId(), volume);
            txn.commit();
            return new DownloadAnswer(cmd.getJobId(), 100, cmd, com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED, volume.getPath(), volume.getName());
        } else {
            _mockVolumeDao.update(volume.getId(), volume);
            txn.commit();
            return new DownloadAnswer(cmd.getJobId(), (int) (pct * 100.0), cmd, com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOAD_IN_PROGRESS, volume.getPath(), volume.getName());
        }
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error during download job " + cmd.getJobId(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MockVolumeVO(com.cloud.simulator.MockVolumeVO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 32 with MockVolumeVO

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

the class MockStorageManagerImpl method Download.

@Override
public DownloadAnswer Download(DownloadCommand cmd) {
    MockSecStorageVO ssvo = null;
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        ssvo = _mockSecStorageDao.findByUrl(cmd.getSecUrl());
        if (ssvo == null) {
            return new DownloadAnswer("can't find secondary storage", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error accessing secondary storage " + cmd.getSecUrl(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    MockVolumeVO volume = new MockVolumeVO();
    volume.setPoolId(ssvo.getId());
    volume.setName(cmd.getName());
    volume.setPath(ssvo.getMountPoint() + cmd.getName());
    volume.setSize(0);
    volume.setType(MockVolumeType.TEMPLATE);
    volume.setStatus(Status.DOWNLOAD_IN_PROGRESS);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        volume = _mockVolumeDao.persist(volume);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when saving volume " + volume, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new DownloadAnswer(String.valueOf(volume.getId()), 0, "Downloading", Status.DOWNLOAD_IN_PROGRESS, cmd.getName(), cmd.getName(), volume.getSize(), volume.getSize(), null);
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 33 with MockVolumeVO

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

the class MockStorageManagerImpl method BackupSnapshot.

@Override
public BackupSnapshotAnswer BackupSnapshot(BackupSnapshotCommand cmd, SimulatorInfo info) {
    // emulate xenserver backupsnapshot, if the base volume is deleted, then
    // backupsnapshot failed
    MockVolumeVO volume = null;
    MockVolumeVO snapshot = null;
    MockSecStorageVO secStorage = null;
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
        if (volume == null) {
            return new BackupSnapshotAnswer(cmd, false, "Can't find base volume: " + cmd.getVolumePath(), null, true);
        }
        String snapshotPath = cmd.getSnapshotUuid();
        snapshot = _mockVolumeDao.findByStoragePathAndType(snapshotPath);
        if (snapshot == null) {
            return new BackupSnapshotAnswer(cmd, false, "can't find snapshot" + snapshotPath, null, true);
        }
        String secStorageUrl = cmd.getSecondaryStorageUrl();
        secStorage = _mockSecStorageDao.findByUrl(secStorageUrl);
        if (secStorage == null) {
            return new BackupSnapshotAnswer(cmd, false, "can't find sec storage" + snapshotPath, null, true);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when backing up snapshot");
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    MockVolumeVO newsnapshot = new MockVolumeVO();
    String name = UUID.randomUUID().toString();
    newsnapshot.setName(name);
    newsnapshot.setPath(secStorage.getMountPoint() + name);
    newsnapshot.setPoolId(secStorage.getId());
    newsnapshot.setSize(snapshot.getSize());
    newsnapshot.setStatus(Status.DOWNLOADED);
    newsnapshot.setType(MockVolumeType.SNAPSHOT);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        snapshot = _mockVolumeDao.persist(snapshot);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when backing up snapshot " + newsnapshot, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new BackupSnapshotAnswer(cmd, true, null, newsnapshot.getName(), true);
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 34 with MockVolumeVO

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

the class MockStorageManagerImpl method primaryStorageDownload.

@Override
public PrimaryStorageDownloadAnswer primaryStorageDownload(PrimaryStorageDownloadCommand cmd) {
    MockVolumeVO template = findVolumeFromSecondary(cmd.getUrl(), cmd.getSecondaryStorageUrl(), MockVolumeType.TEMPLATE);
    if (template == null) {
        return new PrimaryStorageDownloadAnswer("Can't find primary storage");
    }
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    MockStoragePoolVO primaryStorage = null;
    try {
        txn.start();
        primaryStorage = _mockStoragePoolDao.findByUuid(cmd.getPoolUuid());
        txn.commit();
        if (primaryStorage == null) {
            return new PrimaryStorageDownloadAnswer("Can't find primary storage");
        }
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when finding primary storagee " + cmd.getPoolUuid(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    String volumeName = UUID.randomUUID().toString();
    MockVolumeVO newVolume = new MockVolumeVO();
    newVolume.setName(volumeName);
    newVolume.setPath(primaryStorage.getMountPoint() + volumeName);
    newVolume.setPoolId(primaryStorage.getId());
    newVolume.setSize(template.getSize());
    newVolume.setType(MockVolumeType.VOLUME);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        _mockVolumeDao.persist(newVolume);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when saving volume " + newVolume, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new PrimaryStorageDownloadAnswer(newVolume.getPath(), newVolume.getSize());
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) 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)

Example 35 with MockVolumeVO

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

the class MockStorageManagerImpl method CreatePrivateTemplateFromSnapshot.

@Override
public CreatePrivateTemplateAnswer CreatePrivateTemplateFromSnapshot(CreatePrivateTemplateFromSnapshotCommand cmd) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    MockVolumeVO snapshot = null;
    MockSecStorageVO sec = null;
    try {
        txn.start();
        String snapshotUUId = cmd.getSnapshotUuid();
        snapshot = _mockVolumeDao.findByName(snapshotUUId);
        if (snapshot == null) {
            snapshotUUId = cmd.getSnapshotName();
            snapshot = _mockVolumeDao.findByName(snapshotUUId);
            if (snapshot == null) {
                return new CreatePrivateTemplateAnswer(cmd, false, "can't find snapshot:" + snapshotUUId);
            }
        }
        sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
        if (sec == null) {
            return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
        }
        txn.commit();
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    MockVolumeVO template = new MockVolumeVO();
    String uuid = UUID.randomUUID().toString();
    template.setName(uuid);
    template.setPath(sec.getMountPoint() + uuid);
    template.setPoolId(sec.getId());
    template.setSize(snapshot.getSize());
    template.setStatus(Status.DOWNLOADED);
    template.setType(MockVolumeType.TEMPLATE);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        template = _mockVolumeDao.persist(template);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when saving template " + template, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(), template.getSize(), template.getName(), ImageFormat.QCOW2);
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

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