示例#1
0
    def testGetIfaceCfg(self):
        deviceName = "___This_could_never_be_a_device_name___"
        ifcfg = ('GATEWAY0=1.1.1.1\n' 'NETMASK=255.255.0.0\n')
        with namedTemporaryDir() as tempDir:
            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
            filePath = ifcfgPrefix + deviceName

            with MonkeyPatchScope([(misc, 'NET_CONF_PREF', ifcfgPrefix)]):
                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg)
                self.assertEqual(
                    misc.getIfaceCfg(deviceName)['GATEWAY'], '1.1.1.1')
                self.assertEqual(
                    misc.getIfaceCfg(deviceName)['NETMASK'], '255.255.0.0')
示例#2
0
    def testGetIfaceCfg(self):
        deviceName = "___This_could_never_be_a_device_name___"
        ifcfg = ('GATEWAY0=1.1.1.1\n' 'NETMASK=255.255.0.0\n')
        with namedTemporaryDir() as tempDir:
            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
            filePath = ifcfgPrefix + deviceName

            with MonkeyPatchScope([(misc, 'NET_CONF_PREF', ifcfgPrefix)]):
                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg)
                self.assertEqual(
                    misc.getIfaceCfg(deviceName)['GATEWAY'], '1.1.1.1')
                self.assertEqual(
                    misc.getIfaceCfg(deviceName)['NETMASK'], '255.255.0.0')
示例#3
0
 def _getIfaceConfValues(iface):
     ipv4 = copy.deepcopy(iface.ipv4)
     ipv6 = copy.deepcopy(iface.ipv6)
     mtu = iface.mtu
     nameservers = iface.nameservers
     if ifaceUsed(iface.name):
         confParams = misc.getIfaceCfg(iface.name)
         if not ipv4.address and ipv4.bootproto != 'dhcp':
             ipv4.address = confParams.get('IPADDR')
             ipv4.netmask = confParams.get('NETMASK')
             ipv4.gateway = confParams.get('GATEWAY')
             if not ipv4.bootproto:
                 ipv4.bootproto = confParams.get('BOOTPROTO')
         if ipv4.defaultRoute is None and confParams.get('DEFROUTE'):
             ipv4.defaultRoute = _from_ifcfg_bool(confParams['DEFROUTE'])
         if confParams.get('IPV6INIT') == 'yes':
             ipv6.address = confParams.get('IPV6ADDR')
             ipv6.gateway = confParams.get('IPV6_DEFAULTGW')
             ipv6.ipv6autoconf = confParams.get('IPV6_AUTOCONF') == 'yes'
             ipv6.dhcpv6 = confParams.get('DHCPV6C') == 'yes'
         if not iface.mtu:
             mtu = confParams.get('MTU')
             if mtu:
                 mtu = int(mtu)
         if iface.nameservers is None:
             nameservers = [
                 confParams[key] for key in ('DNS1', 'DNS2')
                 if key in confParams
             ]
     return ipv4, ipv6, mtu, nameservers
示例#4
0
文件: ifcfg.py 项目: nirs/vdsm
 def _getIfaceConfValues(iface):
     ipv4 = copy.deepcopy(iface.ipv4)
     ipv6 = copy.deepcopy(iface.ipv6)
     mtu = iface.mtu
     nameservers = iface.nameservers
     if ifaceUsed(iface.name):
         confParams = misc.getIfaceCfg(iface.name)
         if not ipv4.address and ipv4.bootproto != "dhcp":
             ipv4.address = confParams.get("IPADDR")
             ipv4.netmask = confParams.get("NETMASK")
             ipv4.gateway = confParams.get("GATEWAY")
             if not ipv4.bootproto:
                 ipv4.bootproto = confParams.get("BOOTPROTO")
         if ipv4.defaultRoute is None and confParams.get("DEFROUTE"):
             ipv4.defaultRoute = _from_ifcfg_bool(confParams["DEFROUTE"])
         if confParams.get("IPV6INIT") == "yes":
             ipv6.address = confParams.get("IPV6ADDR")
             ipv6.gateway = confParams.get("IPV6_DEFAULTGW")
             ipv6.ipv6autoconf = confParams.get("IPV6_AUTOCONF") == "yes"
             ipv6.dhcpv6 = confParams.get("DHCPV6C") == "yes"
         if not iface.mtu:
             mtu = confParams.get("MTU")
             if mtu:
                 mtu = int(mtu)
         if iface.nameservers is None:
             nameservers = [confParams[key] for key in ("DNS1", "DNS2") if key in confParams]
     return ipv4, ipv6, mtu, nameservers
