Search in sources :

Example 36 with VmwareHypervisorHost

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

the class VmwareResource method prepareNetworkElementCommand.

private ExecutionResult prepareNetworkElementCommand(IpAssocCommand cmd) {
    VmwareContext context = getServiceContext();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        IpAddressTO[] ips = cmd.getIpAddresses();
        String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
        String controlIp = VmwareResource.getRouterSshControlIp(cmd);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
        // the check and will try to find it within cluster
        if (vmMo == null) {
            if (hyperHost instanceof HostMO) {
                ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
                vmMo = clusterMo.findVmOnHyperHost(routerName);
            }
        }
        if (vmMo == null) {
            String msg = "Router " + routerName + " no longer exists to execute IPAssoc command";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        for (IpAddressTO ip : ips) {
            /**
             * TODO support other networks
             */
            URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
            if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
                throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri());
            }
            String vlanId = BroadcastDomainType.getValue(broadcastUri);
            String publicNetworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
            Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNetworkName);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Find public NIC index, public network name: " + publicNetworkName + ", index: " + publicNicInfo.first());
            }
            boolean addVif = false;
            if (ip.isAdd() && publicNicInfo.first() == -1) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Plug new NIC to associate" + controlIp + " to " + ip.getPublicIp());
                }
                addVif = true;
            }
            if (addVif) {
                NicTO nicTO = ip.getNicTO();
                VirtualEthernetCardType nicDeviceType = null;
                if (ip.getDetails() != null) {
                    nicDeviceType = VirtualEthernetCardType.valueOf(ip.getDetails().get("nicAdapter"));
                }
                plugNicCommandInternal(routerName, nicDeviceType, nicTO, VirtualMachine.Type.DomainRouter);
                publicNicInfo = vmMo.getNicDeviceIndex(publicNetworkName);
                if (publicNicInfo.first() >= 0) {
                    networkUsage(controlIp, "addVif", "eth" + publicNicInfo.first());
                }
            }
            if (publicNicInfo.first() < 0) {
                String msg = "Failed to find DomR VIF to associate/disassociate IP with.";
                s_logger.error(msg);
                throw new InternalErrorException(msg);
            }
            ip.setNicDevId(publicNicInfo.first());
            ip.setNewNic(addVif);
        }
    } catch (Throwable e) {
        s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
        return new ExecutionResult(false, e.toString());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) ExecutionResult(com.cloud.utils.ExecutionResult) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) InternalErrorException(com.cloud.exception.InternalErrorException) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) URI(java.net.URI) 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) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) NicTO(com.cloud.agent.api.to.NicTO)

Example 37 with VmwareHypervisorHost

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

the class VmwareResource method prepareSecondaryDatastoreOnHost.

public synchronized ManagedObjectReference prepareSecondaryDatastoreOnHost(String storeUrl) throws Exception {
    String storeName = getSecondaryDatastoreUUID(storeUrl);
    URI uri = new URI(storeUrl);
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    ManagedObjectReference morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), storeName.replace("-", ""), false);
    if (morDatastore == null)
        throw new Exception("Unable to mount secondary storage on host. storeUrl: " + storeUrl);
    return morDatastore;
}
Also used : VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) URI(java.net.URI) 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) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 38 with VmwareHypervisorHost

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

the class VmwareResource method execute.

