예제 #1
0
def configureNetworking(device, config):
    if device == 'all':
        config = 'dhcp'
    config_dict = None
    try:
        if config.startswith('static:'):
            config_dict = {'gateway': None, 'dns': None, 'domain': None}
            for el in config[7:].split(';'):
                k, v = el.split('=', 1)
                config_dict[k] = v
            if 'dns' in config_dict:
                config_dict['dns'] = config_dict['dns'].split(',')
            assert 'ip' in config_dict and 'netmask' in config_dict
    except:
        pass

    nethw = netutil.scanConfiguration()
    netcfg = {}
    for i in nethw.keys():
        if (device == i or device == nethw[i].hwaddr) and config_dict:
            netcfg[i] = NetInterface(NetInterface.Static, nethw[i].hwaddr,
                                     config_dict['ip'], config_dict['netmask'],
                                     config_dict['gateway'], config_dict['dns'],
                                     config_dict['domain'])
        else:
            netcfg[i] = NetInterface(NetInterface.DHCP, nethw[i].hwaddr)

    netutil.writeNetInterfaceFiles(netcfg)
    netutil.writeResolverFile(netcfg, '/etc/resolv.conf')

    if device == 'all':
        for i in nethw.keys():
            netutil.ifup(i)
    elif device.startswith('eth'):
        if nethw.has_key(device):
            netutil.ifup(device)
    else:
        # MAC address
        matching_list = filter(lambda x: x.hwaddr == device, nethw.values())
        if len(matching_list) == 1:
            netutil.ifup(matching_list[0].name)
예제 #2
0
파일: bugtool.py 프로젝트: xtha/pxe
def configureNetworking(device, config):
    if device == 'all':
        config = 'dhcp'
    config_dict = None
    try:
        if config.startswith('static:'):
            config_dict = {'gateway': None, 'dns': None, 'domain': None}
            for el in config[7:].split(';'):
                k, v = el.split('=', 1)
                config_dict[k] = v
            if 'dns' in config_dict:
                config_dict['dns'] = config_dict['dns'].split(',')
            assert 'ip' in config_dict and 'netmask' in config_dict
    except:
        pass

    nethw = netutil.scanConfiguration()
    netcfg = {}
    for i in nethw.keys():
        if (device == i or device == nethw[i].hwaddr) and config_dict:
            netcfg[i] = NetInterface(NetInterface.Static, nethw[i].hwaddr,
                                     config_dict['ip'], config_dict['netmask'],
                                     config_dict['gateway'], config_dict['dns'],
                                     config_dict['domain'])
        else:
            netcfg[i] = NetInterface(NetInterface.DHCP, nethw[i].hwaddr)

    netutil.writeDebStyleInterfaceFile(netcfg, '/etc/network/interfaces')
    netutil.writeResolverFile(netcfg, '/etc/resolv.conf')

    if device == 'all':
        for i in nethw.keys():
            netutil.ifup(i)
    elif device.startswith('eth'):
        if nethw.has_key(device):
            netutil.ifup(device)
    else:
        # MAC address
        matching_list = filter(lambda x: x.hwaddr == device, nethw.values())
        if len(matching_list) == 1:
            netutil.ifup(matching_list[0].name)
