forked from schadr/photon-controller
/
hypervisor.py
164 lines (131 loc) · 5.76 KB
/
hypervisor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2015 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, without
# warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED. See the
# License for then specific language governing permissions and limitations
# under the License.
"""Provides a wrapper around hypervisor specific modules."""
import abc
import logging
from host.hypervisor.image_monitor import ImageMonitor
from host.hypervisor.placement_manager import PlacementManager
from host.hypervisor.placement_manager import PlacementOption
from host.hypervisor.resources import Resource
class UpdateListener(object):
"""
Abstract base class for host update listener.
IMPORTANT: The underlying hypervisor holds a lock while notifying
listeners, so these callbacks should be reasonably light-weight.
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def networks_updated(self):
"""Gets called when there is a change in the list of networks."""
pass
@abc.abstractmethod
def virtual_machines_updated(self):
"""Gets called when there is a change in the list of VMs."""
pass
@abc.abstractmethod
def datastores_updated(self):
"""Gets called when tehre is a change in the list of datastores."""
pass
class Hypervisor(object):
"""A class that wraps hypervisor functionality.
Based on which hypervisor the agent was configured to use, this will setup
the proper modules.
"""
def __init__(self, agent_config):
self._logger = logging.getLogger(__name__)
self._config = agent_config
if self._config.hypervisor == "esx":
from esx.hypervisor import EsxHypervisor
# This will throw an error if it can't connect to the local vim.
self.hypervisor = EsxHypervisor(agent_config)
elif self._config.hypervisor == "fake":
from fake.hypervisor import FakeHypervisor
self.hypervisor = FakeHypervisor(agent_config)
else:
raise ValueError("Invalid hypervisor")
"""
The creation of the Hypervisors above translates datastore names
into datastore ids. Methods that access datastores through this
class should use datastore ids.
"""
self.datastore_manager = self.hypervisor.datastore_manager
self.disk_manager = self.hypervisor.disk_manager
self.image_manager = self.hypervisor.image_manager
self.vm_manager = self.hypervisor.vm_manager
self.network_manager = self.hypervisor.network_manager
self.system = self.hypervisor.system
options = PlacementOption(agent_config.memory_overcommit,
agent_config.cpu_overcommit,
agent_config.image_datastores)
self.placement_manager = PlacementManager(self, options)
self.image_monitor = ImageMonitor(self.datastore_manager,
self.image_manager,
self.vm_manager)
def add_update_listener(self, listener):
"""
Adds an update listener.
"""
if not issubclass(listener.__class__, UpdateListener):
raise TypeError("Not a subclass of UpdateListener")
self.hypervisor.add_update_listener(listener)
def remove_update_listener(self, listener):
"""
Removes an update listener.
"""
if not issubclass(listener.__class__, UpdateListener):
raise TypeError("Not a subclass of UpdateListener")
self.hypervisor.remove_update_listener(listener)
@property
def uuid(self):
return self.hypervisor.uuid
@property
def config(self):
return self.hypervisor.config
def check_image(self, image_id, datastore_id):
return self.hypervisor.check_image(image_id, datastore_id)
def normalized_load(self):
return self.hypervisor.normalized_load()
def get_resources(self):
result = []
if hasattr(self.vm_manager, "get_resources"):
return self.vm_manager.get_resources()
else:
for vm_id in self.vm_manager.get_resource_ids():
result.append(self.get_vm_resource(vm_id))
return result
def get_vm_resource(self, vm_id):
vm = self.vm_manager.get_resource(vm_id)
resource = Resource(vm=vm)
return resource
def acquire_vim_ticket(self):
return self.hypervisor.acquire_vim_ticket()
@property
def memory_overcommit(self):
return self.placement_manager.memory_overcommit
def set_memory_overcommit(self, value):
self.placement_manager.memory_overcommit = value
@property
def cpu_overcommit(self):
return self.placement_manager.cpu_overcommit
def set_cpu_overcommit(self, value):
self.placement_manager.cpu_overcommit = value
def acquire_cgi_ticket(self, url, op):
return self.hypervisor.acquire_cgi_ticket(url, op)
def transfer_image(self, source_image_id, source_datastore,
destination_image_id, destination_datastore,
host, port):
return self.hypervisor.transfer_image(
source_image_id, source_datastore, destination_image_id,
destination_datastore, host, port)
def receive_image(self, image_id, datastore, imported_vm_name):
return self.hypervisor.receive_image(image_id, datastore,
imported_vm_name)