# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from .utilities import bashfrom .cloudException import CloudRuntimeException, CloudInternalExceptionimport loggingimport osimport reimport subprocessclass networkConfig:class devInfo:def __init__(self, macAddr, ipAddr, netmask, gateway, type, name):self.name = nameself.macAdrr = macAddrself.ipAddr = ipAddrself.netmask = netmaskself.gateway = gatewayself.type = typeself.name = name#dhcp or staticself.method = None@staticmethoddef listNetworks():devs = os.listdir("/sys/class/net/")devs = list(filter(networkConfig.isBridge, devs))return devs@staticmethoddef getDefaultNetwork():cmd = bash("ip route show default | awk \'{print 3,ドル5ドル}\'")if not cmd.isSuccess():logging.debug("Failed to get default route")raise CloudRuntimeException("Failed to get default route")output = cmd.getStdout().strip()result = output.split(" ")if len(result) < 2:logging.debug("Output for the default route incomplete: %s. It should have been '<GATEWAY> <DEVICE>'" % output)raise CloudRuntimeException("Output for the default route incomplete")gateway = result[0]dev = result[1]pdi = networkConfig.getDevInfo(dev)logging.debug("Found default network device:%s"%pdi.name)pdi.gateway = gatewayreturn pdi@staticmethoddef createBridge(dev, brName):if not networkConfig.isBridgeSupported():logging.debug("bridge is not supported")return Falseif networkConfig.isBridgeEnslavedWithDevices(brName):logging.debug("bridge: %s has devices enslaved"%brName)return Falsecmds = ""if not networkConfig.isBridge(brName):cmds = "ip link add name %s type bridge ;"%brNamecmds += "ifconfig %s up;"%brNamecmds += "ip link set dev %s master %s"%(dev, brName)return bash(cmds).isSuccess()@staticmethoddef isBridgeEnslavedWithDevices(brName):if not networkConfig.isBridge(brName):return Falseif not os.listdir("/sys/class/net/%s/brif"%brName):return Falsereturn True@staticmethoddef isBridgeSupported():if os.path.exists("/proc/sys/net/bridge"):return Truereturn bash("modprobe -b bridge").isSuccess()@staticmethoddef isNetworkDev(devName):return os.path.exists("/sys/class/net/%s" % devName)@staticmethoddef isBridgePort(devName):return os.path.exists("/sys/class/net/%s/brport" % devName)@staticmethoddef isBridge(devName):return os.path.exists("/sys/class/net/%s/bridge" % devName)@staticmethoddef isOvsBridge(devName):cmd = bash("which ovs-vsctl")if not cmd.isSuccess():return Falsetry:return 0==subprocess.check_call(("ovs-vsctl", "br-exists", devName))except subprocess.CalledProcessError:return False@staticmethoddef getBridge(devName):bridgeName = Noneif os.path.exists("/sys/class/net/%s/brport/bridge"%devName):realPath = os.path.realpath("/sys/class/net/%s/brport/bridge"%devName)bridgeName = realPath.split("/")[-1]return bridgeName@staticmethoddef getEnslavedDev(br, brPort):if not networkConfig.isBridgeEnslavedWithDevices(br):return Nonefor dev in os.listdir("/sys/class/net/%s/brif"%br):br_port = int(open("/sys/class/net/%s/brif/%s/port_no"%(br,dev)).readline().strip("\n"), 16)if br_port == brPort:return devreturn None@staticmethoddef getDevInfo(dev):if not networkConfig.isNetworkDev(dev):logging.debug("dev: " + dev + " is not a network device")raise CloudInternalException("dev: " + dev + " is not a network device")netmask = NoneipAddr = NonemacAddr = Nonecmd = bash("ifconfig " + dev)if not cmd.isSuccess():logging.debug("Failed to get address from ifconfig")raise CloudInternalException("Failed to get network info by ifconfig %s"%dev)for line in cmd.getLines():if line.find("HWaddr") != -1:macAddr = line.split("HWaddr ")[1].strip(" ")elif line.find("inet ") != -1:m = re.search(r"addr:([^\s]+)\s*Bcast:([^\s]+)\s*Mask:([^\s]+)", line)if m is not None:ipAddr = m.group(1).strip()netmask = m.group(3).strip()if networkConfig.isBridgePort(dev):type = "brport"elif networkConfig.isBridge(dev) or networkConfig.isOvsBridge(dev):type = "bridge"else:type = "dev"return networkConfig.devInfo(macAddr, ipAddr, netmask, None, type, dev)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型