1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2012 VMware, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
18
Management class for host-related functions (start, reboot, etc).
21
from nova import exception
22
from nova.openstack.common import log as logging
23
from nova.virt.vmwareapi import vim_util
24
from nova.virt.vmwareapi import vm_util
26
LOG = logging.getLogger(__name__)
31
Implements host related operations.
33
def __init__(self, session):
34
self._session = session
36
def host_power_action(self, host, action):
37
"""Reboots or shuts down the host."""
38
host_mor = self._session._call_method(vim_util, "get_objects",
40
LOG.debug(_("%(action)s %(host)s") % locals())
41
if action == "reboot":
42
host_task = self._session._call_method(
43
self._session._get_vim(),
44
"RebootHost_Task", host_mor,
46
elif action == "shutdown":
47
host_task = self._session._call_method(
48
self._session._get_vim(),
49
"ShutdownHost_Task", host_mor,
51
elif action == "startup":
52
host_task = self._session._call_method(
53
self._session._get_vim(),
54
"PowerUpHostFromStandBy_Task", host_mor,
56
self._session._wait_for_task(host, host_task)
58
def host_maintenance_mode(self, host, mode):
59
"""Start/Stop host maintenance window. On start, it triggers
60
guest VMs evacuation."""
61
host_mor = self._session._call_method(vim_util, "get_objects",
63
LOG.debug(_("Set maintenance mod on %(host)s to %(mode)s") % locals())
65
host_task = self._session._call_method(
66
self._session._get_vim(),
67
"EnterMaintenanceMode_Task",
69
evacuatePoweredOffVms=True)
71
host_task = self._session._call_method(
72
self._session._get_vim(),
73
"ExitMaintenanceMode_Task",
75
self._session._wait_for_task(host, host_task)
77
def set_host_enabled(self, _host, enabled):
78
"""Sets the specified host's ability to accept new instances."""
82
class HostState(object):
83
"""Manages information about the ESX host this compute
86
def __init__(self, session, host_name):
87
super(HostState, self).__init__()
88
self._session = session
89
self._host_name = host_name
93
def get_host_stats(self, refresh=False):
94
"""Return the current state of the host. If 'refresh' is
95
True, run the update first.
101
def update_status(self):
102
"""Update the current state of the host.
104
host_mor = self._session._call_method(vim_util, "get_objects",
106
summary = self._session._call_method(vim_util,
107
"get_dynamic_property",
116
ds = vm_util.get_datastore_ref_and_name(self._session)
117
except exception.DatastoreNotFound:
118
ds = (None, None, 0, 0)
121
data["vcpus"] = summary.hardware.numCpuThreads
123
{"vendor": summary.hardware.vendor,
124
"model": summary.hardware.cpuModel,
125
"topology": {"cores": summary.hardware.numCpuCores,
126
"sockets": summary.hardware.numCpuPkgs,
127
"threads": summary.hardware.numCpuThreads}
129
data["disk_total"] = ds[2] / (1024 * 1024)
130
data["disk_available"] = ds[3] / (1024 * 1024)
131
data["disk_used"] = data["disk_total"] - data["disk_available"]
132
data["host_memory_total"] = summary.hardware.memorySize / (1024 * 1024)
133
data["host_memory_free"] = data["host_memory_total"] - \
134
summary.quickStats.overallMemoryUsage
135
data["hypervisor_type"] = summary.config.product.name
136
data["hypervisor_version"] = summary.config.product.version
137
data["hypervisor_hostname"] = self._host_name
143
class VCState(object):
144
"""Manages information about the VC host this compute
147
def __init__(self, session, host_name, cluster):
148
super(VCState, self).__init__()
149
self._session = session
150
self._host_name = host_name
151
self._cluster = cluster
155
def get_host_stats(self, refresh=False):
156
"""Return the current state of the host. If 'refresh' is
157
True, run the update first.
163
def update_status(self):
164
"""Update the current state of the host.
166
host_mor = vm_util.get_host_ref(self._session, self._cluster)
170
summary = self._session._call_method(vim_util,
171
"get_dynamic_property",
180
ds = vm_util.get_datastore_ref_and_name(self._session,
182
except exception.DatastoreNotFound:
183
ds = (None, None, 0, 0)
186
data["vcpus"] = summary.hardware.numCpuThreads
188
{"vendor": summary.hardware.vendor,
189
"model": summary.hardware.cpuModel,
190
"topology": {"cores": summary.hardware.numCpuCores,
191
"sockets": summary.hardware.numCpuPkgs,
192
"threads": summary.hardware.numCpuThreads}
194
data["disk_total"] = ds[2] / (1024 * 1024)
195
data["disk_available"] = ds[3] / (1024 * 1024)
196
data["disk_used"] = data["disk_total"] - data["disk_available"]
197
data["host_memory_total"] = summary.hardware.memorySize / (1024 * 1024)
198
data["host_memory_free"] = data["host_memory_total"] -\
199
summary.quickStats.overallMemoryUsage
200
data["hypervisor_type"] = summary.config.product.name
201
data["hypervisor_version"] = summary.config.product.version
202
data["hypervisor_hostname"] = self._host_name