utils.py 47.2 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
#
Iustin Pop's avatar
Iustin Pop committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


22
23
24
25
"""Ganeti utility module.

This module holds functions that can be used in both daemons (all) and
the command line scripts.
26

Iustin Pop's avatar
Iustin Pop committed
27
28
29
30
31
32
33
"""


import sys
import os
import sha
import time
34
import subprocess
Iustin Pop's avatar
Iustin Pop committed
35
36
37
38
import re
import socket
import tempfile
import shutil
39
import errno
40
import pwd
Guido Trotter's avatar
Guido Trotter committed
41
import itertools
42
43
import select
import fcntl
44
import resource
45
import logging
Michael Hanselmann's avatar
Michael Hanselmann committed
46
import signal
47
48

from cStringIO import StringIO
Iustin Pop's avatar
Iustin Pop committed
49
50

from ganeti import errors
Iustin Pop's avatar
Iustin Pop committed
51
from ganeti import constants
Iustin Pop's avatar
Iustin Pop committed
52

53

Iustin Pop's avatar
Iustin Pop committed
54
55
56
_locksheld = []
_re_shell_unquoted = re.compile('^[-.,=:/_+@A-Za-z0-9]+$')

57
debug = False
58
debug_locks = False
59
60

#: when set to True, L{RunCmd} is disabled
61
no_fork = False
62

63

Iustin Pop's avatar
Iustin Pop committed
64
class RunResult(object):
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  """Holds the result of running external programs.

  @type exit_code: int
  @ivar exit_code: the exit code of the program, or None (if the program
      didn't exit())
  @type signal: int or None
  @ivar signal: the signal that caused the program to finish, or None
      (if the program wasn't terminated by a signal)
  @type stdout: str
  @ivar stdout: the standard output of the program
  @type stderr: str
  @ivar stderr: the standard error of the program
  @type failed: boolean
  @ivar failed: True in case the program was
      terminated by a signal or exited with a non-zero exit code
  @ivar fail_reason: a string detailing the termination reason
Iustin Pop's avatar
Iustin Pop committed
81
82
83
84
85
86

  """
  __slots__ = ["exit_code", "signal", "stdout", "stderr",
               "failed", "fail_reason", "cmd"]


Iustin Pop's avatar
Iustin Pop committed
87
  def __init__(self, exit_code, signal_, stdout, stderr, cmd):
Iustin Pop's avatar
Iustin Pop committed
88
89
    self.cmd = cmd
    self.exit_code = exit_code
Iustin Pop's avatar
Iustin Pop committed
90
    self.signal = signal_
Iustin Pop's avatar
Iustin Pop committed
91
92
    self.stdout = stdout
    self.stderr = stderr
Iustin Pop's avatar
Iustin Pop committed
93
    self.failed = (signal_ is not None or exit_code != 0)
Iustin Pop's avatar
Iustin Pop committed
94
95
96
97
98
99
100
101

    if self.signal is not None:
      self.fail_reason = "terminated by signal %s" % self.signal
    elif self.exit_code is not None:
      self.fail_reason = "exited with exit code %s" % self.exit_code
    else:
      self.fail_reason = "unable to determine termination reason"

102
103
104
    if self.failed:
      logging.debug("Command '%s' failed (%s); output: %s",
                    self.cmd, self.fail_reason, self.output)
105

Iustin Pop's avatar
Iustin Pop committed
106
107
108
109
110
111
112
113
114
  def _GetOutput(self):
    """Returns the combined stdout and stderr for easier usage.

    """
    return self.stdout + self.stderr

  output = property(_GetOutput, None, None, "Return full output")


115
def RunCmd(cmd, env=None, output=None, cwd='/'):
Iustin Pop's avatar
Iustin Pop committed
116
117
118
119
120
  """Execute a (shell) command.

  The command should not read from its standard input, as it will be
  closed.

121
  @type  cmd: string or list
122
  @param cmd: Command to run
123
  @type env: dict
124
  @param env: Additional environment
125
  @type output: str
126
  @param output: if desired, the output of the command can be
127
128
      saved in a file instead of the RunResult instance; this
      parameter denotes the file name (if not None)
129
130
131
  @type cwd: string
  @param cwd: if specified, will be used as the working
      directory for the command; the default will be /
132
  @rtype: L{RunResult}
133
134
  @return: RunResult instance
  @raise erors.ProgrammerError: if we call this when forks are disabled
Iustin Pop's avatar
Iustin Pop committed
135
136

  """
137
138
139
  if no_fork:
    raise errors.ProgrammerError("utils.RunCmd() called with fork() disabled")

Iustin Pop's avatar
Iustin Pop committed
140
141
  if isinstance(cmd, list):
    cmd = [str(val) for val in cmd]
142
143
144
145
146
    strcmd = " ".join(cmd)
    shell = False
  else:
    strcmd = cmd
    shell = True
147
  logging.debug("RunCmd '%s'", strcmd)
148
149
150
151
152
153

  cmd_env = os.environ.copy()
  cmd_env["LC_ALL"] = "C"
  if env is not None:
    cmd_env.update(env)

154
  if output is None:
155
    out, err, status = _RunCmdPipe(cmd, cmd_env, shell, cwd)
156
  else:
157
    status = _RunCmdFile(cmd, cmd_env, shell, output, cwd)
158
159
160
161
162
163
164
165
166
167
168
    out = err = ""

  if status >= 0:
    exitcode = status
    signal_ = None
  else:
    exitcode = None
    signal_ = -status

  return RunResult(exitcode, signal_, out, err, strcmd)

169
def _RunCmdPipe(cmd, env, via_shell, cwd):
170
171
172
173
174
175
176
177
  """Run a command and return its output.

  @type  cmd: string or list
  @param cmd: Command to run
  @type env: dict
  @param env: The environment to use
  @type via_shell: bool
  @param via_shell: if we should run via the shell
178
179
  @type cwd: string
  @param cwd: the working directory for the program
180
181
182
183
  @rtype: tuple
  @return: (out, err, status)

  """
184
  poller = select.poll()
185
  child = subprocess.Popen(cmd, shell=via_shell,
186
187
188
                           stderr=subprocess.PIPE,
                           stdout=subprocess.PIPE,
                           stdin=subprocess.PIPE,
189
190
                           close_fds=True, env=env,
                           cwd=cwd)
