bin/pvl-config
changeset 10 65d91f6a2d2a
parent 7 0f9cae2d7147
equal deleted inserted replaced
9:2156906bfbf1 10:65d91f6a2d2a
       
     1 #!/usr/bin/env python2.5
       
     2 
       
     3 import data, dhcp, bind_conf, bind
       
     4 
       
     5 import optparse, itertools
       
     6 
       
     7 def parse_args (argv) :
       
     8     """
       
     9         Parse the command-line arguments from the given argv list, returning a (options_struct, args_list) tuple,
       
    10         as per optparse.
       
    11     """
       
    12 
       
    13     usage = "Usage: %prog [options] data-file"
       
    14 
       
    15     # define our options
       
    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')
       
    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')
       
    20     
       
    21     # parse them
       
    22     options, args = parser.parse_args(args=argv[1:])
       
    23 
       
    24     # parse the positional arguments
       
    25     data_file, = args
       
    26 
       
    27     # ok
       
    28     return options, (data_file, )
       
    29 
       
    30 def write_dhcp (options, settings) :
       
    31     """
       
    32         Write the DHCP config module using the data loaded from the given module
       
    33     """
       
    34     
       
    35     # build the config file
       
    36     config = dhcp.Config(path=options.dhcpd_conf,
       
    37         settings        = settings.dhcp_settings,
       
    38         options         = settings.dhcp_options,
       
    39         shared_network  = settings.shared_network,
       
    40         subnets         = settings.subnets,
       
    41         hosts           = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)),
       
    42     )
       
    43 
       
    44     # write it out
       
    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()
       
    68 
       
    69 def main (argv) :
       
    70     """
       
    71         Our app entry point, parse args, load data, write out the config files
       
    72     """
       
    73     
       
    74     # parse args
       
    75     options, (data_file, ) = parse_args(argv)
       
    76 
       
    77     # load the data
       
    78     data_module = data.load_py('pvl_hosts_data', data_file)
       
    79     
       
    80     # write out the config files
       
    81     write_dhcp(options, data_module)
       
    82     write_bind(options, data_module)
       
    83 
       
    84 if __name__ == '__main__' :
       
    85     from sys import argv
       
    86 
       
    87     main(argv)
       
    88