本文整理汇总了Python中pyroute2.netns.remove函数的典型用法代码示例。如果您正苦于以下问题:Python remove函数的具体用法?Python remove怎么用?Python remove使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了remove函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_peer_attrs
def test_create_peer_attrs(self):
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
ifB = uifname()
netnsmod.create(foo)
netnsmod.create(bar)
with IPDB(nl=NetNS(foo)) as ip:
ip.create(ifname=ifA,
kind='veth',
peer={'ifname': ifB,
'net_ns_fd': bar}).commit()
assert ifA in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
with IPDB(nl=NetNS(bar)) as ip:
assert ifA not in ip.interfaces.keys()
assert ifB in ip.interfaces.keys()
ip.interfaces[ifB].remove().commit()
assert ifA not in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
with IPDB(nl=NetNS(foo)) as ip:
assert ifA not in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
netnsmod.remove(foo)
netnsmod.remove(bar)
示例2: test_connect_netns
def test_connect_netns(self):
nsname = str(uuid.uuid4())
with self.ndb.readonly:
s = len(list(self.ndb.interfaces.summary()))
assert self.count_interfaces(nsname) == 0
assert self.count_interfaces('localhost') == s
# connect RTNL source
event = threading.Event()
self.ndb.sources.add(**{'target': nsname,
'kind': 'netns',
'netns': nsname,
'event': event})
assert event.wait(5)
with self.ndb.readonly:
s = len(list(self.ndb.interfaces.summary()))
assert self.count_interfaces(nsname) > 0
assert self.count_interfaces('localhost') < s
# disconnect the source
self.ndb.sources[nsname].close()
with self.ndb.readonly:
s = len(list(self.ndb.interfaces.summary()))
assert self.count_interfaces(nsname) == 0
assert self.count_interfaces('localhost') == s
netns.remove(nsname)
示例3: test_move_ns_fd
def test_move_ns_fd(self):
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
ifB = uifname()
netnsmod.create(foo)
netnsmod.create(bar)
with IPDB(nl=NetNS(foo)) as ip:
ip.create(ifname=ifA, kind='veth', peer=ifB).commit()
assert ifA in ip.interfaces.keys()
assert ifB in ip.interfaces.keys()
with ip.interfaces[ifB] as intf:
intf.net_ns_fd = bar
assert ifA in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
with IPDB(nl=NetNS(bar)) as ip:
assert ifA not in ip.interfaces.keys()
assert ifB in ip.interfaces.keys()
ip.interfaces[ifB].remove().commit()
assert ifA not in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
with IPDB(nl=NetNS(foo)) as ip:
assert ifA not in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
netnsmod.remove(foo)
netnsmod.remove(bar)
示例4: test_move_ns_pid
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)
示例5: setup_vnets
def setup_vnets(n_nets=3):
vnets = list()
for i in map(str, range(0, n_nets)):
try:
vns = IPDB(nl=NetNS('capdnet'+i, flags=os.O_CREAT | os.O_EXCL))
except OSError:
netns.remove('capdnet'+i)
vns = IPDB(nl=NetNS('capdnet'+i, flags=os.O_CREAT | os.O_EXCL))
try:
os.symlink(RUNDIR+'/defaults', '/etc/netns/capdnet'+i)
except OSError:
# some other day.
pass
vns.interfaces.lo.up().commit()
with ip.create(ifname='capd0peer'+i, kind='veth', peer='veth0') as dev:
dev.master = ip.interfaces['capd0'].index
dev.up()
ip.interfaces['veth0'].net_ns_fd = vns.nl.netns
ip.interfaces['veth0'].commit()
with vns.interfaces['veth0'] as dev:
dev.address = 'e0:00:00:00:00:00'
dev.add_ip('10.0.1.{}/8'.format(i)) # gonna break if > 253 threads
dev.up()
vns.routes.add({'dst': '0.0.0.0', 'mask': 0, 'gateway': '10.255.255.254'}).commit()
vnets.append(vns)
return vnets
示例6: test_multithread
def test_multithread(self):
require_user('root')
parallel_count = 5
test_count = 10
ns_names = ['testns%i' % i for i in range(parallel_count)]
success = [True]
for ns_name in ns_names:
NetNS(ns_name)
for _t in range(test_count):
threads = [
Thread(target=_ns_worker,
args=(netnsmod._get_netnspath(ns_name), i, success))
for i, ns_name in enumerate(ns_names)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
for ns_name in ns_names:
netnsmod.remove(ns_name)
assert success[0]
示例7: test_connect_netns
def test_connect_netns(self):
nsname = str(uuid.uuid4())
with self.ndb.schema.db_lock:
s = len(self.ndb.interfaces.summary()) - 1
assert self.count_interfaces(nsname) == 0
assert self.count_interfaces('localhost') == s
# connect RTNL source
event = threading.Event()
self.ndb.connect_source(nsname, NetNS(nsname), event)
assert event.wait(5)
with self.ndb.schema.db_lock:
s = len(self.ndb.interfaces.summary()) - 1
assert self.count_interfaces(nsname) > 0
assert self.count_interfaces('localhost') < s
# disconnect the source
self.ndb.disconnect_source(nsname)
with self.ndb.schema.db_lock:
s = len(self.ndb.interfaces.summary()) - 1
assert self.count_interfaces(nsname) == 0
assert self.count_interfaces('localhost') == s
netns.remove(nsname)
示例8: remove
def remove(self):
'''
Try to remove this network namespace from the system.
This call be be ran only after `NetNS.close()`, otherwise
it will fail.
'''
remove(self.netns)
示例9: remove_netns
def remove_netns(name, **kwargs):
"""Remove a network namespace.
:param name: The name of the namespace to remove
"""
try:
netns.remove(name, **kwargs)
except OSError as e:
if e.errno != errno.ENOENT:
raise
示例10: deleteService
def deleteService(data):
name = data['name']
customer = data['customer']
vr = data['virtualrouter']
svcId = data['Id']
if 'move' in data:
move = data['move']
else:
move = False
if 'delvn' in data:
delvn = data['delvn']
else:
delvn = False
print vr
print name + '_' + str(svcId)
lif = getLogicalInterface(vr, name + '_' + str(svcId))
if lif.get_virtual_machine_interface_refs() and not move:
for vmInt in lif.get_virtual_machine_interface_refs():
vmIntObj = vnc_client.virtual_machine_interface_read(id = vmInt['uuid'])
if vmIntObj.get_instance_ip_back_refs():
for instIp in vmIntObj.get_instance_ip_back_refs():
instIpObj = vnc_client.instance_ip_read(id = instIp['uuid'])
vnc_client.instance_ip_delete(id = instIp['uuid'])
if vmIntObj.get_logical_interface_back_refs():
for logicalInterface in vmIntObj.get_logical_interface_back_refs():
logInt = vnc_client.logical_interface_read(id = logicalInterface['uuid'])
logInt.del_virtual_machine_interface(vmIntObj)
vnc_client.logical_interface_update(logInt)
vnc_client.virtual_machine_interface_delete(id = vmIntObj.get_uuid())
vnc_client.logical_interface_delete(id = lif.get_uuid())
if not move:
vn = getVirtualNetwork(customer, name)
if delvn:
vnc_client.virtual_network_delete(id = vn.get_uuid())
p = subprocess.Popen(['ip','netns','pids',name + '_' + str(svcId)], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
print line
pid = int(line)
try:
os.kill(pid, signal.SIGKILL)
except:
print 'nothing to kill'
netns.remove(name + '_' + str(svcId))
ip_host = IPDB()
if name + '_' + str(svcId) in ip_host.interfaces:
with ip_host.interfaces[name + '_' + str(svcId)] as veth:
veth.remove()
subprocess.call(["ovs-vsctl", "del-port", "br0", name + '_' + str(svcId)])
if not move:
if delvn:
os.remove('/mnt/' + name + '.lease')
return json.dumps({ 'status' : 'deleted service'})
示例11: test_create_from_path
def test_create_from_path(self):
ns_dir = tempfile.mkdtemp()
# Create namespace
ns_name = str(uuid4())
nspath = '%s/%s' % (ns_dir, ns_name)
temp_ns = NetNS(nspath)
temp_ns.close()
fd = open(nspath)
self._test_create(nspath, fd.fileno())
fd.close()
netnsmod.remove(nspath)
assert ns_name not in netnsmod.listnetns()
assert ns_name not in netnsmod.listnetns(ns_dir)
示例12: test_create
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()
示例13: remove
def remove(self):
"""
Removes the Namespace and all included Network-Interfaces.
"""
logging.debug("%sDelete Namespace ...", LoggerSetup.get_log_deep(2))
try:
netns.remove(self.nsp_name)
self.ipdb_netns.release()
logging.debug("%s[+] Namespace(" + self.nsp_name + ") successfully deleted", LoggerSetup.get_log_deep(3))
except Exception as e:
if re.match("\[Errno 2\]*", str(e)):
logging.debug("%s[+] Namespace(" + self.nsp_name + ") is already deleted", LoggerSetup.get_log_deep(3))
return
logging.error("%s[-] Namespace(" + self.nsp_name +
") couldn't be deleted. Try 'ip netns delete <namespace_name>'", LoggerSetup.get_log_deep(3))
logging.error("%s" + str(e), LoggerSetup.get_log_deep(3))
示例14: deleteEndpoint
def deleteEndpoint(name, svcName, endpointtype):
if endpointtype == 'ns':
nsp = NSPopen(name, ['dhclient', '-r', name], stdout=subprocess.PIPE)
nsp.wait()
nsp.release()
netns.remove(name)
ip_host = IPDB()
if name + '_' + svcName in ip_host.interfaces:
with ip_host.interfaces[name + '_' + svcName] as veth:
veth.remove()
subprocess.call(["ovs-vsctl", "del-port", "vs-" + svcName, name + '_' + svcName])
if endpointtype == 'lxc':
subprocess.call(['/usr/bin/lxc-stop','-n',name])
subprocess.call(['/usr/bin/lxc-destroy','-n',name])
pass
return json.dumps({ 'status' : 'deleted endpoint'})
示例15: __removePvd
def __removePvd(self, phyIfaceName, pvdId):
pvd = self.pvds.get((phyIfaceName, pvdId))
if (pvd):
# remove the network namespace associated with the PvD (this in turn removes the PvD network configuration as well)
if (pvd.netnsName in netns.listnetns()):
netns.remove(pvd.netnsName)
# remove the directory containing PvD-related DNS information
(dnsConfDir, dnsConfFile) = self.__getDnsConfPath(pvd.netnsName)
if (os.path.exists(dnsConfDir)):
shutil.rmtree(dnsConfDir, True)
# remove the PvD record from the PvD manager's log
del self.pvds[(phyIfaceName, pvdId)]
LOG.info('PvD {0} received through {1} REMOVED, network namespace {2} deleted, DNS directory {3} deleted, type {4}'.format(pvd.pvdId, pvd.phyIfaceName, pvd.netnsName, dnsConfDir, pvd.pvdInfo.pvdType))
self.pvdserver.stateChanged ("deleted", pvdId)
else:
raise Exception('There is no PvD {0} configured on {1}'.format(pvdInfo.pvdId, phyIfaceName))