191
192

  child.stdin.close()
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  poller.register(child.stdout, select.POLLIN)
  poller.register(child.stderr, select.POLLIN)
  out = StringIO()
  err = StringIO()
  fdmap = {
    child.stdout.fileno(): (out, child.stdout),
    child.stderr.fileno(): (err, child.stderr),
    }
  for fd in fdmap:
    status = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, status | os.O_NONBLOCK)

  while fdmap:
    for fd, event in poller.poll():
      if event & select.POLLIN or event & select.POLLPRI:
        data = fdmap[fd][1].read()
        # no data from read signifies EOF (the same as POLLHUP)
        if not data:
          poller.unregister(fd)
          del fdmap[fd]
          continue
        fdmap[fd][0].write(data)
      if (event & select.POLLNVAL or event & select.POLLHUP or
          event & select.POLLERR):
        poller.unregister(fd)
        del fdmap[fd]

  out = out.getvalue()
  err = err.getvalue()
Iustin Pop's avatar
Iustin Pop committed
222
223

  status = child.wait()
224
  return out, err, status
Iustin Pop's avatar
Iustin Pop committed
225

226

227
def _RunCmdFile(cmd, env, via_shell, output, cwd):
228
229
230
231
232
233
234
235
236
237
  """Run a command and save its output to a file.

  @type  cmd: string or list
  @param cmd: Command to run
  @type env: dict
  @param env: The environment to use
  @type via_shell: bool
  @param via_shell: if we should run via the shell
  @type output: str
  @param output: the filename in which to save the output
238
239
  @type cwd: string
  @param cwd: the working directory for the program
240
241
242
243
244
245
246
247
248
249
  @rtype: int
  @return: the exit status

  """
  fh = open(output, "a")
  try:
    child = subprocess.Popen(cmd, shell=via_shell,
                             stderr=subprocess.STDOUT,
                             stdout=fh,
                             stdin=subprocess.PIPE,
250
251
                             close_fds=True, env=env,
                             cwd=cwd)
252
253
254
255
256
257

    child.stdin.close()
    status = child.wait()
  finally:
    fh.close()
  return status
Iustin Pop's avatar
Iustin Pop committed
258
259
260
261
262
263
264
265


def RemoveFile(filename):
  """Remove a file ignoring some errors.

  Remove a file, ignoring non-existing ones or directories. Other
  errors are passed.

266
267
268
  @type filename: str
  @param filename: the file to be removed

Iustin Pop's avatar
Iustin Pop committed
269
270
271
272
  """
  try:
    os.unlink(filename)
  except OSError, err:
273
    if err.errno not in (errno.ENOENT, errno.EISDIR):
Iustin Pop's avatar
Iustin Pop committed
274
275
276
277
278
279
280
281
282
      raise


def _FingerprintFile(filename):
  """Compute the fingerprint of a file.

  If the file does not exist, a None will be returned
  instead.

283
284
285
286
287
  @type filename: str
  @param filename: the filename to checksum
  @rtype: str
  @return: the hex digest of the sha checksum of the contents
      of the file
Iustin Pop's avatar
Iustin Pop committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

  """
  if not (os.path.exists(filename) and os.path.isfile(filename)):
    return None

  f = open(filename)

  fp = sha.sha()
  while True:
    data = f.read(4096)
    if not data:
      break

    fp.update(data)

  return fp.hexdigest()


def FingerprintFiles(files):
  """Compute fingerprints for a list of files.

309
310
311
312
313
  @type files: list
  @param files: the list of filename to fingerprint
  @rtype: dict
  @return: a dictionary filename: fingerprint, holding only
      existing files
Iustin Pop's avatar
Iustin Pop committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

  """
  ret = {}

  for filename in files:
    cksum = _FingerprintFile(filename)
    if cksum:
      ret[filename] = cksum

  return ret


def CheckDict(target, template, logname=None):
  """Ensure a dictionary has a required set of keys.

329
330
331
  For the given dictionaries I{target} and I{template}, ensure
  I{target} has all the keys from I{template}. Missing keys are added
  with values from template.
Iustin Pop's avatar
Iustin Pop committed
332

333
334
335
336
337
338
339
  @type target: dict
  @param target: the dictionary to update
  @type template: dict
  @param template: the dictionary holding the default values
  @type logname: str or None
  @param logname: if not None, causes the missing keys to be
      logged with this name
Iustin Pop's avatar
Iustin Pop committed
340
341
342
343
344
345
346
347
348

  """
  missing = []
  for k in template:
    if k not in target:
      missing.append(k)
      target[k] = template[k]

  if missing and logname:
349
    logging.warning('%s missing keys %s', logname, ', '.join(missing))
Iustin Pop's avatar
Iustin Pop committed
350
351
352
353
354


def IsProcessAlive(pid):
  """Check if a given pid exists on the system.

355
356
357
358
359
360
  @note: zombie processes treated as not alive, and giving a
      pid M{<= 0} causes the function to return False.
  @type pid: int
  @param pid: the process ID to check
  @rtype: boolean
  @return: True if the process exists
Iustin Pop's avatar
Iustin Pop committed
361
362

  """
363
364
365
  if pid <= 0:
    return False

Iustin Pop's avatar
Iustin Pop committed
366
367
368
  try:
    f = open("/proc/%d/status" % pid)
  except IOError, err:
369
    if err.errno in (errno.ENOENT, errno.ENOTDIR):
Iustin Pop's avatar
Iustin Pop committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
      return False

  alive = True
  try:
    data = f.readlines()
    if len(data) > 1:
      state = data[1].split()
      if len(state) > 1 and state[1] == "Z":
        alive = False
  finally:
    f.close()

  return alive


385
def ReadPidFile(pidfile):
386
  """Read a pid from a file.
387

388
389
390
  @type  pidfile: string
  @param pidfile: path to the file containing the pid
  @rtype: int
391
392
  @return: The process id, if the file exista and contains a valid PID,
           otherwise 0
393
394
395
396

  """
  try:
    pf = open(pidfile, 'r')
397
398
399
400
  except EnvironmentError, err:
    if err.errno != errno.ENOENT:
      logging.exception("Can't read pid file?!")
    return 0
