From 2cd5d8afe2deae8847a10c3cac2b3c023024f0fe Mon Sep 17 00:00:00 2001 From: "Carl D. Roth" Date: Thu, 20 Sep 2018 18:15:07 -0700 Subject: [PATCH] Cleanups for execute method - support tuple arguments - send stdout/stderr output to the logger --- .../src/python/onl/util/__init__.py | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py b/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py index c085b3f2..f625f2b5 100644 --- a/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py +++ b/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py @@ -1,18 +1,49 @@ import subprocess +import logging class OnlServiceMixin(object): - def _execute(self, cmd, root=False, ex=True): - self.logger.debug("Executing: %s" % cmd) + + def _execute(self, cmd, + root=False, ex=True, + logLevel=logging.DEBUG): + self.logger.log(logLevel, "Executing: %s", cmd) + + if isinstance(cmd, basestring): + shell = True + else: + shell = False + if root is True and os.getuid() != 0: - cmd = "sudo " + cmd + if isinstance(cmd, basestring): + cmd = "sudo " + cmd + else: + cmd = ['sudo',] + list(cmd) + try: - subprocess.check_call(cmd, shell=True) - except Exception, e: + pipe = subprocess.Popen(cmd, shell=shell, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + except OSError as e: if ex: - self.logger.error("Command failed: %s" % e) + self.logger.error("Command did not start: %s (%s)", + str(e), str(e.child_traceback),) raise else: - return e.returncode + return -1 + + out, _ = pipe.communicate() + code = pipe.wait() + + lvl = logging.WARN if code else logLevel + out = (out or "").rstrip() + for line in out.splitlines(False): + self.logger.log(lvl, ">>> %s", line) + + if ex and code: + self.logger.error("Command failed with code %s", code) + raise subprocess.CalledProcessError(code, cmd) + + return code def _raise(self, msg, klass): self.logger.critical(msg)