Search in sources :

Example 1 with PowerState

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

the class LibvirtComputingResource method getHostVmStateReport.

private HashMap<String, HostVmStateReportEntry> getHostVmStateReport(final Connect conn) {
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<>();
    String[] vms = null;
    int[] ids = null;
    try {
        ids = conn.listDomains();
    } catch (final LibvirtException e) {
        logger.warn("Unable to listDomains", e);
        return null;
    }
    try {
        vms = conn.listDefinedDomains();
    } catch (final LibvirtException e) {
        logger.warn("Unable to listDomains", e);
        return null;
    }
    Domain dm = null;
    for (final int id : ids) {
        try {
            dm = conn.domainLookupByID(id);
            final DomainState ps = dm.getInfo().state;
            final PowerState state = convertToPowerState(ps);
            logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
            final String vmName = dm.getName();
            vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
        } catch (final LibvirtException e) {
            logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
                logger.trace("Ignoring libvirt error.", e);
            }
        }
    }
    for (final String vm : vms) {
        try {
            dm = conn.domainLookupByName(vm);
            final DomainState ps = dm.getInfo().state;
            final PowerState state = convertToPowerState(ps);
            final String vmName = dm.getName();
            logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
            vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
        } catch (final LibvirtException e) {
            logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
                logger.trace("Ignoring libvirt error.", e);
            }
        }
    }
    return vmStates;
}
Also used : LibvirtException(org.libvirt.LibvirtException) HostVmStateReportEntry(com.cloud.legacymodel.vm.HostVmStateReportEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DomainState(org.libvirt.DomainInfo.DomainState) Domain(org.libvirt.Domain) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 2 with PowerState

use of com.cloud.legacymodel.vm.VirtualMachine.PowerState 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);
            }
            ;
        }
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 3 with PowerState

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

the class CitrixCheckVirtualMachineCommandWrapper method execute.

@Override
public Answer execute(final CheckVirtualMachineCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final PowerState powerState = citrixResourceBase.getVmState(conn, vmName);
    final Integer vncPort = null;
    if (powerState == PowerState.PowerOn) {
        s_logger.debug("3. The VM " + vmName + " is in Running state");
    }
    return new CheckVirtualMachineAnswer(command, powerState, vncPort);
}
Also used : CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) Connection(com.xensource.xenapi.Connection) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 4 with PowerState

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

the class LibvirtComputingResource method getVmState.

public PowerState getVmState(final Connect conn, final String vmName) {
    int retry = 3;
    Domain vms = null;
    while (retry-- > 0) {
        try {
            vms = conn.domainLookupByName(vmName);
            final PowerState s = convertToPowerState(vms.getInfo().state);
            return s;
        } catch (final LibvirtException e) {
            logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
        } finally {
            try {
                if (vms != null) {
                    vms.free();
                }
            } catch (final LibvirtException l) {
                logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
    return PowerState.PowerOff;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 5 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)

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