/
subprocess_runner.py
578 lines (527 loc) · 24.6 KB
/
subprocess_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# Copyright 2018 - 2019 Alexey Stepanov aka penguinolog.
# Copyright 2016 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Python subprocess.Popen wrapper."""
__all__ = ("Subprocess", "SubprocessExecuteAsyncResult", "EnvT")
# Standard Library
import concurrent.futures
import copy
import datetime
import errno
import logging
import os
import subprocess # nosec # Expected usage
import typing
# External Dependencies
import threaded
# Package Implementation
from exec_helpers import _log_templates
from exec_helpers import _subprocess_helpers
from exec_helpers import api
from exec_helpers import constants
from exec_helpers import exceptions
from exec_helpers import exec_result
from exec_helpers import proc_enums
from exec_helpers.api import CalledProcessErrorSubClassT
from exec_helpers.api import OptionalStdinT
from exec_helpers.api import OptionalTimeoutT
from exec_helpers.proc_enums import ExitCodeT
EnvT = typing.Optional[
typing.Union[typing.Mapping[bytes, typing.Union[bytes, str]], typing.Mapping[str, typing.Union[bytes, str]]]
]
_OptionalIOBytes = typing.Optional[typing.IO[bytes]]
# noinspection PyTypeHints
class SubprocessExecuteAsyncResult(api.ExecuteAsyncResult):
"""Override original NamedTuple with proper typing."""
@property
def interface(self) -> "subprocess.Popen[bytes]":
"""Override original NamedTuple with proper typing."""
return super().interface # type: ignore
@property
def stdin(self) -> _OptionalIOBytes: # type: ignore
"""Override original NamedTuple with proper typing."""
return super().stdin
@property
def stderr(self) -> _OptionalIOBytes: # type: ignore
"""Override original NamedTuple with proper typing."""
return super().stderr
@property
def stdout(self) -> _OptionalIOBytes: # type: ignore
"""Override original NamedTuple with proper typing."""
return super().stdout
class Subprocess(api.ExecHelper):
"""Subprocess helper with timeouts and lock-free FIFO."""
def __init__(self, log_mask_re: typing.Optional[str] = None,) -> None:
"""Subprocess helper with timeouts and lock-free FIFO.
For excluding race-conditions we allow to run 1 command simultaneously
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
.. versionchanged:: 3.1.0 Not singleton anymore. Only lock is shared between all instances.
.. versionchanged:: 3.2.0 Logger can be enforced.
.. versionchanged:: 4.1.0 support chroot
.. versionchanged:: 4.3.0 Lock is not shared anymore: allow parallel call of different instances.
"""
mod_name = "exec_helpers" if self.__module__.startswith("exec_helpers") else self.__module__
super(Subprocess, self).__init__(
logger=logging.getLogger(f"{mod_name}.{self.__class__.__name__}"), log_mask_re=log_mask_re
)
def __enter__(self) -> "Subprocess": # pylint: disable=useless-super-delegation
"""Get context manager."""
# noinspection PyTypeChecker
return super().__enter__()
def _exec_command( # type: ignore
self,
command: str,
async_result: SubprocessExecuteAsyncResult,
timeout: OptionalTimeoutT,
*,
verbose: bool = False,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Get exit status from channel with timeout.
:param command: Command for execution
:type command: str
:param async_result: execute_async result
:type async_result: SubprocessExecuteAsyncResult
:param timeout: Timeout for command execution
:type timeout: typing.Union[int, float, None]
:param verbose: produce verbose log record on command call
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises OSError: exception during process kill (and not regarding to already closed process)
:raises ExecHelperNoKillError: Process not dies on SIGTERM & SIGKILL
:raises ExecHelperTimeoutError: Timeout exceeded
.. versionadded:: 1.2.0
"""
@threaded.threadpooled
def poll_stdout() -> None:
"""Sync stdout poll."""
result.read_stdout(src=async_result.stdout, log=self.logger, verbose=verbose)
@threaded.threadpooled
def poll_stderr() -> None:
"""Sync stderr poll."""
result.read_stderr(src=async_result.stderr, log=self.logger, verbose=verbose)
def close_streams() -> None:
"""Enforce FIFO closure."""
if async_result.stdout is not None and not async_result.stdout.closed:
async_result.stdout.close()
if async_result.stderr is not None and not async_result.stderr.closed:
async_result.stderr.close()
# Store command with hidden data
cmd_for_log: str = self._mask_command(cmd=command, log_mask_re=log_mask_re)
result = exec_result.ExecResult(cmd=cmd_for_log, stdin=stdin, started=async_result.started)
# noinspection PyNoneFunctionAssignment,PyTypeChecker
stdout_future: "concurrent.futures.Future[None]" = poll_stdout()
# noinspection PyNoneFunctionAssignment,PyTypeChecker
stderr_future: "concurrent.futures.Future[None]" = poll_stderr()
try:
exit_code: int = async_result.interface.wait(timeout=timeout) # Wait real timeout here
concurrent.futures.wait([stdout_future, stderr_future], timeout=0.1) # Minimal timeout to complete polling
result.exit_code = exit_code
return result
except subprocess.TimeoutExpired:
# kill -9 for all subprocesses
_subprocess_helpers.kill_proc_tree(async_result.interface.pid)
exit_signal: typing.Optional[int] = async_result.interface.poll()
if exit_signal is None:
raise exceptions.ExecHelperNoKillError(result=result, timeout=timeout) # type: ignore
result.exit_code = exit_signal
finally:
stdout_future.cancel()
stderr_future.cancel()
_, not_done = concurrent.futures.wait([stdout_future, stderr_future], timeout=1)
if not_done and async_result.interface.returncode:
self.logger.critical(
f"Process {command!s} was closed with exit code {async_result.interface.returncode!s}, "
f"but FIFO buffers are still open"
)
result.set_timestamp()
close_streams()
wait_err_msg: str = _log_templates.CMD_WAIT_ERROR.format(result=result, timeout=timeout)
self.logger.debug(wait_err_msg)
raise exceptions.ExecHelperTimeoutError(result=result, timeout=timeout) # type: ignore
# noinspection PyMethodOverriding
def _execute_async( # pylint: disable=arguments-differ
self,
command: str,
*,
stdin: typing.Union[str, bytes, bytearray, None] = None,
open_stdout: bool = True,
open_stderr: bool = True,
chroot_path: typing.Optional[str] = None,
cwd: typing.Optional[typing.Union[str, bytes]] = None,
env: EnvT = None,
env_patch: EnvT = None,
**kwargs: typing.Any,
) -> SubprocessExecuteAsyncResult:
"""Execute command in async mode and return Popen with IO objects.
:param command: Command for execution
:type command: str
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[str, bytes, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param chroot_path: chroot path override
:type chroot_path: typing.Optional[str]
:param cwd: Sets the current directory before the child is executed.
:type cwd: typing.Optional[typing.Union[str, bytes]]
:param env: Defines the environment variables for the new process.
:type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param env_patch: Defines the environment variables to ADD for the new process.
:type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Tuple with control interface and file-like objects for STDIN/STDERR/STDOUT
:rtype: typing.NamedTuple(
'SubprocessExecuteAsyncResult',
[
('interface', subprocess.Popen[bytes]),
('stdin', typing.Optional[typing.IO[bytes]]),
('stderr', typing.Optional[typing.IO[bytes]]),
('stdout', typing.Optional[typing.IO[bytes]]),
("started", datetime.datetime),
]
)
:raises OSError: impossible to process STDIN
.. versionadded:: 1.2.0
.. versionchanged:: 2.1.0 Use typed NamedTuple as result
.. versionchanged:: 3.2.0 Expose cwd and env as optional keyword-only arguments
.. versionchanged:: 4.1.0 support chroot
"""
started = datetime.datetime.utcnow()
if env_patch is not None:
# make mutable copy
env = dict(copy.deepcopy(os.environ) if env is None else copy.deepcopy(env)) # type: ignore
env.update(env_patch) # type: ignore
process: "subprocess.Popen[bytes]" = subprocess.Popen(
args=[self._prepare_command(cmd=command, chroot_path=chroot_path)],
stdout=subprocess.PIPE if open_stdout else subprocess.DEVNULL,
stderr=subprocess.PIPE if open_stderr else subprocess.DEVNULL,
stdin=subprocess.PIPE,
shell=True,
cwd=cwd,
env=env,
universal_newlines=False,
**_subprocess_helpers.subprocess_kw,
)
if stdin is None:
process_stdin: _OptionalIOBytes = process.stdin
else:
stdin_str: bytes = self._string_bytes_bytearray_as_bytes(stdin)
try:
process.stdin.write(stdin_str)
except OSError as exc:
if exc.errno == errno.EINVAL:
# bpo-19612, bpo-30418: On Windows, stdin.write() fails
# with EINVAL if the child process exited or if the child
# process is still running but closed the pipe.
self.logger.warning("STDIN Send failed: closed PIPE")
elif exc.errno in (errno.EPIPE, errno.ESHUTDOWN):
self.logger.warning("STDIN Send failed: broken PIPE")
else:
_subprocess_helpers.kill_proc_tree(process.pid)
process.kill()
raise
try:
process.stdin.close()
except OSError as exc:
if exc.errno in (errno.EINVAL, errno.EPIPE, errno.ESHUTDOWN):
pass # PIPE already closed
else:
process.kill()
raise
process_stdin = None
return SubprocessExecuteAsyncResult(process, process_stdin, process.stderr, process.stdout, started)
def execute( # pylint: disable=arguments-differ
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
*,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
cwd: typing.Optional[typing.Union[str, bytes]] = None,
env: EnvT = None,
env_patch: EnvT = None,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command and wait for return code.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param cwd: Sets the current directory before the child is executed.
:type cwd: typing.Optional[typing.Union[str, bytes]]
:param env: Defines the environment variables for the new process.
:type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param env_patch: Defines the environment variables to ADD for the new process.
:type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 2.1.0 Allow parallel calls
"""
return super().execute(
command=command,
verbose=verbose,
timeout=timeout,
log_mask_re=log_mask_re,
stdin=stdin,
open_stdout=open_stdout,
open_stderr=open_stderr,
cwd=cwd,
env=env,
env_patch=env_patch,
**kwargs,
)
def __call__(
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
*,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
cwd: typing.Optional[typing.Union[str, bytes]] = None,
env: EnvT = None,
env_patch: EnvT = None,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command and wait for return code.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param cwd: Sets the current directory before the child is executed.
:type cwd: typing.Optional[typing.Union[str, bytes]]
:param env: Defines the environment variables for the new process.
:type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param env_patch: Defines the environment variables to ADD for the new process.
:type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 2.1.0 Allow parallel calls
"""
return super().__call__(
command=command,
verbose=verbose,
timeout=timeout,
log_mask_re=log_mask_re,
stdin=stdin,
open_stdout=open_stdout,
open_stderr=open_stderr,
cwd=cwd,
env=env,
env_patch=env_patch,
**kwargs,
)
def check_call( # pylint: disable=arguments-differ
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
error_info: typing.Optional[str] = None,
expected: typing.Iterable[ExitCodeT] = (proc_enums.EXPECTED,),
raise_on_err: bool = True,
*,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
cwd: typing.Optional[typing.Union[str, bytes]] = None,
env: EnvT = None,
env_patch: EnvT = None,
exception_class: CalledProcessErrorSubClassT = exceptions.CalledProcessError,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command and check for return code.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param error_info: Text for error details, if fail happens
:type error_info: typing.Optional[str]
:param expected: expected return codes (0 by default)
:type expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]]
:param raise_on_err: Raise exception on unexpected return code
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param cwd: Sets the current directory before the child is executed.
:type cwd: typing.Optional[typing.Union[str, bytes]]
:param env: Defines the environment variables for the new process.
:type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param env_patch: Defines the environment variables to ADD for the new process.
:type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
:type exception_class: typing.Type[exceptions.CalledProcessError]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
:raises CalledProcessError: Unexpected exit code
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 3.2.0 Exception class can be substituted
.. versionchanged:: 3.4.0 Expected is not optional, defaults os dependent
"""
return super().check_call(
command=command,
verbose=verbose,
timeout=timeout,
error_info=error_info,
expected=expected,
raise_on_err=raise_on_err,
log_mask_re=log_mask_re,
stdin=stdin,
open_stdout=open_stdout,
open_stderr=open_stderr,
cwd=cwd,
env=env,
env_patch=env_patch,
exception_class=exception_class,
**kwargs,
)
def check_stderr( # pylint: disable=arguments-differ
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
error_info: typing.Optional[str] = None,
raise_on_err: bool = True,
*,
expected: typing.Iterable[ExitCodeT] = (proc_enums.EXPECTED,),
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
cwd: typing.Optional[typing.Union[str, bytes]] = None,
env: EnvT = None,
env_patch: EnvT = None,
exception_class: CalledProcessErrorSubClassT = exceptions.CalledProcessError,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command expecting return code 0 and empty STDERR.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param error_info: Text for error details, if fail happens
:type error_info: typing.Optional[str]
:param raise_on_err: Raise exception on unexpected return code
:type raise_on_err: bool
:param expected: expected return codes (0 by default)
:type expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param cwd: Sets the current directory before the child is executed.
:type cwd: typing.Optional[typing.Union[str, bytes]]
:param env: Defines the environment variables for the new process.
:type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param env_patch: Defines the environment variables to ADD for the new process.
:type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
:type exception_class: typing.Type[exceptions.CalledProcessError]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
:raises CalledProcessError: Unexpected exit code or stderr presents
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 3.2.0 Exception class can be substituted
.. versionchanged:: 3.4.0 Expected is not optional, defaults os dependent
"""
return super().check_stderr(
command=command,
verbose=verbose,
timeout=timeout,
error_info=error_info,
raise_on_err=raise_on_err,
expected=expected,
log_mask_re=log_mask_re,
stdin=stdin,
open_stdout=open_stdout,
open_stderr=open_stderr,
cwd=cwd,
env=env,
env_patch=env_patch,
exception_class=exception_class,
**kwargs,
)