401
402
403

  try:
    pid = int(pf.read())
404
  except ValueError, err:
405
    logging.info("Can't parse pid file contents", exc_info=True)
406
    return 0
407

408
  return pid
409
410


Iustin Pop's avatar
Iustin Pop committed
411
412
413
414
def MatchNameComponent(key, name_list):
  """Try to match a name against a list.

  This function will try to match a name like test1 against a list
415
416
417
418
419
  like C{['test1.example.com', 'test2.example.com', ...]}. Against
  this list, I{'test1'} as well as I{'test1.example'} will match, but
  not I{'test1.ex'}. A multiple match will be considered as no match
  at all (e.g. I{'test1'} against C{['test1.example.com',
  'test1.example.org']}).
Iustin Pop's avatar
Iustin Pop committed
420

421
422
423
424
  @type key: str
  @param key: the name to be searched
  @type name_list: list
  @param name_list: the list of strings against which to search the key
Iustin Pop's avatar
Iustin Pop committed
425

426
427
428
  @rtype: None or str
  @return: None if there is no match I{or} if there are multiple matches,
      otherwise the element from the list which matches
Iustin Pop's avatar
Iustin Pop committed
429
430
431
432
433
434
435
436
437

  """
  mo = re.compile("^%s(\..*)?$" % re.escape(key))
  names_filtered = [name for name in name_list if mo.match(name) is not None]
  if len(names_filtered) != 1:
    return None
  return names_filtered[0]


438
class HostInfo:
439
  """Class implementing resolver and hostname functionality
440
441

  """
442
  def __init__(self, name=None):
443
444
    """Initialize the host name object.

445
446
    If the name argument is not passed, it will use this system's
    name.
447
448

    """
449
450
451
452
453
    if name is None:
      name = self.SysName()

    self.query = name
    self.name, self.aliases, self.ipaddrs = self.LookupHostname(name)
454
455
    self.ip = self.ipaddrs[0]

456
457
458
459
460
461
  def ShortName(self):
    """Returns the hostname without domain.

    """
    return self.name.split('.')[0]

462
463
464
  @staticmethod
  def SysName():
    """Return the current system's name.
465

466
    This is simply a wrapper over C{socket.gethostname()}.
Iustin Pop's avatar
Iustin Pop committed
467

468
469
    """
    return socket.gethostname()
Iustin Pop's avatar
Iustin Pop committed
470

471
472
473
  @staticmethod
  def LookupHostname(hostname):
    """Look up hostname
Iustin Pop's avatar
Iustin Pop committed
474

475
476
    @type hostname: str
    @param hostname: hostname to look up
477

478
479
480
481
    @rtype: tuple
    @return: a tuple (name, aliases, ipaddrs) as returned by
        C{socket.gethostbyname_ex}
    @raise errors.ResolverError: in case of errors in resolving
482
483
484
485
486
487
488

    """
    try:
      result = socket.gethostbyname_ex(hostname)
    except socket.gaierror, err:
      # hostname not found in DNS
      raise errors.ResolverError(hostname, err.args[0], err.args[1])
Iustin Pop's avatar
Iustin Pop committed
489

490
    return result
Iustin Pop's avatar
Iustin Pop committed
491
492
493
494
495


def ListVolumeGroups():
  """List volume groups and their size

496
497
498
499
  @rtype: dict
  @return:
       Dictionary with keys volume name and values
       the size of the volume
Iustin Pop's avatar
Iustin Pop committed
500
501
502
503
504
505
506
507
508
509
510
511
512

  """
  command = "vgs --noheadings --units m --nosuffix -o name,size"
  result = RunCmd(command)
  retval = {}
  if result.failed:
    return retval

  for line in result.stdout.splitlines():
    try:
      name, size = line.split()
      size = int(float(size))
    except (IndexError, ValueError), err:
513
      logging.error("Invalid output from vgs (%s): %s", err, line)
Iustin Pop's avatar
Iustin Pop committed
514
515
516
517
518
519
520
521
522
523
      continue

    retval[name] = size

  return retval


def BridgeExists(bridge):
  """Check whether the given bridge exists in the system

524
525
526
527
  @type bridge: str
  @param bridge: the bridge name to check
  @rtype: boolean
  @return: True if it does
Iustin Pop's avatar
Iustin Pop committed
528
529
530
531
532
533
534
535

  """
  return os.path.isdir("/sys/class/net/%s/bridge" % bridge)


def NiceSort(name_list):
  """Sort a list of strings based on digit and non-digit groupings.

536
537
538
  Given a list of names C{['a1', 'a10', 'a11', 'a2']} this function
  will sort the list in the logical order C{['a1', 'a2', 'a10',
  'a11']}.
Iustin Pop's avatar
Iustin Pop committed
539
540
541
542
543

  The sort algorithm breaks each name in groups of either only-digits
  or no-digits. Only the first eight such groups are considered, and
  after that we just use what's left of the string.

544
545
546
547
  @type name_list: list
  @param name_list: the names to be sorted
  @rtype: list
  @return: a copy of the name list sorted with our algorithm
Iustin Pop's avatar
Iustin Pop committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572

  """
  _SORTER_BASE = "(\D+|\d+)"
  _SORTER_FULL = "^%s%s?%s?%s?%s?%s?%s?%s?.*$" % (_SORTER_BASE, _SORTER_BASE,
                                                  _SORTER_BASE, _SORTER_BASE,
                                                  _SORTER_BASE, _SORTER_BASE,
                                                  _SORTER_BASE, _SORTER_BASE)
  _SORTER_RE = re.compile(_SORTER_FULL)
  _SORTER_NODIGIT = re.compile("^\D*$")
  def _TryInt(val):
    """Attempts to convert a variable to integer."""
    if val is None or _SORTER_NODIGIT.match(val):
      return val
    rval = int(val)
    return rval

  to_sort = [([_TryInt(grp) for grp in _SORTER_RE.match(name).groups()], name)
             for name in name_list]
  to_sort.sort()
  return [tup[1] for tup in to_sort]