示例#5
0
文件: ifcfg.py 项目: EdDev/vdsm
 def _getIfaceConfValues(iface, net_info):
     ipv4 = copy.deepcopy(iface.ipv4)
     ipv6 = copy.deepcopy(iface.ipv6)
     mtu = iface.mtu
     nameservers = iface.nameservers
     if net_info.ifaceUsers(iface.name):
         confParams = misc.getIfaceCfg(iface.name)
         if not ipv4.address and ipv4.bootproto != 'dhcp':
             ipv4.address = confParams.get('IPADDR')
             ipv4.netmask = confParams.get('NETMASK')
             ipv4.gateway = confParams.get('GATEWAY')
             if not ipv4.bootproto:
                 ipv4.bootproto = confParams.get('BOOTPROTO')
         if ipv4.defaultRoute is None and confParams.get('DEFROUTE'):
             ipv4.defaultRoute = _from_ifcfg_bool(confParams['DEFROUTE'])
         if confParams.get('IPV6INIT') == 'yes':
             ipv6.address = confParams.get('IPV6ADDR')
             ipv6.gateway = confParams.get('IPV6_DEFAULTGW')
             ipv6.ipv6autoconf = confParams.get('IPV6_AUTOCONF') == 'yes'
             ipv6.dhcpv6 = confParams.get('DHCPV6C') == 'yes'
         if not iface.mtu:
             mtu = confParams.get('MTU')
             if mtu:
                 mtu = int(mtu)
         if iface.nameservers is None:
             nameservers = [confParams[key] for key in ('DNS1', 'DNS2')
                            if key in confParams]
     return ipv4, ipv6, mtu, nameservers
示例#6
0
    def test_get_ifcfg(self, mock_open):
        gateway = '1.1.1.1'
        netmask = '255.255.0.0'

        ifcfg = "GATEWAY0={}\nNETMASK={}\n".format(gateway, netmask)
        ifcfg_stream = six.StringIO(ifcfg)
        mock_open.return_value.__enter__.return_value = ifcfg_stream

        resulted_ifcfg = misc.getIfaceCfg('eth0')

        assert resulted_ifcfg['GATEWAY'] == gateway
        assert resulted_ifcfg['NETMASK'] == netmask
示例#7
0
文件: netinfo_test.py 项目: nirs/vdsm
    def test_get_ifcfg(self, mock_open):
        gateway = '1.1.1.1'
        netmask = '255.255.0.0'

        ifcfg = "GATEWAY0={}\nNETMASK={}\n".format(gateway, netmask)
        ifcfg_stream = six.StringIO(ifcfg)
        mock_open.return_value.__enter__.return_value = ifcfg_stream

        resulted_ifcfg = misc.getIfaceCfg('eth0')

        self.assertEqual(resulted_ifcfg['GATEWAY'], gateway)
        self.assertEqual(resulted_ifcfg['NETMASK'], netmask)
    def _processBondings(netinfo):
        bondings = {}
        for bonding, bondingParams in netinfo.bondings.iteritems():
            # If the bond is unused, skip it
            if not bondingParams['slaves']:
                continue

            bondings[bonding] = {'nics': bondingParams['slaves']}
            bondingOptions = misc.getIfaceCfg(bonding). \
                get('BONDING_OPTS')
            if bondingOptions:
                bondings[bonding]['options'] = bondingOptions

        return bondings
示例#9
0
    def _processBondings(netinfo):
        bondings = {}
        for bonding, bondingParams in netinfo.bondings.iteritems():
            # If the bond is unused, skip it
            if not bondingParams['slaves']:
                continue

            bondings[bonding] = {'nics': bondingParams['slaves']}
            bondingOptions = misc.getIfaceCfg(bonding). \
                get('BONDING_OPTS')
            if bondingOptions:
                bondings[bonding]['options'] = bondingOptions

        return bondings
