Ejemplo n.º 1
0
    def get_netiface_info(self, iface):
        try:
            info = self.netcfg.get(iface)
        except OSError:
            return False

        if not info or not info.has_key('family'):
            return False

        info['carrier'] = False
        info['physicalif'] = False
        info['vlanif'] = False
        info['vlan-id'] = None
        info['vlan-raw-device'] = None
        info['aliasif'] = False
        info['alias-raw-device'] = None
        info['hwtypeid'] = None
        info['options'] = None

        if network.is_linux_netdev_if(iface):
            info['carrier'] = network.is_interface_plugged(iface)
            info['flags'] = network.get_interface_flags(iface)
            info['physicalif'] = network.is_linux_phy_if(iface)
            info['dummyif'] = network.is_linux_dummy_if(iface)
            info['vlanif'] = network.is_linux_vlan_if(iface)
            info['hwtypeid'] = network.get_interface_hwtypeid(iface)

            if not info['physicalif'] and info.has_key('gateway'):
                del info['gateway']

            if not info.has_key('hwaddress'):
                info['hwaddress'] = network.get_interface_hwaddress(iface)

            if not info.has_key('mtu'):
                info['mtu'] = network.get_interface_mtu(iface)
        else:
            if network.is_alias_if(iface):
                info['aliasif'] = True
                phyifname = network.phy_name_from_alias_if(iface)
                info['alias-raw-device'] = phyifname

                if network.is_linux_netdev_if(phyifname):
                    if info.has_key('gateway'):
                        try:
                            phyinfo = self.netcfg.get(phyifname)
                            if phyinfo.get('gateway') == info['gateway']:
                                del info['gateway']
                        except OSError:
                            pass

                    info['carrier'] = network.is_interface_plugged(phyifname)
                    info['hwtypeid'] = network.get_interface_hwtypeid(phyifname)

            if not info.has_key('flags'):
                info['flags'] = None

            if not info.has_key('hwaddress'):
                info['hwaddress'] = None

            if not info.has_key('mtu'):
                info['mtu'] = None

        if info['family'] in ('inet', 'inet6'):
            inetxparsed = self.inetxparser.get(iface)
            if inetxparsed:
                info['options'] = xivo_config.unreserved_interfaces_options(inetxparsed)

                xdict = dict(inetxparsed)
                info['method'] = xdict.get('method')
                info['vlan-id'] = xdict.get('vlan-id')
                info['vlan-raw-device'] = xdict.get('vlan-raw-device')

                if 'address' not in info \
                    and 'address' in xdict \
                    and 'netmask' in xdict:
                    info['address'] = xdict.get('address')
                    info['netmask'] = xdict['netmask']

                    if 'broadcast' in xdict:
                        info['broadcast'] = xdict['broadcast']

                    if 'network' in xdict:
                        info['network'] = xdict['network']

                if info['family'] == 'inet' \
                   and not info.has_key('gateway') \
                   and info.has_key('netmask') \
                   and info.has_key('network') \
                   and xdict.has_key('gateway') \
                   and network.ipv4_in_network(network.parse_ipv4(xdict['gateway']),
                                               network.parse_ipv4(info['netmask']),
                                               network.parse_ipv4(info['network'])):
                    info['gateway'] = xdict['gateway']
        else:
            info['method'] = None

        if info['vlanif']:
            vconfig = network.get_vlan_info(iface)
            info['vlan-id'] = vconfig.get('vlan-id', info['vlan-id'])
            info['vlan-raw-device'] = vconfig.get('vlan-raw-device', info['vlan-raw-device'])

        return info