def TryConvert(fn, val):
  """Try to convert a value ignoring errors.

573
574
575
576
577
578
579
580
581
582
  This function tries to apply function I{fn} to I{val}. If no
  C{ValueError} or C{TypeError} exceptions are raised, it will return
  the result, else it will return the original value. Any other
  exceptions are propagated to the caller.

  @type fn: callable
  @param fn: function to apply to the value
  @param val: the value to be converted
  @return: The converted value if the conversion was successful,
      otherwise the original value.
Iustin Pop's avatar
Iustin Pop committed
583
584
585
586
587
588
589
590
591
592

  """
  try:
    nv = fn(val)
  except (ValueError, TypeError), err:
    nv = val
  return nv


def IsValidIP(ip):
593
  """Verifies the syntax of an IPv4 address.
Iustin Pop's avatar
Iustin Pop committed
594

595
596
597
598
599
600
601
602
  This function checks if the IPv4 address passes is valid or not based
  on syntax (not IP range, class calculations, etc.).

  @type ip: str
  @param ip: the address to be checked
  @rtype: a regular expression match object
  @return: a regular epression match object, or None if the
      address is not valid
Iustin Pop's avatar
Iustin Pop committed
603
604
605

  """
  unit = "(0|[1-9]\d{0,2})"
606
  #TODO: convert and return only boolean
Iustin Pop's avatar
Iustin Pop committed
607
608
609
610
611
612
613
614
615
616
617
618
619
  return re.match("^%s\.%s\.%s\.%s$" % (unit, unit, unit, unit), ip)


def IsValidShellParam(word):
  """Verifies is the given word is safe from the shell's p.o.v.

  This means that we can pass this to a command via the shell and be
  sure that it doesn't alter the command line and is passed as such to
  the actual command.

  Note that we are overly restrictive here, in order to be on the safe
  side.

620
621
622
623
624
  @type word: str
  @param word: the word to check
  @rtype: boolean
  @return: True if the word is 'safe'

Iustin Pop's avatar
Iustin Pop committed
625
626
627
628
629
630
631
632
633
634
635
636
  """
  return bool(re.match("^[-a-zA-Z0-9._+/:%@]+$", word))


def BuildShellCmd(template, *args):
  """Build a safe shell command line from the given arguments.

  This function will check all arguments in the args list so that they
  are valid shell parameters (i.e. they don't contain shell
  metacharaters). If everything is ok, it will return the result of
  template % args.

637
638
639
640
641
642
  @type template: str
  @param template: the string holding the template for the
      string formatting
  @rtype: str
  @return: the expanded command line

Iustin Pop's avatar
Iustin Pop committed
643
644
645
  """
  for word in args:
    if not IsValidShellParam(word):
646
647
      raise errors.ProgrammerError("Shell argument '%s' contains"
                                   " invalid characters" % word)
Iustin Pop's avatar
Iustin Pop committed
648
649
650
651
652
653
  return template % args


def FormatUnit(value):
  """Formats an incoming number of MiB with the appropriate unit.

654
655
656
657
  @type value: int
  @param value: integer representing the value in MiB (1048576)
  @rtype: str
  @return: the formatted value (with suffix)
Iustin Pop's avatar
Iustin Pop committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672

  """
  if value < 1024:
    return "%dM" % round(value, 0)

  elif value < (1024 * 1024):
    return "%0.1fG" % round(float(value) / 1024, 1)

  else:
    return "%0.1fT" % round(float(value) / 1024 / 1024, 1)


def ParseUnit(input_string):
  """Tries to extract number and scale from the given string.

673
674
675
  Input must be in the format C{NUMBER+ [DOT NUMBER+] SPACE*
  [UNIT]}. If no unit is specified, it defaults to MiB. Return value
  is always an int in MiB.
Iustin Pop's avatar
Iustin Pop committed
676
677
678
679

  """
  m = re.match('^([.\d]+)\s*([a-zA-Z]+)?$', input_string)
  if not m:
680
    raise errors.UnitParseError("Invalid format")
Iustin Pop's avatar
Iustin Pop committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

  value = float(m.groups()[0])

  unit = m.groups()[1]
  if unit:
    lcunit = unit.lower()
  else:
    lcunit = 'm'

  if lcunit in ('m', 'mb', 'mib'):
    # Value already in MiB
    pass

  elif lcunit in ('g', 'gb', 'gib'):
    value *= 1024

  elif lcunit in ('t', 'tb', 'tib'):
    value *= 1024 * 1024

  else:
701
    raise errors.UnitParseError("Unknown unit: %s" % unit)
Iustin Pop's avatar
Iustin Pop committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717

  # Make sure we round up
  if int(value) < value:
    value += 1

  # Round up to the next multiple of 4
  value = int(value)
  if value % 4:
    value += 4 - value % 4

  return value


def AddAuthorizedKey(file_name, key):
  """Adds an SSH public key to an authorized_keys file.

718
719
720
721
722
  @type file_name: str
  @param file_name: path to authorized_keys file
  @type key: str
  @param key: string containing key

Iustin Pop's avatar
Iustin Pop committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  """
  key_fields = key.split()

  f = open(file_name, 'a+')
  try:
    nl = True
    for line in f:
      # Ignore whitespace changes
      if line.split() == key_fields:
        break
      nl = line.endswith('\n')
    else:
      if not nl:
        f.write("\n")
      f.write(key.rstrip('\r\n'))
      f.write("\n")
      f.flush()
  finally:
    f.close()


def RemoveAuthorizedKey(file_name, key):
  """Removes an SSH public key from an authorized_keys file.

747
748
749
750
751
  @type file_name: str
  @param file_name: path to authorized_keys file
  @type key: str
  @param key: string containing key

Iustin Pop's avatar
Iustin Pop committed
752
753
754
755
756
  """
  key_fields = key.split()

  fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(file_name))
  try:
757
    out = os.fdopen(fd, 'w')
Iustin Pop's avatar
Iustin Pop committed
758
    try:
759
760
761
762
763
764
      f = open(file_name, 'r')
      try:
        for line in f:
          # Ignore whitespace changes while comparing lines
          if line.split() != key_fields:
            out.write(line)
765
766
767
768
769
770
771
772
773
774
775
776

        out.flush()
        os.rename(tmpname, file_name)
      finally:
        f.close()
    finally:
      out.close()
  except:
    RemoveFile(tmpname)
    raise


