Statistics
| Branch: | Tag: | Revision:

root / lib / hypervisor / hv_base.py @ f15a6b03

History | View | Annotate | Download (20.8 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2012, 2013 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
"""Base class for all hypervisors
23

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}.
26

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
37

38
"""
39

    
40
import os
41
import re
42
import logging
43

    
44

    
45
from ganeti import errors
46
from ganeti import utils
47
from ganeti import constants
48

    
49

    
50
def _IsCpuMaskWellFormed(cpu_mask):
51
  """Verifies if the given single CPU mask is valid
52

53
  The single CPU mask should be in the form "a,b,c,d", where each
54
  letter is a positive number or range.
55

56
  """
57
  try:
58
    cpu_list = utils.ParseCpuMask(cpu_mask)
59
  except errors.ParseError, _:
60
    return False
61
  return isinstance(cpu_list, list) and len(cpu_list) > 0
62

    
63

    
64
def _IsMultiCpuMaskWellFormed(cpu_mask):
65
  """Verifies if the given multiple CPU mask is valid
66

67
  A valid multiple CPU mask is in the form "a:b:c:d", where each
68
  letter is a single CPU mask.
69

70
  """
71
  try:
72
    utils.ParseMultiCpuMask(cpu_mask)
73
  except errors.ParseError, _:
74
    return False
75

    
76
  return True
77

    
78

    
79
# Read the BaseHypervisor.PARAMETERS docstring for the syntax of the
80
# _CHECK values
81

    
82
# must be afile
83
_FILE_CHECK = (utils.IsNormAbsPath, "must be an absolute normalized path",
84
               os.path.isfile, "not found or not a file")
85

    
86
# must be a file or a URL
87
_FILE_OR_URL_CHECK = (utils.IsNormAbsPathOrURL,
88
                      "must be an absolute normalized path or a URL",
89
                      lambda x: os.path.isfile(x) or
90
                      re.match(r'(https?|ftps?)://', x),
91
                      "not found or not a file or URL")
92

    
93
# must be a directory
94
_DIR_CHECK = (utils.IsNormAbsPath, "must be an absolute normalized path",
95
              os.path.isdir, "not found or not a directory")
96

    
97
# CPU mask must be well-formed
98
# TODO: implement node level check for the CPU mask
99
_CPU_MASK_CHECK = (_IsCpuMaskWellFormed,
100
                   "CPU mask definition is not well-formed",
101
                   None, None)
102

    
103
# Multiple CPU mask must be well-formed
104
_MULTI_CPU_MASK_CHECK = (_IsMultiCpuMaskWellFormed,
105
                         "Multiple CPU mask definition is not well-formed",
106
                         None, None)
107

    
108
# Check for validity of port number
109
_NET_PORT_CHECK = (lambda x: 0 < x < 65535, "invalid port number",
110
                   None, None)
111

    
112
# Check that an integer is non negative
113
_NONNEGATIVE_INT_CHECK = (lambda x: x >= 0, "cannot be negative", None, None)
114

    
115
# nice wrappers for users
116
REQ_FILE_CHECK = (True, ) + _FILE_CHECK
117
OPT_FILE_CHECK = (False, ) + _FILE_CHECK
118
REQ_FILE_OR_URL_CHECK = (True, ) + _FILE_OR_URL_CHECK
119
OPT_FILE_OR_URL_CHECK = (False, ) + _FILE_OR_URL_CHECK
120
REQ_DIR_CHECK = (True, ) + _DIR_CHECK
121
OPT_DIR_CHECK = (False, ) + _DIR_CHECK
122
REQ_NET_PORT_CHECK = (True, ) + _NET_PORT_CHECK
123
OPT_NET_PORT_CHECK = (False, ) + _NET_PORT_CHECK
124
REQ_CPU_MASK_CHECK = (True, ) + _CPU_MASK_CHECK
125
OPT_CPU_MASK_CHECK = (False, ) + _CPU_MASK_CHECK
126
REQ_MULTI_CPU_MASK_CHECK = (True, ) + _MULTI_CPU_MASK_CHECK
127
OPT_MULTI_CPU_MASK_CHECK = (False, ) + _MULTI_CPU_MASK_CHECK
128
REQ_NONNEGATIVE_INT_CHECK = (True, ) + _NONNEGATIVE_INT_CHECK
129
OPT_NONNEGATIVE_INT_CHECK = (False, ) + _NONNEGATIVE_INT_CHECK
130

    
131
# no checks at all
132
NO_CHECK = (False, None, None, None, None)
133

    
134
# required, but no other checks
135
REQUIRED_CHECK = (True, None, None, None, None)
136

    
137
# migration type
138
MIGRATION_MODE_CHECK = (True, lambda x: x in constants.HT_MIGRATION_MODES,
139
                        "invalid migration mode", None, None)
140

    
141

    
142
def ParamInSet(required, my_set):
143
  """Builds parameter checker for set membership.
144

145
  @type required: boolean
146
  @param required: whether this is a required parameter
147
  @type my_set: tuple, list or set
148
  @param my_set: allowed values set
149

150
  """
151
  fn = lambda x: x in my_set
152
  err = ("The value must be one of: %s" % utils.CommaJoin(my_set))
153
  return (required, fn, err, None, None)
154

    
155

    
156
class HvInstanceState(object):
157
  RUNNING = 0
158
  SHUTDOWN = 1
159

    
160
  @staticmethod
161
  def IsRunning(s):
162
    return s == HvInstanceState.RUNNING
163

    
164
  @staticmethod
165
  def IsShutdown(s):
166
    return s == HvInstanceState.SHUTDOWN
167

    
168

    
169
class BaseHypervisor(object):
170
  """Abstract virtualisation technology interface
171

172
  The goal is that all aspects of the virtualisation technology are
173
  abstracted away from the rest of code.
174

175
  @cvar PARAMETERS: a dict of parameter name: check type; the check type is
176
      a five-tuple containing:
177
          - the required flag (boolean)
178
          - a function to check for syntax, that will be used in
179
            L{CheckParameterSyntax}, in the master daemon process
180
          - an error message for the above function
181
          - a function to check for parameter validity on the remote node,
182
            in the L{ValidateParameters} function
183
          - an error message for the above function
184
  @type CAN_MIGRATE: boolean
185
  @cvar CAN_MIGRATE: whether this hypervisor can do migration (either
186
      live or non-live)
187

188
  """
189
  PARAMETERS = {}
190
  ANCILLARY_FILES = []
191
  ANCILLARY_FILES_OPT = []
192
  CAN_MIGRATE = False
193

    
194
  def StartInstance(self, instance, block_devices, startup_paused):
195
    """Start an instance."""
196
    raise NotImplementedError
197

    
198
  def StopInstance(self, instance, force=False, retry=False, name=None):
199
    """Stop an instance
200

201
    @type instance: L{objects.Instance}
202
    @param instance: instance to stop
203
    @type force: boolean
204
    @param force: whether to do a "hard" stop (destroy)
205
    @type retry: boolean
206
    @param retry: whether this is just a retry call
207
    @type name: string or None
208
    @param name: if this parameter is passed, the the instance object
209
        should not be used (will be passed as None), and the shutdown
210
        must be done by name only
211
    @raise errors.HypervisorError: when a parameter is not valid or
212
        the instance failed to be stopped
213

214
    """
215
    raise NotImplementedError
216

    
217
  def CleanupInstance(self, instance_name):
218
    """Cleanup after a stopped instance
219

220
    This is an optional method, used by hypervisors that need to cleanup after
221
    an instance has been stopped.
222

223
    @type instance_name: string
224
    @param instance_name: instance name to cleanup after
225

226
    """
227
    pass
228

    
229
  def RebootInstance(self, instance):
230
    """Reboot an instance."""
231
    raise NotImplementedError
232

    
233
  def ListInstances(self, hvparams=None):
234
    """Get the list of running instances."""
235
    raise NotImplementedError
236

    
237
  def GetInstanceInfo(self, instance_name, hvparams=None):
238
    """Get instance properties.
239

240
    @type instance_name: string
241
    @param instance_name: the instance name
242
    @type hvparams: dict of strings
243
    @param hvparams: hvparams to be used with this instance
244

245
    @rtype: (string, string, int, int, HvInstanceState, int)
246
    @return: tuple (name, id, memory, vcpus, state, times)
247

248
    """
249
    raise NotImplementedError
250

    
251
  def GetAllInstancesInfo(self, hvparams=None):
252
    """Get properties of all instances.
253

254
    @type hvparams: dict of strings
255
    @param hvparams: hypervisor parameter
256

257
    @rtype: (string, string, int, int, HvInstanceState, int)
258
    @return: list of tuples (name, id, memory, vcpus, state, times)
259

260
    """
261
    raise NotImplementedError
262

    
263
  def GetNodeInfo(self, hvparams=None):
264
    """Return information about the node.
265

266
    @type hvparams: dict of strings
267
    @param hvparams: hypervisor parameters
268

269
    @return: a dict with at least the following keys (memory values in MiB):
270
          - memory_total: the total memory size on the node
271
          - memory_free: the available memory on the node for instances
272
          - memory_dom0: the memory used by the node itself, if available
273
          - cpu_total: total number of CPUs
274
          - cpu_dom0: number of CPUs used by the node OS
275
          - cpu_nodes: number of NUMA domains
276
          - cpu_sockets: number of physical CPU sockets
277

278
    """
279
    raise NotImplementedError
280

    
281
  @classmethod
282
  def GetInstanceConsole(cls, instance, primary_node, node_group,
283
                         hvparams, beparams):
284
    """Return information for connecting to the console of an instance.
285

286
    """
287
    raise NotImplementedError
288

    
289
  @classmethod
290
  def GetAncillaryFiles(cls):
291
    """Return a list of ancillary files to be copied to all nodes as ancillary
292
    configuration files.
293

294
    @rtype: (list of absolute paths, list of absolute paths)
295
    @return: (all files, optional files)
296

297
    """
298
    # By default we return a member variable, so that if an hypervisor has just
299
    # a static list of files it doesn't have to override this function.
300
    assert set(cls.ANCILLARY_FILES).issuperset(cls.ANCILLARY_FILES_OPT), \
301
      "Optional ancillary files must be a subset of ancillary files"
302

    
303
    return (cls.ANCILLARY_FILES, cls.ANCILLARY_FILES_OPT)
304

    
305
  def Verify(self, hvparams=None):
306
    """Verify the hypervisor.
307

308
    @type hvparams: dict of strings
309
    @param hvparams: hypervisor parameters to be verified against
310

311
    @return: Problem description if something is wrong, C{None} otherwise
312

313
    """
314
    raise NotImplementedError
315

    
316
  def MigrationInfo(self, instance): # pylint: disable=R0201,W0613
317
    """Get instance information to perform a migration.
318

319
    By default assume no information is needed.
320

321
    @type instance: L{objects.Instance}
322
    @param instance: instance to be migrated
323
    @rtype: string/data (opaque)
324
    @return: instance migration information - serialized form
325

326
    """
327
    return ""
328

    
329
  def AcceptInstance(self, instance, info, target):
330
    """Prepare to accept an instance.
331

332
    By default assume no preparation is needed.
333

334
    @type instance: L{objects.Instance}
335
    @param instance: instance to be accepted
336
    @type info: string/data (opaque)
337
    @param info: migration information, from the source node
338
    @type target: string
339
    @param target: target host (usually ip), on this node
340

341
    """
342
    pass
343

    
344
  def BalloonInstanceMemory(self, instance, mem):
345
    """Balloon an instance memory to a certain value.
346

347
    @type instance: L{objects.Instance}
348
    @param instance: instance to be accepted
349
    @type mem: int
350
    @param mem: actual memory size to use for instance runtime
351

352
    """
353
    raise NotImplementedError
354

    
355
  def FinalizeMigrationDst(self, instance, info, success):
356
    """Finalize the instance migration on the target node.
357

358
    Should finalize or revert any preparation done to accept the instance.
359
    Since by default we do no preparation, we also don't have anything to do
360

361
    @type instance: L{objects.Instance}
362
    @param instance: instance whose migration is being finalized
363
    @type info: string/data (opaque)
364
    @param info: migration information, from the source node
365
    @type success: boolean
366
    @param success: whether the migration was a success or a failure
367

368
    """
369
    pass
370

    
371
  def MigrateInstance(self, cluster_name, instance, target, live):
372
    """Migrate an instance.
373

374
    @type cluster_name: string
375
    @param cluster_name: name of the cluster
376
    @type instance: L{objects.Instance}
377
    @param instance: the instance to be migrated
378
    @type target: string
379
    @param target: hostname (usually ip) of the target node
380
    @type live: boolean
381
    @param live: whether to do a live or non-live migration
382

383
    """
384
    raise NotImplementedError
385

    
386
  def FinalizeMigrationSource(self, instance, success, live):
387
    """Finalize the instance migration on the source node.
388

389
    @type instance: L{objects.Instance}
390
    @param instance: the instance that was migrated
391
    @type success: bool
392
    @param success: whether the migration succeeded or not
393
    @type live: bool
394
    @param live: whether the user requested a live migration or not
395

396
    """
397
    pass
398

    
399
  def GetMigrationStatus(self, instance):
400
    """Get the migration status
401

402
    @type instance: L{objects.Instance}
403
    @param instance: the instance that is being migrated
404
    @rtype: L{objects.MigrationStatus}
405
    @return: the status of the current migration (one of
406
             L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
407
             progress info that can be retrieved from the hypervisor
408

409
    """
410
    raise NotImplementedError
411

    
412
  def _InstanceStartupMemory(self, instance, hvparams=None):
413
    """Get the correct startup memory for an instance
414

415
    This function calculates how much memory an instance should be started
416
    with, making sure it's a value between the minimum and the maximum memory,
417
    but also trying to use no more than the current free memory on the node.
418

419
    @type instance: L{objects.Instance}
420
    @param instance: the instance that is being started
421
    @rtype: integer
422
    @return: memory the instance should be started with
423

424
    """
425
    free_memory = self.GetNodeInfo(hvparams=hvparams)["memory_free"]
426
    max_start_mem = min(instance.beparams[constants.BE_MAXMEM], free_memory)
427
    start_mem = max(instance.beparams[constants.BE_MINMEM], max_start_mem)
428
    return start_mem
429

    
430
  @classmethod
431
  def CheckParameterSyntax(cls, hvparams):
432
    """Check the given parameters for validity.
433

434
    This should check the passed set of parameters for
435
    validity. Classes should extend, not replace, this function.
436

437
    @type hvparams:  dict
438
    @param hvparams: dictionary with parameter names/value
439
    @raise errors.HypervisorError: when a parameter is not valid
440

441
    """
442
    for key in hvparams:
443
      if key not in cls.PARAMETERS:
444
        raise errors.HypervisorError("Parameter '%s' is not supported" % key)
445

    
446
    # cheap tests that run on the master, should not access the world
447
    for name, (required, check_fn, errstr, _, _) in cls.PARAMETERS.items():
448
      if name not in hvparams:
449
        raise errors.HypervisorError("Parameter '%s' is missing" % name)
450
      value = hvparams[name]
451
      if not required and not value:
452
        continue
453
      if not value:
454
        raise errors.HypervisorError("Parameter '%s' is required but"
455
                                     " is currently not defined" % (name, ))
456
      if check_fn is not None and not check_fn(value):
457
        raise errors.HypervisorError("Parameter '%s' fails syntax"
458
                                     " check: %s (current value: '%s')" %
459
                                     (name, errstr, value))
460

    
461
  @classmethod
462
  def ValidateParameters(cls, hvparams):
463
    """Check the given parameters for validity.
464

465
    This should check the passed set of parameters for
466
    validity. Classes should extend, not replace, this function.
467

468
    @type hvparams:  dict
469
    @param hvparams: dictionary with parameter names/value
470
    @raise errors.HypervisorError: when a parameter is not valid
471

472
    """
473
    for name, (required, _, _, check_fn, errstr) in cls.PARAMETERS.items():
474
      value = hvparams[name]
475
      if not required and not value:
476
        continue
477
      if check_fn is not None and not check_fn(value):
478
        raise errors.HypervisorError("Parameter '%s' fails"
479
                                     " validation: %s (current value: '%s')" %
480
                                     (name, errstr, value))
481

    
482
  @classmethod
483
  def PowercycleNode(cls, hvparams=None):
484
    """Hard powercycle a node using hypervisor specific methods.
485

486
    This method should hard powercycle the node, using whatever
487
    methods the hypervisor provides. Note that this means that all
488
    instances running on the node must be stopped too.
489

490
    @type hvparams: dict of strings
491
    @param hvparams: hypervisor params to be used on this node
492

493
    """
494
    raise NotImplementedError
495

    
496
  @staticmethod
497
  def GetLinuxNodeInfo(meminfo="/proc/meminfo", cpuinfo="/proc/cpuinfo"):
498
    """For linux systems, return actual OS information.
499

500
    This is an abstraction for all non-hypervisor-based classes, where
501
    the node actually sees all the memory and CPUs via the /proc
502
    interface and standard commands. The other case if for example
503
    xen, where you only see the hardware resources via xen-specific
504
    tools.
505

506
    @param meminfo: name of the file containing meminfo
507
    @type meminfo: string
508
    @param cpuinfo: name of the file containing cpuinfo
509
    @type cpuinfo: string
510
    @return: a dict with the following keys (values in MiB):
511
          - memory_total: the total memory size on the node
512
          - memory_free: the available memory on the node for instances
513
          - memory_dom0: the memory used by the node itself, if available
514
          - cpu_total: total number of CPUs
515
          - cpu_dom0: number of CPUs used by the node OS
516
          - cpu_nodes: number of NUMA domains
517
          - cpu_sockets: number of physical CPU sockets
518

519
    """
520
    try:
521
      data = utils.ReadFile(meminfo).splitlines()
522
    except EnvironmentError, err:
523
      raise errors.HypervisorError("Failed to list node info: %s" % (err,))
524

    
525
    result = {}
526
    sum_free = 0
527
    try:
528
      for line in data:
529
        splitfields = line.split(":", 1)
530

    
531
        if len(splitfields) > 1:
532
          key = splitfields[0].strip()
533
          val = splitfields[1].strip()
534
          if key == "MemTotal":
535
            result["memory_total"] = int(val.split()[0]) / 1024
536
          elif key in ("MemFree", "Buffers", "Cached"):
537
            sum_free += int(val.split()[0]) / 1024
538
          elif key == "Active":
539
            result["memory_dom0"] = int(val.split()[0]) / 1024
540
    except (ValueError, TypeError), err:
541
      raise errors.HypervisorError("Failed to compute memory usage: %s" %
542
                                   (err,))
543
    result["memory_free"] = sum_free
544

    
545
    cpu_total = 0
546
    try:
547
      fh = open(cpuinfo)
548
      try:
549
        cpu_total = len(re.findall(r"(?m)^processor\s*:\s*[0-9]+\s*$",
550
                                   fh.read()))
551
      finally:
552
        fh.close()
553
    except EnvironmentError, err:
554
      raise errors.HypervisorError("Failed to list node info: %s" % (err,))
555
    result["cpu_total"] = cpu_total
556
    # We assume that the node OS can access all the CPUs
557
    result["cpu_dom0"] = cpu_total
558
    # FIXME: export correct data here
559
    result["cpu_nodes"] = 1
560
    result["cpu_sockets"] = 1
561

    
562
    return result
563

    
564
  @classmethod
565
  def LinuxPowercycle(cls):
566
    """Linux-specific powercycle method.
567

568
    """
569
    try:
570
      fd = os.open("/proc/sysrq-trigger", os.O_WRONLY)
571
      try:
572
        os.write(fd, "b")
573
      finally:
574
        fd.close()
575
    except OSError:
576
      logging.exception("Can't open the sysrq-trigger file")
577
      result = utils.RunCmd(["reboot", "-n", "-f"])
578
      if not result:
579
        logging.error("Can't run shutdown: %s", result.output)
580

    
581
  @staticmethod
582
  def _FormatVerifyResults(msgs):
583
    """Formats the verification results, given a list of errors.
584

585
    @param msgs: list of errors, possibly empty
586
    @return: overall problem description if something is wrong,
587
        C{None} otherwise
588

589
    """
590
    if msgs:
591
      return "; ".join(msgs)
592
    else:
593
      return None
594

    
595
  # pylint: disable=R0201,W0613
596
  def HotAddDevice(self, instance, dev_type, device, extra, seq):
597
    """Hot-add a device.
598

599
    """
600
    raise errors.HotplugError("Hotplug is not supported by this hypervisor")
601

    
602
  # pylint: disable=R0201,W0613
603
  def HotDelDevice(self, instance, dev_type, device, extra, seq):
604
    """Hot-del a device.
605

606
    """
607
    raise errors.HotplugError("Hotplug is not supported by this hypervisor")
608

    
609
  # pylint: disable=R0201,W0613
610
  def HotModDevice(self, instance, dev_type, device, extra, seq):
611
    """Hot-mod a device.
612

613
    """
614
    raise errors.HotplugError("Hotplug is not supported by this hypervisor")
615

    
616
  # pylint: disable=R0201,W0613
617
  def VerifyHotplugSupport(self, instance, action, dev_type):
618
    """Verifies that hotplug is supported.
619

620
    Given the target device and hotplug action checks if hotplug is
621
    actually supported.
622

623
    @type instance: L{objects.Instance}
624
    @param instance: the instance object
625
    @type action: string
626
    @param action: one of the supported hotplug commands
627
    @type dev_type: string
628
    @param dev_type: one of the supported device types to hotplug
629
    @raise errors.HotplugError: if hotplugging is not supported
630

631
    """
632
    raise errors.HotplugError("Hotplug is not supported.")
633

    
634
  def HotplugSupported(self, instance):
635
    """Checks if hotplug is supported.
636

637
    By default is not. Currently only KVM hypervisor supports it.
638

639
    """
640
    raise errors.HotplugError("Hotplug is not supported by this hypervisor")