Search in sources :

Example 76 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.

the class VmwareResource method configureVnc.

protected OptionValue[] configureVnc(OptionValue[] optionsToMerge, VmwareHypervisorHost hyperHost, String vmName, String vncPassword, String keyboardLayout) throws Exception {
    VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
    VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
    if (!mgr.beginExclusiveOperation(600))
        throw new Exception("Unable to begin exclusive operation, lock time out");
    try {
        int maxVncPorts = 64;
        int vncPort = 0;
        Random random = new Random();
        HostMO vmOwnerHost = vmMo.getRunningHost();
        ManagedObjectReference morParent = vmOwnerHost.getParentMor();
        HashMap<String, Integer> portInfo;
        if (morParent.getType().equalsIgnoreCase("ClusterComputeResource")) {
            ClusterMO clusterMo = new ClusterMO(vmOwnerHost.getContext(), morParent);
            portInfo = clusterMo.getVmVncPortsOnCluster();
        } else {
            portInfo = vmOwnerHost.getVmVncPortsOnHost();
        }
        // allocate first at 5900 - 5964 range
        Collection<Integer> existingPorts = portInfo.values();
        int val = random.nextInt(maxVncPorts);
        int startVal = val;
        do {
            if (!existingPorts.contains(5900 + val)) {
                vncPort = 5900 + val;
                break;
            }
            val = (++val) % maxVncPorts;
        } while (val != startVal);
        if (vncPort == 0) {
            s_logger.info("we've run out of range for ports between 5900-5964 for the cluster, we will try port range at 59000-60000");
            Pair<Integer, Integer> additionalRange = mgr.getAddiionalVncPortRange();
            maxVncPorts = additionalRange.second();
            val = random.nextInt(maxVncPorts);
            startVal = val;
            do {
                if (!existingPorts.contains(additionalRange.first() + val)) {
                    vncPort = additionalRange.first() + val;
                    break;
                }
                val = (++val) % maxVncPorts;
            } while (val != startVal);
        }
        if (vncPort == 0) {
            throw new Exception("Unable to find an available VNC port on host");
        }
        if (s_logger.isInfoEnabled()) {
            s_logger.info("Configure VNC port for VM " + vmName + ", port: " + vncPort + ", host: " + vmOwnerHost.getHyperHostName());
        }
        return VmwareHelper.composeVncOptions(optionsToMerge, true, vncPassword, vncPort, keyboardLayout);
    } finally {
        try {
            mgr.endExclusiveOperation();
        } catch (Throwable e) {
            assert (false);
            s_logger.error("Unexpected exception ", e);
        }
    }
}
Also used : VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) Random(java.util.Random) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 77 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(RebootCommand cmd) {
    boolean toolsInstallerMounted = false;
    VirtualMachineMO vmMo = null;
    VmwareContext context = getServiceContext();
    VmwareHypervisorHost hyperHost = getHyperHost(context);
    try {
        vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
        if (vmMo != null) {
            if (vmMo.isToolsInstallerMounted()) {
                toolsInstallerMounted = true;
                s_logger.trace("Detected mounted vmware tools installer for :[" + cmd.getVmName() + "]");
            }
            try {
                if (canSetEnableSetupConfig(vmMo, cmd.getVirtualMachine())) {
                    vmMo.rebootGuest();
                    return new RebootAnswer(cmd, "reboot succeeded", true);
                } else {
                    return new RebootAnswer(cmd, "Failed to configure VM to boot into hardware setup menu: " + vmMo.getName(), false);
                }
            } catch (ToolsUnavailableFaultMsg e) {
                s_logger.warn("VMware tools is not installed at guest OS, we will perform hard reset for reboot");
            } catch (Exception e) {
                s_logger.warn("We are not able to perform gracefull guest reboot due to " + VmwareHelper.getExceptionMessage(e));
            }
            // continue to try with hard-reset
            if (vmMo.reset()) {
                return new RebootAnswer(cmd, "reboot succeeded", true);
            }
            String msg = "Reboot failed in vSphere. vm: " + cmd.getVmName();
            s_logger.warn(msg);
            return new RebootAnswer(cmd, msg, false);
        } else {
            String msg = "Unable to find the VM in vSphere to reboot. vm: " + cmd.getVmName();
            s_logger.warn(msg);
            return new RebootAnswer(cmd, msg, false);
        }
    } catch (Exception e) {
        return new RebootAnswer(cmd, createLogMessageException(e, cmd), false);
    } finally {
        if (toolsInstallerMounted) {
            try {
                vmMo.mountToolsInstaller();
                s_logger.debug(String.format("Successfully re-mounted vmware tools installer for :[%s].", cmd.getVmName()));
            } catch (Exception e) {
                s_logger.error(String.format("Unabled to re-mount vmware tools installer for: [%s].", cmd.getVmName()), e);
            }
        }
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ToolsUnavailableFaultMsg(com.vmware.vim25.ToolsUnavailableFaultMsg) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RebootAnswer(com.cloud.agent.api.RebootAnswer) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Example 78 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.

the class VmwareHelper method pickOneVmOnRunningHost.

public static VirtualMachineMO pickOneVmOnRunningHost(List<VirtualMachineMO> vmList, boolean bFirstFit) throws Exception {
    List<VirtualMachineMO> candidates = new ArrayList<VirtualMachineMO>();
    for (VirtualMachineMO vmMo : vmList) {
        HostMO hostMo = vmMo.getRunningHost();
        if (hostMo.isHyperHostConnected())
            candidates.add(vmMo);
    }
    if (candidates.size() == 0)
        return null;
    if (bFirstFit)
        return candidates.get(0);
    Random random = new Random();
    return candidates.get(random.nextInt(candidates.size()));
}
Also used : Random(java.util.Random) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ArrayList(java.util.ArrayList)

Aggregations

VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)78 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)68 RemoteException (java.rmi.RemoteException)52 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)47 UnsupportedEncodingException (java.io.UnsupportedEncodingException)46 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)40 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)38 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)28 CloudException (com.cloud.exception.CloudException)26 InternalErrorException (com.cloud.exception.InternalErrorException)26 IOException (java.io.IOException)26 ConnectException (java.net.ConnectException)26 ConfigurationException (javax.naming.ConfigurationException)26 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)24 VirtualDisk (com.vmware.vim25.VirtualDisk)20 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)17 Script (com.cloud.utils.script.Script)17 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)16 File (java.io.File)14 Pair (com.cloud.utils.Pair)13