Search in sources :

Example 91 with VmwareContext

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

the class VmwareManagerImpl method importVsphereStoragePoliciesInternal.

public List<? extends VsphereStoragePolicy> importVsphereStoragePoliciesInternal(Long zoneId, Long vmwareDcId) {
    // Get DC associated with this zone
    VmwareDatacenterVO vmwareDatacenter = vmwareDcDao.findById(vmwareDcId);
    String vmwareDcName = vmwareDatacenter.getVmwareDatacenterName();
    String vCenterHost = vmwareDatacenter.getVcenterHost();
    String userName = vmwareDatacenter.getUser();
    String password = vmwareDatacenter.getPassword();
    List<PbmProfile> storageProfiles = null;
    try {
        s_logger.debug(String.format("Importing vSphere Storage Policies for the vmware DC %d in zone %d", vmwareDcId, zoneId));
        VmwareContext context = VmwareContextFactory.getContext(vCenterHost, userName, password);
        PbmProfileManagerMO profileManagerMO = new PbmProfileManagerMO(context);
        storageProfiles = profileManagerMO.getStorageProfiles();
        s_logger.debug(String.format("Import vSphere Storage Policies for the vmware DC %d in zone %d is successful", vmwareDcId, zoneId));
    } catch (Exception e) {
        String msg = String.format("Unable to list storage profiles from DC %s due to : %s", vmwareDcName, VmwareHelper.getExceptionMessage(e));
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    for (PbmProfile storageProfile : storageProfiles) {
        VsphereStoragePolicyVO storagePolicyVO = vsphereStoragePolicyDao.findByPolicyId(zoneId, storageProfile.getProfileId().getUniqueId());
        if (storagePolicyVO == null) {
            storagePolicyVO = new VsphereStoragePolicyVO(zoneId, storageProfile.getProfileId().getUniqueId(), storageProfile.getName(), storageProfile.getDescription());
            vsphereStoragePolicyDao.persist(storagePolicyVO);
        } else {
            storagePolicyVO.setDescription(storageProfile.getDescription());
            storagePolicyVO.setName(storageProfile.getName());
            vsphereStoragePolicyDao.update(storagePolicyVO.getId(), storagePolicyVO);
        }
    }
    List<VsphereStoragePolicyVO> allStoragePolicies = vsphereStoragePolicyDao.listAll();
    List<PbmProfile> finalStorageProfiles = storageProfiles;
    List<VsphereStoragePolicyVO> needToMarkRemoved = allStoragePolicies.stream().filter(existingPolicy -> !finalStorageProfiles.stream().anyMatch(storageProfile -> storageProfile.getProfileId().getUniqueId().equals(existingPolicy.getPolicyId()))).collect(Collectors.toList());
    for (VsphereStoragePolicyVO storagePolicy : needToMarkRemoved) {
        vsphereStoragePolicyDao.remove(storagePolicy.getId());
    }
    List<VsphereStoragePolicyVO> storagePolicies = vsphereStoragePolicyDao.listAll();
    return storagePolicies;
}
Also used : DiscoveredWithErrorException(com.cloud.exception.DiscoveredWithErrorException) NetworkModel(com.cloud.network.NetworkModel) HostDao(com.cloud.host.dao.HostDao) HypervisorGuruManager(com.cloud.hypervisor.HypervisorGuruManager) VmwareDatacenterZoneMap(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMap) Transaction(com.cloud.utils.db.Transaction) ClusterType(com.cloud.org.Cluster.ClusterType) StringUtils(org.apache.commons.lang3.StringUtils) AgentManager(com.cloud.agent.AgentManager) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) VirtualSwitchType(com.cloud.hypervisor.vmware.mo.VirtualSwitchType) VMTemplatePoolDao(com.cloud.storage.dao.VMTemplatePoolDao) RemoveVmwareDcCmd(org.apache.cloudstack.api.command.admin.zone.RemoveVmwareDcCmd) Duration(java.time.Duration) Map(java.util.Map) VsphereStoragePolicy(com.cloud.dc.VsphereStoragePolicy) TrafficType(com.cloud.network.Networks.TrafficType) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) VmwareContextFactory(com.cloud.hypervisor.vmware.resource.VmwareContextFactory) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) VmwareDatacenter(com.cloud.hypervisor.vmware.VmwareDatacenter) TransactionStatus(com.cloud.utils.db.TransactionStatus) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) FileUtil(com.cloud.utils.FileUtil) Executors(java.util.concurrent.Executors) Script(com.cloud.utils.script.Script) ConfigurationServer(com.cloud.server.ConfigurationServer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) ImageStoreDetailsUtil(com.cloud.storage.ImageStoreDetailsUtil) AsyncJobManagerImpl(org.apache.cloudstack.framework.jobs.impl.AsyncJobManagerImpl) ClusterVSMMapDao(com.cloud.dc.dao.ClusterVSMMapDao) ManagementServerHostPeerDao(com.cloud.cluster.dao.ManagementServerHostPeerDao) HypervisorCapabilitiesDao(com.cloud.hypervisor.dao.HypervisorCapabilitiesDao) JavaStorageLayer(com.cloud.storage.JavaStorageLayer) NumbersUtil(com.cloud.utils.NumbersUtil) ManagerBase(com.cloud.utils.component.ManagerBase) DataCenterVO(com.cloud.dc.DataCenterVO) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) CiscoNexusVSMDeviceVO(com.cloud.network.CiscoNexusVSMDeviceVO) ArrayList(java.util.ArrayList) LegacyZoneDao(com.cloud.hypervisor.vmware.dao.LegacyZoneDao) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ClusterDao(com.cloud.dc.dao.ClusterDao) DomainRouterVO(com.cloud.vm.DomainRouterVO) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) UpdateVmwareDcCmd(org.apache.cloudstack.api.command.admin.zone.UpdateVmwareDcCmd) ClusterVO(com.cloud.dc.ClusterVO) VMInstanceDao(com.cloud.vm.dao.VMInstanceDao) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) VmwareCleanupMaid(com.cloud.hypervisor.vmware.VmwareCleanupMaid) VmwareDatacenterDao(com.cloud.hypervisor.vmware.dao.VmwareDatacenterDao) UserVmCloneSettingDao(com.cloud.vm.dao.UserVmCloneSettingDao) ClusterVSMMapVO(com.cloud.dc.ClusterVSMMapVO) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) IOException(java.io.IOException) DB(com.cloud.utils.db.DB) File(java.io.File) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ClusterManager(com.cloud.cluster.ClusterManager) VmwareHelper(com.cloud.hypervisor.vmware.util.VmwareHelper) Command(com.cloud.agent.api.Command) Host(com.cloud.host.Host) AddVmwareDcCmd(org.apache.cloudstack.api.command.admin.zone.AddVmwareDcCmd) ActionEvent(com.cloud.event.ActionEvent) ClusterDetailsDao(com.cloud.dc.ClusterDetailsDao) GlobalLock(com.cloud.utils.db.GlobalLock) ConfigurationDao(org.apache.cloudstack.framework.config.dao.ConfigurationDao) ResourceInUseException(com.cloud.exception.ResourceInUseException) StorageManager(com.cloud.storage.StorageManager) TemplateManager(com.cloud.template.TemplateManager) URLDecoder(java.net.URLDecoder) HypervisorHostHelper(com.cloud.hypervisor.vmware.mo.HypervisorHostHelper) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) StoragePoolStatus(com.cloud.storage.StoragePoolStatus) Random(java.util.Random) StoragePool(com.cloud.storage.StoragePool) UriUtils(com.cloud.utils.UriUtils) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Cluster(com.cloud.org.Cluster) Logger(org.apache.log4j.Logger) StartupCommand(com.cloud.agent.api.StartupCommand) CommandExecLogDao(com.cloud.secstorage.CommandExecLogDao) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) URI(java.net.URI) Answer(com.cloud.agent.api.Answer) Config(com.cloud.configuration.Config) VmwareHostType(com.cloud.hypervisor.vmware.mo.VmwareHostType) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) PrimaryDataStoreDao(org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao) TransactionCallback(com.cloud.utils.db.TransactionCallback) UUID(java.util.UUID) Pair(com.cloud.utils.Pair) Instant(java.time.Instant) VsphereStoragePolicyVO(com.cloud.dc.VsphereStoragePolicyVO) HostFirewallSystemMO(com.cloud.hypervisor.vmware.mo.HostFirewallSystemMO) Collectors(java.util.stream.Collectors) CheckDataStoreStoragePolicyComplainceCommand(org.apache.cloudstack.storage.command.CheckDataStoreStoragePolicyComplainceCommand) RemoteException(java.rmi.RemoteException) Status(com.cloud.host.Status) List(java.util.List) DataCenterDao(com.cloud.dc.dao.DataCenterDao) DiskControllerType(com.cloud.hypervisor.vmware.mo.DiskControllerType) AboutInfo(com.vmware.vim25.AboutInfo) VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) BroadcastDomainType(com.cloud.network.Networks.BroadcastDomainType) DataStoreManager(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager) HashMap(java.util.HashMap) ListVsphereStoragePoliciesCmd(org.apache.cloudstack.api.command.admin.zone.ListVsphereStoragePoliciesCmd) ConfigurationException(javax.naming.ConfigurationException) EventTypes(com.cloud.event.EventTypes) CollectionUtils(com.amazonaws.util.CollectionUtils) Inject(javax.inject.Inject) ConfigKey(org.apache.cloudstack.framework.config.ConfigKey) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Listener(com.cloud.agent.Listener) ListVmwareDcsCmd(org.apache.cloudstack.api.command.admin.zone.ListVmwareDcsCmd) SshHelper(com.cloud.utils.ssh.SshHelper) PbmProfile(com.vmware.pbm.PbmProfile) CiscoNexusVSMDeviceDao(com.cloud.network.dao.CiscoNexusVSMDeviceDao) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) Configurable(org.apache.cloudstack.framework.config.Configurable) ListVsphereStoragePolicyCompatiblePoolsCmd(org.apache.cloudstack.api.command.admin.zone.ListVsphereStoragePolicyCompatiblePoolsCmd) ManagementServerNode(org.apache.cloudstack.utils.identity.ManagementServerNode) NamedThreadFactory(com.cloud.utils.concurrency.NamedThreadFactory) HostVO(com.cloud.host.HostVO) VsphereStoragePolicyDao(com.cloud.dc.dao.VsphereStoragePolicyDao) MapUtils(org.apache.commons.collections.MapUtils) VmwareDatacenterZoneMapDao(com.cloud.hypervisor.vmware.dao.VmwareDatacenterZoneMapDao) CustomFieldConstants(com.cloud.hypervisor.vmware.mo.CustomFieldConstants) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) Hypervisor(com.cloud.hypervisor.Hypervisor) LegacyZoneVO(com.cloud.hypervisor.vmware.LegacyZoneVO) FileInputStream(java.io.FileInputStream) PbmProfileManagerMO(com.cloud.hypervisor.vmware.mo.PbmProfileManagerMO) ImportVsphereStoragePoliciesCmd(org.apache.cloudstack.api.command.admin.zone.ImportVsphereStoragePoliciesCmd) HostDetailsDao(com.cloud.host.dao.HostDetailsDao) TimeUnit(java.util.concurrent.TimeUnit) TemplateJoinDao(com.cloud.api.query.dao.TemplateJoinDao) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VmwareTrafficLabel(com.cloud.network.VmwareTrafficLabel) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) VmwareDatacenterService(com.cloud.hypervisor.vmware.VmwareDatacenterService) DigestUtils(org.apache.commons.codec.digest.DigestUtils) StorageLayer(com.cloud.storage.StorageLayer) Collections(java.util.Collections) ManagementServerHost(org.apache.cloudstack.management.ManagementServerHost) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) PbmProfile(com.vmware.pbm.PbmProfile) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PbmProfileManagerMO(com.cloud.hypervisor.vmware.mo.PbmProfileManagerMO) DiscoveredWithErrorException(com.cloud.exception.DiscoveredWithErrorException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) RemoteException(java.rmi.RemoteException) ConfigurationException(javax.naming.ConfigurationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) VsphereStoragePolicyVO(com.cloud.dc.VsphereStoragePolicyVO)

