Esempio n. 1
0
 def test_warnings_when_executable_isnt_matched(self, monkeypatch, capsys):
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: True)
     monkeypatch.setattr(system.os.path, 'exists', lambda x: False)
     system.which('exedir')
     stdout, stderr = capsys.readouterr()
     assert 'Absolute path not found for executable: exedir' in stdout
     assert 'Ensure $PATH environment variable contains common executable locations' in stdout
Esempio n. 2
0
 def test_warnings_when_executable_isnt_matched(self, monkeypatch, capsys):
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: True)
     monkeypatch.setattr(system.os.path, 'exists', lambda x: False)
     system.which('exedir')
     cap = capsys.readouterr()
     assert 'Absolute path not found for executable: exedir' in cap.err
     assert 'Ensure $PATH environment variable contains common executable locations' in cap.err
Esempio n. 3
0
def which(executable):
    """
    Proxy function to ceph_volume.util.system.which because the ``system``
    module does import ``process``
    """
    from ceph_volume.util import system
    return system.which(executable)
Esempio n. 4
0
def which(executable):
    """
    Proxy function to ceph_volume.util.system.which because the ``system``
    module does import ``process``
    """
    from ceph_volume.util import system
    return system.which(executable)
Esempio n. 5
0
def run(command, run_on_host=False, **kw):
    """
    A real-time-logging implementation of a remote subprocess.Popen call where
    a command is just executed on the remote end and no other handling is done.

    :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
    """
    executable = which(command.pop(0), run_on_host)
    command.insert(0, executable)
    if run_on_host and path.isdir(host_rootfs):
        command = run_host_cmd + command
    stop_on_error = kw.pop('stop_on_error', True)
    command_msg = obfuscate(command, kw.pop('obfuscate', 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,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        close_fds=True,
        **kw
    )

    while True:
        reads, _, _ = select(
            [process.stdout.fileno(), process.stderr.fileno()],
            [], []
        )
        log_descriptors(reads, process, terminal_logging)

        if process.poll() is not None:
            # ensure we do not have anything pending in stdout or stderr
            log_descriptors(reads, process, terminal_logging)

            break

    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:
            if terminal_logging:
                terminal.warning(msg)
            logger.warning(msg)
Esempio n. 6
0
 def test_run_on_host_not_found(self, mock_find_executable_on_host):
     mock_find_executable_on_host(stdout="", stderr="some stderr message\n")
     assert system.which('lvs', run_on_host=True) == 'lvs'
Esempio n. 7
0
 def test_warnings_when_executable_isnt_matched(self, monkeypatch, capsys):
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: True)
     monkeypatch.setattr(system.os.path, 'exists', lambda x: False)
     system.which('exedir')
     cap = capsys.readouterr()
     assert 'Executable exedir not in PATH' in cap.err
Esempio n. 8
0
 def test_executable_exists_as_file(self, monkeypatch):
     monkeypatch.setattr(system.os, 'getenv', lambda x, y: '')
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: x != 'ceph')
     monkeypatch.setattr(system.os.path, 'exists', lambda x: x != 'ceph')
     assert system.which('ceph') == '/usr/local/bin/ceph'
Esempio n. 9
0
 def test_executable_does_not_exist(self, monkeypatch):
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: False)
     monkeypatch.setattr(system.os.path, 'exists', lambda x: False)
     assert system.which('exedir') == 'exedir'
Esempio n. 10
0
 def test_executable_exists_as_file(self, monkeypatch):
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: True)
     monkeypatch.setattr(system.os.path, 'exists', lambda x: True)
     assert system.which('ceph') == '/usr/local/bin/ceph'
Esempio n. 11
0
 def test_executable_does_not_exist(self, monkeypatch):
     monkeypatch.setattr(system.os.path, 'isfile', lambda x: False)
     monkeypatch.setattr(system.os.path, 'exists', lambda x: False)
     assert system.which('exedir') == 'exedir'
Esempio n. 12
0
def call(command, run_on_host=False, **kw):
    """
    Similar to ``subprocess.Popen`` with the following changes:

    * returns stdout, stderr, and exit code (vs. just the exit code)
    * logs the full contents of stderr and stdout (separately) to the file log

    By default, no terminal output is given, not even the command that is going
    to run.

    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
    :param verbose_on_failure: On a non-zero exit status, it will forcefully set logging ON for
                               the terminal. Defaults to True
    """
    executable = which(command.pop(0), run_on_host)
    command.insert(0, executable)
    if run_on_host and path.isdir(host_rootfs):
        command = run_host_cmd + command
    terminal_verbose = kw.pop('terminal_verbose', False)
    logfile_verbose = kw.pop('logfile_verbose', True)
    verbose_on_failure = kw.pop('verbose_on_failure', True)
    show_command = kw.pop('show_command', False)
    command_msg = "Running command: %s" % ' '.join(command)
    stdin = kw.pop('stdin', None)
    logger.info(command_msg)
    if show_command:
        terminal.write(command_msg)

    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE,
        close_fds=True,
        **kw
    )

    if stdin:
        stdout_stream, stderr_stream = process.communicate(as_bytes(stdin))
    else:
        stdout_stream = process.stdout.read()
        stderr_stream = process.stderr.read()
    returncode = process.wait()
    if not isinstance(stdout_stream, str):
        stdout_stream = stdout_stream.decode('utf-8')
    if not isinstance(stderr_stream, str):
        stderr_stream = stderr_stream.decode('utf-8')
    stdout = stdout_stream.splitlines()
    stderr = stderr_stream.splitlines()

    if returncode != 0:
        # set to true so that we can log the stderr/stdout that callers would
        # do anyway as long as verbose_on_failure is set (defaults to True)
        if verbose_on_failure:
            terminal_verbose = True
        # logfiles aren't disruptive visually, unlike the terminal, so this
        # should always be on when there is a failure
        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, logfile_verbose)
    for line in stderr:
        log_output('stderr', line, terminal_verbose, logfile_verbose)
    return stdout, stderr, returncode