use of com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method revertVMSnapshot.
@Override
public boolean revertVMSnapshot(final VMSnapshot vmSnapshot) {
final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
final UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.RevertRequested);
} catch (final NoTransitionException e) {
throw new CloudRuntimeException(e.getMessage());
}
boolean result = false;
try {
final VMSnapshotVO snapshot = vmSnapshotDao.findById(vmSnapshotVO.getId());
final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
final String vmInstanceName = userVm.getInstanceName();
final VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(snapshot).getParent();
final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, true);
final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
final RevertToVMSnapshotCommand revertToSnapshotCommand = new RevertToVMSnapshotCommand(vmInstanceName, userVm.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
final HostVO host = hostDao.findById(hostId);
final GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
if (guestOsMapping == null) {
revertToSnapshotCommand.setPlatformEmulator(null);
} else {
revertToSnapshotCommand.setPlatformEmulator(guestOsMapping.getGuestOsName());
}
final RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) agentMgr.send(hostId, revertToSnapshotCommand);
if (answer != null && answer.getResult()) {
processAnswer(vmSnapshotVO, userVm, answer, hostId);
result = true;
} else {
String errMsg = "Revert VM: " + userVm.getInstanceName() + " to snapshot: " + vmSnapshotVO.getName() + " failed";
if (answer != null && answer.getDetails() != null) {
errMsg = errMsg + " due to " + answer.getDetails();
}
s_logger.error(errMsg);
throw new CloudRuntimeException(errMsg);
}
} catch (final OperationTimedoutException e) {
s_logger.debug("Failed to revert vm snapshot", e);
throw new CloudRuntimeException(e.getMessage());
} catch (final AgentUnavailableException e) {
s_logger.debug("Failed to revert vm snapshot", e);
throw new CloudRuntimeException(e.getMessage());
} finally {
if (!result) {
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
} catch (final NoTransitionException e1) {
s_logger.error("Cannot set vm snapshot state due to: " + e1.getMessage());
}
}
}
return result;
}
use of com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method processAnswer.
@DB
protected void processAnswer(final VMSnapshotVO vmSnapshot, final UserVm userVm, final Answer as, final Long hostId) {
try {
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<NoTransitionException>() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) throws NoTransitionException {
if (as instanceof CreateVMSnapshotAnswer) {
final CreateVMSnapshotAnswer answer = (CreateVMSnapshotAnswer) as;
finalizeCreate(vmSnapshot, answer.getVolumeTOs());
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded);
} else if (as instanceof RevertToVMSnapshotAnswer) {
final RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) as;
finalizeRevert(vmSnapshot, answer.getVolumeTOs());
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded);
} else if (as instanceof DeleteVMSnapshotAnswer) {
final DeleteVMSnapshotAnswer answer = (DeleteVMSnapshotAnswer) as;
finalizeDelete(vmSnapshot, answer.getVolumeTOs());
vmSnapshotDao.remove(vmSnapshot.getId());
}
}
});
} catch (final Exception e) {
final String errMsg = "Error while process answer: " + as.getClass() + " due to " + e.getMessage();
s_logger.error(errMsg, e);
throw new CloudRuntimeException(errMsg);
}
}
use of com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class LibvirtRevertToVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
final PowerState vmState;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
}
final DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
if (snapshot == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
}
dm.revertToSnapshot(snapshot, VIR_DOMAIN_SNAPSHOT_REVERT_FORCE | VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING);
snapshot.free();
if (!snapshotMemory) {
dm.destroy();
if (dm.isPersistent() == 1) {
dm.undefine();
}
vmState = PowerState.PowerOff;
} else {
vmState = PowerState.PowerOn;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (final LibvirtException e) {
final String msg = " Revert to VM snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class VMSnapshotStrategyTest method testRevertSnapshot.
@Test
public void testRevertSnapshot() throws AgentUnavailableException, OperationTimedoutException {
final Long hostId = 1L;
final Long vmId = 1L;
final Long guestOsId = 1L;
final HypervisorType hypervisorType = HypervisorType.Any;
final String hypervisorVersion = "default";
final String guestOsName = "Other";
final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
Mockito.when(vmSnapshot.getId()).thenReturn(1L);
Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
final HostVO hostVO = Mockito.mock(HostVO.class);
Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
Exception e = null;
try {
vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
} catch (final CloudRuntimeException e1) {
e = e1;
}
assertNotNull(e);
final RevertToVMSnapshotAnswer answer = Mockito.mock(RevertToVMSnapshotAnswer.class);
Mockito.when(answer.getResult()).thenReturn(Boolean.TRUE);
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
final boolean result = vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
assertTrue(result);
}
use of com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class CitrixRevertToVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
final String vmName = command.getVmName();
final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
final VMSnapshot.Type vmSnapshotType = command.getTarget().getType();
final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
final Connection conn = citrixResourceBase.getConnection();
PowerState vmState = null;
VM vm = null;
try {
final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
if (vmSnapshots == null || vmSnapshots.size() == 0) {
return new RevertToVMSnapshotAnswer(command, false, "Cannot find vmSnapshot with name: " + command.getTarget().getSnapshotName());
}
final VM vmSnapshot = vmSnapshots.iterator().next();
// find target VM or creating a work VM
try {
vm = citrixResourceBase.getVM(conn, vmName);
} catch (final Exception e) {
vm = citrixResourceBase.createWorkingVM(conn, vmName, command.getGuestOSType(), command.getPlatformEmulator(), listVolumeTo);
}
if (vm == null) {
return new RevertToVMSnapshotAnswer(command, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
}
// call plugin to execute revert
citrixResourceBase.revertToSnapshot(conn, vmSnapshot, vmName, vm.getUuid(conn), snapshotMemory, citrixResourceBase.getHost().getUuid());
vm = citrixResourceBase.getVM(conn, vmName);
final Set<VBD> vbds = vm.getVBDs(conn);
final Map<String, VDI> vdiMap = new HashMap<>();
// get vdi:vbdr to a map
for (final VBD vbd : vbds) {
final VBD.Record vbdr = vbd.getRecord(conn);
if (vbdr.type == Types.VbdType.DISK) {
final VDI vdi = vbdr.VDI;
vdiMap.put(vbdr.userdevice, vdi);
}
}
if (!snapshotMemory) {
vm.destroy(conn);
vmState = PowerState.PowerOff;
} else {
vmState = PowerState.PowerOn;
}
// after revert, VM's volumes path have been changed, need to report to manager
for (final VolumeObjectTO volumeTo : listVolumeTo) {
final Long deviceId = volumeTo.getDeviceId();
final VDI vdi = vdiMap.get(deviceId.toString());
volumeTo.setPath(vdi.getUuid(conn));
}
return new RevertToVMSnapshotAnswer(command, listVolumeTo, vmState);
} catch (final Exception e) {
s_logger.error("revert vm " + vmName + " to snapshot " + command.getTarget().getSnapshotName() + " failed due to " + e.getMessage());
return new RevertToVMSnapshotAnswer(command, false, e.getMessage());
}
}
Aggregations