777
778
def SetEtcHostsEntry(file_name, ip, hostname, aliases):
  """Sets the name of an IP address and hostname in /etc/hosts.
779

780
781
782
783
784
785
786
787
788
  @type file_name: str
  @param file_name: path to the file to modify (usually C{/etc/hosts})
  @type ip: str
  @param ip: the IP address
  @type hostname: str
  @param hostname: the hostname to be added
  @type aliases: list
  @param aliases: the list of aliases to add for the hostname

789
  """
790
791
792
  # Ensure aliases are unique
  aliases = UniqueSequence([hostname] + aliases)[1:]

793
  fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(file_name))
794
  try:
795
796
797
798
799
800
801
    out = os.fdopen(fd, 'w')
    try:
      f = open(file_name, 'r')
      try:
        written = False
        for line in f:
          fields = line.split()
802
          if fields and not fields[0].startswith('#') and ip == fields[0]:
803
804
805
            continue
          out.write(line)

806
        out.write("%s\t%s" % (ip, hostname))
807
808
809
810
811
        if aliases:
          out.write(" %s" % ' '.join(aliases))
        out.write('\n')

        out.flush()
812
        os.fsync(out)
813
814
815
816
817
818
819
820
        os.rename(tmpname, file_name)
      finally:
        f.close()
    finally:
      out.close()
  except:
    RemoveFile(tmpname)
    raise
821
822


823
824
825
def AddHostToEtcHosts(hostname):
  """Wrapper around SetEtcHostsEntry.

826
827
828
829
  @type hostname: str
  @param hostname: a hostname that will be resolved and added to
      L{constants.ETC_HOSTS}

830
831
832
833
834
  """
  hi = HostInfo(name=hostname)
  SetEtcHostsEntry(constants.ETC_HOSTS, hi.ip, hi.name, [hi.ShortName()])


835
def RemoveEtcHostsEntry(file_name, hostname):
836
  """Removes a hostname from /etc/hosts.
837

838
  IP addresses without names are removed from the file.
839
840
841
842
843
844

  @type file_name: str
  @param file_name: path to the file to modify (usually C{/etc/hosts})
  @type hostname: str
  @param hostname: the hostname to be removed

845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
  """
  fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(file_name))
  try:
    out = os.fdopen(fd, 'w')
    try:
      f = open(file_name, 'r')
      try:
        for line in f:
          fields = line.split()
          if len(fields) > 1 and not fields[0].startswith('#'):
            names = fields[1:]
            if hostname in names:
              while hostname in names:
                names.remove(hostname)
              if names:
860
                out.write("%s %s\n" % (fields[0], ' '.join(names)))
861
862
863
              continue

          out.write(line)
864
865

        out.flush()
866
        os.fsync(out)
867
868
869
        os.rename(tmpname, file_name)
      finally:
        f.close()
Iustin Pop's avatar
Iustin Pop committed
870
    finally:
871
872
873
874
      out.close()
  except:
    RemoveFile(tmpname)
    raise
Iustin Pop's avatar
Iustin Pop committed
875
876


877
878
879
def RemoveHostFromEtcHosts(hostname):
  """Wrapper around RemoveEtcHostsEntry.

880
881
882
883
884
  @type hostname: str
  @param hostname: hostname that will be resolved and its
      full and shot name will be removed from
      L{constants.ETC_HOSTS}

885
886
887
888
889
890
  """
  hi = HostInfo(name=hostname)
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.name)
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName())


Iustin Pop's avatar
Iustin Pop committed
891
892
893
def CreateBackup(file_name):
  """Creates a backup of a file.

894
895
896
897
898
  @type file_name: str
  @param file_name: file to be backed up
  @rtype: str
  @return: the path to the newly created backup
  @raise errors.ProgrammerError: for invalid file names
Iustin Pop's avatar
Iustin Pop committed
899
900
901

  """
  if not os.path.isfile(file_name):
902
903
    raise errors.ProgrammerError("Can't make a backup of a non-file '%s'" %
                                file_name)
Iustin Pop's avatar
Iustin Pop committed
904

905
  prefix = '%s.backup-%d.' % (os.path.basename(file_name), int(time.time()))
Iustin Pop's avatar
Iustin Pop committed
906
  dir_name = os.path.dirname(file_name)
907
908
909

  fsrc = open(file_name, 'rb')
  try:
Iustin Pop's avatar
Iustin Pop committed
910
    (fd, backup_name) = tempfile.mkstemp(prefix=prefix, dir=dir_name)
911
912
913
914
915
916
917
918
    fdst = os.fdopen(fd, 'wb')
    try:
      shutil.copyfileobj(fsrc, fdst)
    finally:
      fdst.close()
  finally:
    fsrc.close()

Iustin Pop's avatar
Iustin Pop committed
919
920
921
922
923
  return backup_name


def ShellQuote(value):
  """Quotes shell argument according to POSIX.
924

925
926
927
928
929
  @type value: str
  @param value: the argument to be quoted
  @rtype: str
  @return: the quoted value

Iustin Pop's avatar
Iustin Pop committed
930
931
932
933
934
935
936
937
  """
  if _re_shell_unquoted.match(value):
    return value
  else:
    return "'%s'" % value.replace("'", "'\\''")


def ShellQuoteArgs(args):
938
939
940
941
942
943
  """Quotes a list of shell arguments.

  @type args: list
  @param args: list of arguments to be quoted
  @rtype: str
  @return: the quoted arguments concatenaned with spaces
Iustin Pop's avatar
Iustin Pop committed
944
945
946

  """
  return ' '.join([ShellQuote(i) for i in args])
947
948


949
def TcpPing(target, port, timeout=10, live_port_needed=False, source=None):
950
951
  """Simple ping implementation using TCP connect(2).

952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  Check if the given IP is reachable by doing attempting a TCP connect
  to it.

  @type target: str
  @param target: the IP or hostname to ping
  @type port: int
  @param port: the port to connect to
  @type timeout: int
  @param timeout: the timeout on the connection attemp
  @type live_port_needed: boolean
  @param live_port_needed: whether a closed port will cause the
      function to return failure, as if there was a timeout
  @type source: str or None
  @param source: if specified, will cause the connect to be made
      from this specific source address; failures to bind other
      than C{EADDRNOTAVAIL} will be ignored
968
969
970
971
972
973

  """
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  sucess = False

