Revision 89e1fc26 lib/utils.py

b/lib/utils.py
397 397

  
398 398

  
399 399
class HostInfo:
400
  """Class holding host info as returned by gethostbyname
400
  """Class implementing resolver and hostname functionality
401 401

  
402 402
  """
403
  def __init__(self, name, aliases, ipaddrs):
403
  def __init__(self, name=None):
404 404
    """Initialize the host name object.
405 405

  
406
    Arguments are the same as returned by socket.gethostbyname_ex()
406
    If the name argument is not passed, it will use this system's
407
    name.
407 408

  
408 409
    """
409
    self.name = name
410
    self.aliases = aliases
411
    self.ipaddrs = ipaddrs
410
    if name is None:
411
      name = self.SysName()
412

  
413
    self.query = name
414
    self.name, self.aliases, self.ipaddrs = self.LookupHostname(name)
412 415
    self.ip = self.ipaddrs[0]
413 416

  
417
  @staticmethod
418
  def SysName():
419
    """Return the current system's name.
414 420

  
415
def LookupHostname(hostname):
416
  """Look up hostname
421
    This is simply a wrapper over socket.gethostname()
417 422

  
418
  Args:
419
    hostname: hostname to look up, can be also be a non FQDN
423
    """
424
    return socket.gethostname()
420 425

  
421
  Returns:
422
    a HostInfo object
426
  @staticmethod
427
  def LookupHostname(hostname):
428
    """Look up hostname
423 429

  
424
  """
425
  try:
426
    (name, aliases, ipaddrs) = socket.gethostbyname_ex(hostname)
427
  except socket.gaierror:
428
    # hostname not found in DNS
429
    return None
430
    Args:
431
      hostname: hostname to look up
432

  
433
    Returns:
434
      a tuple (name, aliases, ipaddrs) as returned by socket.gethostbyname_ex
435
      in case of errors in resolving, we raise a ResolverError
436

  
437
    """
438
    try:
439
      result = socket.gethostbyname_ex(hostname)
440
    except socket.gaierror, err:
441
      # hostname not found in DNS
442
      raise errors.ResolverError(hostname, err.args[0], err.args[1])
430 443

  
431
  return HostInfo(name, aliases, ipaddrs)
444
    return result
432 445

  
433 446

  
434 447
def ListVolumeGroups():

Also available in: Unified diff