Example 92 with VmwareContext

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

the class VmwareResource method execute.

protected Answer execute(CheckVirtualMachineCommand cmd) {
    final String vmName = cmd.getVmName();
    PowerState powerState = PowerState.PowerUnknown;
    Integer vncPort = null;
    VmwareContext context = getServiceContext();
    VmwareHypervisorHost hyperHost = getHyperHost(context);
    try {
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo != null) {
            powerState = getVmPowerState(vmMo);
            return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
        } else {
            s_logger.warn("Can not find vm " + vmName + " to execute CheckVirtualMachineCommand");
            return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
        }
    } catch (Throwable e) {
        createLogMessageException(e, cmd);
        return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) PowerState(com.cloud.vm.VirtualMachine.PowerState) VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState)

Example 93 with VmwareContext

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

the class VmwareResource method execute.

protected Answer execute(PingTestCommand cmd) {
    String controlIp = cmd.getRouterIp();
    if (controlIp != null) {
        String args = " -c 1 -n -q " + cmd.getPrivateIp();
        try {
            Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", getSystemVmKeyFile(), null, "/bin/ping" + args);
            if (result.first())
                return new Answer(cmd);
        } catch (Exception e) {
            s_logger.error("Unable to execute ping command on DomR (" + controlIp + "), domR may not be ready yet. failure due to " + VmwareHelper.getExceptionMessage(e), e);
        }
        return new Answer(cmd, false, "PingTestCommand failed");
    } else {
        VmwareContext context = getServiceContext();
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        try {
            HostMO hostMo = (HostMO) hyperHost;
            ClusterMO clusterMo = new ClusterMO(context, hostMo.getHyperHostCluster());
            VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
            List<Pair<ManagedObjectReference, String>> hosts = clusterMo.getClusterHosts();
            for (Pair<ManagedObjectReference, String> entry : hosts) {
                HostMO hostInCluster = new HostMO(context, entry.first());
                String hostIp = hostInCluster.getHostManagementIp(mgr.getManagementPortGroupName());
                if (hostIp != null && hostIp.equals(cmd.getComputingHostIp())) {
                    if (hostInCluster.isHyperHostConnected())
                        return new Answer(cmd);
                    else
                        return new Answer(cmd, false, "PingTestCommand failed");
                }
            }
        } catch (Exception e) {
            s_logger.error("Unable to execute ping command on host (" + cmd.getComputingHostIp() + "). failure due to " + VmwareHelper.getExceptionMessage(e), e);
        }
        return new Answer(cmd, false, "PingTestCommand failed");
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) 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) SetupPersistentNetworkAnswer(com.cloud.agent.api.SetupPersistentNetworkAnswer) ModifyTargetsAnswer(com.cloud.agent.api.ModifyTargetsAnswer) MigrateVmToPoolAnswer(com.cloud.agent.api.MigrateVmToPoolAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) GetVmVncTicketAnswer(com.cloud.agent.api.GetVmVncTicketAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) GetVolumeStatsAnswer(com.cloud.agent.api.GetVolumeStatsAnswer) SetupAnswer(com.cloud.agent.api.SetupAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) GetVmNetworkStatsAnswer(com.cloud.agent.api.GetVmNetworkStatsAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) CheckOnHostAnswer(com.cloud.agent.api.CheckOnHostAnswer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) AttachIsoAnswer(com.cloud.agent.api.AttachIsoAnswer) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) GetStoragePoolCapabilitiesAnswer(com.cloud.agent.api.GetStoragePoolCapabilitiesAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) CheckNetworkAnswer(com.cloud.agent.api.CheckNetworkAnswer) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) MigrateWithStorageAnswer(com.cloud.agent.api.MigrateWithStorageAnswer) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) ValidateSnapshotAnswer(com.cloud.agent.api.ValidateSnapshotAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) PrepareUnmanageVMInstanceAnswer(com.cloud.agent.api.PrepareUnmanageVMInstanceAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) Pair(com.cloud.utils.Pair) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 94 with VmwareContext

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

