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
22 """Base class for all hypervisors
24 The syntax for the _CHECK variables and the contents of the PARAMETERS
25 dict is the same, see the docstring for L{BaseHypervisor.PARAMETERS}.
27 @var _FILE_CHECK: stub for file checks, without the required flag
28 @var _DIR_CHECK: stub for directory checks, without the required flag
29 @var REQ_FILE_CHECK: mandatory file parameter
30 @var OPT_FILE_CHECK: optional file parameter
31 @var REQ_DIR_CHECK: mandatory directory parametr
32 @var OPT_DIR_CHECK: optional directory parameter
33 @var NO_CHECK: parameter without any checks at all
34 @var REQUIRED_CHECK: parameter required to exist (and non-false), but
35 without other checks; beware that this can't be used for boolean
36 parameters, where you should use NO_CHECK or a custom checker
45 from ganeti import errors
46 from ganeti import utils
49 # Read the BaseHypervisor.PARAMETERS docstring for the syntax of the
53 _FILE_CHECK = (utils.IsNormAbsPath, "must be an absolute normalized path",
54 os.path.isfile, "not found or not a file")
57 _DIR_CHECK = (utils.IsNormAbsPath, "must be an absolute normalized path",
58 os.path.isdir, "not found or not a directory")
60 # nice wrappers for users
61 REQ_FILE_CHECK = (True, ) + _FILE_CHECK
62 OPT_FILE_CHECK = (False, ) + _FILE_CHECK
63 REQ_DIR_CHECK = (True, ) + _DIR_CHECK
64 OPT_DIR_CHECK = (False, ) + _DIR_CHECK
67 NO_CHECK = (False, None, None, None, None)
69 # required, but no other checks
70 REQUIRED_CHECK = (True, None, None, None, None)
73 def ParamInSet(required, my_set):
74 """Builds parameter checker for set membership.
76 @type required: boolean
77 @param required: whether this is a required parameter
78 @type my_set: tuple, list or set
79 @param my_set: allowed values set
82 fn = lambda x: x in my_set
83 err = ("The value must be one of: %s" % utils.CommaJoin(my_set))
84 return (required, fn, err, None, None)
87 class BaseHypervisor(object):
88 """Abstract virtualisation technology interface
90 The goal is that all aspects of the virtualisation technology are
91 abstracted away from the rest of code.
93 @cvar PARAMETERS: a dict of parameter name: check type; the check type is
94 a five-tuple containing:
95 - the required flag (boolean)
96 - a function to check for syntax, that will be used in
97 L{CheckParameterSyntax}, in the master daemon process
98 - an error message for the above function
99 - a function to check for parameter validity on the remote node,
100 in the L{ValidateParameters} function
101 - an error message for the above function
110 def StartInstance(self, instance, block_devices):
111 """Start an instance."""
112 raise NotImplementedError
114 def StopInstance(self, instance, force=False):
115 """Stop an instance."""
116 raise NotImplementedError
118 def RebootInstance(self, instance):
119 """Reboot an instance."""
120 raise NotImplementedError
122 def ListInstances(self):
123 """Get the list of running instances."""
124 raise NotImplementedError
126 def GetInstanceInfo(self, instance_name):
127 """Get instance properties.
129 @type instance_name: string
130 @param instance_name: the instance name
132 @return: tuple (name, id, memory, vcpus, state, times)
135 raise NotImplementedError
137 def GetAllInstancesInfo(self):
138 """Get properties of all instances.
140 @return: list of tuples (name, id, memory, vcpus, stat, times)
143 raise NotImplementedError
145 def GetNodeInfo(self):
146 """Return information about the node.
148 @return: a dict with the following keys (values in MiB):
149 - memory_total: the total memory size on the node
150 - memory_free: the available memory on the node for instances
151 - memory_dom0: the memory used by the node itself, if available
154 raise NotImplementedError
157 def GetShellCommandForConsole(cls, instance, hvparams, beparams):
158 """Return a command for connecting to the console of an instance.
161 raise NotImplementedError
164 def GetAncillaryFiles(cls):
165 """Return a list of ancillary files to be copied to all nodes as ancillary
168 @rtype: list of strings
169 @return: list of absolute paths of files to ship cluster-wide
172 # By default we return a member variable, so that if an hypervisor has just
173 # a static list of files it doesn't have to override this function.
174 return cls.ANCILLARY_FILES
177 """Verify the hypervisor.
180 raise NotImplementedError
182 def MigrationInfo(self, instance):
183 """Get instance information to perform a migration.
185 By default assume no information is needed.
187 @type instance: L{objects.Instance}
188 @param instance: instance to be migrated
189 @rtype: string/data (opaque)
190 @return: instance migration information - serialized form
195 def AcceptInstance(self, instance, info, target):
196 """Prepare to accept an instance.
198 By default assume no preparation is needed.
200 @type instance: L{objects.Instance}
201 @param instance: instance to be accepted
202 @type info: string/data (opaque)
203 @param info: migration information, from the source node
205 @param target: target host (usually ip), on this node
210 def FinalizeMigration(self, instance, info, success):
211 """Finalized an instance migration.
213 Should finalize or revert any preparation done to accept the instance.
214 Since by default we do no preparation, we also don't have anything to do
216 @type instance: L{objects.Instance}
217 @param instance: instance whose migration is being aborted
218 @type info: string/data (opaque)
219 @param info: migration information, from the source node
220 @type success: boolean
221 @param success: whether the migration was a success or a failure
226 def MigrateInstance(self, name, target, live):
227 """Migrate an instance.
230 @param name: name of the instance to be migrated
232 @param target: hostname (usually ip) of the target node
234 @param live: whether to do a live or non-live migration
237 raise NotImplementedError
240 def CheckParameterSyntax(cls, hvparams):
241 """Check the given parameters for validity.
243 This should check the passed set of parameters for
244 validity. Classes should extend, not replace, this function.
247 @param hvparams: dictionary with parameter names/value
248 @raise errors.HypervisorError: when a parameter is not valid
252 if key not in cls.PARAMETERS:
253 raise errors.HypervisorError("Parameter '%s' is not supported" % key)
255 # cheap tests that run on the master, should not access the world
256 for name, (required, check_fn, errstr, _, _) in cls.PARAMETERS.items():
257 if name not in hvparams:
258 raise errors.HypervisorError("Parameter '%s' is missing" % name)
259 value = hvparams[name]
260 if not required and not value:
263 raise errors.HypervisorError("Parameter '%s' is required but"
264 " is currently not defined" % (name, ))
265 if check_fn is not None and not check_fn(value):
266 raise errors.HypervisorError("Parameter '%s' fails syntax"
267 " check: %s (current value: '%s')" %
268 (name, errstr, value))
271 def ValidateParameters(cls, hvparams):
272 """Check the given parameters for validity.
274 This should check the passed set of parameters for
275 validity. Classes should extend, not replace, this function.
278 @param hvparams: dictionary with parameter names/value
279 @raise errors.HypervisorError: when a parameter is not valid
282 for name, (required, _, _, check_fn, errstr) in cls.PARAMETERS.items():
283 value = hvparams[name]
284 if not required and not value:
286 if check_fn is not None and not check_fn(value):
287 raise errors.HypervisorError("Parameter '%s' fails"
288 " validation: %s (current value: '%s')" %
289 (name, errstr, value))
292 def PowercycleNode(cls):
293 """Hard powercycle a node using hypervisor specific methods.
295 This method should hard powercycle the node, using whatever
296 methods the hypervisor provides. Note that this means that all
297 instances running on the node must be stopped too.
300 raise NotImplementedError
303 def GetLinuxNodeInfo(self):
304 """For linux systems, return actual OS information.
306 This is an abstraction for all non-hypervisor-based classes, where
307 the node actually sees all the memory and CPUs via the /proc
308 interface and standard commands. The other case if for example
309 xen, where you only see the hardware resources via xen-specific
312 @return: a dict with the following keys (values in MiB):
313 - memory_total: the total memory size on the node
314 - memory_free: the available memory on the node for instances
315 - memory_dom0: the memory used by the node itself, if available
319 data = utils.ReadFile("/proc/meminfo").splitlines()
320 except EnvironmentError, err:
321 raise errors.HypervisorError("Failed to list node info: %s" % (err,))
327 splitfields = line.split(":", 1)
329 if len(splitfields) > 1:
330 key = splitfields[0].strip()
331 val = splitfields[1].strip()
332 if key == 'MemTotal':
333 result['memory_total'] = int(val.split()[0])/1024
334 elif key in ('MemFree', 'Buffers', 'Cached'):
335 sum_free += int(val.split()[0])/1024
336 elif key == 'Active':
337 result['memory_dom0'] = int(val.split()[0])/1024
338 except (ValueError, TypeError), err:
339 raise errors.HypervisorError("Failed to compute memory usage: %s" %
341 result['memory_free'] = sum_free
345 fh = open("/proc/cpuinfo")
347 cpu_total = len(re.findall("(?m)^processor\s*:\s*[0-9]+\s*$",
351 except EnvironmentError, err:
352 raise errors.HypervisorError("Failed to list node info: %s" % (err,))
353 result['cpu_total'] = cpu_total
354 # FIXME: export correct data here
355 result['cpu_nodes'] = 1
356 result['cpu_sockets'] = 1
361 def LinuxPowercycle(cls):
362 """Linux-specific powercycle method.
366 fd = os.open("/proc/sysrq-trigger", os.O_WRONLY)
372 logging.exception("Can't open the sysrq-trigger file")
373 result = utils.RunCmd(["reboot", "-n", "-f"])
375 logging.error("Can't run shutdown: %s", result.output)