示例#10
0
 def _getIfaceConfValues(iface, net_info):
     ipv4 = copy.deepcopy(iface.ipv4)
     ipv6 = copy.deepcopy(iface.ipv6)
     mtu = iface.mtu
     nameservers = iface.nameservers
     iface_users = net_info.ifaceUsers(iface.name)
     iface_users_excluding_bonds_and_bridges = (
         ifuser for ifuser in iface_users
         if (ifuser not in net_info.bridges) and (
             ifuser not in net_info.bondings))
     if iface_users:
         confParams = misc.getIfaceCfg(iface.name)
         if not iface.mtu:
             mtu = confParams.get('MTU')
             if mtu:
                 mtu = int(mtu)
         iface_master_bridge_or_bond = iface.bridge or iface.bond
         if (any(iface_users_excluding_bonds_and_bridges)
                 and not iface_master_bridge_or_bond):
             if not ipv4.address and ipv4.bootproto != 'dhcp':
                 try:
                     ipv4.address = confParams['IPADDR']
                     ipv4.netmask = confParams['NETMASK']
                     ipv4.gateway = confParams['GATEWAY']
                 except KeyError:
                     ipv4.address = ipv4.netmask = ipv4.gateway = None
                 if not ipv4.bootproto:
                     ipv4.bootproto = confParams.get('BOOTPROTO')
             if ipv4.defaultRoute is None and confParams.get('DEFROUTE'):
                 ipv4.defaultRoute = _from_ifcfg_bool(
                     confParams['DEFROUTE'])
             if confParams.get('IPV6INIT') == 'yes':
                 ipv6.address = confParams.get('IPV6ADDR')
                 ipv6.gateway = confParams.get('IPV6_DEFAULTGW')
                 ipv6.ipv6autoconf = (
                     confParams.get('IPV6_AUTOCONF') == 'yes')
                 ipv6.dhcpv6 = confParams.get('DHCPV6C') == 'yes'
             if iface.nameservers is None:
                 nameservers = [
                     confParams[key] for key in ('DNS1', 'DNS2')
                     if key in confParams
                 ]
     return ipv4, ipv6, mtu, nameservers
示例#11
0
文件: ifcfg.py 项目: oVirt/vdsm
 def _getIfaceConfValues(iface, net_info):
     ipv4 = copy.deepcopy(iface.ipv4)
     ipv6 = copy.deepcopy(iface.ipv6)
     mtu = iface.mtu
     nameservers = iface.nameservers
     iface_users = net_info.ifaceUsers(iface.name)
     iface_users_excluding_bonds_and_bridges = (
         ifuser for ifuser in iface_users
         if (ifuser not in net_info.bridges) and
            (ifuser not in net_info.bondings)
     )
     if iface_users:
         confParams = misc.getIfaceCfg(iface.name)
         if not iface.mtu:
             mtu = confParams.get('MTU')
             if mtu:
                 mtu = int(mtu)
         iface_master_bridge_or_bond = iface.bridge or iface.bond
         if (any(iface_users_excluding_bonds_and_bridges) and
                 not iface_master_bridge_or_bond):
             if not ipv4.address and ipv4.bootproto != 'dhcp':
                 try:
                     ipv4.address = confParams['IPADDR']
                     ipv4.netmask = confParams['NETMASK']
                     ipv4.gateway = confParams['GATEWAY']
                 except KeyError:
                     ipv4.address = ipv4.netmask = ipv4.gateway = None
                 if not ipv4.bootproto:
                     ipv4.bootproto = confParams.get('BOOTPROTO')
             if ipv4.defaultRoute is None and confParams.get('DEFROUTE'):
                 ipv4.defaultRoute = _from_ifcfg_bool(
                     confParams['DEFROUTE'])
             if confParams.get('IPV6INIT') == 'yes':
                 ipv6.address = confParams.get('IPV6ADDR')
                 ipv6.gateway = confParams.get('IPV6_DEFAULTGW')
                 ipv6.ipv6autoconf = (
                     confParams.get('IPV6_AUTOCONF') == 'yes')
                 ipv6.dhcpv6 = confParams.get('DHCPV6C') == 'yes'
             if iface.nameservers is None:
                 nameservers = [confParams[key] for key in ('DNS1', 'DNS2')
                                if key in confParams]
     return ipv4, ipv6, mtu, nameservers
示例#12
0
    def test_missing_ifcfg_file(self, mock_open):
        mock_open.return_value.__enter__.side_effect = IOError()

        ifcfg = misc.getIfaceCfg('eth0')

        assert ifcfg == {}
示例#13
0
文件: netinfo_test.py 项目: nirs/vdsm
    def test_missing_ifcfg_file(self, mock_open):
        mock_open.return_value.__enter__.side_effect = IOError()

        ifcfg = misc.getIfaceCfg('eth0')

        self.assertEqual(ifcfg, {})
