Search in sources :

Example 6 with PowerState

use of com.cloud.legacymodel.vm.VirtualMachine.PowerState in project cosmic by MissionCriticalCloud.

the class LibvirtCheckVirtualMachineCommandWrapper method execute.

@Override
public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName());
        Integer vncPort = null;
        if (state == PowerState.PowerOn) {
            vncPort = libvirtComputingResource.getVncPort(conn, command.getVmName());
        }
        return new CheckVirtualMachineAnswer(command, state, vncPort);
    } catch (final LibvirtException e) {
        return new CheckVirtualMachineAnswer(command, e.getMessage());
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 7 with PowerState

use of com.cloud.legacymodel.vm.VirtualMachine.PowerState 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(8);
        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.legacymodel.communication.answer.RestoreVMSnapshotAnswer) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Aggregations

PowerState (com.cloud.legacymodel.vm.VirtualMachine.PowerState)7 LibvirtException (org.libvirt.LibvirtException)5 Domain (org.libvirt.Domain)4 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)3 Connect (org.libvirt.Connect)3 CheckVirtualMachineAnswer (com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer)2 RevertToVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer)2 VMSnapshot (com.cloud.legacymodel.storage.VMSnapshot)2 Connection (com.xensource.xenapi.Connection)2 HashMap (java.util.HashMap)2 RestoreVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer)1 VMSnapshotTO (com.cloud.legacymodel.to.VMSnapshotTO)1 HostVmStateReportEntry (com.cloud.legacymodel.vm.HostVmStateReportEntry)1 VBD (com.xensource.xenapi.VBD)1 VDI (com.xensource.xenapi.VDI)1 VM (com.xensource.xenapi.VM)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DomainState (org.libvirt.DomainInfo.DomainState)1 DomainSnapshot (org.libvirt.DomainSnapshot)1