974
975
976
977
978
979
  if source is not None:
    try:
      sock.bind((source, 0))
    except socket.error, (errcode, errstring):
      if errcode == errno.EADDRNOTAVAIL:
        success = False
980
981
982
983
984
985
986
987
988
989

  sock.settimeout(timeout)

  try:
    sock.connect((target, port))
    sock.close()
    success = True
  except socket.timeout:
    success = False
  except socket.error, (errcode, errstring):
990
    success = (not live_port_needed) and (errcode == errno.ECONNREFUSED)
991
992

  return success
993
994


995
996
997
def OwnIpAddress(address):
  """Check if the current host has the the given IP address.

998
  Currently this is done by TCP-pinging the address from the loopback
999
1000
1001
1002
1003
  address.

  @type address: string
  @param address: the addres to check
  @rtype: bool
1004
  @return: True if we own the address
1005
1006
1007
1008
1009
1010

  """
  return TcpPing(address, constants.DEFAULT_NODED_PORT,
                 source=constants.LOCALHOST_IP_ADDRESS)


1011
def ListVisibleFiles(path):
1012
1013
1014
1015
1016
1017
  """Returns a list of visible files in a directory.

  @type path: str
  @param path: the directory to enumerate
  @rtype: list
  @return: the list of all files not starting with a dot
1018
1019

  """
1020
1021
1022
  files = [i for i in os.listdir(path) if not i.startswith(".")]
  files.sort()
  return files
1023
1024


1025
1026
1027
1028
1029
1030
def GetHomeDir(user, default=None):
  """Try to get the homedir of the given user.

  The user can be passed either as a string (denoting the name) or as
  an integer (denoting the user id). If the user is not found, the
  'default' argument is returned, which defaults to None.
1031
1032
1033

  """
  try:
1034
1035
1036
1037
1038
1039
1040
    if isinstance(user, basestring):
      result = pwd.getpwnam(user)
    elif isinstance(user, (int, long)):
      result = pwd.getpwuid(user)
    else:
      raise errors.ProgrammerError("Invalid type passed to GetHomeDir (%s)" %
                                   type(user))
1041
1042
1043
  except KeyError:
    return default
  return result.pw_dir
1044
1045


1046
def NewUUID():
1047
1048
  """Returns a random UUID.

1049
1050
1051
1052
  @note: This is a Linux-specific method as it uses the /proc
      filesystem.
  @rtype: str

1053
1054
1055
1056
1057
1058
  """
  f = open("/proc/sys/kernel/random/uuid", "r")
  try:
    return f.read(128).rstrip("\n")
  finally:
    f.close()
Iustin Pop's avatar
Iustin Pop committed
1059
1060


1061
1062
1063
1064
1065
1066
def GenerateSecret():
  """Generates a random secret.

  This will generate a pseudo-random secret, and return its sha digest
  (so that it can be used where an ASCII string is needed).

1067
1068
1069
  @rtype: str
  @return: a sha1 hexdigest of a block of 64 random bytes

1070
1071
1072
1073
  """
  return sha.new(os.urandom(64)).hexdigest()


1074
1075
1076
1077
1078
def ReadFile(file_name, size=None):
  """Reads a file.

  @type size: None or int
  @param size: Read at most size bytes
1079
1080
  @rtype: str
  @return: the (possibly partial) conent of the file
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092

  """
  f = open(file_name, "r")
  try:
    if size is None:
      return f.read()
    else:
      return f.read(size)
  finally:
    f.close()


Iustin Pop's avatar
Iustin Pop committed
1093
1094
def WriteFile(file_name, fn=None, data=None,
              mode=None, uid=-1, gid=-1,
1095
              atime=None, mtime=None, close=True,
1096
              dry_run=False, backup=False,
1097
              prewrite=None, postwrite=None):
Iustin Pop's avatar
Iustin Pop committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
  """(Over)write a file atomically.

  The file_name and either fn (a function taking one argument, the
  file descriptor, and which should write the data to it) or data (the
  contents of the file) must be passed. The other arguments are
  optional and allow setting the file mode, owner and group, and the
  mtime/atime of the file.

  If the function doesn't raise an exception, it has succeeded and the
  target file has the new contents. If the file has raised an
  exception, an existing target file should be unmodified and the
  temporary file should be removed.

1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
  @type file_name: str
  @param file_name: the target filename
  @type fn: callable
  @param fn: content writing function, called with
      file descriptor as parameter
  @type data: sr
  @param data: contents of the file
  @type mode: int
  @param mode: file mode
  @type uid: int
  @param uid: the owner of the file
  @type gid: int
  @param gid: the group of the file
  @type atime: int
  @param atime: a custom access time to be set on the file
  @type mtime: int
  @param mtime: a custom modification time to be set on the file
  @type close: boolean
  @param close: whether to close file after writing it
  @type prewrite: callable
  @param prewrite: function to be called before writing content
  @type postwrite: callable
  @param postwrite: function to be called after writing content

  @rtype: None or int
  @return: None if the 'close' parameter evaluates to True,
      otherwise the file descriptor

  @raise errors.ProgrammerError: if an of the arguments are not valid
1140

Iustin Pop's avatar
Iustin Pop committed
1141
  """
1142
  if not os.path.isabs(file_name):
Iustin Pop's avatar
Iustin Pop committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
    raise errors.ProgrammerError("Path passed to WriteFile is not"
                                 " absolute: '%s'" % file_name)

  if [fn, data].count(None) != 1:
    raise errors.ProgrammerError("fn or data required")

  if [atime, mtime].count(None) == 1:
    raise errors.ProgrammerError("Both atime and mtime must be either"
                                 " set or None")

Michael Hanselmann's avatar
Michael Hanselmann committed
1153
1154
  if backup and not dry_run and os.path.isfile(file_name):
    CreateBackup(file_name)
Iustin Pop's avatar
Iustin Pop committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

  dir_name, base_name = os.path.split(file_name)
  fd, new_name = tempfile.mkstemp('.new', base_name, dir_name)
  # here we need to make sure we remove the temp file, if any error
  # leaves it in place
  try:
    if uid != -1 or gid != -1:
      os.chown(new_name, uid, gid)
    if mode:
      os.chmod(new_name, mode)
