use of org.apache.cloudstack.storage.command.SnapshotAndCopyCommand in project cloudstack by apache.
the class StorageSystemSnapshotStrategy method performSnapshotAndCopyOnHostSide.
private void performSnapshotAndCopyOnHostSide(VolumeInfo volumeInfo, SnapshotInfo snapshotInfo) {
Map<String, String> sourceDetails = null;
VolumeVO volumeVO = volumeDao.findById(volumeInfo.getId());
Long vmInstanceId = volumeVO.getInstanceId();
VMInstanceVO vmInstanceVO = vmInstanceDao.findById(vmInstanceId);
Long hostId = null;
// if the volume to snapshot is associated with a VM
if (vmInstanceVO != null) {
hostId = vmInstanceVO.getHostId();
// if the VM is not associated with a host
if (hostId == null) {
hostId = vmInstanceVO.getLastHostId();
if (hostId == null) {
sourceDetails = getSourceDetails(volumeInfo);
}
}
} else // volume to snapshot is not associated with a VM (could be a data disk in the detached state)
{
sourceDetails = getSourceDetails(volumeInfo);
}
HostVO hostVO = null;
if (hostId != null) {
hostVO = hostDao.findById(hostId);
}
if (hostVO == null || !ResourceState.Enabled.equals(hostVO.getResourceState())) {
Optional<HostVO> optHostVO = getHost(volumeInfo.getDataCenterId(), false);
if (optHostVO.isPresent()) {
hostVO = optHostVO.get();
}
}
if (hostVO == null) {
final String errMsg = "Unable to locate an applicable host";
s_logger.error("performSnapshotAndCopyOnHostSide: " + errMsg);
throw new CloudRuntimeException(errMsg);
}
long storagePoolId = volumeVO.getPoolId();
StoragePoolVO storagePoolVO = storagePoolDao.findById(storagePoolId);
DataStore dataStore = dataStoreMgr.getDataStore(storagePoolId, DataStoreRole.Primary);
Map<String, String> destDetails = getDestDetails(storagePoolVO, snapshotInfo);
SnapshotAndCopyCommand snapshotAndCopyCommand = new SnapshotAndCopyCommand(volumeInfo.getPath(), sourceDetails, destDetails);
SnapshotAndCopyAnswer snapshotAndCopyAnswer;
try {
// if sourceDetails != null, we need to connect the host(s) to the volume
if (sourceDetails != null) {
volService.grantAccess(volumeInfo, hostVO, dataStore);
}
volService.grantAccess(snapshotInfo, hostVO, dataStore);
snapshotAndCopyAnswer = (SnapshotAndCopyAnswer) agentMgr.send(hostVO.getId(), snapshotAndCopyCommand);
} catch (Exception ex) {
throw new CloudRuntimeException(ex.getMessage());
} finally {
try {
volService.revokeAccess(snapshotInfo, hostVO, dataStore);
// if sourceDetails != null, we need to disconnect the host(s) from the volume
if (sourceDetails != null) {
volService.revokeAccess(volumeInfo, hostVO, dataStore);
}
} catch (Exception ex) {
s_logger.debug(ex.getMessage(), ex);
}
}
if (snapshotAndCopyAnswer == null || !snapshotAndCopyAnswer.getResult()) {
final String errMsg;
if (snapshotAndCopyAnswer != null && snapshotAndCopyAnswer.getDetails() != null && !snapshotAndCopyAnswer.getDetails().isEmpty()) {
errMsg = snapshotAndCopyAnswer.getDetails();
} else {
errMsg = "Unable to perform host-side operation";
}
throw new CloudRuntimeException(errMsg);
}
// for XenServer, this is the VDI's UUID
String path = snapshotAndCopyAnswer.getPath();
SnapshotDetailsVO snapshotDetail = new SnapshotDetailsVO(snapshotInfo.getId(), DiskTO.PATH, path, false);
snapshotDetailsDao.persist(snapshotDetail);
}
Aggregations