main.py
changeset 5 86b05c0ab5cd
parent 2 e66102ab7048
equal deleted inserted replaced
4:8b633782f02d 5:86b05c0ab5cd
     1 #!/usr/bin/env python2.5
     1 #!/usr/bin/env python2.5
     2 
     2 
     3 import data, dhcp_conf, dhcp
     3 import data, dhcp, bind_conf, bind
     4 
     4 
     5 import optparse, itertools
     5 import optparse, itertools
     6 
     6 
     7 def parse_args (argv) :
     7 def parse_args (argv) :
     8     """
     8     """
    12 
    12 
    13     usage = "Usage: %prog [options] data-file"
    13     usage = "Usage: %prog [options] data-file"
    14 
    14 
    15     # define our options
    15     # define our options
    16     parser = optparse.OptionParser(usage=usage)
    16     parser = optparse.OptionParser(usage=usage)
    17     parser.add_option('--dhcpd-conf',    dest='dhcpd_conf', metavar='PATH', help="path to dhcpd.conf", default='/etc/dhcp3/dhcpd.conf')
    17     parser.add_option('--dhcpd-conf',   dest='dhcpd_conf',      metavar="PATH", help="path to dhcpd.conf", default='/etc/dhcp3/dhcpd.conf')
       
    18     parser.add_option('--bind-zone',    dest='bind_zone',       metavar="PATH", help="path to bind zone file", default=None)
       
    19     parser.add_option('--autoserial',   dest='autoserial_path', metavar="PATH", help="path to autoserial file", default='autoserial')
    18     
    20     
    19     # parse them
    21     # parse them
    20     options, args = parser.parse_args(args=argv[1:])
    22     options, args = parser.parse_args(args=argv[1:])
    21 
    23 
    22     # parse the positional arguments
    24     # parse the positional arguments
    28 def write_dhcp (options, settings) :
    30 def write_dhcp (options, settings) :
    29     """
    31     """
    30         Write the DHCP config module using the data loaded from the given module
    32         Write the DHCP config module using the data loaded from the given module
    31     """
    33     """
    32     
    34     
    33     # build the config f file
    35     # build the config file
    34     config = dhcp.Config(path=options.dhcpd_conf,
    36     config = dhcp.Config(path=options.dhcpd_conf,
    35         settings        = settings.dhcp_settings,
    37         settings        = settings.dhcp_settings,
    36         options         = settings.dhcp_options,
    38         options         = settings.dhcp_options,
    37         shared_network  = settings.shared_network,
    39         shared_network  = settings.shared_network,
    38         subnets         = settings.subnets,
    40         subnets         = settings.subnets,
    39         hosts           = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)),
    41         hosts           = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)),
    40     )
    42     )
    41 
    43 
    42     # write it out
    44     # write it out
    43     config.write()
    45     config.write()
       
    46 
       
    47 def write_bind (options, settings) :
       
    48     """
       
    49         Write a BIND config for a forward zone
       
    50     """
       
    51 
       
    52     assert options.bind_zone
       
    53 
       
    54     # load the serial
       
    55     autoserial = bind.AutoSerial(options.autoserial_path)
       
    56 
       
    57     # build the zone file
       
    58     zone = bind.Domain(domain=settings.domain, path=options.bind_zone,
       
    59             nameservers     = settings.nameservers,
       
    60             mailservers     = [((i+1)*10, label) for i, label in enumerate(settings.mailservers)],
       
    61             serial          = autoserial.serial(),
       
    62             settings        = settings.bind_settings,
       
    63             objs            = itertools.chain(*[host.build_bind_domain_records(settings.domain) for host in settings.hosts]),
       
    64     )
       
    65 
       
    66     # write it out
       
    67     zone.write()
    44 
    68 
    45 def main (argv) :
    69 def main (argv) :
    46     """
    70     """
    47         Our app entry point, parse args, load data, write out the config files
    71         Our app entry point, parse args, load data, write out the config files
    48     """
    72     """
    53     # load the data
    77     # load the data
    54     data_module = data.load_py('pvl_hosts_data', data_file)
    78     data_module = data.load_py('pvl_hosts_data', data_file)
    55     
    79     
    56     # write out the config files
    80     # write out the config files
    57     write_dhcp(options, data_module)
    81     write_dhcp(options, data_module)
       
    82     write_bind(options, data_module)
    58 
    83 
    59 if __name__ == '__main__' :
    84 if __name__ == '__main__' :
    60     from sys import argv
    85     from sys import argv
    61 
    86 
    62     main(argv)
    87     main(argv)