root / lib / hypervisor / hv_fake.py @ bbcf7ad0
History | View | Annotate | Download (7.8 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Fake hypervisor
|
23 |
|
24 |
"""
|
25 |
|
26 |
import os |
27 |
import os.path |
28 |
import logging |
29 |
|
30 |
from ganeti import utils |
31 |
from ganeti import constants |
32 |
from ganeti import errors |
33 |
from ganeti.hypervisor import hv_base |
34 |
|
35 |
|
36 |
class FakeHypervisor(hv_base.BaseHypervisor): |
37 |
"""Fake hypervisor interface.
|
38 |
|
39 |
This can be used for testing the ganeti code without having to have
|
40 |
a real virtualisation software installed.
|
41 |
|
42 |
"""
|
43 |
_ROOT_DIR = constants.RUN_DIR + "/ganeti-fake-hypervisor"
|
44 |
|
45 |
def __init__(self): |
46 |
hv_base.BaseHypervisor.__init__(self)
|
47 |
if not os.path.exists(self._ROOT_DIR): |
48 |
os.mkdir(self._ROOT_DIR)
|
49 |
|
50 |
def ListInstances(self): |
51 |
"""Get the list of running instances.
|
52 |
|
53 |
"""
|
54 |
return os.listdir(self._ROOT_DIR) |
55 |
|
56 |
def GetInstanceInfo(self, instance_name): |
57 |
"""Get instance properties.
|
58 |
|
59 |
@param instance_name: the instance name
|
60 |
|
61 |
@return: tuple of (name, id, memory, vcpus, stat, times)
|
62 |
|
63 |
"""
|
64 |
file_name = self._InstanceFile(instance_name)
|
65 |
if not os.path.exists(file_name): |
66 |
return None |
67 |
try:
|
68 |
fh = open(file_name, "r") |
69 |
try:
|
70 |
inst_id = fh.readline().strip() |
71 |
memory = utils.TryConvert(int, fh.readline().strip())
|
72 |
vcpus = utils.TryConvert(int, fh.readline().strip())
|
73 |
stat = "---b-"
|
74 |
times = "0"
|
75 |
return (instance_name, inst_id, memory, vcpus, stat, times)
|
76 |
finally:
|
77 |
fh.close() |
78 |
except IOError, err: |
79 |
raise errors.HypervisorError("Failed to list instance %s: %s" % |
80 |
(instance_name, err)) |
81 |
|
82 |
def GetAllInstancesInfo(self): |
83 |
"""Get properties of all instances.
|
84 |
|
85 |
@return: list of tuples (name, id, memory, vcpus, stat, times)
|
86 |
|
87 |
"""
|
88 |
data = [] |
89 |
for file_name in os.listdir(self._ROOT_DIR): |
90 |
try:
|
91 |
fh = open(utils.PathJoin(self._ROOT_DIR, file_name), "r") |
92 |
inst_id = "-1"
|
93 |
memory = 0
|
94 |
vcpus = 1
|
95 |
stat = "-----"
|
96 |
times = "-1"
|
97 |
try:
|
98 |
inst_id = fh.readline().strip() |
99 |
memory = utils.TryConvert(int, fh.readline().strip())
|
100 |
vcpus = utils.TryConvert(int, fh.readline().strip())
|
101 |
stat = "---b-"
|
102 |
times = "0"
|
103 |
finally:
|
104 |
fh.close() |
105 |
data.append((file_name, inst_id, memory, vcpus, stat, times)) |
106 |
except IOError, err: |
107 |
raise errors.HypervisorError("Failed to list instances: %s" % err) |
108 |
return data
|
109 |
|
110 |
@classmethod
|
111 |
def _InstanceFile(cls, instance_name): |
112 |
"""Compute the instance file for an instance name.
|
113 |
|
114 |
"""
|
115 |
return utils.PathJoin(cls._ROOT_DIR, instance_name)
|
116 |
|
117 |
def _IsAlive(self, instance_name): |
118 |
"""Checks if an instance is alive.
|
119 |
|
120 |
"""
|
121 |
file_name = self._InstanceFile(instance_name)
|
122 |
return os.path.exists(file_name)
|
123 |
|
124 |
def _MarkUp(self, instance): |
125 |
"""Mark the instance as running.
|
126 |
|
127 |
This does no checks, which should be done by its callers.
|
128 |
|
129 |
"""
|
130 |
file_name = self._InstanceFile(instance.name)
|
131 |
fh = file(file_name, "w") |
132 |
try:
|
133 |
fh.write("0\n%d\n%d\n" %
|
134 |
(instance.beparams[constants.BE_MEMORY], |
135 |
instance.beparams[constants.BE_VCPUS])) |
136 |
finally:
|
137 |
fh.close() |
138 |
|
139 |
def _MarkDown(self, instance_name): |
140 |
"""Mark the instance as running.
|
141 |
|
142 |
This does no checks, which should be done by its callers.
|
143 |
|
144 |
"""
|
145 |
file_name = self._InstanceFile(instance_name)
|
146 |
utils.RemoveFile(file_name) |
147 |
|
148 |
def StartInstance(self, instance, block_devices): |
149 |
"""Start an instance.
|
150 |
|
151 |
For the fake hypervisor, it just creates a file in the base dir,
|
152 |
creating an exception if it already exists. We don't actually
|
153 |
handle race conditions properly, since these are *FAKE* instances.
|
154 |
|
155 |
"""
|
156 |
if self._IsAlive(instance.name): |
157 |
raise errors.HypervisorError("Failed to start instance %s: %s" % |
158 |
(instance.name, "already running"))
|
159 |
try:
|
160 |
self._MarkUp(instance)
|
161 |
except IOError, err: |
162 |
raise errors.HypervisorError("Failed to start instance %s: %s" % |
163 |
(instance.name, err)) |
164 |
|
165 |
def StopInstance(self, instance, force=False, retry=False, name=None): |
166 |
"""Stop an instance.
|
167 |
|
168 |
For the fake hypervisor, this just removes the file in the base
|
169 |
dir, if it exist, otherwise we raise an exception.
|
170 |
|
171 |
"""
|
172 |
if name is None: |
173 |
name = instance.name |
174 |
if not self._IsAlive(name): |
175 |
raise errors.HypervisorError("Failed to stop instance %s: %s" % |
176 |
(name, "not running"))
|
177 |
self._MarkDown(name)
|
178 |
|
179 |
def RebootInstance(self, instance): |
180 |
"""Reboot an instance.
|
181 |
|
182 |
For the fake hypervisor, this does nothing.
|
183 |
|
184 |
"""
|
185 |
return
|
186 |
|
187 |
def GetNodeInfo(self): |
188 |
"""Return information about the node.
|
189 |
|
190 |
This is just a wrapper over the base GetLinuxNodeInfo method.
|
191 |
|
192 |
@return: a dict with the following keys (values in MiB):
|
193 |
- memory_total: the total memory size on the node
|
194 |
- memory_free: the available memory on the node for instances
|
195 |
- memory_dom0: the memory used by the node itself, if available
|
196 |
|
197 |
"""
|
198 |
result = self.GetLinuxNodeInfo()
|
199 |
# substract running instances
|
200 |
all_instances = self.GetAllInstancesInfo()
|
201 |
result['memory_free'] -= min(result['memory_free'], |
202 |
sum([row[2] for row in all_instances])) |
203 |
return result
|
204 |
|
205 |
@classmethod
|
206 |
def GetShellCommandForConsole(cls, instance, hvparams, beparams): |
207 |
"""Return a command for connecting to the console of an instance.
|
208 |
|
209 |
"""
|
210 |
return "echo Console not available for fake hypervisor" |
211 |
|
212 |
def Verify(self): |
213 |
"""Verify the hypervisor.
|
214 |
|
215 |
For the fake hypervisor, it just checks the existence of the base
|
216 |
dir.
|
217 |
|
218 |
"""
|
219 |
if not os.path.exists(self._ROOT_DIR): |
220 |
return "The required directory '%s' does not exist." % self._ROOT_DIR |
221 |
|
222 |
@classmethod
|
223 |
def PowercycleNode(cls): |
224 |
"""Fake hypervisor powercycle, just a wrapper over Linux powercycle.
|
225 |
|
226 |
"""
|
227 |
cls.LinuxPowercycle() |
228 |
|
229 |
def AcceptInstance(self, instance, info, target): |
230 |
"""Prepare to accept an instance.
|
231 |
|
232 |
@type instance: L{objects.Instance}
|
233 |
@param instance: instance to be accepted
|
234 |
@type info: string
|
235 |
@param info: instance info, not used
|
236 |
@type target: string
|
237 |
@param target: target host (usually ip), on this node
|
238 |
|
239 |
"""
|
240 |
if self._IsAlive(instance.name): |
241 |
raise errors.HypervisorError("Can't accept instance, already running") |
242 |
|
243 |
def MigrateInstance(self, instance, target, live): |
244 |
"""Migrate an instance.
|
245 |
|
246 |
@type instance: L{objects.Instance}
|
247 |
@param instance: the instance to be migrated
|
248 |
@type target: string
|
249 |
@param target: hostname (usually ip) of the target node
|
250 |
@type live: boolean
|
251 |
@param live: whether to do a live or non-live migration
|
252 |
|
253 |
"""
|
254 |
logging.debug("Fake hypervisor migrating %s to %s (live=%s)",
|
255 |
instance, target, live) |
256 |
|
257 |
self._MarkDown(instance.name)
|
258 |
|
259 |
def FinalizeMigration(self, instance, info, success): |
260 |
"""Finalize an instance migration.
|
261 |
|
262 |
For the fake hv, this just marks the instance up.
|
263 |
|
264 |
@type instance: L{objects.Instance}
|
265 |
@param instance: instance whose migration is being finalized
|
266 |
|
267 |
"""
|
268 |
if success:
|
269 |
self._MarkUp(instance)
|
270 |
else:
|
271 |
# ensure it's down
|
272 |
self._MarkDown(instance.name)
|