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):

    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
def execute(cmd):
    if not cmd:
        return
    cmd = map(str, cmd)
    LOG.debug("Running command: %s", cmd)
    env = os.environ.copy()
    obj = utils.subprocess_popen(cmd, shell=False,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=env)

    _stdout, _stderr = obj.communicate()
    _stdout = helpers.safe_decode_utf8(_stdout)
    _stderr = helpers.safe_decode_utf8(_stderr)
    msg = ('Command: %(cmd)s Exit code: %(returncode)s '
           'Stdout: %(stdout)s Stderr: %(stderr)s' %
           {'cmd': cmd,
            'returncode': obj.returncode,
            'stdout': _stdout,
            'stderr': _stderr})
    LOG.debug(msg)
    obj.stdin.close()
    # Pass the output to calling process
    sys.stdout.write(msg)
    sys.stdout.flush()
    return obj.returncode
def execute(cmd):
    if not cmd:
        return
    cmd = list(map(str, cmd))
    LOG.debug("Running command: %s", cmd)
    env = os.environ.copy()
    obj = utils.subprocess_popen(cmd,
                                 shell=False,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=env)

    _stdout, _stderr = obj.communicate()
    _stdout = helpers.safe_decode_utf8(_stdout)
    _stderr = helpers.safe_decode_utf8(_stderr)
    msg = ('Command: %(cmd)s Exit code: %(returncode)s '
           'Stdout: %(stdout)s Stderr: %(stderr)s' % {
               'cmd': cmd,
               'returncode': obj.returncode,
               'stdout': _stdout,
               'stderr': _stderr
           })
    LOG.debug(msg)
    obj.stdin.close()
    # Pass the output to calling process
    sys.stdout.write(msg)
    sys.stdout.flush()
    return obj.returncode
Beispiel #4
0
def _execute_process(cmd, _process_input, addl_env, run_as_root):
    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)
    return _stdout, _stderr, returncode
Beispiel #5
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
Beispiel #6
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 = 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 ProcessExecutionError(msg, returncode=returncode)
        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 #7
0
def execute_rootwrap_daemon(cmd, process_input, addl_env):
    cmd = list(map(str, addl_env_args(addl_env) + cmd))
    # NOTE(twilson) oslo_rootwrap.daemon will raise on filter match
    # errors, whereas oslo_rootwrap.cmd converts them to return codes.
    # In practice, no neutron code should be trying to execute something that
    # would throw those errors, and if it does it should be fixed as opposed to
    # just logging the execution error.
    LOG.debug("Running command (rootwrap daemon): %s", cmd)
    client = RootwrapDaemonHelper.get_client()
    try:
        returncode, _stdout, _stderr = client.execute(cmd, process_input)
    except Exception:
        with excutils.save_and_reraise_exception():
            LOG.error("Rootwrap error running command: %s", cmd)

    _stdout = helpers.safe_decode_utf8(_stdout)
    _stderr = helpers.safe_decode_utf8(_stderr)
    return _stdout, _stderr, returncode
Beispiel #8
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 not None:
            _process_input = encodeutils.to_utf8(process_input)
        else:
            _process_input = None
        obj, cmd = create_process(cmd, addl_env=addl_env)
        _stdout, _stderr = 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 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 #9
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):

    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
 def sh(self, cmd, as_root=True):
     if as_root and self.root_helper:
         cmd = "%s %s" % (self.root_helper, cmd)
     LOG.debug("%(name)s: Running command: %(cmd)s",
               {'name': self.name, 'cmd': cmd})
     ret = ''
     try:
         sanitized_cmd = encodeutils.to_utf8(cmd)
         data = subprocess.check_output(
             sanitized_cmd, stderr=subprocess.STDOUT, shell=True)
         ret = helpers.safe_decode_utf8(data)
     except Exception as e:
         LOG.error("In running command: %(cmd)s: %(exc)s",
                   {'cmd': cmd, 'exc': str(e)})
     LOG.debug("%(name)s: Command output: %(ret)s",
               {'name': self.name, 'ret': ret})
     return ret
Beispiel #11
0
 def _local_protocol_name_map(self):
     local_protocol_name_map = {}
     try:
         class protoent(ctypes.Structure):
             _fields_ = [("p_name", ctypes.c_char_p),
                         ("p_aliases", ctypes.POINTER(ctypes.c_char_p)),
                         ("p_proto", ctypes.c_int)]
         libc.getprotoent.restype = ctypes.POINTER(protoent)
         libc.setprotoent(0)
         while True:
             pr = libc.getprotoent()
             if not pr:
                 break
             r = pr[0]
             p_name = helpers.safe_decode_utf8(r.p_name)
             local_protocol_name_map[str(r.p_proto)] = p_name
     except Exception:
         LOG.exception("Unable to create local protocol name map: %s",
                       sys.exc_info()[0])
     finally:
         libc.endprotoent()
     return local_protocol_name_map
Beispiel #12
0
 def _local_protocol_name_map(self):
     local_protocol_name_map = {}
     try:
         class protoent(ctypes.Structure):
             _fields_ = [("p_name", ctypes.c_char_p),
                         ("p_aliases", ctypes.POINTER(ctypes.c_char_p)),
                         ("p_proto", ctypes.c_int)]
         libc.getprotoent.restype = ctypes.POINTER(protoent)
         libc.setprotoent(0)
         while True:
             pr = libc.getprotoent()
             if not pr:
                 break
             r = pr[0]
             p_name = helpers.safe_decode_utf8(r.p_name)
             local_protocol_name_map[str(r.p_proto)] = p_name
     except Exception:
         LOG.exception("Unable to create local protocol name map: %s",
                       sys.exc_info()[0])
     finally:
         libc.endprotoent()
     return local_protocol_name_map
Beispiel #13
0
 def _read(self, stream, queue):
     data = stream.readline()
     if data:
         data = helpers.safe_decode_utf8(data.strip())
         queue.put(data)
         return data
Beispiel #14
0
 def test_py3_decoded_valid_bytes(self):
     s = bytes('test-py2', 'utf-8')
     decoded_str = helpers.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
     self.assertEqual(s, decoded_str.encode('utf-8'))
Beispiel #15
0
 def test_py3_decoded_invalid_bytes(self):
     s = bytes('test-py2', 'utf_16')
     decoded_str = helpers.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
Beispiel #16
0
def safe_decode_utf8(s):
    return helpers.safe_decode_utf8(s)
Beispiel #17
0
 def test_py3_decoded_invalid_bytes(self):
     s = bytes('test-py2', 'utf_16')
     decoded_str = helpers.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
Beispiel #18
0
 def test_py3_decoded_valid_bytes(self):
     s = bytes('test-py2', 'utf-8')
     decoded_str = helpers.safe_decode_utf8(s)
     self.assertIsInstance(decoded_str, six.text_type)
     self.assertEqual(s, decoded_str.encode('utf-8'))
Beispiel #19
0
 def test_py2_does_nothing(self):
     s = 'test-py2'
     self.assertIs(s, helpers.safe_decode_utf8(s))
Beispiel #20
0
 def test_py2_does_nothing(self):
     s = 'test-py2'
     self.assertIs(s, helpers.safe_decode_utf8(s))
Beispiel #21
0
def safe_decode_utf8(s):
    return helpers.safe_decode_utf8(s)
Beispiel #22
0
 def _read(self, stream, queue):
     data = stream.readline()
     if data:
         data = helpers.safe_decode_utf8(data.strip())
         queue.put(data)
         return data