use of com.cloud.agent.api.RestoreVMSnapshotAnswer in project cloudstack by apache.
the class LibvirtRestoreVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
String vmName = cmd.getVmName();
List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
}
String xmlDesc = dm.getXMLDesc(0);
List<VMSnapshotTO> snapshots = cmd.getSnapshots();
Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
for (VMSnapshotTO snapshot : snapshots) {
VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
try {
// VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
int flags = 1;
if (snapshot.getCurrent()) {
// VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
flags += 2;
}
dm.snapshotCreateXML(vmSnapshotXML, flags);
} catch (LibvirtException e) {
s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
}
}
return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (LibvirtException e) {
String msg = " Restore snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RestoreVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of com.cloud.agent.api.RestoreVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class VirtualMachineManagerImpl method checkVmOnHost.
protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) throws AgentUnavailableException, OperationTimedoutException {
final Answer answer = _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName()));
if (answer == null || !answer.getResult()) {
return false;
}
if (answer instanceof CheckVirtualMachineAnswer) {
final CheckVirtualMachineAnswer vmAnswer = (CheckVirtualMachineAnswer) answer;
if (vmAnswer.getState() == PowerState.PowerOff) {
return false;
}
}
UserVmVO userVm = _userVmDao.findById(vm.getId());
if (userVm != null) {
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vm.getId());
RestoreVMSnapshotCommand command = _vmSnapshotMgr.createRestoreCommand(userVm, vmSnapshots);
if (command != null) {
RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) _agentMgr.send(hostId, command);
if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) {
s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer.getDetails());
}
}
}
return true;
}
use of com.cloud.agent.api.RestoreVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class LibvirtRestoreVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
final PowerState vmState = PowerState.PowerOn;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
}
final String xmlDesc = dm.getXMLDesc(0);
final List<VMSnapshotTO> snapshots = cmd.getSnapshots();
final Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
for (final VMSnapshotTO snapshot : snapshots) {
final VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
final String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
try {
// VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
int flags = 1;
if (snapshot.getCurrent()) {
// VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
flags += 2;
}
dm.snapshotCreateXML(vmSnapshotXML, flags);
} catch (final LibvirtException e) {
s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
}
}
return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (final LibvirtException e) {
final String msg = " Restore snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RestoreVMSnapshotAnswer(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.agent.api.RestoreVMSnapshotAnswer in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method finalizeStart.
@Override
public boolean finalizeStart(final VirtualMachineProfile profile, final long hostId, final Commands cmds, final ReservationContext context) {
final UserVmVO vm = _vmDao.findById(profile.getId());
final Answer[] answersToCmds = cmds.getAnswers();
if (answersToCmds == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Returning from finalizeStart() since there are no answers to read");
}
return true;
}
final Answer startAnswer = cmds.getAnswer(StartAnswer.class);
String returnedIp = null;
String originalIp = null;
if (startAnswer != null) {
final StartAnswer startAns = (StartAnswer) startAnswer;
final VirtualMachineTO vmTO = startAns.getVirtualMachine();
for (final NicTO nicTO : vmTO.getNics()) {
if (nicTO.getType() == TrafficType.Guest) {
returnedIp = nicTO.getIp();
}
}
}
final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
NicVO guestNic = null;
NetworkVO guestNetwork = null;
for (final NicVO nic : nics) {
final NetworkVO network = _networkDao.findById(nic.getNetworkId());
final long isDefault = nic.isDefaultNic() ? 1 : 0;
if (network.getTrafficType() == TrafficType.Guest) {
originalIp = nic.getIPv4Address();
guestNic = nic;
guestNetwork = network;
if (nic.getBroadcastUri().getScheme().equals("pvlan")) {
final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, "pvlan-nic");
if (!setupVmForPvlan(true, hostId, nicProfile)) {
return false;
}
}
}
}
boolean ipChanged = false;
if (originalIp != null && !originalIp.equalsIgnoreCase(returnedIp)) {
if (returnedIp != null && guestNic != null) {
guestNic.setIPv4Address(returnedIp);
ipChanged = true;
}
}
if (returnedIp != null && !returnedIp.equalsIgnoreCase(originalIp)) {
if (guestNic != null) {
guestNic.setIPv4Address(returnedIp);
ipChanged = true;
}
}
// get system ip and create static nat rule for the vm
try {
_rulesMgr.getSystemIpAndEnableStaticNatForVm(profile.getVirtualMachine(), false);
} catch (final Exception ex) {
s_logger.warn("Failed to get system ip and enable static nat for the vm " + profile.getVirtualMachine() + " due to exception ", ex);
return false;
}
final Answer answer = cmds.getAnswer("restoreVMSnapshot");
if (answer != null && answer instanceof RestoreVMSnapshotAnswer) {
final RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) answer;
if (!restoreVMSnapshotAnswer.getResult()) {
s_logger.warn("Unable to restore the vm snapshot from image file to the VM: " + restoreVMSnapshotAnswer.getDetails());
}
}
return true;
}
use of com.cloud.agent.api.RestoreVMSnapshotAnswer in project cloudstack by apache.
the class VirtualMachineManagerImpl method checkVmOnHost.
protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) throws AgentUnavailableException, OperationTimedoutException {
final Answer answer = _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName()));
if (answer == null || !answer.getResult()) {
return false;
}
if (answer instanceof CheckVirtualMachineAnswer) {
final CheckVirtualMachineAnswer vmAnswer = (CheckVirtualMachineAnswer) answer;
if (vmAnswer.getState() == PowerState.PowerOff) {
return false;
}
}
UserVmVO userVm = _userVmDao.findById(vm.getId());
if (userVm != null) {
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vm.getId());
RestoreVMSnapshotCommand command = _vmSnapshotMgr.createRestoreCommand(userVm, vmSnapshots);
if (command != null) {
RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) _agentMgr.send(hostId, command);
if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) {
s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer == null ? "null answer" : restoreVMSnapshotAnswer.getDetails());
}
}
}
return true;
}
Aggregations