Search in sources :

Example 66 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method execute.

protected ScaleVmAnswer execute(ScaleVmCommand cmd) {
    VmwareContext context = getServiceContext();
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        int ramMb = getReservedMemoryMb(vmSpec);
        long hotaddIncrementSizeInMb;
        long hotaddMemoryLimitInMb;
        long requestedMaxMemoryInMb = vmSpec.getMaxRam() / (1024 * 1024);
        // Check if VM is really running on hypervisor host
        if (getVmPowerState(vmMo) != PowerState.PowerOn) {
            throw new CloudRuntimeException("Found that the VM " + vmMo.getVmName() + " is not running. Unable to scale-up this VM");
        }
        // Check max hot add limit
        hotaddIncrementSizeInMb = vmMo.getHotAddMemoryIncrementSizeInMb();
        hotaddMemoryLimitInMb = vmMo.getHotAddMemoryLimitInMb();
        if (requestedMaxMemoryInMb > hotaddMemoryLimitInMb) {
            throw new CloudRuntimeException("Memory of VM " + vmMo.getVmName() + " cannot be scaled to " + requestedMaxMemoryInMb + "MB." + " Requested memory limit is beyond the hotadd memory limit for this VM at the moment is " + hotaddMemoryLimitInMb + "MB.");
        }
        // Check increment is multiple of increment size
        long reminder = requestedMaxMemoryInMb % hotaddIncrementSizeInMb;
        if (reminder != 0) {
            requestedMaxMemoryInMb = requestedMaxMemoryInMb + hotaddIncrementSizeInMb - reminder;
        }
        // Check if license supports the feature
        VmwareHelper.isFeatureLicensed(hyperHost, FeatureKeyConstants.HOTPLUG);
        VmwareHelper.setVmScaleUpConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), vmSpec.getMinSpeed(), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Unable to execute ScaleVmCommand");
        }
    } catch (Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new ScaleVmAnswer(cmd, false, "Unable to execute ScaleVmCommand due to " + e.toString());
    }
    return new ScaleVmAnswer(cmd, true, null);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) 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 67 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method gcAndKillHungWorkerVMs.

private void gcAndKillHungWorkerVMs() {
    try {
        // take the chance to do left-over dummy VM cleanup from previous run
        VmwareContext context = getServiceContext();
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        if (hyperHost.isHyperHostConnected()) {
            mgr.gcLeftOverVMs(context);
            s_logger.info("Scan hung worker VM to recycle");
            int workerKey = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_WORKER);
            int workerTagKey = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_WORKER_TAG);
            String workerPropName = String.format("value[%d]", workerKey);
            String workerTagPropName = String.format("value[%d]", workerTagKey);
            // GC worker that has been running for too long
            ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "config.template", workerPropName, workerTagPropName });
            if (ocs != null) {
                for (ObjectContent oc : ocs) {
                    List<DynamicProperty> props = oc.getPropSet();
                    if (props != null) {
                        boolean template = false;
                        boolean isWorker = false;
                        String workerTag = null;
                        for (DynamicProperty prop : props) {
                            if (prop.getName().equals("config.template")) {
                                template = (Boolean) prop.getVal();
                            } else if (prop.getName().equals(workerPropName)) {
                                CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
                                if (val != null && val.getValue() != null && val.getValue().equalsIgnoreCase("true"))
                                    isWorker = true;
                            } else if (prop.getName().equals(workerTagPropName)) {
                                CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
                                workerTag = val.getValue();
                            }
                        }
                        VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
                        if (!template && isWorker) {
                            boolean recycle = false;
                            recycle = mgr.needRecycle(workerTag);
                            if (recycle) {
                                s_logger.info("Recycle pending worker VM: " + vmMo.getName());
                                vmMo.powerOff();
                                vmMo.detachAllDisks();
                                vmMo.destroy();
                            }
                        }
                    }
                }
            }
        } else {
            s_logger.error("Host is no longer connected.");
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
    }
}
Also used : DynamicProperty(com.vmware.vim25.DynamicProperty) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) ObjectContent(com.vmware.vim25.ObjectContent) CustomFieldStringValue(com.vmware.vim25.CustomFieldStringValue) RemoteException(java.rmi.RemoteException)

