4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
30 from ganeti import utils
31 from ganeti import constants
32 from ganeti import errors
33 from ganeti.hypervisor import hv_base
36 class FakeHypervisor(hv_base.BaseHypervisor):
37 """Fake hypervisor interface.
39 This can be used for testing the ganeti code without having to have
40 a real virtualisation software installed.
45 _ROOT_DIR = constants.RUN_GANETI_DIR + "/fake-hypervisor"
48 hv_base.BaseHypervisor.__init__(self)
49 utils.EnsureDirs([(self._ROOT_DIR, constants.RUN_DIRS_MODE)])
51 def ListInstances(self):
52 """Get the list of running instances.
55 return os.listdir(self._ROOT_DIR)
57 def GetInstanceInfo(self, instance_name):
58 """Get instance properties.
60 @param instance_name: the instance name
62 @return: tuple of (name, id, memory, vcpus, stat, times)
65 file_name = self._InstanceFile(instance_name)
66 if not os.path.exists(file_name):
69 fh = open(file_name, "r")
71 inst_id = fh.readline().strip()
72 memory = utils.TryConvert(int, fh.readline().strip())
73 vcpus = utils.TryConvert(int, fh.readline().strip())
76 return (instance_name, inst_id, memory, vcpus, stat, times)
80 raise errors.HypervisorError("Failed to list instance %s: %s" %
83 def GetAllInstancesInfo(self):
84 """Get properties of all instances.
86 @return: list of tuples (name, id, memory, vcpus, stat, times)
90 for file_name in os.listdir(self._ROOT_DIR):
92 fh = open(utils.PathJoin(self._ROOT_DIR, file_name), "r")
99 inst_id = fh.readline().strip()
100 memory = utils.TryConvert(int, fh.readline().strip())
101 vcpus = utils.TryConvert(int, fh.readline().strip())
106 data.append((file_name, inst_id, memory, vcpus, stat, times))
108 raise errors.HypervisorError("Failed to list instances: %s" % err)
112 def _InstanceFile(cls, instance_name):
113 """Compute the instance file for an instance name.
116 return utils.PathJoin(cls._ROOT_DIR, instance_name)
118 def _IsAlive(self, instance_name):
119 """Checks if an instance is alive.
122 file_name = self._InstanceFile(instance_name)
123 return os.path.exists(file_name)
125 def _MarkUp(self, instance):
126 """Mark the instance as running.
128 This does no checks, which should be done by its callers.
131 file_name = self._InstanceFile(instance.name)
132 fh = file(file_name, "w")
134 fh.write("0\n%d\n%d\n" %
135 (instance.beparams[constants.BE_MEMORY],
136 instance.beparams[constants.BE_VCPUS]))
140 def _MarkDown(self, instance_name):
141 """Mark the instance as running.
143 This does no checks, which should be done by its callers.
146 file_name = self._InstanceFile(instance_name)
147 utils.RemoveFile(file_name)
149 def StartInstance(self, instance, block_devices):
150 """Start an instance.
152 For the fake hypervisor, it just creates a file in the base dir,
153 creating an exception if it already exists. We don't actually
154 handle race conditions properly, since these are *FAKE* instances.
157 if self._IsAlive(instance.name):
158 raise errors.HypervisorError("Failed to start instance %s: %s" %
159 (instance.name, "already running"))
161 self._MarkUp(instance)
163 raise errors.HypervisorError("Failed to start instance %s: %s" %
164 (instance.name, err))
166 def StopInstance(self, instance, force=False, retry=False, name=None):
169 For the fake hypervisor, this just removes the file in the base
170 dir, if it exist, otherwise we raise an exception.
175 if not self._IsAlive(name):
176 raise errors.HypervisorError("Failed to stop instance %s: %s" %
177 (name, "not running"))
180 def RebootInstance(self, instance):
181 """Reboot an instance.
183 For the fake hypervisor, this does nothing.
188 def GetNodeInfo(self):
189 """Return information about the node.
191 This is just a wrapper over the base GetLinuxNodeInfo method.
193 @return: a dict with the following keys (values in MiB):
194 - memory_total: the total memory size on the node
195 - memory_free: the available memory on the node for instances
196 - memory_dom0: the memory used by the node itself, if available
199 result = self.GetLinuxNodeInfo()
200 # substract running instances
201 all_instances = self.GetAllInstancesInfo()
202 result['memory_free'] -= min(result['memory_free'],
203 sum([row[2] for row in all_instances]))
207 def GetShellCommandForConsole(cls, instance, hvparams, beparams):
208 """Return a command for connecting to the console of an instance.
211 return "echo Console not available for fake hypervisor"
214 """Verify the hypervisor.
216 For the fake hypervisor, it just checks the existence of the base
220 if not os.path.exists(self._ROOT_DIR):
221 return "The required directory '%s' does not exist." % self._ROOT_DIR
224 def PowercycleNode(cls):
225 """Fake hypervisor powercycle, just a wrapper over Linux powercycle.
228 cls.LinuxPowercycle()
230 def AcceptInstance(self, instance, info, target):
231 """Prepare to accept an instance.
233 @type instance: L{objects.Instance}
234 @param instance: instance to be accepted
236 @param info: instance info, not used
238 @param target: target host (usually ip), on this node
241 if self._IsAlive(instance.name):
242 raise errors.HypervisorError("Can't accept instance, already running")
244 def MigrateInstance(self, instance, target, live):
245 """Migrate an instance.
247 @type instance: L{objects.Instance}
248 @param instance: the instance to be migrated
250 @param target: hostname (usually ip) of the target node
252 @param live: whether to do a live or non-live migration
255 logging.debug("Fake hypervisor migrating %s to %s (live=%s)",
256 instance, target, live)
258 self._MarkDown(instance.name)
260 def FinalizeMigration(self, instance, info, success):
261 """Finalize an instance migration.
263 For the fake hv, this just marks the instance up.
265 @type instance: L{objects.Instance}
266 @param instance: instance whose migration is being finalized
270 self._MarkUp(instance)
273 self._MarkDown(instance.name)