pvl/hosts/host.py
changeset 689 c258e3ff6d32
parent 503 a56456f901e8
child 691 760bab5a959f
equal deleted inserted replaced
688:dfc5fcb6a06c 689:c258e3ff6d32
    66     """
    66     """
    67         Normalize ethernet str.
    67         Normalize ethernet str.
    68     """
    68     """
    69 
    69 
    70     return ':'.join('%02x' % int(x, 16) for x in value.split(':'))
    70     return ':'.join('%02x' % int(x, 16) for x in value.split(':'))
       
    71 
       
    72 def parse_dhcp_boot(boot):
       
    73     """
       
    74         Parse the dhcp boot=... option
       
    75 
       
    76         >>> print parse_dhcp_boot(None)
       
    77         {}
       
    78         >>> print parse_dhcp_boot({'filename': '/foo'})
       
    79         {'filename': '/foo'}
       
    80         >>> print parse_dhcp_boot({'filename': '/foo', 'next-server': 'bar'})
       
    81         {'next-server': 'bar', 'filename': '/foo'}
       
    82         >>> print parse_dhcp_boot('/foo')
       
    83         {'filename': '/foo'}
       
    84         >>> print parse_dhcp_boot('bar:/foo')
       
    85         {'next-server': 'bar', 'filename': '/foo'}
       
    86         >>> print parse_dhcp_boot('bar:')
       
    87         {'next-server': 'bar'}
       
    88         >>> print parse_dhcp_boot('foo')
       
    89         Traceback (most recent call last):
       
    90             ...
       
    91         ValueError: invalid boot=foo
       
    92     """
       
    93 
       
    94     if not boot:
       
    95         return { }
       
    96 
       
    97     elif isinstance(boot, dict):
       
    98         if set(boot) <= set(('filename', 'next-server')):
       
    99             return boot
       
   100         else:
       
   101             raise ValueError("invalid boot={boot}".format(boot=boot))
       
   102 
       
   103     elif boot.startswith('/'):
       
   104         return {'filename': boot}
       
   105 
       
   106     elif boot.endswith(':'):
       
   107         return {'next-server': boot[:-1]}
       
   108 
       
   109     elif ':' in boot :
       
   110         next_server, filename = boot.split(':', 1)
       
   111         return {'next-server': next_server, 'filename': filename}
       
   112 
       
   113     else :
       
   114         raise ValueError("invalid boot={boot}".format(boot=boot))
    71 
   115 
    72 def parse_str(value):
   116 def parse_str(value):
    73     """
   117     """
    74         Normalize optional string value.
   118         Normalize optional string value.
    75     """
   119     """
   131                 alias4      = parse_list(alias4),
   175                 alias4      = parse_list(alias4),
   132                 alias6      = parse_list(alias6),
   176                 alias6      = parse_list(alias6),
   133                 forward     = parse_str(forward),
   177                 forward     = parse_str(forward),
   134                 reverse     = parse_str(reverse),
   178                 reverse     = parse_str(reverse),
   135                 down        = parse_bool(down),
   179                 down        = parse_bool(down),
   136                 boot        = boot,
   180                 boot        = parse_dhcp_boot(boot),
   137                 extensions  = extensions,
   181                 extensions  = extensions,
   138         )
   182         )
   139 
   183 
   140     def __init__ (self, name, domain,
   184     def __init__ (self, name, domain,
   141             ip=None, ip6=None,
   185             ip=None, ip6=None,