Coverage for nova/virt/vmwareapi/host.py: 0%
67 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
1# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
2# Copyright (c) 2012 VMware, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16"""
17Management class for host-related functions (start, reboot, etc).
18"""
20from oslo_log import log as logging
21from oslo_utils import units
22from oslo_utils import versionutils
23from oslo_vmware import exceptions as vexc
25import nova.conf
26from nova import context
27from nova import exception
28from nova import objects
29from nova.objects import fields as obj_fields
30from nova.virt.vmwareapi import ds_util
31from nova.virt.vmwareapi import vim_util
32from nova.virt.vmwareapi import vm_util
34CONF = nova.conf.CONF
35LOG = logging.getLogger(__name__)
38def _get_ds_capacity_and_freespace(session, cluster=None,
39 datastore_regex=None):
40 try:
41 ds = ds_util.get_datastore(session, cluster,
42 datastore_regex)
43 return ds.capacity, ds.freespace
44 except exception.DatastoreNotFound:
45 return 0, 0
48class VCState(object):
49 """Manages information about the vCenter cluster"""
51 def __init__(self, session, host_name, cluster, datastore_regex):
52 super(VCState, self).__init__()
53 self._session = session
54 self._host_name = host_name
55 self._cluster = cluster
56 self._datastore_regex = datastore_regex
57 self._stats = {}
58 self._auto_service_disabled = False
59 about_info = self._session._call_method(vim_util, "get_about_info")
60 self._hypervisor_type = about_info.name
61 self._hypervisor_version = versionutils.convert_version_to_int(
62 str(about_info.version))
63 self.update_status()
65 def get_host_stats(self, refresh=False):
66 """Return the current state of the cluster. If 'refresh' is
67 True, run the update first.
68 """
69 if refresh or not self._stats:
70 self.update_status()
71 return self._stats
73 def update_status(self):
74 """Update the current state of the cluster."""
75 data = {}
76 try:
77 capacity, freespace = _get_ds_capacity_and_freespace(self._session,
78 self._cluster, self._datastore_regex)
80 # Get cpu, memory stats from the cluster
81 stats = vm_util.get_stats_from_cluster(self._session,
82 self._cluster)
83 except (vexc.VimConnectionException, vexc.VimAttributeException) as ex:
84 # VimAttributeException is thrown when vpxd service is down
85 LOG.warning("Failed to connect with %(node)s. "
86 "Error: %(error)s",
87 {'node': self._host_name, 'error': ex})
88 self._set_host_enabled(False)
89 return data
91 data["vcpus"] = stats['cpu']['vcpus']
92 data["disk_total"] = capacity // units.Gi
93 data["disk_available"] = freespace // units.Gi
94 data["disk_used"] = data["disk_total"] - data["disk_available"]
95 data["host_memory_total"] = stats['mem']['total']
96 data["host_memory_free"] = stats['mem']['free']
97 data["hypervisor_type"] = self._hypervisor_type
98 data["hypervisor_version"] = self._hypervisor_version
99 data["hypervisor_hostname"] = self._host_name
100 data["supported_instances"] = [
101 (obj_fields.Architecture.I686,
102 obj_fields.HVType.VMWARE,
103 obj_fields.VMMode.HVM),
104 (obj_fields.Architecture.X86_64,
105 obj_fields.HVType.VMWARE,
106 obj_fields.VMMode.HVM)]
108 self._stats = data
109 if self._auto_service_disabled:
110 self._set_host_enabled(True)
111 return data
113 def _set_host_enabled(self, enabled):
114 """Sets the compute host's ability to accept new instances."""
115 ctx = context.get_admin_context()
116 service = objects.Service.get_by_compute_host(ctx, CONF.host)
117 service.disabled = not enabled
118 service.disabled_reason = 'set by vmwareapi host_state'
119 service.save()
120 self._auto_service_disabled = service.disabled