예제 #3
0
def requireNetworking(answers,
                      defaults=None,
                      msg=None,
                      keys=['net-admin-interface', 'net-admin-configuration']):
    """ Display the correct sequence of screens to get networking
    configuration.  Bring up the network according to this configuration.
    If answers is a dictionary, set 
      answers[keys[0]] to the interface chosen, and 
      answers[keys[1]] to the interface configuration chosen, and
      answers['runtime-iface-configuration'] to current manual network config, in format (all-dhcp, manual-config).
    If defaults.has_key[keys[0]] then use defaults[keys[0]] as the default network interface.
    If defaults.has_key[keys[1]] then use defaults[keys[1]] as the default network interface configuration."""

    interface_key = keys[0]
    config_key = keys[1]

    nethw = answers['network-hardware']
    if len(nethw.keys()) == 0:
        tui.progress.OKDialog("Networking",
                              "No available ethernet device found")
        return REPEAT_STEP

    # Display a screen asking which interface to configure, then what the
    # configuration for that interface should be:
    def select_interface(answers, default, msg):
        """ Show the dialog for selecting an interface.  Sets
        answers['interface'] to the name of the interface selected (a
        string). """
        if answers.has_key('interface'):
            default = answers['interface']
        if msg == None:
            msg = "%s Setup needs network access to continue.\n\nWhich network interface would you like to configure to access your %s product repository?" % (
                version.PRODUCT_BRAND or version.PLATFORM_NAME,
                version.PRODUCT_BRAND or version.PLATFORM_NAME)
        direction, iface = select_netif(msg, nethw, True, default)
        if direction == RIGHT_FORWARDS:
            answers['reuse-networking'] = (iface == None)
            if iface:
                answers['interface'] = iface
        return direction

    def specify_configuration(answers, txt, defaults):
        """ Show the dialog for setting nic config.  Sets answers['config']
        to the configuration used.  Assumes answers['interface'] is a string
        identifying by name the interface to configure. """

        if 'reuse-networking' in answers and answers['reuse-networking']:
            return RIGHT_FORWARDS

        direction, conf = get_iface_configuration(nethw[answers['interface']],
                                                  txt,
                                                  defaults=defaults,
                                                  include_dns=True)
        if direction == RIGHT_FORWARDS:
            answers['config'] = conf
        return direction

    conf_dict = {}
    def_iface = None
    def_conf = None
    if type(defaults) == dict:
        if defaults.has_key(interface_key):
            def_iface = defaults[interface_key]
        if defaults.has_key(config_key):
            def_conf = defaults[config_key]
    if len(nethw.keys()) > 1 or netutil.networkingUp():
        seq = [
            uicontroller.Step(select_interface, args=[def_iface, msg]),
            uicontroller.Step(specify_configuration, args=[None, def_conf])
        ]
    else:
        text = "%s Setup needs network access to continue.\n\nHow should networking be configured at this time?" % (
            version.PRODUCT_BRAND or version.PLATFORM_NAME)
        conf_dict['interface'] = nethw.keys()[0]
        seq = [uicontroller.Step(specify_configuration, args=[text, def_conf])]
    direction = uicontroller.runSequence(seq, conf_dict)

    if direction == RIGHT_FORWARDS and 'config' in conf_dict:
        netutil.writeNetInterfaceFiles(
            {conf_dict['interface']: conf_dict['config']})
        netutil.writeResolverFile(
            {conf_dict['interface']: conf_dict['config']}, '/etc/resolv.conf')
        tui.progress.showMessageDialog(
            "Networking",
            "Configuring network interface, please wait...",
        )
        ifaceName = conf_dict['config'].getInterfaceName(
            conf_dict['interface'])
        netutil.ifdown(ifaceName)

        # check that we have *some* network:
        if netutil.ifup(ifaceName) != 0 or not netutil.interfaceUp(ifaceName):
            tui.progress.clearModelessDialog()
            tui.progress.OKDialog(
                "Networking",
                "The network still does not appear to be active.  Please check your settings, and try again."
            )
            direction = REPEAT_STEP
        else:
            if answers and type(answers) == dict:
                # write out results
                answers[interface_key] = conf_dict['interface']
                answers[config_key] = conf_dict['config']
                # update cache of manual configurations
                manual_config = {}
                all_dhcp = False
                if answers.has_key('runtime-iface-configuration'):
                    manual_config = answers['runtime-iface-configuration'][1]
                manual_config[conf_dict['interface']] = conf_dict['config']
                answers['runtime-iface-configuration'] = (all_dhcp,
                                                          manual_config)
            tui.progress.clearModelessDialog()

    return direction
