def _run_command(self, command, env=None, return_output=False): """Run the given command with the given environment. Return the output as a string. """ assert isinstance( command, (list, tuple)), ("list or tuple argument expected, got: %s" % command) # export target_host, target_hostname, target_fqdn # for use in __remote_{exec,copy} scripts os_environ = os.environ.copy() os_environ['__target_host'] = self.target_host[0] os_environ['__target_hostname'] = self.target_host[1] os_environ['__target_fqdn'] = self.target_host[2] self.log.debug("Remote run: %s", command) try: output, errout = exec_util.call_get_output(command, env=os_environ) self.log.debug("Remote stdout: {}".format(output)) # Currently, stderr is not captured. # self.log.debug("Remote stderr: {}".format(errout)) if return_output: return output.decode() except subprocess.CalledProcessError as e: exec_util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) except UnicodeDecodeError: raise DecodeError(command)
def _run_command(self, command, env=None, return_output=False): """Run the given command with the given environment. Return the output as a string. """ assert isinstance(command, (list, tuple)), ( "list or tuple argument expected, got: %s" % command) # export target_host, target_hostname, target_fqdn # for use in __remote_{exec,copy} scripts os_environ = os.environ.copy() os_environ['__target_host'] = self.target_host[0] os_environ['__target_hostname'] = self.target_host[1] os_environ['__target_fqdn'] = self.target_host[2] self.log.trace("Remote run: %s", command) try: if self.quiet_mode: stderr = subprocess.DEVNULL else: stderr = None output, errout = exec_util.call_get_output( command, env=os_environ, stderr=stderr) self.log.trace("Command: {}; remote stdout: {}".format( command, output)) # Currently, stderr is not captured. # self.log.trace("Remote stderr: {}".format(errout)) if return_output: return output.decode() except subprocess.CalledProcessError as e: exec_util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) except UnicodeDecodeError: raise DecodeError(command)
def run(self, command, env=None, return_output=False, message_prefix=None, save_output=True, quiet_mode=False): """Run the given command with the given environment. Return the output as a string. """ assert isinstance(command, (list, tuple)), ( "list or tuple argument expected, got: %s" % command) if env is None: env = os.environ.copy() # Export __target_host, __target_hostname, __target_fqdn # for use in __remote_{copy,exec} scripts env['__target_host'] = self.target_host[0] env['__target_hostname'] = self.target_host[1] env['__target_fqdn'] = self.target_host[2] # Export for emulator env['__cdist_object_marker'] = self.object_marker_name if message_prefix: message = cdist.message.Message(message_prefix, self.messages_path) env.update(message.env) self.log.trace("Local run: %s", command) try: if self.quiet_mode or quiet_mode: stderr = subprocess.DEVNULL else: stderr = None if save_output: output, errout = exec_util.call_get_output( command, env=env, stderr=stderr) self.log.trace("Command: {}; local stdout: {}".format( command, output)) # Currently, stderr is not captured. # self.log.trace("Local stderr: {}".format(errout)) if return_output: return output.decode() else: # In some cases no output is saved. # This is used for shell command, stdout and stderr # must not be catched. if self.quiet_mode or quiet_mode: stdout = subprocess.DEVNULL else: stdout = None subprocess.check_call(command, env=env, stderr=stderr, stdout=stdout) except subprocess.CalledProcessError as e: exec_util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) finally: if message_prefix: message.merge_messages()
def run(self, command, env=None, return_output=False, message_prefix=None, save_output=True): """Run the given command with the given environment. Return the output as a string. """ self.log.debug("Local run: %s", command) assert isinstance( command, (list, tuple)), ("list or tuple argument expected, got: %s" % command) if env is None: env = os.environ.copy() # Export __target_host, __target_hostname, __target_fqdn # for use in __remote_{copy,exec} scripts env['__target_host'] = self.target_host[0] env['__target_hostname'] = self.target_host[1] env['__target_fqdn'] = self.target_host[2] # Export for emulator env['__cdist_object_marker'] = self.object_marker_name if message_prefix: message = cdist.message.Message(message_prefix, self.messages_path) env.update(message.env) try: if save_output: output, errout = exec_util.call_get_output(command, env=env) self.log.debug("Local stdout: {}".format(output)) # Currently, stderr is not captured. # self.log.debug("Local stderr: {}".format(errout)) if return_output: return output.decode() else: # In some cases no output is saved. # This is used for shell command, stdout and stderr # must not be catched. subprocess.check_call(command, env=env) except subprocess.CalledProcessError as e: exec_util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) finally: if message_prefix: message.merge_messages()
def _run_command(self, command, env=None, return_output=False, stdout=None, stderr=None): """Run the given command with the given environment. Return the output as a string. """ assert isinstance( command, (list, tuple)), ("list or tuple argument expected, got: %s" % command) if return_output and stdout is not subprocess.PIPE: self.log.debug("return_output is True, ignoring stdout") close_stdout = False close_stderr = False if not return_output and stdout is None: stdout = util.get_std_fd(self.stdout_base_path, 'remote') close_stdout = True if stderr is None: stderr = util.get_std_fd(self.stderr_base_path, 'remote') close_stderr = True # export target_host, target_hostname, target_fqdn # for use in __remote_{exec,copy} scripts os_environ = os.environ.copy() os_environ['__target_host'] = self.target_host[0] os_environ['__target_hostname'] = self.target_host[1] os_environ['__target_fqdn'] = self.target_host[2] self.log.trace("Remote run: %s", command) try: if self.quiet_mode: stderr = subprocess.DEVNULL if return_output: output = subprocess.check_output(command, env=os_environ, stderr=stderr).decode() else: subprocess.check_call(command, env=os_environ, stdout=stdout, stderr=stderr) output = None util.log_std_fd(self.log, command, stderr, 'Remote stderr') util.log_std_fd(self.log, command, stdout, 'Remote stdout') return output except subprocess.CalledProcessError as e: util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) except UnicodeDecodeError: raise DecodeError(command) finally: if close_stdout: stdout.close() if close_stderr: stderr.close()
def run(self, command, env=None, return_output=False, message_prefix=None, stdout=None, stderr=None, save_output=True, quiet_mode=False): """Run the given command with the given environment. Return the output as a string. """ assert isinstance( command, (list, tuple)), ("list or tuple argument expected, got: %s" % command) quiet = self.quiet_mode or quiet_mode do_save_output = save_output and not quiet and self.save_output_streams close_stdout = False close_stderr = False if quiet: stderr = subprocess.DEVNULL stdout = subprocess.DEVNULL elif do_save_output: if not return_output and stdout is None: stdout = util.get_std_fd(self.stdout_base_path, 'local') close_stdout = True if stderr is None: stderr = util.get_std_fd(self.stderr_base_path, 'local') close_stderr = True if env is None: env = os.environ.copy() # Export __target_host, __target_hostname, __target_fqdn # for use in __remote_{copy,exec} scripts env['__target_host'] = self.target_host[0] env['__target_hostname'] = self.target_host[1] env['__target_fqdn'] = self.target_host[2] # Export for emulator env['__cdist_object_marker'] = self.object_marker_name if message_prefix: message = cdist.message.Message(message_prefix, self.messages_path) env.update(message.env) self.log.trace("Local run: %s", command) try: if return_output: output = subprocess.check_output(command, env=env, stderr=stderr).decode() else: subprocess.check_call(command, env=env, stderr=stderr, stdout=stdout) output = None if do_save_output: util.log_std_fd(self.log, command, stderr, 'Local stderr') util.log_std_fd(self.log, command, stdout, 'Local stdout') return output except subprocess.CalledProcessError as e: util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) finally: if message_prefix: message.merge_messages() if close_stdout: stdout.close() if close_stderr: stderr.close()