Example 68 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
    VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    String vmName = srcVolume.getVmName();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        DataStoreTO primaryStorage = srcVolume.getDataStore();
        NfsTO destStore = (NfsTO) destVolume.getDataStore();
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        Pair<String, String> result;
        result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(), destVolume.getPath(), destStore.getUrl(), hostService.getWorkerName(context, cmd, 0));
        VolumeObjectTO newVolume = new VolumeObjectTO();
        newVolume.setPath(result.first() + File.separator + result.second());
        return new CopyCmdAnswer(newVolume);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to execute CopyVolumeCommand due to exception";
        s_logger.error(msg, e);
        return new CopyCmdAnswer("copy volume from primary to secondary failed due to exception: " + VmwareHelper.getExceptionMessage(e));
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 69 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method removeManagedTargetsFromCluster.

private void removeManagedTargetsFromCluster(List<String> iqns) throws Exception {
    List<HostInternetScsiHbaStaticTarget> lstManagedTargets = new ArrayList<HostInternetScsiHbaStaticTarget>();
    VmwareContext context = hostService.getServiceContext(null);
    VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
    ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
    ClusterMO cluster = new ClusterMO(context, morCluster);
    List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
    HostMO host = new HostMO(context, lstHosts.get(0).first());
    HostStorageSystemMO hostStorageSystem = host.getHostStorageSystemMO();
    for (HostHostBusAdapter hba : hostStorageSystem.getStorageDeviceInfo().getHostBusAdapter()) {
        if (hba instanceof HostInternetScsiHba) {
            List<HostInternetScsiHbaStaticTarget> lstTargets = ((HostInternetScsiHba) hba).getConfiguredStaticTarget();
            if (lstTargets != null) {
                for (HostInternetScsiHbaStaticTarget target : lstTargets) {
                    if (iqns.contains(target.getIScsiName())) {
                        lstManagedTargets.add(target);
                    }
                }
            }
        }
    }
    addRemoveInternetScsiTargetsToAllHosts(context, false, lstManagedTargets, lstHosts);
    rescanAllHosts(context, lstHosts);
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HostInternetScsiHbaStaticTarget(com.vmware.vim25.HostInternetScsiHbaStaticTarget) ArrayList(java.util.ArrayList) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HostInternetScsiHba(com.vmware.vim25.HostInternetScsiHba) HostStorageSystemMO(com.cloud.hypervisor.vmware.mo.HostStorageSystemMO) HostHostBusAdapter(com.vmware.vim25.HostHostBusAdapter) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 70 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method attachIso.

private Answer attachIso(DiskTO disk, boolean isAttach, String vmName) {
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        NfsTO nfsImageStore = (NfsTO) iso.getDataStore();
        String storeUrl = null;
        if (nfsImageStore != null) {
            storeUrl = nfsImageStore.getUrl();
        }
        if (storeUrl == null) {
            if (!iso.getName().equalsIgnoreCase("vmware-tools.iso")) {
                String msg = "ISO store root url is not found in AttachIsoCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            } else {
                if (isAttach) {
                    vmMo.mountToolsInstaller();
                } else {
                    try {
                        if (!vmMo.unmountToolsInstaller()) {
                            return new AttachAnswer("Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
                        }
                    } catch (Throwable e) {
                        vmMo.detachIso(null);
                    }
                }
                return new AttachAnswer(disk);
            }
        }
        ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
        String isoPath = nfsImageStore.getUrl() + File.separator + iso.getPath();
        if (!isoPath.startsWith(storeUrl)) {
            assert (false);
            String msg = "ISO path does not start with the secondary storage root";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        int isoNameStartPos = isoPath.lastIndexOf('/');
        String isoFileName = isoPath.substring(isoNameStartPos + 1);
        String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos);
        // TODO, check if iso is already attached, or if there is a previous
        // attachment
        DatastoreMO secondaryDsMo = new DatastoreMO(context, morSecondaryDs);
        String storeName = secondaryDsMo.getName();
        String isoDatastorePath = String.format("[%s] %s/%s", storeName, isoStorePathFromRoot, isoFileName);
        if (isAttach) {
            vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false);
        } else {
            vmMo.detachIso(isoDatastorePath);
        }
        return new AttachAnswer(disk);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        if (isAttach) {
            String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e);
            msg = msg + " Also check if your guest os is a supported version";
            s_logger.error(msg, e);
            return new AttachAnswer(msg);
        } else {
            String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e);
            msg = msg + " Also check if your guest os is a supported version";
            s_logger.warn(msg, e);
            return new AttachAnswer(msg);
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)76 RemoteException (java.rmi.RemoteException)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)47 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)35 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)32 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)28 IOException (java.io.IOException)26 CloudException (com.cloud.exception.CloudException)25 ConfigurationException (javax.naming.ConfigurationException)25 InternalErrorException (com.cloud.exception.InternalErrorException)23 ConnectException (java.net.ConnectException)23 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)16 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)14 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)13 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)11 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)10