'DHCP files are written when manage_dhcp is set in
/etc/cobbler/settings.'

def write_dhcp_file(self):

    template_file = '/etc/cobbler/dhcp.template'
    blender_cache = {}
    try:
        f2 = open(template_file, 'r')
    except:
        raise CX((_('error    reading    template:    %s') % template_file))
    template_data = ''
    template_data = f2.read()
    f2.close()
    counter = 0
    dhcp_tags = {'default': {}}
    yaboot = '/yaboot'
    ding = {}
    ignore_macs = []
    for system in self.systems:
        if (not system.is_management_supported(cidr_ok=False)):
            continue
        profile = system.get_conceptual_parent()
        distro = profile.get_conceptual_parent()
        for (name, interface) in system.interfaces.iteritems():
            interface['gateway'] = system.gateway
            mac = interface['mac_address']
            if (interface['interface_type'] in ('bond_slave', 'bridge_slave', 'bonded_bridge_slave')):
                if (interface['interface_master'] not in system.interfaces):
                    continue
                if (system.name not in ding):
                    ding[system.name] = {interface['interface_master']: []}
                if (len(ding[system.name][interface['interface_master']]) == 0):
                    ding[system.name][interface['interface_master']].append(mac)
                else:
                    ignore_macs.append(mac)
                ip = system.interfaces[interface['interface_master']]['ip_address']
                netmask = system.interfaces[interface['interface_master']]['netmask']
                dhcp_tag = system.interfaces[interface['interface_master']]['dhcp_tag']
                host = system.interfaces[interface['interface_master']]['dns_name']
                if ((ip is None) or (ip == '')):
                    for (nam2, int2) in system.interfaces.iteritems():
                        if (nam2.startswith((interface['interface_master'] + '.')) and (int2['ip_address'] is not None) and (int2['ip_address'] != '')):
                            ip = int2['ip_address']
                            break
                interface['ip_address'] = ip
                interface['netmask'] = netmask
            else:
                ip = interface['ip_address']
                netmask = interface['netmask']
                dhcp_tag = interface['dhcp_tag']
                host = interface['dns_name']
            if (distro is not None):
                interface['distro'] = distro.to_dict()
            if ((mac is None) or (mac == '')):
                continue
            counter = (counter + 1)
            if ((host is not None) and (host != '')):
                if (name != 'eth0'):
                    interface['name'] = ('%s-%s' % (host, name))
                else:
                    interface['name'] = ('%s' % host)
            else:
                interface['name'] = ('generic%d' % counter)
            if (system.name in blender_cache):
                blended_system = blender_cache[system.name]
            else:
                blended_system = utils.blender(self.api, False, system)
                blender_cache[system.name] = blended_system
            interface['next_server'] = blended_system['next_server']
            interface['netboot_enabled'] = blended_system['netboot_enabled']
            interface['hostname'] = blended_system['hostname']
            interface['owner'] = blended_system['name']
            interface['enable_gpxe'] = blended_system['enable_gpxe']
            interface['name_servers'] = blended_system['name_servers']
            if (not self.settings.always_write_dhcp_entries):
                if ((not interface['netboot_enabled']) and interface['static']):
                    continue
            if (distro is not None):
                if distro.arch.startswith('ppc'):
                    if (blended_system['boot_loader'] == 'pxelinux'):
                        del interface['filename']
                    elif ((distro.boot_loader == 'grub2') or (blended_system['boot_loader'] == 'grub2')):
                        interface['filename'] = 'boot/grub/powerpc-ieee1275/core.elf'
                    else:
                        interface['filename'] = yaboot
            if (dhcp_tag == ''):
                dhcp_tag = blended_system['dhcp_tag']
                if (dhcp_tag == ''):
                    dhcp_tag = 'default'
            if (dhcp_tag not in dhcp_tags):
                dhcp_tags[dhcp_tag] = {mac: interface}
            else:
                dhcp_tags[dhcp_tag][mac] = interface
    for dt in dhcp_tags.keys():
        for m in dhcp_tags[dt].keys():
            if (m in ignore_macs):
                del dhcp_tags[dt][m]
    metadata = {'date': time.asctime(time.gmtime()), 'cobbler_server': ('%s:%s' % (self.settings.server, self.settings.http_port)), 'next_server': self.settings.next_server, 'yaboot': yaboot, 'dhcp_tags': dhcp_tags}
    if (self.logger is not None):
        self.logger.info(('generating    %s' % self.settings_file))
    self.templar.render(template_data, metadata, self.settings_file, None)