1165
1166
    if callable(prewrite):
      prewrite(fd)
Iustin Pop's avatar
Iustin Pop committed
1167
1168
1169
1170
    if data is not None:
      os.write(fd, data)
    else:
      fn(fd)
1171
1172
    if callable(postwrite):
      postwrite(fd)
Iustin Pop's avatar
Iustin Pop committed
1173
1174
1175
    os.fsync(fd)
    if atime is not None and mtime is not None:
      os.utime(new_name, (atime, mtime))
Michael Hanselmann's avatar
Michael Hanselmann committed
1176
1177
    if not dry_run:
      os.rename(new_name, file_name)
Iustin Pop's avatar
Iustin Pop committed
1178
  finally:
1179
1180
1181
1182
1183
    if close:
      os.close(fd)
      result = None
    else:
      result = fd
Iustin Pop's avatar
Iustin Pop committed
1184
    RemoveFile(new_name)
Guido Trotter's avatar
Guido Trotter committed
1185

1186
1187
  return result

Guido Trotter's avatar
Guido Trotter committed
1188

1189
1190
1191
1192
1193
1194
1195
1196
def FirstFree(seq, base=0):
  """Returns the first non-existing integer from seq.

  The seq argument should be a sorted list of positive integers. The
  first time the index of an element is smaller than the element
  value, the index will be returned.

  The base argument is used to start at a different offset,
1197
1198
1199
  i.e. C{[3, 4, 6]} with I{offset=3} will return 5.

  Example: C{[0, 1, 3]} will return I{2}.
1200

1201
1202
1203
1204
1205
1206
  @type seq: sequence
  @param seq: the sequence to be analyzed.
  @type base: int
  @param base: use this value as the base index of the sequence
  @rtype: int
  @return: the first non-used index in the sequence
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216

  """
  for idx, elem in enumerate(seq):
    assert elem >= base, "Passed element is higher than base offset"
    if elem > idx + base:
      # idx is not used
      return idx + base
  return None


Guido Trotter's avatar
Guido Trotter committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
def all(seq, pred=bool):
  "Returns True if pred(x) is True for every element in the iterable"
  for elem in itertools.ifilterfalse(pred, seq):
    return False
  return True


def any(seq, pred=bool):
  "Returns True if pred(x) is True for at least one element in the iterable"
  for elem in itertools.ifilter(pred, seq):
    return True
  return False
1229
1230
1231
1232
1233
1234


def UniqueSequence(seq):
  """Returns a list with unique elements.

  Element order is preserved.
1235
1236
1237
1238
1239
1240

  @type seq: sequence
  @param seq: the sequence with the source elementes
  @rtype: list
  @return: list of unique elements from seq

1241
1242
1243
  """
  seen = set()
  return [i for i in seq if i not in seen and not seen.add(i)]
1244
1245
1246
1247
1248
1249
1250


def IsValidMac(mac):
  """Predicate to check if a MAC address is valid.

  Checks wether the supplied MAC address is formally correct, only
  accepts colon separated format.
1251
1252
1253
1254
1255
1256

  @type mac: str
  @param mac: the MAC to be validated
  @rtype: boolean
  @return: True is the MAC seems valid

1257
1258
1259
  """
  mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$")
  return mac_check.match(mac) is not None
1260
1261
1262
1263
1264


def TestDelay(duration):
  """Sleep for a fixed amount of time.

1265
1266
1267
1268
1269
  @type duration: float
  @param duration: the sleep duration
  @rtype: boolean
  @return: False for negative value, True otherwise

1270
1271
1272
1273
1274
  """
  if duration < 0:
    return False
  time.sleep(duration)
  return True
1275
1276


1277
def Daemonize(logfile, noclose_fds=None):
1278
1279
1280
1281
1282
  """Daemonize the current process.

  This detaches the current process from the controlling terminal and
  runs it in the background as a daemon.

1283
1284
1285
1286
1287
1288
1289
1290
  @type logfile: str
  @param logfile: the logfile to which we should redirect stdout/stderr
  @type noclose_fds: list or None
  @param noclose_fds: if given, it denotes a list of file descriptor
      that should not be closed
  @rtype: int
  @returns: the value zero

1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  """
  UMASK = 077
  WORKDIR = "/"
  # Default maximum for the number of available file descriptors.
  if 'SC_OPEN_MAX' in os.sysconf_names:
    try:
      MAXFD = os.sysconf('SC_OPEN_MAX')
      if MAXFD < 0:
        MAXFD = 1024
    except OSError:
      MAXFD = 1024
  else:
    MAXFD = 1024

  # this might fail
  pid = os.fork()
  if (pid == 0):  # The first child.
    os.setsid()
    # this might fail
    pid = os.fork() # Fork a second child.
    if (pid == 0):  # The second child.
      os.chdir(WORKDIR)
      os.umask(UMASK)
    else:
      # exit() or _exit()?  See below.
      os._exit(0) # Exit parent (the first child) of the second child.
  else:
    os._exit(0) # Exit parent of the first child.
  maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  if (maxfd == resource.RLIM_INFINITY):
    maxfd = MAXFD

  # Iterate through and close all file descriptors.
  for fd in range(0, maxfd):
1325
1326
    if noclose_fds and fd in noclose_fds:
      continue
1327
1328
1329
1330
1331
1332
1333
1334
1335
    try:
      os.close(fd)
    except OSError: # ERROR, fd wasn't open to begin with (ignored)
      pass
  os.open(logfile, os.O_RDWR|os.O_CREAT|os.O_APPEND, 0600)
  # Duplicate standard input to standard output and standard error.
  os.dup2(0, 1)     # standard output (1)
  os.dup2(0, 2)     # standard error (2)
  return 0
1336
1337


Iustin Pop's avatar
Iustin Pop committed
1338
def DaemonPidFileName(name):
1339
1340
1341
1342
1343
1344
1345
  """Compute a ganeti pid file absolute path

  @type name: str
  @param name: the daemon name
  @rtype: str
  @return: the full path to the pidfile corresponding to the given
      daemon name
1346
1347
1348
1349
1350
1351
1352
1353

  """
  return os.path.join(constants.RUN_GANETI_DIR, "%s.pid" % name)