the class VmwareResource method execute.

private Answer execute(SetupPersistentNetworkCommand cmd) {
    VmwareHypervisorHost host = getHyperHost(getServiceContext());
    String hostname = null;
    VmwareContext context = getServiceContext();
    HostMO hostMO = new HostMO(context, host.getMor());
    try {
        prepareNetworkFromNicInfo(hostMO, cmd.getNic(), false, null);
        hostname = host.getHyperHostName();
    } catch (Exception e) {
        return new SetupPersistentNetworkAnswer(cmd, false, "failed to setup port-group due to: " + e.getLocalizedMessage());
    }
    return new SetupPersistentNetworkAnswer(cmd, true, hostname);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) SetupPersistentNetworkAnswer(com.cloud.agent.api.SetupPersistentNetworkAnswer) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) 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 95 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 = hotaddIncrementSizeInMb > 0 ? requestedMaxMemoryInMb % hotaddIncrementSizeInMb : 0;
        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(), getReservedCpuMHZ(vmSpec), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Unable to execute ScaleVmCommand");
        }
    } catch (Exception e) {
        s_logger.error(String.format("ScaleVmCommand failed due to: [%s].", VmwareHelper.getExceptionMessage(e)), e);
        return new ScaleVmAnswer(cmd, false, String.format("Unable to execute ScaleVmCommand due to: [%s].", 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)

Aggregations

VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)106 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)66 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)65 RemoteException (java.rmi.RemoteException)53 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)49 UnsupportedEncodingException (java.io.UnsupportedEncodingException)48 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)39 IOException (java.io.IOException)38 CloudException (com.cloud.exception.CloudException)36 ConfigurationException (javax.naming.ConfigurationException)34 InternalErrorException (com.cloud.exception.InternalErrorException)31 ConnectException (java.net.ConnectException)31 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)26 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)23 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)21 Pair (com.cloud.utils.Pair)20 ClusterMO (com.cloud.hypervisor.vmware.mo.ClusterMO)15 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)14 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)13 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)13