]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/ceph-volume/ceph_volume/process.py
update sources to v12.2.3
[ceph.git] / ceph / src / ceph-volume / ceph_volume / process.py
index 4b6a9c284741f50d5cb22644214a0293d19c266f..1a2c268dbd8e0426a7cfbcf26bc7892d2c8c1a41 100644 (file)
@@ -9,7 +9,7 @@ import logging
 logger = logging.getLogger(__name__)
 
 
-def log_output(descriptor, message, terminal_logging):
+def log_output(descriptor, message, terminal_logging, logfile_logging):
     """
     log output to both the logger and the terminal if terminal_logging is
     enabled
@@ -20,7 +20,8 @@ def log_output(descriptor, message, terminal_logging):
     line = '%s %s' % (descriptor, message)
     if terminal_logging:
         getattr(terminal, descriptor)(message)
-    logger.info(line)
+    if logfile_logging:
+        logger.info(line)
 
 
 def log_descriptors(reads, process, terminal_logging):
@@ -42,7 +43,7 @@ def log_descriptors(reads, process, terminal_logging):
     for descriptor in reads:
         descriptor_name = descriptor_names[descriptor]
         try:
-            log_output(descriptor_name, read(descriptor, 1024), terminal_logging)
+            log_output(descriptor_name, read(descriptor, 1024), terminal_logging, True)
         except (IOError, OSError):
             # nothing else to log
             pass
@@ -96,25 +97,23 @@ def run(command, **kw):
 
     :param command: The command to pass in to the remote subprocess.Popen as a list
     :param stop_on_error: If a nonzero exit status is return, it raises a ``RuntimeError``
+    :param fail_msg: If a nonzero exit status is returned this message will be included in the log
     """
     stop_on_error = kw.pop('stop_on_error', True)
     command_msg = obfuscate(command, kw.pop('obfuscate', None))
-    stdin = kw.pop('stdin', None)
+    fail_msg = kw.pop('fail_msg', None)
     logger.info(command_msg)
     terminal.write(command_msg)
     terminal_logging = kw.pop('terminal_logging', True)
 
     process = subprocess.Popen(
         command,
-        stdin=subprocess.PIPE,
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
         close_fds=True,
         **kw
     )
 
-    if stdin:
-        process.communicate(stdin)
     while True:
         reads, _, _ = select(
             [process.stdout.fileno(), process.stderr.fileno()],
@@ -131,6 +130,10 @@ def run(command, **kw):
     returncode = process.wait()
     if returncode != 0:
         msg = "command returned non-zero exit status: %s" % returncode
+        if fail_msg:
+            logger.warning(fail_msg)
+            if terminal_logging:
+                terminal.warning(fail_msg)
         if stop_on_error:
             raise RuntimeError(msg)
         else:
@@ -152,10 +155,16 @@ def call(command, **kw):
     Useful when system calls are needed to act on output, and that same output
     shouldn't get displayed on the terminal.
 
+    Optionally, the command can be displayed on the terminal and the log file,
+    and log file output can be turned off. This is useful to prevent sensitive
+    output going to stderr/stdout and being captured on a log file.
+
     :param terminal_verbose: Log command output to terminal, defaults to False, and
                              it is forcefully set to True if a return code is non-zero
+    :param logfile_verbose: Log stderr/stdout output to log file. Defaults to True
     """
     terminal_verbose = kw.pop('terminal_verbose', False)
+    logfile_verbose = kw.pop('logfile_verbose', True)
     show_command = kw.pop('show_command', False)
     command_msg = "Running command: %s" % ' '.join(command)
     stdin = kw.pop('stdin', None)
@@ -188,12 +197,13 @@ def call(command, **kw):
         # set to true so that we can log the stderr/stdout that callers would
         # do anyway
         terminal_verbose = True
+        logfile_verbose = True
 
     # the following can get a messed up order in the log if the system call
     # returns output with both stderr and stdout intermingled. This separates
     # that.
     for line in stdout:
-        log_output('stdout', line, terminal_verbose)
+        log_output('stdout', line, terminal_verbose, logfile_verbose)
     for line in stderr:
-        log_output('stderr', line, terminal_verbose)
+        log_output('stderr', line, terminal_verbose, logfile_verbose)
     return stdout, stderr, returncode