updated utils.check_cmd to accept the same parameters as other commands and be leveraged for node cmds

This commit is contained in:
Blake Harnden 2019-10-11 13:15:57 -07:00
parent d326f246a7
commit fc7a161221
6 changed files with 25 additions and 22 deletions

View file

@ -98,7 +98,7 @@ class NodeBase(object):
:raises CoreCommandError: when a non-zero exit status occurs :raises CoreCommandError: when a non-zero exit status occurs
""" """
if self.server is None: if self.server is None:
return utils.check_cmd(args, env=env, cwd=cwd) return utils.check_cmd(args, env, cwd, wait)
else: else:
args = " ".join(args) args = " ".join(args)
return distributed.remote_cmd(self.server, args, env, cwd, wait) return distributed.remote_cmd(self.server, args, env, cwd, wait)

View file

@ -56,7 +56,7 @@ class DockerClient(object):
cmd=cmd cmd=cmd
)) ))
def ns_cmd(self, cmd): def ns_cmd(self, cmd, wait):
if isinstance(cmd, list): if isinstance(cmd, list):
cmd = " ".join(cmd) cmd = " ".join(cmd)
args = "nsenter -t {pid} -u -i -p -n {cmd}".format( args = "nsenter -t {pid} -u -i -p -n {cmd}".format(
@ -64,7 +64,7 @@ class DockerClient(object):
cmd=cmd cmd=cmd
) )
logging.info("ns cmd: %s", args) logging.info("ns cmd: %s", args)
return utils.check_cmd(args) return utils.check_cmd(args, wait=wait)
def get_pid(self): def get_pid(self):
args = "docker inspect -f '{{{{.State.Pid}}}}' {name}".format(name=self.name) args = "docker inspect -f '{{{{.State.Pid}}}}' {name}".format(name=self.name)
@ -147,7 +147,7 @@ class DockerNode(CoreNode):
logging.debug("node down, not running network command: %s", args) logging.debug("node down, not running network command: %s", args)
return "" return ""
return self.client.ns_cmd(args) return self.client.ns_cmd(args, wait)
def termcmdstring(self, sh="/bin/sh"): def termcmdstring(self, sh="/bin/sh"):
""" """

View file

@ -61,7 +61,7 @@ class CoreInterface(object):
:raises CoreCommandError: when a non-zero exit status occurs :raises CoreCommandError: when a non-zero exit status occurs
""" """
if self.server is None: if self.server is None:
return utils.check_cmd(args, env=env, cwd=cwd) return utils.check_cmd(args, env, cwd, wait)
else: else:
args = " ".join(args) args = " ".join(args)
return distributed.remote_cmd(self.server, args, env, cwd, wait) return distributed.remote_cmd(self.server, args, env, cwd, wait)

View file

@ -46,12 +46,12 @@ class LxdClient(object):
def _cmd_args(self, cmd): def _cmd_args(self, cmd):
return "lxc exec -nT {name} -- {cmd}".format(name=self.name, cmd=cmd) return "lxc exec -nT {name} -- {cmd}".format(name=self.name, cmd=cmd)
def check_cmd(self, cmd): def check_cmd(self, cmd, wait):
if isinstance(cmd, list): if isinstance(cmd, list):
cmd = " ".join(cmd) cmd = " ".join(cmd)
args = self._cmd_args(cmd) args = self._cmd_args(cmd)
logging.info("lxc cmd output: %s", args) logging.info("lxc cmd output: %s", args)
return utils.check_cmd(args) return utils.check_cmd(args, wait=wait)
def _ns_args(self, cmd): def _ns_args(self, cmd):
return "nsenter -t {pid} -m -u -i -p -n {cmd}".format(pid=self.pid, cmd=cmd) return "nsenter -t {pid} -m -u -i -p -n {cmd}".format(pid=self.pid, cmd=cmd)
@ -144,7 +144,7 @@ class LxcNode(CoreNode):
if not self.up: if not self.up:
logging.debug("node down, not running network command: %s", args) logging.debug("node down, not running network command: %s", args)
return "" return ""
return self.client.check_cmd(args) return self.client.check_cmd(args, wait)
def termcmdstring(self, sh="/bin/sh"): def termcmdstring(self, sh="/bin/sh"):
""" """

View file

@ -322,7 +322,7 @@ class CoreNetwork(CoreNetworkBase):
:raises CoreCommandError: when a non-zero exit status occurs :raises CoreCommandError: when a non-zero exit status occurs
""" """
logging.info("network node(%s) cmd", self.name) logging.info("network node(%s) cmd", self.name)
output = utils.check_cmd(args, env=env, cwd=cwd) output = utils.check_cmd(args, env, cwd, wait)
args = " ".join(args) args = " ".join(args)
for server in self.session.servers: for server in self.session.servers:

View file

@ -11,8 +11,8 @@ import logging
import logging.config import logging.config
import os import os
import shlex import shlex
import subprocess
import sys import sys
from subprocess import PIPE, STDOUT, Popen
from past.builtins import basestring from past.builtins import basestring
@ -203,33 +203,36 @@ def mute_detach(args, **kwargs):
args = split_args(args) args = split_args(args)
kwargs["preexec_fn"] = _detach_init kwargs["preexec_fn"] = _detach_init
kwargs["stdout"] = DEVNULL kwargs["stdout"] = DEVNULL
kwargs["stderr"] = subprocess.STDOUT kwargs["stderr"] = STDOUT
return subprocess.Popen(args, **kwargs).pid return Popen(args, **kwargs).pid
def check_cmd(args, **kwargs): def check_cmd(args, env=None, cwd=None, wait=True):
""" """
Execute a command on the host and return a tuple containing the exit status and 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. result string. stderr output is folded into the stdout result string.
:param list[str]|str args: command arguments :param list[str]|str args: command arguments
:param dict kwargs: keyword arguments to pass to subprocess.Popen :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
:return: combined stdout and stderr :return: combined stdout and stderr
:rtype: str :rtype: str
:raises CoreCommandError: when there is a non-zero exit status or the file to :raises CoreCommandError: when there is a non-zero exit status or the file to
execute is not found execute is not found
""" """
kwargs["stdout"] = subprocess.PIPE
kwargs["stderr"] = subprocess.STDOUT
args = split_args(args) args = split_args(args)
logging.info("command: %s", args) logging.info("command: %s", args)
try: try:
p = subprocess.Popen(args, **kwargs) p = Popen(args, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd)
stdout, _ = p.communicate() if wait:
stdout, stderr = p.communicate()
status = p.wait() status = p.wait()
if status != 0: if status != 0:
raise CoreCommandError(status, args, stdout) raise CoreCommandError(status, args, stdout, stderr)
return stdout.decode("utf-8").strip() return stdout.decode("utf-8").strip()
else:
return ""
except OSError: except OSError:
raise CoreCommandError(-1, args) raise CoreCommandError(-1, args)