Beispiel #1
0
def execute(cmd,
            process_input=None,
            addl_env=None,
            check_exit_code=True,
            return_stderr=False,
            log_fail_as_error=True,
            extra_ok_codes=None,
            run_as_root=False):
    try:
        if (process_input is None
                or isinstance(process_input, six.binary_type)):
            _process_input = process_input
        else:
            _process_input = process_input.encode('utf-8')
        if run_as_root and cfg.CONF.AGENT.root_helper_daemon:
            returncode, _stdout, _stderr = (execute_rootwrap_daemon(
                cmd, process_input, addl_env))
        else:
            obj, cmd = create_process(cmd,
                                      run_as_root=run_as_root,
                                      addl_env=addl_env)
            _stdout, _stderr = obj.communicate(_process_input)
            returncode = obj.returncode
            obj.stdin.close()
        _stdout = utils.safe_decode_utf8(_stdout)
        _stderr = utils.safe_decode_utf8(_stderr)

        extra_ok_codes = extra_ok_codes or []
        if returncode and returncode not in extra_ok_codes:
            msg = _("Exit code: %(returncode)d; "
                    "Stdin: %(stdin)s; "
                    "Stdout: %(stdout)s; "
                    "Stderr: %(stderr)s") % {
                        'returncode': returncode,
                        'stdin': process_input or '',
                        'stdout': _stdout,
                        'stderr': _stderr
                    }

            if log_fail_as_error:
                LOG.error(msg)
            if check_exit_code:
                raise RuntimeError(msg)
        else:
            LOG.debug("Exit code: %d", returncode)

    finally:
        # NOTE(termie): this appears to be necessary to let the subprocess
        #               call clean something up in between calls, without
        #               it two execute calls in a row hangs the second one
        greenthread.sleep(0)

    return (_stdout, _stderr) if return_stderr else _stdout
Beispiel #2
0
def execute(cmd,
            process_input=None,
            addl_env=None,
            check_exit_code=True,
            return_stderr=False,
            log_fail_as_error=True,
            extra_ok_codes=None,
            run_as_root=False,
            do_decode=True):

    try:
        if (process_input is None
                or isinstance(process_input, six.binary_type)):
            _process_input = process_input
        else:
            _process_input = process_input.encode('utf-8')
        obj, cmd = create_process(cmd, addl_env=addl_env)
        _stdout, _stderr = obj.communicate(_process_input)
        obj.stdin.close()
        _stdout = utils.safe_decode_utf8(_stdout)
        _stderr = utils.safe_decode_utf8(_stderr)

        m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdin: %(stdin)s\n"
              "Stdout: %(stdout)s\nStderr: %(stderr)s") % \
            {'cmd': cmd,
             'code': obj.returncode,
             'stdin': process_input or '',
             'stdout': _stdout,
             'stderr': _stderr}

        extra_ok_codes = extra_ok_codes or []
        if obj.returncode and obj.returncode in extra_ok_codes:
            obj.returncode = None

        log_msg = m.strip().replace('\n', '; ')
        if obj.returncode and log_fail_as_error:
            LOG.error(log_msg)
        else:
            LOG.debug(log_msg)

        if obj.returncode and check_exit_code:
            raise RuntimeError(m)
    finally:
        # NOTE(termie): this appears to be necessary to let the subprocess
        #               call clean something up in between calls, without
        #               it two execute calls in a row hangs the second one
        greenthread.sleep(0)

    return (_stdout, _stderr) if return_stderr else _stdout