Ejemplo n.º 2
0
    def replace_virtual_eth_ipv4(self, args, options):
        """
        POST /replace_virtual_eth_ipv4

        >>> replace_virtual_eth_ipv4({'ifname': 'eth0:0',
                                      'method': 'dhcp',
                                      'auto':   True},
                                     {'ifname': 'eth0:0'})
        """
        self.args = args
        self.options = options
        phyifname = None
        phyinfo = None

        if 'ifname' not in self.options \
           or not isinstance(self.options['ifname'], basestring) \
           or not xivo_config.netif_managed(self.options['ifname']):
            raise HttpReqError(415, "invalid interface name, ifname: %r" % self.options['ifname'])
        elif not xys.validate(self.args, self.REPLACE_VIRTUAL_ETH_IPV4_SCHEMA):
            raise HttpReqError(415, "invalid arguments for command")

        info = self.get_netiface_info(self.options['ifname'])

        if info and info['physicalif']:
            raise HttpReqError(415, "invalid interface, it is a physical interface")
        elif network.is_alias_if(self.args['ifname']):
            phyifname = network.phy_name_from_alias_if(self.args['ifname'])
            phyinfo = self.get_netiface_info(phyifname)
            if not phyinfo or True not in (phyinfo['physicalif'], phyinfo['vlanif']):
                raise HttpReqError(415, "invalid interface, it is not an alias interface")
            elif self.args['method'] != 'static':
                raise HttpReqError(415, "invalid method, must be static")

            if 'vlanrawdevice' in self.args:
                del self.args['vlanrawdevice']
            if 'vlanid' in self.args:
                del self.args['vlanid']
        elif network.is_vlan_if(self.args['ifname']):
            if not 'vlanrawdevice' in self.args:
                raise HttpReqError(415, "invalid arguments for command, missing vlanrawdevice")
            if not 'vlanid' in self.args:
                raise HttpReqError(415, "invalid arguments for command, missing vlanid")

            phyifname = self.args['vlanrawdevice']
            phyinfo = self.get_netiface_info(phyifname)
            if not phyinfo or not phyinfo['physicalif']:
                raise HttpReqError(415, "invalid vlanrawdevice, it is not a physical interface")

            vconfig = network.get_vlan_info_from_ifname(self.args['ifname'])

            if 'vlan-id' not in vconfig:
                raise HttpReqError(415, "invalid vlan interface name")
            elif vconfig['vlan-id'] != int(self.args['vlanid']):
                raise HttpReqError(415, "invalid vlanid")
            elif vconfig.get('vlan-raw-device', self.args['vlanrawdevice']) != self.args['vlanrawdevice']:
                raise HttpReqError(415, "invalid vlanrawdevice")

            self.args['vlan-id'] = self.args.pop('vlanid')
            self.args['vlan-raw-device'] = self.args.pop('vlanrawdevice')
        else:
            raise HttpReqError(415, "invalid ifname argument for command")

        if phyinfo.get('type') != 'eth':
            raise HttpReqError(415, "invalid interface type")
        elif phyinfo.get('family') != 'inet':
            raise HttpReqError(415, "invalid address family")

        self.normalize_inet_options()

        if not os.access(self.CONFIG['interfaces_path'], (os.X_OK | os.W_OK)):
            raise HttpReqError(415, "path not found or not writable or not executable: %r" % self.CONFIG['interfaces_path'])
        elif not self.LOCK.acquire_read(self.CONFIG['lock_timeout']):
            raise HttpReqError(503, "unable to take LOCK for reading after %s seconds" % self.CONFIG['lock_timeout'])

        self.args['auto'] = self.args.get('auto', True)
        self.args['family'] = 'inet'

        conf = {'netIfaces':        {},
                'vlans':            {},
                'customipConfs':    {}}

        netifacesbakfile = None

        try:
            if self.CONFIG['netiface_down_cmd']:
                subprocess.call(self.CONFIG['netiface_down_cmd'].strip().split() + [self.options['ifname']])

            for iface in netifaces.interfaces():
                if self.options['ifname'] != iface:
                    conf['netIfaces'][iface] = 'reserved'

            conf['netIfaces'][self.args['ifname']] = self.args['ifname']
            conf['vlans'][self.args['ifname']] = {self.args.get('vlan-id', 0): self.args['ifname']}
            conf['customipConfs'][self.args['ifname']] = self.args

            filecontent, netifacesbakfile = self.get_interface_filecontent(conf)

            try:
                system.file_writelines_flush_sync(self.CONFIG['interfaces_file'], filecontent)

                if self.args.get('up', True) and self.CONFIG['netiface_up_cmd']:
                    subprocess.call(self.CONFIG['netiface_up_cmd'].strip().split() + [self.args['ifname']])
            except Exception, e:
                if netifacesbakfile:
                    copy2(netifacesbakfile, self.CONFIG['interfaces_file'])
                raise e.__class__(str(e))
            return True
