本文整理汇总了Python中pyroute2.IPDB.create方法的典型用法代码示例。如果您正苦于以下问题:Python IPDB.create方法的具体用法?Python IPDB.create怎么用?Python IPDB.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPDB
的用法示例。
在下文中一共展示了IPDB.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_move_ns_pid
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_move_ns_pid(self):
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
netnsmod.create(foo)
netnsmod.create(bar)
ns_foo = IPDB(nl=NetNS(foo))
ns_bar = IPDB(nl=NetNS(bar))
try:
ns_foo.create(ifname=ifA, kind='dummy').commit()
with ns_foo.interfaces[ifA] as iface:
iface.net_ns_pid = ns_bar.nl.server.pid
assert ifA in ns_bar.interfaces.keys()
assert ifA not in ns_foo.interfaces.keys()
with ns_bar.interfaces[ifA] as iface:
iface.net_ns_pid = ns_foo.nl.server.pid
assert ifA not in ns_bar.interfaces.keys()
assert ifA in ns_foo.interfaces.keys()
finally:
ns_foo.release()
ns_bar.release()
netnsmod.remove(foo)
netnsmod.remove(bar)
示例2: test_dummy0_unloaded
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_dummy0_unloaded(object):
require_user('root')
# firstly unload the dummy module
with open(os.devnull, 'w') as fnull:
subprocess.call(['modprobe', '-r', 'dummy'],
stdout=fnull,
stderr=fnull)
ip = None
try:
# now create the dummy0 -- it will cause the
# module autoload
ip = IPDB()
# that must succeed
ip.create(ifname='dummy0', kind='dummy').commit()
# just in case: the second attempt must fail on the
# create() stage, even w/o any commit()
try:
ip.create(ifname='dummy0', kind='dummy')
except CreateException:
pass
except Exception:
raise
finally:
if ip is not None:
ip.release()
示例3: test_commit_barrier
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_commit_barrier(self):
require_user("root")
ifname = uifname()
# barrier 0
try:
ip = IPDB()
config.commit_barrier = 0
ts1 = time.time()
ip.create(ifname=ifname, kind="dummy").commit()
ts2 = time.time()
assert 0 < (ts2 - ts1) < 1
except:
raise
finally:
config.commit_barrier = 0.2
ip.interfaces[ifname].remove().commit()
ip.release()
# barrier 5
try:
ip = IPDB()
config.commit_barrier = 5
ts1 = time.time()
ip.create(ifname=ifname, kind="dummy").commit()
ts2 = time.time()
assert 5 < (ts2 - ts1) < 6
except:
raise
finally:
config.commit_barrier = 0.2
ip.interfaces[ifname].remove().commit()
ip.release()
示例4: TestDirect
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
class TestDirect(object):
def setup(self):
self.ifname = uifname()
self.ip = IPDB(mode='direct')
try:
self.ip.create(ifname=self.ifname, kind='dummy')
except:
pass
def teardown(self):
try:
self.ip.interfaces[self.ifname].remove()
except KeyError:
pass
self.ip.release()
def test_context_fail(self):
require_user('root')
try:
with self.ip.interfaces[self.ifname] as i:
i.down()
except TypeError:
pass
def test_create(self):
require_user('root')
ifname = uifname()
assert ifname not in self.ip.interfaces
self.ip.create(ifname=ifname, kind='dummy')
assert ifname in self.ip.interfaces
self.ip.interfaces[ifname].remove()
assert ifname not in self.ip.interfaces
def test_updown(self):
require_user('root')
assert not (self.ip.interfaces[self.ifname].flags & 1)
self.ip.interfaces[self.ifname].up()
assert self.ip.interfaces[self.ifname].flags & 1
self.ip.interfaces[self.ifname].down()
assert not (self.ip.interfaces[self.ifname].flags & 1)
def test_exceptions_last(self):
try:
self.ip.interfaces.lo.last()
except TypeError:
pass
def test_exception_review(self):
try:
self.ip.interfaces.lo.review()
except TypeError:
pass
示例5: _TestDhcpClient
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
class _TestDhcpClient(object):
def setup(self):
require_user('root')
require_executable('busybox')
self.ip = IPDB()
# create internal network
self.if1 = uifname()
self.if2 = uifname()
self.ip.create(kind='veth', ifname=self.if1, peer=self.if2).commit()
# set interfaces up
with self.ip.interfaces[self.if1] as i:
i.add_ip('172.16.101.1/24')
i.up()
with self.ip.interfaces[self.if2] as i:
i.up()
# prepare configuration for udhcpd
with open('udhcpd.conf.in', 'r') as conf_in:
with open('udhcpd.conf', 'w') as conf_out:
conf_out.write('interface %s\n' % self.if1)
conf_out.write(conf_in.read())
# run busybox dhcp server on $if1
with open(os.devnull, 'w') as fnull:
subprocess.check_call(['busybox', 'udhcpd', 'udhcpd.conf'],
stdout=fnull,
stderr=fnull)
def teardown(self):
# read pid from file and kill the server
with open('udhcpd.pid', 'r') as pid_file:
pid = int(pid_file.read())
os.kill(pid, 15)
# teardown interfaces (enough to remove only master)
self.ip.interfaces[self.if1].remove().commit()
# release IPDB
self.ip.release()
# remove configuration file
os.unlink('udhcpd.conf')
# collect garbage
gc.collect()
def test_defaults(self):
msg = dhclient.action(self.if2)
assert msg['yiaddr'].startswith('172.16.101.')
assert msg['op'] == BOOTREPLY
assert msg['options']['message_type'] == DHCPACK
assert msg['options']['router'] == ['172.16.101.1']
assert msg['options']['server_id'] == '172.16.101.1'
assert msg['options']['subnet_mask'] == '255.255.255.0'
assert set(msg['options']['name_server']) ==\
set(('172.16.101.1', '172.16.101.2'))
示例6: createInterface
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def createInterface(self):
ipdb = IPDB()
ipdb.create(ifname=self.veth0, kind='veth', peer=self.veth1).commit()
with ipdb.interfaces[self.veth0] as i:
i.up()
with ipdb.interfaces[self.veth1] as i:
i.up()
i.net_ns_fd = self.nsname
with ipdb.interfaces[self.bridge_iface] as i:
i.add_port(ipdb.interfaces[self.veth0])
ipdb.release()
示例7: createService
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def createService(name, terminalName, svcId):
subprocess.call(["ovs-vsctl", "add-br", "vs-" + name])
if_svc_name = name
if_terminal_name = name + '_' + terminalName
ip_host = IPDB()
ip_host.create(ifname=if_svc_name, kind='veth', peer=if_terminal_name).commit()
with ip_host.interfaces[if_svc_name] as veth:
veth.up()
with ip_host.interfaces[if_terminal_name] as veth:
veth.up()
ip_host.release()
subprocess.call(["ovs-vsctl", "add-port", "vs-" + name, if_svc_name])
subprocess.call(["ovs-vsctl", "add-port", "br0", if_terminal_name])
subprocess.call(["ovs-vsctl", "set", "port", if_terminal_name, "tag=" + str(svcId)])
return json.dumps({ 'status' : 'created service'})
示例8: createEndpoint
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def createEndpoint(name, svcName, endpointtype):
if endpointtype == 'ns':
ip_host = IPDB()
ip_host.create(ifname=name, kind='veth', peer=name + '_' + svcName).commit()
ip_ns = IPDB(nl=NetNS(name))
with ip_host.interfaces[name] as veth:
veth.net_ns_fd = name
veth.up()
with ip_host.interfaces[name + '_' + svcName] as veth:
veth.up()
subprocess.call(["ovs-vsctl", "add-port", "vs-" + svcName, name + '_' + svcName])
ip_host.release()
ip_ns.release()
nsp = NSPopen(name, ['dhclient', '-lf', '/tmp/' + name + '.lease', name], stdout=subprocess.PIPE)
nsp.wait()
nsp.release()
if endpointtype == 'lxc':
subprocess.call(['/usr/bin/lxc-clone','template',name])
lxcUpOvsScript = '#!/bin/bash\n'
lxcUpOvsScript += 'BRIDGE="vs-'+ svcName + '"\n'
lxcUpOvsScript += 'ovs-vsctl --if-exists del-port $BRIDGE $5\n'
lxcUpOvsScript += 'ovs-vsctl add-port $BRIDGE $5\n'
f = open('/var/lib/lxc/' + name + '/ovsup.sh','w+')
f.write(lxcUpOvsScript)
f.close()
lxcDownOvsScript = '#!/bin/bash\n'
lxcDownOvsScript += 'BRIDGE="vs-'+ svcName + '"\n'
lxcDownOvsScript += 'ovs-vsctl --if-exists del-port $BRIDGE $5\n'
f = open('/var/lib/lxc/' + name + '/ovsdown.sh','w+')
f.write(lxcDownOvsScript)
f.close()
os.chmod('/var/lib/lxc/' + name + '/ovsup.sh',stat.S_IRWXU)
os.chmod('/var/lib/lxc/' + name + '/ovsdown.sh',stat.S_IRWXU)
lxcConfig = 'lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n'
lxcConfig += 'lxc.arch = x86_64\n'
lxcConfig += 'lxc.rootfs = /var/lib/lxc/' + name + '/rootfs\n'
lxcConfig += 'lxc.utsname = ' + name + '\n'
lxcConfig += 'lxc.network.type = veth\n'
lxcConfig += 'lxc.network.veth.pair = ' + name + '\n'
lxcConfig += 'lxc.network.script.up = /var/lib/lxc/' + name + '/ovsup.sh\n'
lxcConfig += 'lxc.network.script.down = /var/lib/lxc/' + name + '/ovsdown.sh\n'
lxcConfig += 'lxc.network.flags = up\n'
f = open('/var/lib/lxc/' + name + '/config','w+')
f.write(lxcConfig)
f.close()
subprocess.call(['/usr/bin/lxc-start','-d','-n',name])
pass
return json.dumps({ 'status' : 'created endpoint'})
示例9: test_create
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_create(self):
require_user('root')
nsid = str(uuid4())
ipdb_main = IPDB()
ipdb_test = IPDB(nl=NetNS(nsid))
if1 = uifname()
if2 = uifname()
# create VETH pair
ipdb_main.create(ifname=if1, kind='veth', peer=if2).commit()
# move the peer to netns
with ipdb_main.interfaces[if2] as veth:
veth.net_ns_fd = nsid
# assign addresses
with ipdb_main.interfaces[if1] as veth:
veth.add_ip('172.16.200.1/24')
veth.up()
with ipdb_test.interfaces[if2] as veth:
veth.add_ip('172.16.200.2/24')
veth.up()
# ping peer
try:
with open('/dev/null', 'w') as fnull:
subprocess.check_call(['ping', '-c', '1', '172.16.200.2'],
stdout=fnull, stderr=fnull)
ret_ping = True
except Exception:
ret_ping = False
# check ARP
time.sleep(0.5)
ret_arp = '172.16.200.1' in list(ipdb_test.interfaces[if2].neighbours)
# ret_arp = list(ipdb_test.interfaces.v0p1.neighbours)
# cleanup
ipdb_main.interfaces[if1].remove().commit()
ipdb_main.release()
ipdb_test.release()
netnsmod.remove(nsid)
assert ret_ping
assert ret_arp
assert nsid not in netnsmod.listnetns()
示例10: test_dummy0_loaded
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_dummy0_loaded(object):
require_user("root")
# assert the module is loaded
ifA = uifname()
ip = IPDB()
ip.create(ifname=ifA, kind="dummy").commit()
try:
# try to create and fail in create()
ip.create(ifname="dummy0", kind="dummy")
except CreateException:
pass
except Exception:
raise
finally:
ip.interfaces[ifA].remove().commit()
ip.release()
示例11: TestDhcpClient
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
class TestDhcpClient(object):
def setup(self):
require_user("root")
require_executable("busybox")
self.ip = IPDB()
# create internal network
self.if1 = "dh1-%i" % os.getpid()
self.if2 = "dh2-%i" % os.getpid()
self.ip.create(kind="veth", ifname=self.if1, peer=self.if2).commit()
# set interfaces up
with self.ip.interfaces[self.if1] as i:
i.add_ip("172.16.101.1/24")
i.up()
with self.ip.interfaces[self.if2] as i:
i.up()
# prepare configuration for udhcpd
with open("udhcpd.conf.in", "r") as conf_in:
with open("udhcpd.conf", "w") as conf_out:
conf_out.write("interface %s\n" % self.if1)
conf_out.write(conf_in.read())
# run busybox dhcp server on $if1
with open(os.devnull, "w") as fnull:
subprocess.check_call(["busybox", "udhcpd", "udhcpd.conf"], stdout=fnull, stderr=fnull)
def teardown(self):
# read pid from file and kill the server
with open("udhcpd.pid", "r") as pid_file:
pid = int(pid_file.read())
os.kill(pid, 15)
# teardown interfaces (enough to remove only master)
self.ip.interfaces[self.if1].remove().commit()
# release IPDB
self.ip.release()
# remove configuration file
os.unlink("udhcpd.conf")
def test_defaults(self):
msg = dhclient.action(self.if2)
assert msg["yiaddr"].startswith("172.16.101.")
assert msg["op"] == BOOTREPLY
assert msg["options"]["message_type"] == DHCPACK
assert msg["options"]["router"] == ["172.16.101.1"]
assert msg["options"]["server_id"] == "172.16.101.1"
assert msg["options"]["subnet_mask"] == "255.255.255.0"
assert set(msg["options"]["name_server"]) == set(("172.16.101.1", "172.16.101.2"))
示例12: test_rename_plus_ipv6
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_rename_plus_ipv6(self):
require_user('root')
mtu = 1280 # mtu must be >= 1280 if you plan to use IPv6
txqlen = 2000
nsid = str(uuid4())
ipdb_main = IPDB()
ipdb_test = IPDB(nl=NetNS(nsid))
if1 = uifname()
if2 = uifname()
if3 = uifname()
# create
ipdb_main.create(kind='veth',
ifname=if1,
peer=if2,
mtu=mtu,
txqlen=txqlen).commit()
# move
with ipdb_main.interfaces[if2] as veth:
veth.net_ns_fd = nsid
# set it up
with ipdb_test.interfaces[if2] as veth:
veth.add_ip('fdb3:84e5:4ff4:55e4::1/64')
veth.add_ip('fdff:ffff:ffff:ffc0::1/64')
veth.mtu = mtu
veth.txqlen = txqlen
veth.up()
veth.ifname = if3
veth = ipdb_test.interfaces.get(if3, None)
ipdb_main.release()
ipdb_test.release()
netnsmod.remove(nsid)
# check everything
assert ('fdb3:84e5:4ff4:55e4::1', 64) in veth.ipaddr
assert ('fdff:ffff:ffff:ffc0::1', 64) in veth.ipaddr
assert veth.flags & 1
assert veth.mtu == mtu
assert veth.txqlen == txqlen
示例13: create
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def create(self):
iface = self.containerName + "veth0"
ifacePeer = self.containerName + "veth1"
ip_main = IPDB()
ip_sub = IPDB(nl=NetNS(self.containerName))
ip_main.create(ifname=iface, kind="veth", peer=ifacePeer).commit()
with ip_main.interfaces[ifacePeer] as veth:
veth.net_ns_fd = self.containerName
with ip_main.interfaces[iface] as veth:
veth.up()
ip_main.release()
with ip_sub.interfaces[ifacePeer] as veth:
# if not self.containerDhcp:
if not hasattr(self, "containerDhcp"):
veth.add_ip(self.containerIp)
if hasattr(self, "containerMac"):
veth.address = self.containerMac
ip_sub.release()
ns = NetNS(self.containerName)
idx = ns.link_lookup(ifname=ifacePeer)[0]
ns.link("set", index=idx, net_ns_fs=self.containerName, ifname="eth0")
ns.link("set", index=idx, net_ns_fs=self.containerName, state="up")
if hasattr(self, "containerGateway"):
request = {"dst": "0.0.0.0/0", "gateway": self.containerGateway}
ns.route("add", **IPRouteRequest(request))
ns.close()
subprocess.call(["ovs-vsctl", "add-port", "br0", iface])
dockerControl = DockerControl(self.containerObject)
if hasattr(self, "containerDhcp"):
dhcpCmd = "dhclient eth0"
dockerControl.runCmd(dhcpCmd)
addressCmd = "ip address show dev eth0"
addressInfo = dockerControl.runCmd(addressCmd)
addressInfoList = addressInfo.splitlines()
macAddressInfo = addressInfoList[1].split()[1]
ipAddressInfo = addressInfoList[2].split()[1]
ipAddressInfoDict = dict(
{"containerName": self.containerName, "macAddress": macAddressInfo, "ipAddress": ipAddressInfo}
)
return json.dumps(ipAddressInfoDict)
示例14: test_vrouter
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
def test_vrouter(self):
require_user('root')
nsid = str(uuid.uuid4())
ns = NetNS(nsid)
ipdb = IPDB()
ipns = IPDB(nl=ns)
try:
ipdb.create(ifname='ve0p0', peer='ve0p1', kind='veth').commit()
ipdb.interfaces.ve0p1.net_ns_fd = nsid
ipdb.commit()
with ipns.interfaces.ve0p1 as i:
i.set_ifname('eth0')
i.up()
except:
raise
finally:
ipdb.interfaces.ve0p0.remove()
ipdb.commit()
ipdb.release()
ipns.release()
ns.remove()
示例15: ConfigApplier
# 需要导入模块: from pyroute2 import IPDB [as 别名]
# 或者: from pyroute2.IPDB import create [as 别名]
class ConfigApplier(object):
def __init__(self):
self.ip = IPDB()
def _setIpConfig(self, iface):
ipv4 = iface.ipv4
ipv6 = iface.ipv6
if ipv4.address or ipv6.address:
self.removeIpConfig(iface)
if ipv4.address:
with self.ip.interfaces[iface.name] as i:
i.add_ip(ipv4.address + '/' + ipv4.netmask)
if ipv4.gateway and ipv4.defaultRoute:
self.ip.routes.add({'dst': 'default',
'gateway': ipv4.gateway}).commit()
if ipv6.address:
with self.ip.interfaces[iface.name] as i:
i.add_ip(ipv6.address)
if ipv6.gateway:
self.ip.routes.add({'dst': 'default',
'gateway': ipv6.gateway}).commit()
if ipv6.ipv6autoconf is not None:
with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface.name,
'w') as ipv6_autoconf:
ipv6_autoconf.write('1' if ipv6.ipv6autoconf else '0')
def removeIpConfig(self, iface):
ipwrapper.addrFlush(iface.name)
def setIfaceMtu(self, iface, mtu):
with self.ip.interfaces[iface] as i:
i['mtu'] = int(mtu)
def setBondingMtu(self, iface, mtu):
self.setIfaceMtu(iface, mtu)
def ifup(self, iface):
with self.ip.interfaces[iface.name] as i:
i.up()
if iface.ipv4.bootproto == 'dhcp':
runDhclient(iface)
if iface.ipv6.dhcpv6:
runDhclient(iface, 6)
def ifdown(self, iface):
with self.ip.interfaces[iface.name] as i:
i.down()
dhclient = DhcpClient(iface.name)
dhclient.shutdown()
def setIfaceConfigAndUp(self, iface):
if iface.ipv4 or iface.ipv6:
self._setIpConfig(iface)
if iface.mtu:
self.setIfaceMtu(iface.name, iface.mtu)
self.ifup(iface)
def addBridge(self, bridge):
self.ip.create(kind='bridge', ifname=bridge.name).commit()
def addBridgePort(self, bridge):
with self.ip.interfaces[bridge.name] as i:
i.add_port(self.ip.interfaces[bridge.port.name])
def removeBridge(self, bridge):
with self.ip.interfaces[bridge.name] as i:
i.remove()
def removeBridgePort(self, bridge):
with self.ip.interfaces[bridge.name] as i:
i.del_port(self.ip.interfaces[bridge.port.name])
def addVlan(self, vlan):
link = self.ip.interfaces[vlan.device.name].index
self.ip.create(kind='vlan', ifname=vlan.name,
link=link, vlan_id=vlan.tag).commit()
def removeVlan(self, vlan):
with self.ip.interfaces[vlan.name] as i:
i.remove()
def addBond(self, bond):
if bond.name not in netinfo.bondings():
self.ip.create(kind='bond', ifname=bond.name).commit()
def removeBond(self, bond):
with self.ip.interfaces[bond.name] as i:
i.remove()
def addBondSlave(self, bond, slave):
self.ifdown(slave)
with self.ip.interfaces[bond.name] as i:
i.add_port(self.ip.interfaces[slave.name])
self.ifup(slave)
def removeBondSlave(self, bond, slave):
with self.ip.interfaces[bond.name] as i:
i.del_port(self.ip.interfaces[slave.name])
def addBondOptions(self, bond):
#.........这里部分代码省略.........