Search in sources :

Example 16 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class VMTemplatePoolDaoImpl method listByTemplateStatus.

@Override
public List<VMTemplateStoragePoolVO> listByTemplateStatus(long templateId, long datacenterId, long podId, VMTemplateStoragePoolVO.Status downloadState) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    List<VMTemplateStoragePoolVO> result = new ArrayList<VMTemplateStoragePoolVO>();
    String sql = DOWNLOADS_STATE_DC_POD;
    try (PreparedStatement pstmt = txn.prepareStatement(sql)) {
        pstmt.setLong(1, datacenterId);
        pstmt.setLong(2, podId);
        pstmt.setLong(3, templateId);
        pstmt.setString(4, downloadState.toString());
        try (ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                // result.add(toEntityBean(rs, false)); TODO: this is buggy in
                // GenericDaoBase for hand constructed queries
                // ID column
                long id = rs.getLong(1);
                result.add(findById(id));
            }
        } catch (Exception e) {
            s_logger.warn("Exception: ", e);
        }
    } catch (Exception e) {
        s_logger.warn("Exception: ", e);
    }
    return result;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class MockStorageManagerImpl method AttachIso.

@Override
public Answer AttachIso(AttachIsoCommand cmd) {
    MockVolumeVO iso = findVolumeFromSecondary(cmd.getIsoPath(), cmd.getStoreUrl(), MockVolumeType.ISO);
    if (iso == null) {
        return new Answer(cmd, false, "Failed to find the iso: " + cmd.getIsoPath() + "on secondary storage " + cmd.getStoreUrl());
    }
    String vmName = cmd.getVmName();
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    MockVMVO vm = null;
    try {
        txn.start();
        vm = _mockVMDao.findByVmName(vmName);
        txn.commit();
        if (vm == null) {
            return new Answer(cmd, false, "can't find vm :" + vmName);
        }
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when attaching iso to vm " + vmName, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new Answer(cmd);
}
Also used : MockVMVO(com.cloud.simulator.MockVMVO) 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) 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) UploadStatusAnswer(org.apache.cloudstack.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 18 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class MockVmManagerImpl method getVmStates.

@Override
public Map<String, PowerState> getVmStates(final String hostGuid) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        final Map<String, PowerState> states = new HashMap<String, PowerState>();
        final List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid);
        if (vms.isEmpty()) {
            txn.commit();
            return states;
        }
        for (final MockVm vm : vms) {
            states.put(vm.getName(), vm.getPowerState());
        }
        txn.commit();
        return states;
    } catch (final Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("unable to fetch vms from host " + hostGuid, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
}
Also used : MockVMVO(com.cloud.simulator.MockVMVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PowerState(com.cloud.vm.VirtualMachine.PowerState) MockVm(com.cloud.simulator.MockVm) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 19 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class MockVmManagerImpl method scaleVm.

@Override
public Answer scaleVm(final ScaleVmCommand cmd) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        final String vmName = cmd.getVmName();
        final MockVMVO vm = _mockVmDao.findByVmName(vmName);
        if (vm == null) {
            return new ScaleVmAnswer(cmd, false, "Can't find VM " + vmName);
        }
        vm.setCpu(cmd.getCpus() * cmd.getMaxSpeed());
        vm.setMemory(cmd.getMaxRam());
        _mockVmDao.update(vm.getId(), vm);
        s_logger.debug("Scaled up VM " + vmName);
        txn.commit();
        return new ScaleVmAnswer(cmd, true, null);
    } catch (final Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Unable to scale up VM", ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
}
Also used : MockVMVO(com.cloud.simulator.MockVMVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 20 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class MockStorageManagerImpl method preinstallTemplates.

@Override
public void preinstallTemplates(String url, long zoneId) {
    MockSecStorageVO storage = null;
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        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/100/" + UUID.randomUUID().toString());
            template.setPoolId(storage.getId());
            template.setSize(defaultTemplateSize);
            template.setType(MockVolumeType.TEMPLATE);
            template.setStatus(Status.DOWNLOADED);
            template = _mockVolumeDao.persist(template);
            template = new MockVolumeVO();
            template.setName("simulator-Centos");
            template.setPath(storage.getMountPoint() + "template/tmpl/1/111/" + UUID.randomUUID().toString());
            template.setPoolId(storage.getId());
            template.setSize(defaultTemplateSize);
            template.setType(MockVolumeType.TEMPLATE);
            template.setStatus(Status.DOWNLOADED);
            template = _mockVolumeDao.persist(template);
            txn.commit();
        }
    } catch (Exception ex) {
        throw new CloudRuntimeException("Unable to find sec storage at " + url, ex);
    } finally {
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) URISyntaxException(java.net.URISyntaxException) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

TransactionLegacy (com.cloud.utils.db.TransactionLegacy)368 PreparedStatement (java.sql.PreparedStatement)174 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)149 SQLException (java.sql.SQLException)133 ResultSet (java.sql.ResultSet)102 ArrayList (java.util.ArrayList)98 DB (com.cloud.utils.db.DB)95 ConfigurationException (javax.naming.ConfigurationException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)35 Date (java.util.Date)34 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 HashMap (java.util.HashMap)29 URISyntaxException (java.net.URISyntaxException)28 AccountVO (com.cloud.user.AccountVO)21 CloudException (com.cloud.exception.CloudException)20 Account (com.cloud.user.Account)20 Field (java.lang.reflect.Field)19 MockVolumeVO (com.cloud.simulator.MockVolumeVO)18 AgentManager (com.cloud.agent.AgentManager)13 IPAddressDao (com.cloud.network.dao.IPAddressDao)13