Search in sources :

Example 1 with PowerState

use of com.cloud.vm.VirtualMachine.PowerState in project cloudstack by apache.

the class VmwareResource method getVmStates.

private HashMap<String, PowerState> getVmStates() throws Exception {
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    int key = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }
    String instanceNameCustomField = "value[" + key + "]";
    // CLOUD_VM_INTERNAL_NAME stores the internal CS generated vm name. This was earlier stored in name. Now, name can be either the hostname or
    // the internal CS name, but the custom field CLOUD_VM_INTERNAL_NAME always stores the internal CS name.
    ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template", instanceNameCustomField });
    HashMap<String, PowerState> newStates = new HashMap<String, PowerState>();
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                boolean isTemplate = false;
                String name = null;
                String VMInternalCSName = null;
                VirtualMachinePowerState powerState = VirtualMachinePowerState.POWERED_OFF;
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getName().equals("config.template")) {
                        if (objProp.getVal().toString().equalsIgnoreCase("true")) {
                            isTemplate = true;
                        }
                    } else if (objProp.getName().equals("runtime.powerState")) {
                        powerState = (VirtualMachinePowerState) objProp.getVal();
                    } else if (objProp.getName().equals("name")) {
                        name = (String) objProp.getVal();
                    } else if (objProp.getName().contains(instanceNameCustomField)) {
                        if (objProp.getVal() != null)
                            VMInternalCSName = ((CustomFieldStringValue) objProp.getVal()).getValue();
                    } else {
                        assert (false);
                    }
                }
                if (VMInternalCSName != null)
                    name = VMInternalCSName;
                if (!isTemplate) {
                    newStates.put(name, convertPowerState(powerState));
                }
            }
        }
    }
    return newStates;
}
Also used : DynamicProperty(com.vmware.vim25.DynamicProperty) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HashMap(java.util.HashMap) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ObjectContent(com.vmware.vim25.ObjectContent) VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState) PowerState(com.cloud.vm.VirtualMachine.PowerState) VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState)

Example 2 with PowerState

use of com.cloud.vm.VirtualMachine.PowerState in project cloudstack by apache.

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) {
            s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
        } finally {
            try {
                if (vms != null) {
                    vms.free();
                }
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
    return PowerState.PowerOff;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 3 with PowerState

use of com.cloud.vm.VirtualMachine.PowerState in project cloudstack by apache.

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.agent.api.CheckVirtualMachineAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 4 with PowerState

use of com.cloud.vm.VirtualMachine.PowerState in project cloudstack by apache.

the class OvmResourceBase method getHostVmStateReport.

protected HashMap<String, HostVmStateReportEntry> getHostVmStateReport() throws XmlRpcException {
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<String, HostVmStateReportEntry>();
    Map<String, String> vms = OvmHost.getAllVms(_conn);
    for (final Map.Entry<String, String> entry : vms.entrySet()) {
        PowerState state = toPowerState(entry.getKey(), entry.getValue());
        vmStates.put(entry.getKey(), new HostVmStateReportEntry(state, _conn.getIp()));
    }
    return vmStates;
}
Also used : HostVmStateReportEntry(com.cloud.agent.api.HostVmStateReportEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 5 with PowerState

use of com.cloud.vm.VirtualMachine.PowerState in project cloudstack by apache.

the class OvmResourceBase method getAllVms.

protected HashMap<String, PowerState> getAllVms() throws XmlRpcException {
    final HashMap<String, PowerState> vmStates = new HashMap<String, PowerState>();
    Map<String, String> vms = OvmHost.getAllVms(_conn);
    for (final Map.Entry<String, String> entry : vms.entrySet()) {
        PowerState powerState = toPowerState(entry.getKey(), entry.getValue());
        vmStates.put(entry.getKey(), powerState);
    }
    return vmStates;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PowerState(com.cloud.vm.VirtualMachine.PowerState) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

PowerState (com.cloud.vm.VirtualMachine.PowerState)21 HashMap (java.util.HashMap)9 LibvirtException (org.libvirt.LibvirtException)8 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)7 Domain (org.libvirt.Domain)6 HostVmStateReportEntry (com.cloud.agent.api.HostVmStateReportEntry)4 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)4 Connection (com.xensource.xenapi.Connection)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Connect (org.libvirt.Connect)4 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)3 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)3 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)3 VBD (com.xensource.xenapi.VBD)2 VDI (com.xensource.xenapi.VDI)2 VM (com.xensource.xenapi.VM)2 Map (java.util.Map)2 Answer (com.cloud.agent.api.Answer)1 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)1 CheckHealthAnswer (com.cloud.agent.api.CheckHealthAnswer)1