Changeset 10410 in genesis for tools


Ignore:
Timestamp:
Apr 9, 2012, 11:15:50 AM (13 years ago)
Author:
rick
Message:

dhcpd configuration for FAT node setup.

Related-To: nodefactory#121

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/gformat.py

    r10400 r10410  
    6565    'authorized_keys',
    6666    'dnsmasq.conf',
     67    'dhcpd.conf',
    6768    'rc.conf.local',
    6869    'resolv.conf',
     
    327328
    328329
    329 
    330 def netmask2subnet(netmask):
     330def cidr2netmask(netmask):
    331331  """ Given a 'netmask' return corresponding CIDR """
    332332  return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
     333
     334def get_network(addr, mask):
     335  return showaddr(parseaddr(addr) & ~((1 << (32 - int(mask))) - 1))
     336
     337
     338def generate_dhcpd_conf(datadump):
     339  """ Generate config file '/usr/local/etc/dhcpd.conf """
     340  output = generate_header()
     341  output += Template("""\
     342# option definitions common to all supported networks...
     343option domain-name "dhcp.{{ autogen_fqdn }}";
     344
     345default-lease-time 600;
     346max-lease-time 7200;
     347
     348# Use this to enble / disable dynamic dns updates globally.
     349#ddns-update-style none;
     350
     351# If this DHCP server is the official DHCP server for the local
     352# network, the authoritative directive should be uncommented.
     353authoritative;
     354
     355# Use this to send dhcp log messages to a different log file (you also
     356# have to hack syslog.conf to complete the redirection).
     357log-facility local7;
     358
     359#
     360# Interface definitions
     361#
     362
     363}
     364\n""").render(datadump)
     365
     366  for iface_key in datadump['autogen_iface_keys']:
     367    if not datadump[iface_key].has_key('comment'):
     368      datadump[iface_key]['comment'] = none
     369    output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
     370
     371    (addr, mask) = datadump[iface_key]['ip'].split('/')
     372    datadump[iface_key]['addr'] = addr
     373    datadump[iface_key]['netmask'] = cidr2netmask(mask)
     374    datadump[iface_key]['subnet'] = get_network(addr, mask)
     375    try:
     376      (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
     377    except (AttributeError, ValueError):
     378      output += "subnet %(subnet)s netmask %(netmask)s {\n  ### not autoritive\n}\n\n" % datadump[iface_key]
     379      continue
     380
     381    dhcp_part = ".".join(addr.split('.')[0:3])
     382    datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
     383    datadump[iface_key]['dhcp_stop'] =  dhcp_part + "." + dhcp_stop
     384    output += """\
     385subnet %(subnet)s netmask %(netmask)s {
     386  range %(dhcp_start)s %(dhcp_stop)s;
     387  option routers %(addr)s;
     388  option domain-name-servers %(addr)s;
     389}
     390\n""" % datadump[iface_key]
     391
     392  return output
    333393
    334394
     
    353413  for iface_key in datadump['autogen_iface_keys']:
    354414    if not datadump[iface_key].has_key('comment'):
    355       datadump[iface_key]['comment'] = None
     415      datadump[iface_key]['comment'] = none
    356416    output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
    357417
    358418    try:
    359419      (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
    360       (ip, netmask) = datadump[iface_key]['ip'].split('/')
    361       datadump[iface_key]['subnet'] = netmask2subnet(netmask)
     420      (ip, cidr) = datadump[iface_key]['ip'].split('/')
     421      datadump[iface_key]['netmask'] = cidr2netmask(cidr)
    362422    except (AttributeError, ValueError):
    363423      output += "# not autoritive\n\n"
     
    367427    datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
    368428    datadump[iface_key]['dhcp_stop'] =  dhcp_part + "." + dhcp_stop
    369     output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(subnet)s,24h\n\n" % datadump[iface_key]
     429    output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(netmask)s,24h\n\n" % datadump[iface_key]
    370430
    371431  return output
     
    701761    elif config == 'dnsmasq.conf':
    702762      output += generate_dnsmasq_conf(datadump)
     763    elif config == 'dhcpd.conf':
     764      output += generate_dhcpd_conf(datadump)
    703765    elif config == 'rc.conf.local':
    704766      output += generate_rc_conf_local(datadump)
     
    791853    for iface_key in get_interface_keys(datadump):
    792854      iface_name = datadump[iface_key]['interface'].replace(':',"-alias-")
    793       (ip, netmask) = datadump[iface_key]['ip'].split('/')
     855      (ip, cidr) = datadump[iface_key]['ip'].split('/')
    794856      try:
    795857        (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
    796         datadump[iface_key]['subnet'] = netmask2subnet(netmask)
     858        datadump[iface_key]['netmask'] = cidr2netmask(cidr)
    797859        dhcp_part = ".".join(ip.split('.')[0:3])
    798860        if ip != datadump['masterip']:
Note: See TracChangeset for help on using the changeset viewer.