renamed utils.check_cmd to utils.cmd, updated host_cmd to allow for shell commands for output redirection

This commit is contained in:
Blake Harnden 2019-10-21 10:32:42 -07:00
parent 16b7e70c33
commit 78f981463d
15 changed files with 34 additions and 31 deletions

View file

@ -878,7 +878,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
):
if message.flags & MessageFlags.LOCAL.value:
try:
res = utils.check_cmd(command)
res = utils.cmd(command)
status = 0
except CoreCommandError as e:
res = e.stderr

View file

@ -140,7 +140,7 @@ class EmaneManager(ModelManager):
try:
# check for emane
args = "emane --version"
emane_version = utils.check_cmd(args)
emane_version = utils.cmd(args)
logging.info("using EMANE: %s", emane_version)
self.session.distributed.execute(lambda x: x.remote_cmd(args))
@ -594,7 +594,7 @@ class EmaneManager(ModelManager):
log_file = os.path.join(path, "emane.log")
platform_xml = os.path.join(path, "platform.xml")
emanecmd += f" -f {log_file} {platform_xml}"
utils.check_cmd(emanecmd, cwd=path)
utils.cmd(emanecmd, cwd=path)
self.session.distributed.execute(lambda x: x.remote_cmd(emanecmd, cwd=path))
logging.info("host emane daemon running: %s", emanecmd)
@ -618,8 +618,8 @@ class EmaneManager(ModelManager):
if stop_emane_on_host:
try:
utils.check_cmd(kill_emaned)
utils.check_cmd(kill_transortd)
utils.cmd(kill_emaned)
utils.cmd(kill_transortd)
self.session.distributed.execute(lambda x: x.remote_cmd(kill_emaned))
self.session.distributed.execute(lambda x: x.remote_cmd(kill_transortd))
except CoreCommandError:

View file

@ -63,4 +63,4 @@ class EmaneTdmaModel(emanemodel.EmaneModel):
"setting up tdma schedule: schedule(%s) device(%s)", schedule, event_device
)
args = f"emaneevent-tdmaschedule -i {event_device} {schedule}"
utils.check_cmd(args)
utils.cmd(args)

View file

