Commit b420be8c authored by Martín Ferrari's avatar Martín Ferrari

hide execute(), fix a bug in get_if_name

parent d0d3ea07
...@@ -64,7 +64,7 @@ def create_if_pair(if1, if2): ...@@ -64,7 +64,7 @@ def create_if_pair(if1, if2):
cmd[i] += ["mtu", str(iface[i].mtu)] cmd[i] += ["mtu", str(iface[i].mtu)]
cmd = ["ip", "link", "add"] + cmd[0] + ["type", "veth", "peer"] + cmd[1] cmd = ["ip", "link", "add"] + cmd[0] + ["type", "veth", "peer"] + cmd[1]
execute(cmd) _execute(cmd)
try: try:
set_if(if1) set_if(if1)
set_if(if2) set_if(if2)
...@@ -81,7 +81,7 @@ def create_if_pair(if1, if2): ...@@ -81,7 +81,7 @@ def create_if_pair(if1, if2):
def del_if(iface): def del_if(iface):
ifname = get_if_name(iface) ifname = get_if_name(iface)
execute(["ip", "link", "del", ifname]) _execute(["ip", "link", "del", ifname])
def set_if(iface, recover = True): def set_if(iface, recover = True):
orig_iface = get_real_if(iface) orig_iface = get_real_if(iface)
...@@ -106,7 +106,7 @@ def set_if(iface, recover = True): ...@@ -106,7 +106,7 @@ def set_if(iface, recover = True):
#print cmds #print cmds
for c in cmds: for c in cmds:
try: try:
execute(c) _execute(c)
except: except:
if recover: if recover:
set_if(orig_iface, recover = False) # rollback set_if(orig_iface, recover = False) # rollback
...@@ -114,7 +114,7 @@ def set_if(iface, recover = True): ...@@ -114,7 +114,7 @@ def set_if(iface, recover = True):
def change_netns(iface, netns): def change_netns(iface, netns):
ifname = get_if_name(iface) ifname = get_if_name(iface)
execute(["ip", "link", "set", "dev", ifname, "netns", str(netns)]) _execute(["ip", "link", "set", "dev", ifname, "netns", str(netns)])
def add_addr(iface, address): def add_addr(iface, address):
ifname = get_if_name(iface) ifname = get_if_name(iface)
...@@ -125,7 +125,7 @@ def add_addr(iface, address): ...@@ -125,7 +125,7 @@ def add_addr(iface, address):
"%s/%d" % (address.address, int(address.prefix_len))] "%s/%d" % (address.address, int(address.prefix_len))]
if hasattr(address, "broadcast"): if hasattr(address, "broadcast"):
cmd += ["broadcast", address.broadcast if address.broadcast else "+"] cmd += ["broadcast", address.broadcast if address.broadcast else "+"]
execute(cmd) _execute(cmd)
def del_addr(iface, address): def del_addr(iface, address):
ifname = get_if_name(iface) ifname = get_if_name(iface)
...@@ -134,7 +134,7 @@ def del_addr(iface, address): ...@@ -134,7 +134,7 @@ def del_addr(iface, address):
cmd = ["ip", "addr", "del", "dev", ifname, "local", cmd = ["ip", "addr", "del", "dev", ifname, "local",
"%s/%d" % (address.address, int(address.prefix_len))] "%s/%d" % (address.address, int(address.prefix_len))]
execute(cmd) _execute(cmd)
def set_addr(iface, addresses, recover = True): def set_addr(iface, addresses, recover = True):
ifname = get_if_name(iface) ifname = get_if_name(iface)
...@@ -160,7 +160,7 @@ def set_addr(iface, addresses, recover = True): ...@@ -160,7 +160,7 @@ def set_addr(iface, addresses, recover = True):
# Useful stuff # Useful stuff
def execute(cmd): def _execute(cmd):
print " ".join(cmd)#; return print " ".join(cmd)#; return
null = open('/dev/null', 'r+') null = open('/dev/null', 'r+')
p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE) p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
...@@ -182,7 +182,7 @@ def get_real_if(iface): ...@@ -182,7 +182,7 @@ def get_real_if(iface):
def get_if_name(iface): def get_if_name(iface):
if isinstance(iface, netns.interface.interface): if isinstance(iface, netns.interface.interface):
if iface.name != None: if iface.name != None:
return iface return iface.name
if isinstance(iface, str): if isinstance(iface, str):
return iface return iface
return get_real_if(iface).name return get_real_if(iface).name
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment