Home | History | Annotate | Download | only in cellular
      1 #!/usr/bin/python
      2 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 DOCS="""Print DHCP and /etc/hosts stanzas for hosts in a specified cell."""
      7 
      8 import exceptions, io, sys
      9 import labconfig_data
     10 
     11 
     12 def usage(message=''):
     13     print '%s:\n\t%s\n' % (sys.argv[0], DOCS)
     14     print '%susage: %s CELLNAME' % (message, sys.argv[0])
     15     sys.exit(1)
     16 
     17 
     18 def find_names(visitor, root):
     19     """Traverse config tree, calling visitor on dicts with 'name' field."""
     20     if type(root) == dict and 'name' in root:
     21         visitor(root)
     22     if type(root) == dict:
     23         for child in root.values():
     24             find_names(visitor, child)
     25     elif hasattr(root, '__iter__'):
     26         for entry in root:
     27             find_names(visitor, entry)
     28 
     29 
     30 class Formatter(object):
     31     def __init__(self):
     32         self.dns = io.StringIO()
     33         self.dhcp = io.StringIO()
     34 
     35     def Visit(self, d):
     36         if 'address' in d and 'name' in d:
     37             self.dns.write(u'%(address)s\t%(name)s\n' % d)
     38         else:
     39             return
     40         if 'ethernet_mac' in d:
     41             self.dhcp.write((u'host %(name)s {\n' +
     42                               '\thardware ethernet %(ethernet_mac)s;\n' +
     43                               '\tfixed-address %(address)s;\n' +
     44                               '}\n') % d)
     45 
     46 
     47 if __name__ == '__main__':
     48     if len(sys.argv) < 2:
     49         usage()
     50 
     51     [cell] = sys.argv[1:]
     52     if cell not in labconfig_data.CELLS:
     53         usage('Could not find cell %s\n' % cell)
     54 
     55     f = Formatter()
     56     find_names(f.Visit, labconfig_data.CELLS[cell])
     57 
     58     print f.dhcp.getvalue()
     59     print '\n'
     60     print f.dns.getvalue()
     61