protected Answer execute(GetStorageStatsCommand cmd) {
    try {
        VmwareContext context = getServiceContext();
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getStorageId());
        if (morDs != null) {
            long capacity = 0;
            long free = 0;
            if (cmd.getPooltype() == StoragePoolType.DatastoreCluster) {
                StoragepodMO datastoreClusterMo = new StoragepodMO(getServiceContext(), morDs);
                StoragePodSummary summary = datastoreClusterMo.getDatastoreClusterSummary();
                capacity = summary.getCapacity();
                free = summary.getFreeSpace();
            } else {
                DatastoreMO datastoreMo = new DatastoreMO(context, morDs);
                DatastoreSummary summary = datastoreMo.getDatastoreSummary();
                capacity = summary.getCapacity();
                free = summary.getFreeSpace();
            }
            long used = capacity - free;
            s_logger.debug(String.format("Datastore summary info: [storageId: %s, ], localPath: %s, poolType: %s, capacity: %s, free: %s, used: %s].", cmd.getStorageId(), cmd.getLocalPath(), cmd.getPooltype(), toHumanReadableSize(capacity), toHumanReadableSize(free), toHumanReadableSize(used)));
            if (capacity <= 0) {
                s_logger.warn("Something is wrong with vSphere NFS datastore, rebooting ESX(ESXi) host should help");
            }
            return new GetStorageStatsAnswer(cmd, capacity, used);
        } else {
            String msg = String.format("Could not find datastore for GetStorageStatsCommand: [storageId: %s, localPath: %s, poolType: %s].", cmd.getStorageId(), cmd.getLocalPath(), cmd.getPooltype());
            s_logger.error(msg);
            return new GetStorageStatsAnswer(cmd, msg);
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        String msg = String.format("Unable to execute GetStorageStatsCommand(storageId : [%s], localPath: [%s], poolType: [%s]) due to [%s]", cmd.getStorageId(), cmd.getLocalPath(), cmd.getPooltype(), VmwareHelper.getExceptionMessage(e));
        s_logger.error(msg, e);
        return new GetStorageStatsAnswer(cmd, msg);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) DatastoreSummary(com.vmware.vim25.DatastoreSummary) StoragepodMO(com.cloud.hypervisor.vmware.mo.StoragepodMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) StoragePodSummary(com.vmware.vim25.StoragePodSummary) RemoteException(java.rmi.RemoteException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 39 with VmwareHypervisorHost

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

the class VmwareResource method getHostVmStateReport.

private HashMap<String, HostVmStateReportEntry> getHostVmStateReport() 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, HostVmStateReportEntry> newStates = new HashMap<String, HostVmStateReportEntry>();
    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, new HostVmStateReportEntry(convertPowerState(powerState), hyperHost.getHyperHostName()));
                }
            }
        }
    }
    return newStates;
}
Also used : DynamicProperty(com.vmware.vim25.DynamicProperty) HostVmStateReportEntry(com.cloud.agent.api.HostVmStateReportEntry) 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)

Example 40 with VmwareHypervisorHost

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

the class VmwareResource method execute.

protected Answer execute(ModifyTargetsCommand cmd) {
    VmwareContext context = getServiceContext(cmd);
    VmwareHypervisorHost hyperHost = getHyperHost(context);
    List<HostMO> hostMOs = new ArrayList<>();
    if (cmd.getApplyToAllHostsInCluster()) {
        try {
            ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
            ClusterMO clusterMO = new ClusterMO(context, morCluster);
            List<Pair<ManagedObjectReference, String>> hosts = clusterMO.getClusterHosts();
            for (Pair<ManagedObjectReference, String> host : hosts) {
                HostMO hostMO = new HostMO(context, host.first());
                hostMOs.add(hostMO);
            }
        } catch (Exception ex) {
            s_logger.error(ex.getMessage(), ex);
            throw new CloudRuntimeException(ex.getMessage(), ex);
        }
    } else {
        hostMOs.add((HostMO) hyperHost);
    }
    handleTargets(cmd.getAdd(), cmd.getTargetTypeToRemove(), cmd.isRemoveAsync(), cmd.getTargets(), hostMOs);
    return new ModifyTargetsAnswer();
}
Also used : ModifyTargetsAnswer(com.cloud.agent.api.ModifyTargetsAnswer) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) ArrayList(java.util.ArrayList) 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) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Aggregations

VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)85 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)65 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)65 RemoteException (java.rmi.RemoteException)59 UnsupportedEncodingException (java.io.UnsupportedEncodingException)57 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)48 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)46 CloudException (com.cloud.exception.CloudException)40 InternalErrorException (com.cloud.exception.InternalErrorException)40 IOException (java.io.IOException)40 ConnectException (java.net.ConnectException)40 ConfigurationException (javax.naming.ConfigurationException)40 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)31 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)27 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)23 Pair (com.cloud.utils.Pair)15 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 Answer (com.cloud.agent.api.Answer)11 AttachIsoAnswer (com.cloud.agent.api.AttachIsoAnswer)10 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)10