@ -1187,6 +1187,6 @@ class Ns2ScriptedMobility(WayPointMobility):
return
filename = self.findfile(filename)
args = f"/bin/sh {filename} {typestr}"
utils.check_cmd(
utils.cmd(
args, cwd=self.session.session_dir, env=self.session.get_environment()
)

View file

@ -79,7 +79,7 @@ class NodeBase(object):
"""
raise NotImplementedError
def host_cmd(self, args, env=None, cwd=None, wait=True):
def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False):
"""
Runs a command on the host system or distributed server.
@ -87,12 +87,13 @@ class NodeBase(object):
:param dict env: environment to run command with
:param str cwd: directory to run command in
:param bool wait: True to wait for status, False otherwise
:param bool shell: True to use shell, False otherwise
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
if self.server is None:
return utils.check_cmd(args, env, cwd, wait)
return utils.cmd(args, env, cwd, wait, shell)
else:
return self.server.remote_cmd(args, env, cwd, wait)

View file

@ -65,4 +65,4 @@ class VnodeClient(object):
"""
self._verify_connection()
args = self.create_cmd(args)
return utils.check_cmd(args, wait=wait)
return utils.cmd(args, wait=wait)

View file

@ -45,14 +45,14 @@ class DockerClient(object):
def check_cmd(self, cmd):
logging.info("docker cmd output: %s", cmd)
return utils.check_cmd(f"docker exec {self.name} {cmd}")
return utils.cmd(f"docker exec {self.name} {cmd}")
def create_ns_cmd(self, cmd):
return f"nsenter -t {self.pid} -u -i -p -n {cmd}"
def ns_cmd(self, cmd, wait):
args = f"nsenter -t {self.pid} -u -i -p -n {cmd}"
return utils.check_cmd(args, wait=wait)
return utils.cmd(args, wait=wait)
def get_pid(self):
args = f"docker inspect -f '{{{{.State.Pid}}}}' {self.name}"
@ -153,7 +153,7 @@ class DockerNode(CoreNode):
def nsenter_cmd(self, args, wait=True):
if self.server is None:
args = self.client.create_ns_cmd(args)
return utils.check_cmd(args, wait=wait)
return utils.cmd(args, wait=wait)
else:
args = self.client.create_ns_cmd(args)
return self.server.remote_cmd(args, wait=wait)

View file

@ -48,7 +48,7 @@ class CoreInterface(object):
use_ovs = session.options.get_config("ovs") == "True"
self.net_client = get_net_client(use_ovs, self.host_cmd)
def host_cmd(self, args, env=None, cwd=None, wait=True):
def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False):
"""
Runs a command on the host system or distributed server.
@ -56,12 +56,13 @@ class CoreInterface(object):
:param dict env: environment to run command with
:param str cwd: directory to run command in
:param bool wait: True to wait for status, False otherwise
:param bool shell: True to use shell, False otherwise
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
if self.server is None:
return utils.check_cmd(args, env, cwd, wait)
return utils.cmd(args, env, cwd, wait, shell)
else:
return self.server.remote_cmd(args, env, cwd, wait)

View file

@ -49,7 +49,7 @@ class LxdClient(object):
def check_cmd(self, cmd, wait=True):
args = self.create_cmd(cmd)
return utils.check_cmd(args, wait=wait)
return utils.cmd(args, wait=wait)
def copy_file(self, source, destination):
if destination[0] != "/":

View file

@ -2,8 +2,6 @@
Clients for dealing with bridge/interface commands.
"""
import os
from core.constants import BRCTL_BIN, ETHTOOL_BIN, IP_BIN, OVS_BIN, TC_BIN
@ -236,10 +234,10 @@ class LinuxNetClient(object):
self.device_up(name)
# turn off multicast snooping so forwarding occurs w/o IGMP joins
snoop = f"/sys/devices/virtual/net/{name}/bridge/multicast_snooping"
if os.path.exists(snoop):
with open(snoop, "w") as f:
f.write("0")
snoop_file = "multicast_snooping"
snoop = f"/sys/devices/virtual/net/{name}/bridge/{snoop_file}"
self.run(f"echo 0 > /tmp/{snoop_file}", shell=True)
self.run(f"cp /tmp/{snoop_file} {snoop}")
def delete_bridge(self, name):
"""

View file

@ -270,7 +270,7 @@ class CoreNetwork(CoreNetworkBase):
self.startup()
ebq.startupdateloop(self)
def host_cmd(self, args, env=None, cwd=None, wait=True):
def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False):
"""
Runs a command that is used to configure and setup the network on the host
system and all configured distributed servers.
@ -279,12 +279,13 @@ class CoreNetwork(CoreNetworkBase):
:param dict env: environment to run command with
:param str cwd: directory to run command in
:param bool wait: True to wait for status, False otherwise
:param bool shell: True to use shell, False otherwise
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("network node(%s) cmd", self.name)
output = utils.check_cmd(args, env, cwd, wait)
output = utils.cmd(args, env, cwd, wait, shell)
self.session.distributed.execute(lambda x: x.remote_cmd(args, env, cwd, wait))
return output
@ -765,7 +766,7 @@ class CtrlNet(CoreNetwork):
"""
use_ovs = self.session.options.get_config("ovs") == "True"
current = f"{address}/{self.prefix.prefixlen}"
net_client = get_net_client(use_ovs, utils.check_cmd)
net_client = get_net_client(use_ovs, utils.cmd)
net_client.create_address(self.brname, current)
servers = self.session.distributed.servers
for name in servers:

View file

@ -415,7 +415,7 @@ class HttpService(UtilService):
Detect the apache2 version using the 'a2query' command.
"""
try:
result = utils.check_cmd("a2query -v")
result = utils.cmd("a2query -v")
status = 0
except CoreCommandError as e:
status = e.returncode

View file

@ -191,7 +191,7 @@ def mute_detach(args, **kwargs):
return Popen(args, **kwargs).pid
def check_cmd(args, env=None, cwd=None, wait=True):
def cmd(args, env=None, cwd=None, wait=True, shell=False):
"""
Execute a command on the host and return a tuple containing the exit status and
result string. stderr output is folded into the stdout result string.
@ -200,15 +200,17 @@ def check_cmd(args, env=None, cwd=None, wait=True):
:param dict env: environment to run command with
:param str cwd: directory to run command in
:param bool wait: True to wait for status, False otherwise
:param bool shell: True to use shell, False otherwise
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when there is a non-zero exit status or the file to
execute is not found
"""
logging.info("command cwd(%s) wait(%s): %s", cwd, wait, args)
if shell is False:
args = shlex.split(args)
try:
p = Popen(args, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd)
p = Popen(args, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd, shell=shell)
if wait:
stdout, stderr = p.communicate()
status = p.wait()

View file

@ -69,7 +69,7 @@ def get_ipv4_addresses(hostname):
if hostname == "localhost":
addresses = []
args = f"{IP_BIN} -o -f inet address show"
output = utils.check_cmd(args)
output = utils.cmd(args)
for line in output.split(os.linesep):
split = line.split()
if not split:

View file

@ -67,4 +67,4 @@ class TestNodes:
# then
assert node
assert node.up
assert utils.check_cmd(f"brctl show {node.brname}")
assert utils.cmd(f"brctl show {node.brname}")