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;
}
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);
}
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();
}
}
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();
}
}
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();
}
}
Aggregations