Пример #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):

    if process_input is not None:
        _process_input = encodeutils.to_utf8(process_input)
    else:
        _process_input = None
    obj, cmd = create_process(cmd, addl_env=addl_env, tpool_proxy=False)
    _stdout, _stderr = avoid_blocking_call(obj.communicate, _process_input)
    obj.stdin.close()
    _stdout = helpers.safe_decode_utf8(_stdout)
    _stderr = helpers.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 exceptions.ProcessExecutionError(m, returncode=obj.returncode)
    return (_stdout, _stderr) if return_stderr else _stdout
Пример #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):
    LOG.info('%s() caller: %s()',
             sys._getframe(0).f_code.co_name,
             sys._getframe(1).f_code.co_name)
    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 = helpers.safe_decode_utf8(_stdout)
        _stderr = helpers.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 exceptions.ProcessExecutionError(msg,
                                                       returncode=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
        time.sleep(0)

    return (_stdout, _stderr) if return_stderr else _stdout
Пример #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,
            privsep_exec=False):
    try:
        if process_input is not None:
            _process_input = encodeutils.to_utf8(process_input)
        else:
            _process_input = None

        if run_as_root and privsep_exec:
            _stdout, _stderr, returncode = priv_utils.execute_process(
                cmd, _process_input, addl_env)
        elif run_as_root and cfg.CONF.AGENT.root_helper_daemon:
            _stdout, _stderr, returncode = execute_rootwrap_daemon(
                cmd, process_input, addl_env)
        else:
            _stdout, _stderr, returncode = _execute_process(
                cmd, _process_input, addl_env, run_as_root)

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

            if log_fail_as_error:
                LOG.error(msg)
            if check_exit_code:
                raise exceptions.ProcessExecutionError(msg,
                                                       returncode=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
        time.sleep(0)

    return (_stdout, _stderr) if return_stderr else _stdout
Пример #4
0
    def _test_kill_process(self, pid, raise_exception=False,
                           kill_signal=signal.SIGKILL, pid_killed=True):
        if raise_exception:
            exc = exceptions.ProcessExecutionError('', returncode=0)
        else:
            exc = None
        with mock.patch.object(utils, 'execute',
                               side_effect=exc) as mock_execute:
            with mock.patch.object(utils, 'process_is_running',
                                   return_value=not pid_killed):
                utils.kill_process(pid, kill_signal, run_as_root=True)

        mock_execute.assert_called_with(['kill', '-%d' % kill_signal, pid],
                                        run_as_root=True)
Пример #5
0
 def test_plug_new(self):
     with mock.patch('neutron.agent.ovsdb.impl_idl._connection'):
         bridge = 'br-int'
         namespace = '01234567-1234-1234-99'
         with mock.patch.object(ovs_lib.OVSBridge,
                                'delete_port') as delete_port:
             with mock.patch.object(ovs_lib.OVSBridge, 'replace_port'):
                 ovs = interface.OVSInterfaceDriver(self.conf)
                 reraise = mock.patch.object(excutils,
                                             'save_and_reraise_exception')
                 reraise.start()
                 proEr = exceptions.ProcessExecutionError('', 2)
                 processExecutionError = mock.Mock(side_effect=proEr)
                 ip = self.ip.return_value
                 ip.ensure_namespace.side_effect = processExecutionError
                 ovs.plug_new('01234567-1234-1234-99',
                              'port-1234',
                              'tap0',
                              'aa:bb:cc:dd:ee:ff',
                              bridge=bridge,
                              namespace=namespace,
                              prefix='veth',
                              mtu=9000)
                 delete_port.assert_called_once_with('tap0')
Пример #6
0
 def test_returns_empty_list_for_exit_code_1(self):
     with mock.patch.object(utils, 'execute',
                            side_effect=exceptions.ProcessExecutionError(
                                '', returncode=1)):
         self.assertEqual([], utils.find_child_pids(-1))
Пример #7
0
 def test_raises_exception_returncode_0(self):
     with testtools.ExpectedException(exceptions.ProcessExecutionError):
         self.m_execute.side_effect = \
             exceptions.ProcessExecutionError('', returncode=0)
         utils.find_parent_pid(-1)
Пример #8
0
 def test_returns_none_for_no_valid_pid(self):
     self.m_execute.side_effect = exceptions.ProcessExecutionError(
         '', returncode=1)
     self.assertIsNone(utils.find_parent_pid(-1))