Search in sources :

Example 1 with RestoreVMSnapshotAnswer

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);
            }
            ;
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 2 with RestoreVMSnapshotAnswer

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;
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.agent.api.ClusterVMMetaDataSyncAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand)

Example 3 with RestoreVMSnapshotAnswer

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);
            }
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 4 with RestoreVMSnapshotAnswer

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;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) NicTO(com.cloud.agent.api.to.NicTO)

Example 5 with RestoreVMSnapshotAnswer

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;
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) MigrateVmToPoolAnswer(com.cloud.agent.api.MigrateVmToPoolAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.agent.api.ClusterVMMetaDataSyncAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand)

Aggregations

RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)6 Answer (com.cloud.agent.api.Answer)4 StartAnswer (com.cloud.agent.api.StartAnswer)4 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)2 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)2 CheckVirtualMachineCommand (com.cloud.agent.api.CheckVirtualMachineCommand)2 ClusterVMMetaDataSyncAnswer (com.cloud.agent.api.ClusterVMMetaDataSyncAnswer)2 GetVmDiskStatsAnswer (com.cloud.agent.api.GetVmDiskStatsAnswer)2 GetVmStatsAnswer (com.cloud.agent.api.GetVmStatsAnswer)2 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)2 RebootAnswer (com.cloud.agent.api.RebootAnswer)2 RestoreVMSnapshotCommand (com.cloud.agent.api.RestoreVMSnapshotCommand)2 StopAnswer (com.cloud.agent.api.StopAnswer)2 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)2 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)2 NicTO (com.cloud.agent.api.to.NicTO)2 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2 CloudException (com.cloud.exception.CloudException)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)2