updates to consolidate commands that need to be defined by a new node type

This commit is contained in:
Blake J. Harnden 2018-03-01 13:21:25 -08:00
parent 0b8ee7760d
commit d3bd61ddcf
25 changed files with 314 additions and 175 deletions

View file

@ -114,11 +114,11 @@ class SimpleLxcNode(PyCoreNode):
try:
# bring up the loopback interface
logger.info("bringing up loopback interface")
self.client.check_alloutput([constants.IP_BIN, "link", "set", "lo", "up"])
self.check_cmd([constants.IP_BIN, "link", "set", "lo", "up"])
# set hostname for node
logger.info("setting hostname: %s" % self.name)
self.client.check_alloutput(["hostname", self.name])
self.check_cmd(["hostname", self.name])
except subprocess.CalledProcessError:
logger.exception("error setting up loopback and hostname: %s")
@ -171,6 +171,47 @@ class SimpleLxcNode(PyCoreNode):
"""
pass
def cmd(self, cmd, wait=True):
"""
Runs shell command on node, with option to not wait for a result.
:param list[str]/str cmd: command to run
:param bool wait: wait for command to exit, defaults to True
:return: exit status for command
:rtype: int
"""
return self.client.cmd(cmd, wait)
def cmd_output(self, cmd):
"""
Runs shell command on node and get exit status and output.
:param list[str]/str cmd: command to run
:return: exit status and combined stdout and stderr
:rtype: tuple[int, str]
"""
return self.client.cmd_output(cmd)
def check_cmd(self, cmd):
"""
Runs shell command on node.
:param list[str]/str cmd: command to run
:return: exist status and combined stdout and stderr
:rtype: tuple[int, str]
:raises subprocess.CalledProcessError: when a non-zero exit status occurs
"""
return self.client.check_cmd(cmd)
def termcmdstring(self, sh="/bin/sh"):
"""
Create a terminal command string.
:param str sh: shell to execute command in
:return: str
"""
return self.client.termcmdstring(sh)
# TODO: should change how this exception is just swallowed up
def mount(self, source, target):
"""
@ -200,7 +241,7 @@ class SimpleLxcNode(PyCoreNode):
"""
logger.info("unmounting: %s", target)
try:
self.client.check_alloutput([constants.UMOUNT_BIN, "-n", "-l", target])
self.check_cmd([constants.UMOUNT_BIN, "-n", "-l", target])
except subprocess.CalledProcessError:
logger.exception("error during unmount")
@ -247,8 +288,8 @@ class SimpleLxcNode(PyCoreNode):
if self.up:
try:
utils.check_alloutput([constants.IP_BIN, "link", "set", veth.name, "netns", str(self.pid)])
self.client.check_alloutput([constants.IP_BIN, "link", "set", veth.name, "name", ifname])
utils.check_cmd([constants.IP_BIN, "link", "set", veth.name, "netns", str(self.pid)])
self.check_cmd([constants.IP_BIN, "link", "set", veth.name, "name", ifname])
except subprocess.CalledProcessError:
logger.exception("failure setting eth name")
@ -258,7 +299,7 @@ class SimpleLxcNode(PyCoreNode):
# TODO: potentially find better way to query interface ID
# retrieve interface information
try:
output = self.client.check_alloutput(["ip", "link", "show", veth.name])
_, output = self.check_cmd(["ip", "link", "show", veth.name])
logger.info("interface command output: %s", output)
output = output.split("\n")
veth.flow_id = int(output[0].strip().split(":")[0]) + 1
@ -320,7 +361,7 @@ class SimpleLxcNode(PyCoreNode):
if self.up:
cmd = [constants.IP_BIN, "link", "set", "dev", self.ifname(ifindex), "address", str(addr)]
try:
self.client.check_alloutput(cmd)
self.check_cmd(cmd)
except subprocess.CalledProcessError:
logger.exception("error setting MAC address %s: %s", addr)
@ -337,10 +378,10 @@ class SimpleLxcNode(PyCoreNode):
# check if addr is ipv6
if ":" in str(addr):
cmd = [constants.IP_BIN, "addr", "add", str(addr), "dev", self.ifname(ifindex)]
self.client.check_alloutput(cmd)
self.check_cmd(cmd)
else:
cmd = [constants.IP_BIN, "addr", "add", str(addr), "broadcast", "+", "dev", self.ifname(ifindex)]
self.client.check_alloutput(cmd)
self.check_cmd(cmd)
except subprocess.CalledProcessError:
logger.exception("failure adding interface address")
@ -361,7 +402,7 @@ class SimpleLxcNode(PyCoreNode):
if self.up:
try:
self.client.check_alloutput([constants.IP_BIN, "addr", "del", str(addr), "dev", self.ifname(ifindex)])
self.check_cmd([constants.IP_BIN, "addr", "del", str(addr), "dev", self.ifname(ifindex)])
except subprocess.CalledProcessError:
logger.exception("failure deleting address")
@ -394,7 +435,7 @@ class SimpleLxcNode(PyCoreNode):
"""
if self.up:
try:
self.client.check_alloutput([constants.IP_BIN, "link", "set", self.ifname(ifindex), "up"])
self.check_cmd([constants.IP_BIN, "link", "set", self.ifname(ifindex), "up"])
except subprocess.CalledProcessError:
logger.exception("failure bringing interface up")
@ -454,15 +495,15 @@ class SimpleLxcNode(PyCoreNode):
tmplen = 8
tmp1 = "tmp." + "".join([random.choice(string.ascii_lowercase) for _ in xrange(tmplen)])
tmp2 = "tmp." + "".join([random.choice(string.ascii_lowercase) for _ in xrange(tmplen)])
utils.check_alloutput([constants.IP_BIN, "link", "add", "name", tmp1, "type", "veth", "peer", "name", tmp2])
utils.check_cmd([constants.IP_BIN, "link", "add", "name", tmp1, "type", "veth", "peer", "name", tmp2])
utils.check_alloutput([constants.IP_BIN, "link", "set", tmp1, "netns", str(self.pid)])
self.client.check_alloutput([constants.IP_BIN, "link", "set", tmp1, "name", ifname])
utils.check_cmd([constants.IP_BIN, "link", "set", tmp1, "netns", str(self.pid)])
self.check_cmd([constants.IP_BIN, "link", "set", tmp1, "name", ifname])
interface = PyCoreNetIf(node=self, name=ifname, mtu=_DEFAULT_MTU)
self.addnetif(interface, self.newifindex())
utils.check_alloutput([constants.IP_BIN, "link", "set", tmp2, "netns", str(othernode.pid)])
othernode.client.check_alloutput([constants.IP_BIN, "link", "set", tmp2, "name", otherifname])
utils.check_cmd([constants.IP_BIN, "link", "set", tmp2, "netns", str(othernode.pid)])
othernode.check_cmd([constants.IP_BIN, "link", "set", tmp2, "name", otherifname])
other_interface = PyCoreNetIf(node=othernode, name=otherifname, mtu=_DEFAULT_MTU)
othernode.addnetif(other_interface, othernode.newifindex())