1 from fcntl
import fcntl
, F_GETFL
, F_SETFL
2 from os
import O_NONBLOCK
, read
4 from select
import select
5 from ceph_volume
import terminal
9 logger
= logging
.getLogger(__name__
)
12 def log_output(descriptor
, message
, terminal_logging
):
14 log output to both the logger and the terminal if terminal_logging is
19 message
= message
.strip()
20 line
= '%s %s' % (descriptor
, message
)
22 getattr(terminal
, descriptor
)(message
)
26 def log_descriptors(reads
, process
, terminal_logging
):
28 Helper to send output to the terminal while polling the subprocess
30 # these fcntl are set to O_NONBLOCK for the filedescriptors coming from
31 # subprocess so that the logging does not block. Without these a prompt in
32 # a subprocess output would hang and nothing would get printed. Note how
33 # these are just set when logging subprocess, not globally.
34 stdout_flags
= fcntl(process
.stdout
, F_GETFL
) # get current p.stdout flags
35 stderr_flags
= fcntl(process
.stderr
, F_GETFL
) # get current p.stderr flags
36 fcntl(process
.stdout
, F_SETFL
, stdout_flags | O_NONBLOCK
)
37 fcntl(process
.stderr
, F_SETFL
, stderr_flags | O_NONBLOCK
)
39 process
.stdout
.fileno(): 'stdout',
40 process
.stderr
.fileno(): 'stderr'
42 for descriptor
in reads
:
43 descriptor_name
= descriptor_names
[descriptor
]
45 log_output(descriptor_name
, read(descriptor
, 1024), terminal_logging
)
46 except (IOError, OSError):
51 def run(command
, **kw
):
53 A real-time-logging implementation of a remote subprocess.Popen call where
54 a command is just executed on the remote end and no other handling is done.
56 :param command: The command to pass in to the remote subprocess.Popen as a list
57 :param stop_on_error: If a nonzero exit status is return, it raises a ``RuntimeError``
59 stop_on_error
= kw
.pop('stop_on_error', True)
60 command_msg
= "Running command: %s" % ' '.join(command
)
61 stdin
= kw
.pop('stdin', None)
62 logger
.info(command_msg
)
63 terminal
.write(command_msg
)
64 terminal_logging
= kw
.pop('terminal_logging', True)
66 process
= subprocess
.Popen(
68 stdin
=subprocess
.PIPE
,
69 stdout
=subprocess
.PIPE
,
70 stderr
=subprocess
.PIPE
,
76 process
.communicate(stdin
)
79 [process
.stdout
.fileno(), process
.stderr
.fileno()],
82 log_descriptors(reads
, process
, terminal_logging
)
84 if process
.poll() is not None:
85 # ensure we do not have anything pending in stdout or stderr
86 log_descriptors(reads
, process
, terminal_logging
)
90 returncode
= process
.wait()
92 msg
= "command returned non-zero exit status: %s" % returncode
94 raise RuntimeError(msg
)
101 def call(command
, **kw
):
103 Similar to ``subprocess.Popen`` with the following changes:
105 * returns stdout, stderr, and exit code (vs. just the exit code)
106 * logs the full contents of stderr and stdout (separately) to the file log
108 By default, no terminal output is given, not even the command that is going
111 Useful when system calls are needed to act on output, and that same output
112 shouldn't get displayed on the terminal.
114 :param terminal_verbose: Log command output to terminal, defaults to False, and
115 it is forcefully set to True if a return code is non-zero
117 terminal_verbose
= kw
.pop('terminal_verbose', False)
118 command_msg
= "Running command: %s" % ' '.join(command
)
119 stdin
= kw
.pop('stdin', None)
120 logger
.info(command_msg
)
121 terminal
.write(command_msg
)
123 process
= subprocess
.Popen(
125 stdout
=subprocess
.PIPE
,
126 stderr
=subprocess
.PIPE
,
127 stdin
=subprocess
.PIPE
,
132 stdout_stream
, stderr_stream
= process
.communicate(stdin
)
134 stdout_stream
= process
.stdout
.read()
135 stderr_stream
= process
.stderr
.read()
136 returncode
= process
.wait()
137 if not isinstance(stdout_stream
, str):
138 stdout_stream
= stdout_stream
.decode('utf-8')
139 if not isinstance(stderr_stream
, str):
140 stderr_stream
= stderr_stream
.decode('utf-8')
141 stdout
= stdout_stream
.splitlines()
142 stderr
= stderr_stream
.splitlines()
145 # set to true so that we can log the stderr/stdout that callers would
147 terminal_verbose
= True
149 # the following can get a messed up order in the log if the system call
150 # returns output with both stderr and stdout intermingled. This separates
153 log_output('stdout', line
, terminal_verbose
)
155 log_output('stderr', line
, terminal_verbose
)
156 return stdout
, stderr
, returncode