Ejemplo n.º 3
0
    def get_netiface_info(self, iface):
        try:
            info = self.netcfg.get(iface)
        except OSError:
            return False

        if not info or not info.has_key("family"):
            return False

        info["carrier"] = False
        info["physicalif"] = False
        info["vlanif"] = False
        info["vlan-id"] = None
        info["vlan-raw-device"] = None
        info["aliasif"] = False
        info["alias-raw-device"] = None
        info["hwtypeid"] = None
        info["options"] = None

        if network.is_linux_netdev_if(iface):
            info["carrier"] = network.is_interface_plugged(iface)
            info["flags"] = network.get_interface_flags(iface)
            info["physicalif"] = network.is_linux_phy_if(iface)
            info["dummyif"] = network.is_linux_dummy_if(iface)
            info["vlanif"] = network.is_linux_vlan_if(iface)
            info["hwtypeid"] = network.get_interface_hwtypeid(iface)

            if not info["physicalif"] and info.has_key("gateway"):
                del info["gateway"]

            if not info.has_key("hwaddress"):
                info["hwaddress"] = network.get_interface_hwaddress(iface)

            if not info.has_key("mtu"):
                info["mtu"] = network.get_interface_mtu(iface)
        else:
            if network.is_alias_if(iface):
                info["aliasif"] = True
                phyifname = network.phy_name_from_alias_if(iface)
                info["alias-raw-device"] = phyifname

                if network.is_linux_netdev_if(phyifname):
                    if info.has_key("gateway"):
                        try:
                            phyinfo = self.netcfg.get(phyifname)
                            if phyinfo.get("gateway") == info["gateway"]:
                                del info["gateway"]
                        except OSError:
                            pass

                    info["carrier"] = network.is_interface_plugged(phyifname)
                    info["hwtypeid"] = network.get_interface_hwtypeid(phyifname)

            if not info.has_key("flags"):
                info["flags"] = None

            if not info.has_key("hwaddress"):
                info["hwaddress"] = None

            if not info.has_key("mtu"):
                info["mtu"] = None

        if info["family"] in ("inet", "inet6"):
            inetxparsed = self.inetxparser.get(iface)
            if inetxparsed:
                info["options"] = xivo_config.unreserved_interfaces_options(inetxparsed)

                xdict = dict(inetxparsed)
                info["method"] = xdict.get("method")
                info["vlan-id"] = xdict.get("vlan-id")
                info["vlan-raw-device"] = xdict.get("vlan-raw-device")

                if "address" not in info and "address" in xdict and "netmask" in xdict:
                    info["address"] = xdict.get("address")
                    info["netmask"] = xdict["netmask"]

                    if "broadcast" in xdict:
                        info["broadcast"] = xdict["broadcast"]

                    if "network" in xdict:
                        info["network"] = xdict["network"]

                if (
                    info["family"] == "inet"
                    and not info.has_key("gateway")
                    and info.has_key("netmask")
                    and info.has_key("network")
                    and xdict.has_key("gateway")
                    and network.ipv4_in_network(
                        network.parse_ipv4(xdict["gateway"]),
                        network.parse_ipv4(info["netmask"]),
                        network.parse_ipv4(info["network"]),
                    )
                ):
                    info["gateway"] = xdict["gateway"]
        else:
            info["method"] = None

        if info["vlanif"]:
            vconfig = network.get_vlan_info(iface)
            info["vlan-id"] = vconfig.get("vlan-id", info["vlan-id"])
            info["vlan-raw-device"] = vconfig.get("vlan-raw-device", info["vlan-raw-device"])

        return info