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.
43 _ROOT_DIR = constants.RUN_DIR + "/ganeti-fake-hypervisor"
46 hv_base.BaseHypervisor.__init__(self)
47 if not os.path.exists(self._ROOT_DIR):
48 os.mkdir(self._ROOT_DIR)
50 def ListInstances(self):
51 """Get the list of running instances.
54 return os.listdir(self._ROOT_DIR)
56 def GetInstanceInfo(self, instance_name):
57 """Get instance properties.
59 @param instance_name: the instance name
61 @return: tuple of (name, id, memory, vcpus, stat, times)
64 file_name = self._InstanceFile(instance_name)
65 if not os.path.exists(file_name):
68 fh = open(file_name, "r")
70 inst_id = fh.readline().strip()
71 memory = utils.TryConvert(int, fh.readline().strip())
72 vcpus = utils.TryConvert(int, fh.readline().strip())
75 return (instance_name, inst_id, memory, vcpus, stat, times)
79 raise errors.HypervisorError("Failed to list instance %s: %s" %
82 def GetAllInstancesInfo(self):
83 """Get properties of all instances.
85 @return: list of tuples (name, id, memory, vcpus, stat, times)
89 for file_name in os.listdir(self._ROOT_DIR):
91 fh = open(utils.PathJoin(self._ROOT_DIR, file_name), "r")
98 inst_id = fh.readline().strip()
99 memory = utils.TryConvert(int, fh.readline().strip())
100 vcpus = utils.TryConvert(int, fh.readline().strip())
105 data.append((file_name, inst_id, memory, vcpus, stat, times))
107 raise errors.HypervisorError("Failed to list instances: %s" % err)
111 def _InstanceFile(cls, instance_name):
112 """Compute the instance file for an instance name.
115 return utils.PathJoin(cls._ROOT_DIR, instance_name)
117 def _IsAlive(self, instance_name):
118 """Checks if an instance is alive.
121 file_name = self._InstanceFile(instance_name)
122 return os.path.exists(file_name)
124 def _MarkUp(self, instance):
125 """Mark the instance as running.
127 This does no checks, which should be done by its callers.
130 file_name = self._InstanceFile(instance.name)
131 fh = file(file_name, "w")
133 fh.write("0\n%d\n%d\n" %
134 (instance.beparams[constants.BE_MEMORY],
135 instance.beparams[constants.BE_VCPUS]))
139 def _MarkDown(self, instance):
140 """Mark the instance as running.
142 This does no checks, which should be done by its callers.
145 file_name = self._InstanceFile(instance.name)
146 utils.RemoveFile(file_name)
148 def StartInstance(self, instance, block_devices):
149 """Start an instance.
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.
156 if self._IsAlive(instance.name):
157 raise errors.HypervisorError("Failed to start instance %s: %s" %
158 (instance.name, "already running"))
160 self._MarkUp(instance)
162 raise errors.HypervisorError("Failed to start instance %s: %s" %
163 (instance.name, err))
165 def StopInstance(self, instance, force=False, retry=False):
168 For the fake hypervisor, this just removes the file in the base
169 dir, if it exist, otherwise we raise an exception.
172 if not self._IsAlive(instance.name):
173 raise errors.HypervisorError("Failed to stop instance %s: %s" %
174 (instance.name, "not running"))
175 self._MarkDown(instance)
177 def RebootInstance(self, instance):
178 """Reboot an instance.
180 For the fake hypervisor, this does nothing.
185 def GetNodeInfo(self):
186 """Return information about the node.
188 This is just a wrapper over the base GetLinuxNodeInfo method.
190 @return: a dict with the following keys (values in MiB):
191 - memory_total: the total memory size on the node
192 - memory_free: the available memory on the node for instances
193 - memory_dom0: the memory used by the node itself, if available
196 result = self.GetLinuxNodeInfo()
197 # substract running instances
198 all_instances = self.GetAllInstancesInfo()
199 result['memory_free'] -= min(result['memory_free'],
200 sum([row[2] for row in all_instances]))
204 def GetShellCommandForConsole(cls, instance, hvparams, beparams):
205 """Return a command for connecting to the console of an instance.
208 return "echo Console not available for fake hypervisor"
211 """Verify the hypervisor.
213 For the fake hypervisor, it just checks the existence of the base
217 if not os.path.exists(self._ROOT_DIR):
218 return "The required directory '%s' does not exist." % self._ROOT_DIR
221 def PowercycleNode(cls):
222 """Fake hypervisor powercycle, just a wrapper over Linux powercycle.
225 cls.LinuxPowercycle()
227 def AcceptInstance(self, instance, info, target):
228 """Prepare to accept an instance.
230 @type instance: L{objects.Instance}
231 @param instance: instance to be accepted
233 @param info: instance info, not used
235 @param target: target host (usually ip), on this node
238 if self._IsAlive(instance.name):
239 raise errors.HypervisorError("Can't accept instance, already running")
241 def MigrateInstance(self, instance, target, live):
242 """Migrate an instance.
244 @type instance: L{object.Instance}
245 @param instance: the instance to be migrated
247 @param target: hostname (usually ip) of the target node
249 @param live: whether to do a live or non-live migration
252 logging.debug("Fake hypervisor migrating %s to %s (live=%s)",
253 instance, target, live)
255 self._MarkDown(instance)
257 def FinalizeMigration(self, instance, info, success):
258 """Finalize an instance migration.
260 For the fake hv, this just marks the instance up.
262 @type instance: L{objects.Instance}
263 @param instance: instance whose migration is being finalized
267 self._MarkUp(instance)
270 self._MarkDown(instance)