示例#14
0
    def _processNetworks(netinfo):
        networks = {}
        defaultGateway = routes.getDefaultGateway()

        for network, netParams in netinfo.networks.iteritems():
            networks[network] = {}

            # Translate key/value pairs from netinfo to unified if key matches
            for key, value in netParams.iteritems():
                if key in NET_ATTR_WHITELIST and value != "":
                    networks[network][key] = NET_ATTR_WHITELIST[key](value)

            networks[network]['bridged'] = netParams['bridged']

            # Determine devices: nic/bond -> vlan -> bridge
            topLevelDevice = netParams['iface']
            if netParams['bridged']:
                devices = (netinfo.nics.keys() + netinfo.vlans.keys() +
                           netinfo.bondings.keys())
                nonVnicPorts = [dev for dev in netParams['ports'] if
                                dev in devices]
                # A network should only ever have (at most) an underlying
                # device hierarchy
                if nonVnicPorts:
                    physicalDevice, = nonVnicPorts
                else:
                    physicalDevice = None  # vdsm allows nicless VM nets
            else:
                physicalDevice = topLevelDevice

            # Copy ip addressing information
            bootproto = str(misc.getIfaceCfg(topLevelDevice).get('BOOTPROTO'))
            if bootproto == 'dhcp':
                networks[network]['bootproto'] = bootproto
            else:
                if netParams['addr'] != '':
                    networks[network]['ipaddr'] = netParams['addr']
                if netParams['netmask'] != '':
                    networks[network]['netmask'] = netParams['netmask']
                if netParams['gateway'] != '':
                    networks[network]['gateway'] = netParams['gateway']

            if defaultGateway is not None:
                networks[network]['defaultRoute'] = (defaultGateway.device ==
                                                     topLevelDevice)

            # What if the 'physical device' is actually a VLAN?
            if physicalDevice in netinfo.vlans:
                vlanDevice = physicalDevice
                networks[network]['vlan'] = \
                    str(netinfo.vlans[vlanDevice]['vlanid'])
                # The 'new' physical device is the VLAN's device
                physicalDevice = netinfo.vlans[vlanDevice]['iface']

            # Is the physical device a bond or a nic?
            if physicalDevice in netinfo.bondings:
                networks[network]['bonding'] = physicalDevice
            elif physicalDevice in netinfo.nics:
                networks[network]['nic'] = physicalDevice
            else:  # Nic-less networks
                pass

        return networks
示例#15
0
    def _processNetworks(netinfo):
        networks = {}
        defaultGateway = routes.getDefaultGateway()

        for network, netParams in netinfo.networks.iteritems():
            networks[network] = {}

            # Translate key/value pairs from netinfo to unified if key matches
            for key, value in netParams.iteritems():
                if key in NET_ATTR_WHITELIST and value != "":
                    networks[network][key] = NET_ATTR_WHITELIST[key](value)

            networks[network]['bridged'] = netParams['bridged']

            # Determine devices: nic/bond -> vlan -> bridge
            topLevelDevice = netParams['iface']
            if netParams['bridged']:
                devices = (netinfo.nics.keys() + netinfo.vlans.keys() +
                           netinfo.bondings.keys())
                nonVnicPorts = [
                    dev for dev in netParams['ports'] if dev in devices
                ]
                # A network should only ever have (at most) an underlying
                # device hierarchy
                if nonVnicPorts:
                    physicalDevice, = nonVnicPorts
                else:
                    physicalDevice = None  # vdsm allows nicless VM nets
            else:
                physicalDevice = topLevelDevice

            # Copy ip addressing information
            bootproto = str(misc.getIfaceCfg(topLevelDevice).get('BOOTPROTO'))
            if bootproto == 'dhcp':
                networks[network]['bootproto'] = bootproto
            else:
                if netParams['addr'] != '':
                    networks[network]['ipaddr'] = netParams['addr']
                if netParams['netmask'] != '':
                    networks[network]['netmask'] = netParams['netmask']
                if netParams['gateway'] != '':
                    networks[network]['gateway'] = netParams['gateway']

            if defaultGateway is not None:
                networks[network]['defaultRoute'] = (
                    defaultGateway.device == topLevelDevice)

            # What if the 'physical device' is actually a VLAN?
            if physicalDevice in netinfo.vlans:
                vlanDevice = physicalDevice
                networks[network]['vlan'] = \
                    str(netinfo.vlans[vlanDevice]['vlanid'])
                # The 'new' physical device is the VLAN's device
                physicalDevice = netinfo.vlans[vlanDevice]['iface']

            # Is the physical device a bond or a nic?
            if physicalDevice in netinfo.bondings:
                networks[network]['bonding'] = physicalDevice
            elif physicalDevice in netinfo.nics:
                networks[network]['nic'] = physicalDevice
            else:  # Nic-less networks
                pass

        return networks