'Create bootable CD image to be used for network installations'

def generate_netboot_iso(self, imagesdir, isolinuxdir, profiles=None, systems=None, exclude_dns=None):

    which_profiles = self.filter_systems_or_profiles(profiles, 'profile')
    which_systems = self.filter_systems_or_profiles(systems, 'system')
    isolinuxcfg = os.path.join(isolinuxdir, 'isolinux.cfg')
    cfg = open(isolinuxcfg, 'w+')
    cfg.write(self.iso_template)
    for profile in which_profiles:
        self.logger.info(('processing    profile:    %s' % profile.name))
        dist = profile.get_conceptual_parent()
        distname = self.make_shorter(dist.name)
        self.copy_boot_files(dist, isolinuxdir, distname)
        cfg.write('\n')
        cfg.write(('LABEL    %s\n' % profile.name))
        cfg.write(('        MENU    LABEL    %s\n' % profile.name))
        cfg.write(('        kernel    %s.krn\n' % distname))
        data = utils.blender(self.api, False, profile)
        if (not re.match('[a-z]+://.*', data['autoinstall'])):
            data['autoinstall'] = ('http://%s:%s/cblr/svc/op/autoinstall/profile/%s' % (data['server'], self.api.settings().http_port, profile.name))
        append_line = ('    append    initrd=%s.img' % distname)
        if (dist.breed == 'suse'):
            if (('proxy' in data) and (data['proxy'] != '')):
                append_line += ('    proxy=%s' % data['proxy'])
            if (('install' in data['kernel_options']) and (data['kernel_options']['install'] != '')):
                append_line += ('    install=%s' % data['kernel_options']['install'])
                del data['kernel_options']['install']
            else:
                append_line += ('    install=http://%s:%s/cblr/links/%s' % (data['server'], self.api.settings().http_port, dist.name))
            if (('autoyast' in data['kernel_options']) and (data['kernel_options']['autoyast'] != '')):
                append_line += ('    autoyast=%s' % data['kernel_options']['autoyast'])
                del data['kernel_options']['autoyast']
            else:
                append_line += ('    autoyast=%s' % data['autoinstall'])
        if (dist.breed == 'redhat'):
            if (('proxy' in data) and (data['proxy'] != '')):
                append_line += ('    proxy=%s    http_proxy=%s' % (data['proxy'], data['proxy']))
            append_line += ('    ks=%s' % data['autoinstall'])
        if (dist.breed in ['ubuntu', 'debian']):
            append_line += ('    auto-install/enable=true    url=%s' % data['autoinstall'])
            if (('proxy' in data) and (data['proxy'] != '')):
                append_line += ('    mirror/http/proxy=%s' % data['proxy'])
        append_line += self.add_remaining_kopts(data['kernel_options'])
        cfg.write(append_line)
    cfg.write('\nMENU    SEPARATOR\n')
    for system in which_systems:
        self.logger.info(('processing    system:    %s' % system.name))
        profile = system.get_conceptual_parent()
        dist = profile.get_conceptual_parent()
        distname = self.make_shorter(dist.name)
        self.copy_boot_files(dist, isolinuxdir, distname)
        cfg.write('\n')
        cfg.write(('LABEL    %s\n' % system.name))
        cfg.write(('        MENU    LABEL    %s\n' % system.name))
        cfg.write(('        kernel    %s.krn\n' % distname))
        data = utils.blender(self.api, False, system)
        if (not re.match('[a-z]+://.*', data['autoinstall'])):
            data['autoinstall'] = ('http://%s:%s/cblr/svc/op/autoinstall/system/%s' % (data['server'], self.api.settings().http_port, system.name))
        append_line = ('    append    initrd=%s.img' % distname)
        if (dist.breed == 'suse'):
            if (('proxy' in data) and (data['proxy'] != '')):
                append_line += ('    proxy=%s' % data['proxy'])
            if (('install' in data['kernel_options']) and (data['kernel_options']['install'] != '')):
                append_line += ('    install=%s' % data['kernel_options']['install'])
                del data['kernel_options']['install']
            else:
                append_line += ('    install=http://%s:%s/cblr/links/%s' % (data['server'], self.api.settings().http_port, dist.name))
            if (('autoyast' in data['kernel_options']) and (data['kernel_options']['autoyast'] != '')):
                append_line += ('    autoyast=%s' % data['kernel_options']['autoyast'])
                del data['kernel_options']['autoyast']
            else:
                append_line += ('    autoyast=%s' % data['autoinstall'])
        if (dist.breed == 'redhat'):
            if (('proxy' in data) and (data['proxy'] != '')):
                append_line += ('    proxy=%s    http_proxy=%s' % (data['proxy'], data['proxy']))
            append_line += ('    ks=%s' % data['autoinstall'])
        if (dist.breed in ['ubuntu', 'debian']):
            append_line += ('    auto-install/enable=true    url=%s    netcfg/disable_dhcp=true' % data['autoinstall'])
            if (('proxy' in data) and (data['proxy'] != '')):
                append_line += ('    mirror/http/proxy=%s' % data['proxy'])
            my_domain = 'local.lan'
            if (system.hostname != ''):
                my_hostname = system.hostname.split('.')[0]
                _domain = system.hostname.split('.')[1:]
                if _domain:
                    my_domain = '.'.join(_domain)
            else:
                my_hostname = system.name.split('.')[0]
                _domain = system.name.split('.')[1:]
                if _domain:
                    my_domain = '.'.join(_domain)
            append_line += ('    hostname=%s    domain=%s' % (my_hostname, my_domain))
            append_line += ('    suite=%s' % dist.os_version)
        my_int = None
        my_ip = None
        my_mask = None
        my_gw = None
        my_dns = None
        if (dist.breed in ['suse', 'redhat']):
            if (('netmask' in data['kernel_options']) and (data['kernel_options']['netmask'] != '')):
                my_mask = data['kernel_options']['netmask']
                del data['kernel_options']['netmask']
            if (('gateway' in data['kernel_options']) and (data['kernel_options']['gateway'] != '')):
                my_gw = data['kernel_options']['gateway']
                del data['kernel_options']['gateway']
        if (dist.breed == 'redhat'):
            if (('ksdevice' in data['kernel_options']) and (data['kernel_options']['ksdevice'] != '')):
                my_int = data['kernel_options']['ksdevice']
                if (my_int == 'bootif'):
                    my_int = None
                del data['kernel_options']['ksdevice']
            if (('ip' in data['kernel_options']) and (data['kernel_options']['ip'] != '')):
                my_ip = data['kernel_options']['ip']
                del data['kernel_options']['ip']
            if (('dns' in data['kernel_options']) and (data['kernel_options']['dns'] != '')):
                my_dns = data['kernel_options']['dns']
                del data['kernel_options']['dns']
        if (dist.breed == 'suse'):
            if (('netdevice' in data['kernel_options']) and (data['kernel_options']['netdevice'] != '')):
                my_int = data['kernel_options']['netdevice']
                del data['kernel_options']['netdevice']
            if (('hostip' in data['kernel_options']) and (data['kernel_options']['hostip'] != '')):
                my_ip = data['kernel_options']['hostip']
                del data['kernel_options']['hostip']
            if (('nameserver' in data['kernel_options']) and (data['kernel_options']['nameserver'] != '')):
                my_dns = data['kernel_options']['nameserver']
                del data['kernel_options']['nameserver']
        if (dist.breed in ['ubuntu', 'debian']):
            if (('netcfg/choose_interface' in data['kernel_options']) and (data['kernel_options']['netcfg/choose_interface'] != '')):
                my_int = data['kernel_options']['netcfg/choose_interface']
                del data['kernel_options']['netcfg/choose_interface']
            if (('netcfg/get_ipaddress' in data['kernel_options']) and (data['kernel_options']['netcfg/get_ipaddress'] != '')):
                my_ip = data['kernel_options']['netcfg/get_ipaddress']
                del data['kernel_options']['netcfg/get_ipaddress']
            if (('netcfg/get_netmask' in data['kernel_options']) and (data['kernel_options']['netcfg/get_netmask'] != '')):
                my_mask = data['kernel_options']['netcfg/get_netmask']
                del data['kernel_options']['netcfg/get_netmask']
            if (('netcfg/get_gateway' in data['kernel_options']) and (data['kernel_options']['netcfg/get_gateway'] != '')):
                my_gw = data['kernel_options']['netcfg/get_gateway']
                del data['kernel_options']['netcfg/get_gateway']
            if (('netcfg/get_nameservers' in data['kernel_options']) and (data['kernel_options']['netcfg/get_nameservers'] != '')):
                my_dns = data['kernel_options']['netcfg/get_nameservers']
                del data['kernel_options']['netcfg/get_nameservers']
        if (my_int is None):
            mgmt_ints = []
            mgmt_ints_multi = []
            slave_ints = []
            if (len(data['interfaces'].keys()) >= 1):
                for (iname, idata) in data['interfaces'].iteritems():
                    if (idata['management'] and (idata['interface_type'] in ['bond', 'bridge'])):
                        mgmt_ints_multi.append(iname)
                    if (idata['management'] and (idata['interface_type'] not in ['bond', 'bridge', 'bond_slave', 'bridge_slave', 'bonded_bridge_slave'])):
                        mgmt_ints.append(iname)
            if ((len(mgmt_ints_multi) == 1) and (len(mgmt_ints) == 0)):
                for (iname, idata) in data['interfaces'].iteritems():
                    if ((idata['interface_type'] in ['bond_slave', 'bridge_slave', 'bonded_bridge_slave']) and (idata['interface_master'] == mgmt_ints_multi[0])):
                        slave_ints.append(iname)
                if ('eth0' in slave_ints):
                    my_int = 'eth0'
                else:
                    my_int = slave_ints[0]
                my_ip = data[('ip_address_' + data[('interface_master_' + my_int)])]
                my_mask = data[('netmask_' + data[('interface_master_' + my_int)])]
            if ((len(mgmt_ints) == 1) and (len(mgmt_ints_multi) == 0)):
                my_int = mgmt_ints[0]
        if ((my_ip is None) and (my_int is not None)):
            intip = ('ip_address_' + my_int)
            if ((intip in data) and (data[intip] != '')):
                my_ip = data[('ip_address_' + my_int)]
        if ((my_mask is None) and (my_int is not None)):
            intmask = ('netmask_' + my_int)
            if ((intmask in data) and (data[intmask] != '')):
                my_mask = data[('netmask_' + my_int)]
        if (my_gw is None):
            if (('gateway' in data) and (data['gateway'] != '')):
                my_gw = data['gateway']
        if (my_dns is None):
            if (('name_servers' in data) and (data['name_servers'] != '')):
                my_dns = data['name_servers']
        if (my_int is not None):
            intmac = ('mac_address_' + my_int)
            if (dist.breed == 'suse'):
                if ((intmac in data) and (data[intmac] != '')):
                    append_line += ('    netdevice=%s' % data[('mac_address_' + my_int)].lower())
                else:
                    append_line += ('    netdevice=%s' % my_int)
            if (dist.breed == 'redhat'):
                if ((intmac in data) and (data[intmac] != '')):
                    append_line += ('    ksdevice=%s' % data[('mac_address_' + my_int)])
                else:
                    append_line += ('    ksdevice=%s' % my_int)
            if (dist.breed in ['ubuntu', 'debian']):
                append_line += ('    netcfg/choose_interface=%s' % my_int)
        if (my_ip is not None):
            if (dist.breed == 'suse'):
                append_line += ('    hostip=%s' % my_ip)
            if (dist.breed == 'redhat'):
                append_line += ('    ip=%s' % my_ip)
            if (dist.breed in ['ubuntu', 'debian']):
                append_line += ('    netcfg/get_ipaddress=%s' % my_ip)
        if (my_mask is not None):
            if (dist.breed in ['suse', 'redhat']):
                append_line += ('    netmask=%s' % my_mask)
            if (dist.breed in ['ubuntu', 'debian']):
                append_line += ('    netcfg/get_netmask=%s' % my_mask)
        if (my_gw is not None):
            if (dist.breed in ['suse', 'redhat']):
                append_line += ('    gateway=%s' % my_gw)
            if (dist.breed in ['ubuntu', 'debian']):
                append_line += ('    netcfg/get_gateway=%s' % my_gw)
        if ((exclude_dns is None) or (my_dns is not None)):
            if (dist.breed == 'suse'):
                if (type(my_dns) == list):
                    append_line += ('    nameserver=%s' % ','.join(my_dns))
                else:
                    append_line += ('    nameserver=%s' % my_dns)
            if (dist.breed == 'redhat'):
                if (type(my_dns) == list):
                    append_line += ('    dns=%s' % ','.join(my_dns))
                else:
                    append_line += ('    dns=%s' % my_dns)
            if (dist.breed in ['ubuntu', 'debian']):
                if (type(my_dns) == list):
                    append_line += ('    netcfg/get_nameservers=%s' % ','.join(my_dns))
                else:
                    append_line += ('    netcfg/get_nameservers=%s' % my_dns)
        append_line += self.add_remaining_kopts(data['kernel_options'])
        cfg.write(append_line)
    cfg.write('\n')
    cfg.write('MENU    END\n')
    cfg.close()