예제 #4
0
파일: network.py 프로젝트: xtha/pxe
def requireNetworking(answers, defaults=None, msg=None, keys=['net-admin-interface', 'net-admin-configuration']):
    """ Display the correct sequence of screens to get networking
    configuration.  Bring up the network according to this configuration.
    If answers is a dictionary, set 
      answers[keys[0]] to the interface chosen, and 
      answers[keys[1]] to the interface configuration chosen, and
      answers['runtime-iface-configuration'] to current manual network config, in format (all-dhcp, manual-config).
    If defaults.has_key[keys[0]] then use defaults[keys[0]] as the default network interface.
    If defaults.has_key[keys[1]] then use defaults[keys[1]] as the default network interface configuration."""

    interface_key = keys[0]
    config_key = keys[1]

    nethw = answers['network-hardware']
    if len(nethw.keys()) == 0:
        tui.progress.OKDialog("Networking", "No available ethernet device found")
        return REPEAT_STEP

    # Display a screen asking which interface to configure, then what the 
    # configuration for that interface should be:
    def select_interface(answers, default, msg):
        """ Show the dialog for selecting an interface.  Sets
        answers['interface'] to the name of the interface selected (a
        string). """
        if answers.has_key('interface'):
            default = answers['interface']
        if msg == None:
            msg = "%s Setup needs network access to continue.\n\nWhich network interface would you like to configure to access your %s product repository?" % (version.PRODUCT_BRAND or version.PLATFORM_NAME, version.PRODUCT_BRAND or version.PLATFORM_NAME)
        direction, iface = select_netif(msg, nethw, True, default)
        if direction == RIGHT_FORWARDS:
            answers['reuse-networking'] = (iface == None)
            if iface:
                answers['interface'] = iface
        return direction

    def specify_configuration(answers, txt, defaults):
        """ Show the dialog for setting nic config.  Sets answers['config']
        to the configuration used.  Assumes answers['interface'] is a string
        identifying by name the interface to configure. """

        if 'reuse-networking' in answers and answers['reuse-networking']:
            return RIGHT_FORWARDS

        direction, conf = get_iface_configuration(nethw[answers['interface']], txt, 
                                                  defaults=defaults, include_dns=True)
        if direction == RIGHT_FORWARDS:
            answers['config'] = conf
        return direction

    conf_dict = {}
    def_iface = None
    def_conf = None
    if type(defaults) == dict:
        if defaults.has_key(interface_key):
            def_iface = defaults[interface_key]
        if defaults.has_key(config_key):
            def_conf = defaults[config_key]
    if len(nethw.keys()) > 1 or netutil.networkingUp():
        seq = [ uicontroller.Step(select_interface, args=[def_iface, msg]), 
                uicontroller.Step(specify_configuration, args=[None, def_conf]) ]
    else:
        text = "%s Setup needs network access to continue.\n\nHow should networking be configured at this time?" % (version.PRODUCT_BRAND or version.PLATFORM_NAME)
        conf_dict['interface'] = nethw.keys()[0]
        seq = [ uicontroller.Step(specify_configuration, args=[text, def_conf]) ]
    direction = uicontroller.runSequence(seq, conf_dict)

    if direction == RIGHT_FORWARDS and 'config' in conf_dict:
        netutil.writeDebStyleInterfaceFile(
            {conf_dict['interface']: conf_dict['config']},
            '/etc/network/interfaces'
            )
        netutil.writeResolverFile(
            {conf_dict['interface']: conf_dict['config']},
            '/etc/resolv.conf'
            )
        tui.progress.showMessageDialog(
            "Networking",
            "Configuring network interface, please wait...",
            )
        netutil.ifdown(conf_dict['interface'])

        # check that we have *some* network:
        if netutil.ifup(conf_dict['interface']) != 0 or not netutil.interfaceUp(conf_dict['interface']):
            tui.progress.clearModelessDialog()
            tui.progress.OKDialog("Networking", "The network still does not appear to be active.  Please check your settings, and try again.")
            direction = REPEAT_STEP
        else:
            if answers and type(answers) == dict:
                # write out results
                answers[interface_key] = conf_dict['interface']
                answers[config_key] = conf_dict['config']
                # update cache of manual configurations
                manual_config = {}
                all_dhcp = False
                if answers.has_key('runtime-iface-configuration'):
                    manual_config = answers['runtime-iface-configuration'][1]
                manual_config[conf_dict['interface']] = conf_dict['config']
                answers['runtime-iface-configuration'] = (all_dhcp, manual_config)
            tui.progress.clearModelessDialog()
        
    return direction