def WritePidFile(name):
  """Write the current process pidfile.

1354
1355
1356
1357
1358
1359
  The file will be written to L{constants.RUN_GANETI_DIR}I{/name.pid}

  @type name: str
  @param name: the daemon name to use
  @raise errors.GenericError: if the pid file already exists and
      points to a live process
1360
1361
1362

  """
  pid = os.getpid()
Iustin Pop's avatar
Iustin Pop committed
1363
  pidfilename = DaemonPidFileName(name)
1364
  if IsProcessAlive(ReadPidFile(pidfilename)):
1365
    raise errors.GenericError("%s contains a live process" % pidfilename)
1366
1367
1368
1369
1370
1371
1372
1373
1374

  WriteFile(pidfilename, data="%d\n" % pid)


def RemovePidFile(name):
  """Remove the current process pidfile.

  Any errors are ignored.

1375
1376
1377
  @type name: str
  @param name: the daemon name used to derive the pidfile name

1378
1379
  """
  pid = os.getpid()
Iustin Pop's avatar
Iustin Pop committed
1380
  pidfilename = DaemonPidFileName(name)
1381
1382
1383
1384
1385
1386
1387
  # TODO: we could check here that the file contains our pid
  try:
    RemoveFile(pidfilename)
  except:
    pass


Iustin Pop's avatar
Iustin Pop committed
1388
def KillProcess(pid, signal_=signal.SIGTERM, timeout=30):
Iustin Pop's avatar
Iustin Pop committed
1389
1390
1391
1392
  """Kill a process given by its pid.

  @type pid: int
  @param pid: The PID to terminate.
Iustin Pop's avatar
Iustin Pop committed
1393
1394
  @type signal_: int
  @param signal_: The signal to send, by default SIGTERM
Iustin Pop's avatar
Iustin Pop committed
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
  @type timeout: int
  @param timeout: The timeout after which, if the process is still alive,
                  a SIGKILL will be sent. If not positive, no such checking
                  will be done

  """
  if pid <= 0:
    # kill with pid=0 == suicide
    raise errors.ProgrammerError("Invalid pid given '%s'" % pid)

  if not IsProcessAlive(pid):
    return
Iustin Pop's avatar
Iustin Pop committed
1407
  os.kill(pid, signal_)
Iustin Pop's avatar
Iustin Pop committed
1408
1409
1410
1411
1412
1413
1414
1415
1416
  if timeout <= 0:
    return
  end = time.time() + timeout
  while time.time() < end and IsProcessAlive(pid):
    time.sleep(0.1)
  if IsProcessAlive(pid):
    os.kill(pid, signal.SIGKILL)


1417
1418
1419
1420
1421
1422
def FindFile(name, search_path, test=os.path.exists):
  """Look for a filesystem object in a given path.

  This is an abstract method to search for filesystem object (files,
  dirs) under a given search path.

1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
  @type name: str
  @param name: the name to look for
  @type search_path: str
  @param search_path: location to start at
  @type test: callable
  @param test: a function taking one argument that should return True
      if the a given object is valid; the default value is
      os.path.exists, causing only existing files to be returned
  @rtype: str or None
  @return: full path to the object if found, None otherwise
1433
1434
1435
1436
1437
1438
1439

  """
  for dir_name in search_path:
    item_name = os.path.sep.join([dir_name, name])
    if test(item_name):
      return item_name
  return None
1440
1441
1442
1443
1444


def CheckVolumeGroupSize(vglist, vgname, minsize):
  """Checks if the volume group list is valid.

1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
  The function will check if a given volume group is in the list of
  volume groups and has a minimum size.

  @type vglist: dict
  @param vglist: dictionary of volume group names and their size
  @type vgname: str
  @param vgname: the volume group we should check
  @type minsize: int
  @param minsize: the minimum size we accept
  @rtype: None or str
  @return: None for success, otherwise the error message
1456
1457
1458
1459
1460
1461
1462
1463
1464

  """
  vgsize = vglist.get(vgname, None)
  if vgsize is None:
    return "volume group '%s' missing" % vgname
  elif vgsize < minsize:
    return ("volume group '%s' too small (%s MiB required, %d MiB found)" %
            (vgname, minsize, vgsize))
  return None
1465
1466


1467
def SplitTime(value):
1468
1469
  """Splits time as floating point number into a tuple.

1470
1471
1472
  @param value: Time in seconds
  @type value: int or float
  @return: Tuple containing (seconds, microseconds)
1473
1474

  """
1475
1476
1477
1478
1479
1480
1481
1482
  (seconds, microseconds) = divmod(int(value * 1000000), 1000000)

  assert 0 <= seconds, \
    "Seconds must be larger than or equal to 0, but are %s" % seconds
  assert 0 <= microseconds <= 999999, \
    "Microseconds must be 0-999999, but are %s" % microseconds

  return (int(seconds), int(microseconds))
1483
1484
1485
1486
1487


def MergeTime(timetuple):
  """Merges a tuple into time as a floating point number.

1488
  @param timetuple: Time as tuple, (seconds, microseconds)
1489
1490
1491
1492
  @type timetuple: tuple
  @return: Time as a floating point number expressed in seconds

  """
1493
  (seconds, microseconds) = timetuple
1494

1495
1496
1497
1498
  assert 0 <= seconds, \
    "Seconds must be larger than or equal to 0, but are %s" % seconds
  assert 0 <= microseconds <= 999999, \
    "Microseconds must be 0-999999, but are %s" % microseconds
1499

1500
  return float(seconds) + (float(microseconds) * 0.000001)
1501
1502


1503
1504
1505
1506
def GetNodeDaemonPort():
  """Get the node daemon port for this cluster.

  Note that this routine does not read a ganeti-specific file, but
1507
  instead uses C{socket.getservbyname} to allow pre-customization of
1508
1509
  this parameter outside of Ganeti.

1510
1511
  @rtype: int

1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
  """
  try:
    port = socket.getservbyname("ganeti-noded", "tcp")
  except socket.error:
    port = constants.DEFAULT_NODED_PORT

  return port


def GetNodeDaemonPassword():
  """Get the node password for the cluster.

1524