Beispiel #3
0
def execute(
    cmd,
    process_input=None,
    addl_env=None,
    check_exit_code=True,
    return_stderr=False,
    log_fail_as_error=True,
    extra_ok_codes=None,
    run_as_root=False,
):
    try:
        if process_input is not None:
            _process_input = encodeutils.to_utf8(process_input)
        else:
            _process_input = None
        if run_as_root and cfg.CONF.AGENT.root_helper_daemon:
            returncode, _stdout, _stderr = execute_rootwrap_daemon(cmd, process_input, addl_env)
        else:
            obj, cmd = create_process(cmd, run_as_root=run_as_root, addl_env=addl_env)
            _stdout, _stderr = obj.communicate(_process_input)
            returncode = obj.returncode
            obj.stdin.close()
        _stdout = utils.safe_decode_utf8(_stdout)
        _stderr = utils.safe_decode_utf8(_stderr)

        extra_ok_codes = extra_ok_codes or []
        if returncode and returncode not in extra_ok_codes:
            msg = _("Exit code: %(returncode)d; " "Stdin: %(stdin)s; " "Stdout: %(stdout)s; " "Stderr: %(stderr)s") % {
                "returncode": returncode,
                "stdin": process_input or "",
                "stdout": _stdout,
                "stderr": _stderr,
            }

            if log_fail_as_error:
                LOG.error(msg)
            if check_exit_code:
                raise RuntimeError(msg)
        else:
            LOG.debug("Exit code: %d", returncode)

    finally:
        # NOTE(termie): this appears to be necessary to let the subprocess
        #               call clean something up in between calls, without
        #               it two execute calls in a row hangs the second one
        greenthread.sleep(0)

    return (_stdout, _stderr) if return_stderr else _stdout
Beispiel #4
0
def execute(cmd, process_input=None, addl_env=None,
            check_exit_code=True, return_stderr=False, log_fail_as_error=True,
            extra_ok_codes=None, run_as_root=False, do_decode=True):

    try:
        if (process_input is None or
            isinstance(process_input, six.binary_type)):
            _process_input = process_input
        else:
            _process_input = process_input.encode('utf-8')
        obj, cmd = create_process(cmd, addl_env=addl_env)
        _stdout, _stderr = obj.communicate(_process_input)
        obj.stdin.close()
        _stdout = utils.safe_decode_utf8(_stdout)
        _stderr = utils.safe_decode_utf8(_stderr)

        m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdin: %(stdin)s\n"
              "Stdout: %(stdout)s\nStderr: %(stderr)s") % \
            {'cmd': cmd,
             'code': obj.returncode,
             'stdin': process_input or '',
             'stdout': _stdout,
             'stderr': _stderr}

        extra_ok_codes = extra_ok_codes or []
        if obj.returncode and obj.returncode in extra_ok_codes:
            obj.returncode = None

        log_msg = m.strip().replace('\n', '; ')
        if obj.returncode and log_fail_as_error:
            LOG.error(log_msg)
        else:
            LOG.debug(log_msg)

        if obj.returncode and check_exit_code:
            raise RuntimeError(m)
    finally:
        # NOTE(termie): this appears to be necessary to let the subprocess
        #               call clean something up in between calls, without
        #               it two execute calls in a row hangs the second one
        greenthread.sleep(0)

    return (_stdout, _stderr) if return_stderr else _stdout
 def test_py3_decoded_invalid_bytes(self):
     s = bytes('test-py2', 'utf_16')
     decoded_str = utils.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
 def test_py3_decoded_valid_bytes(self):
     s = bytes('test-py2', 'utf-8')
     decoded_str = utils.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
     self.assertEqual(s, decoded_str.encode('utf-8'))
 def test_py2_does_nothing(self):
     s = 'test-py2'
     self.assertIs(s, utils.safe_decode_utf8(s))
Beispiel #8
0
 def _read(self, stream, queue):
     data = stream.readline()
     if data:
         data = common_utils.safe_decode_utf8(data.strip())
         queue.put(data)
         return data
Beispiel #9
0
 def test_py3_decoded_invalid_bytes(self):
     s = bytes('test-py2', 'utf_16')
     decoded_str = utils.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
Beispiel #10
0
 def test_py3_decoded_valid_bytes(self):
     s = bytes('test-py2', 'utf-8')
     decoded_str = utils.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
     self.assertEqual(s, decoded_str.encode('utf-8'))
Beispiel #11
0
 def test_py2_does_nothing(self):
     s = 'test-py2'
     self.assertIs(s, utils.safe_decode_utf8(s))
Beispiel #12
0
 def _read(self, stream, queue):
     data = stream.readline()
     if data:
         data = common_utils.safe_decode_utf8(data.strip())
         queue.put(data)
         return data