<s> import os <EOL> import pwd <EOL> import uuid <EOL> from oslo_config import cfg <EOL> from eventlet . green import subprocess <EOL> from st2common import log as logging <EOL> from st2actions . runners import ActionRunner <EOL> from st2actions . runners import ShellRunnerMixin <EOL> from st2common . models . system . action import ShellCommandAction <EOL> from st2common . models . system . action import ShellScriptAction <EOL> from st2common . constants . action import LIVEACTION_STATUS_SUCCEEDED <EOL> from st2common . constants . action import LIVEACTION_STATUS_FAILED <EOL> from st2common . constants . action import LIVEACTION_STATUS_TIMED_OUT <EOL> from st2common . constants . runners import LOCAL_RUNNER_DEFAULT_ACTION_TIMEOUT <EOL> from st2common . util . misc import strip_shell_chars <EOL> from st2common . util . green . shell import run_command <EOL> from st2common . util . shell import kill_process <EOL> import st2common . util . jsonify as jsonify <EOL> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> LOG = logging . getLogger ( __name__ ) <EOL> DEFAULT_KWARG_OP = '<STR_LIT>' <EOL> LOGGED_USER_USERNAME = pwd . getpwuid ( os . getuid ( ) ) [ <NUM_LIT:0> ] <EOL> RUNNER_SUDO = '<STR_LIT>' <EOL> RUNNER_ON_BEHALF_USER = '<STR_LIT:user>' <EOL> RUNNER_COMMAND = '<STR_LIT>' <EOL> RUNNER_CWD = '<STR_LIT>' <EOL> RUNNER_ENV = '<STR_LIT>' <EOL> RUNNER_KWARG_OP = '<STR_LIT>' <EOL> RUNNER_TIMEOUT = '<STR_LIT>' <EOL> def get_runner ( ) : <EOL> return LocalShellRunner ( str ( uuid . uuid4 ( ) ) ) <EOL> class LocalShellRunner ( ActionRunner , ShellRunnerMixin ) : <EOL> """<STR_LIT>""" <EOL> KEYS_TO_TRANSFORM = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , runner_id ) : <EOL> super ( LocalShellRunner , self ) . __init__ ( runner_id = runner_id ) <EOL> def pre_run ( self ) : <EOL> self . _sudo = self . runner_parameters . get ( RUNNER_SUDO , False ) <EOL> self . _on_behalf_user = self . context . get ( RUNNER_ON_BEHALF_USER , LOGGED_USER_USERNAME ) <EOL> self . _user = cfg . CONF . system_user . user <EOL> self . _cwd = self . runner_parameters . get ( RUNNER_CWD , None ) <EOL> self . _env = self . runner_parameters . get ( RUNNER_ENV , { } ) <EOL> self . _env = self . _env or { } <EOL> self . _kwarg_op = self . runner_parameters . get ( RUNNER_KWARG_OP , DEFAULT_KWARG_OP ) <EOL> self . _timeout = self . runner_parameters . get ( RUNNER_TIMEOUT , <EOL> LOCAL_RUNNER_DEFAULT_ACTION_TIMEOUT ) <EOL> def run ( self , action_parameters ) : <EOL> env_vars = self . _env <EOL> if not self . entry_point : <EOL> script_action = False <EOL> command = self . runner_parameters . get ( RUNNER_COMMAND , None ) <EOL> action = ShellCommandAction ( name = self . action_name , <EOL> action_exec_id = str ( self . liveaction_id ) , <EOL> command = command , <EOL> user = self . _user , <EOL> env_vars = env_vars , <EOL> sudo = self . _sudo , <EOL> timeout = self . _timeout ) <EOL> else : <EOL> script_action = True <EOL> script_local_path_abs = self . entry_point <EOL> positional_args , named_args = self . _get_script_args ( action_parameters ) <EOL> named_args = self . _transform_named_args ( named_args ) <EOL> action = ShellScriptAction ( name = self . action_name , <EOL> action_exec_id = str ( self . liveaction_id ) , <EOL> script_local_path_abs = script_local_path_abs , <EOL> named_args = named_args , <EOL> positional_args = positional_args , <EOL> user = self . _user , <EOL> env_vars = env_vars , <EOL> sudo = self . _sudo , <EOL> timeout = self . _timeout , <EOL> cwd = self . _cwd ) <EOL> args = action . get_full_command_string ( ) <EOL> if script_action : <EOL> args = '<STR_LIT>' % ( script_local_path_abs , args ) <EOL> env = os . environ . copy ( ) <EOL> env . update ( env_vars ) <EOL> st2_env_vars = self . _get_common_action_env_variables ( ) <EOL> env . update ( st2_env_vars ) <EOL> LOG . info ( '<STR_LIT>' , self . runner_id ) <EOL> LOG . info ( '<STR_LIT>' % <EOL> ( action . name , action . action_exec_id , args , action . user , action . sudo ) ) <EOL> exit_code , stdout , stderr , timed_out = run_command ( cmd = args , stdin = None , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> shell = True , <EOL> cwd = self . _cwd , <EOL> env = env , <EOL> timeout = self . _timeout , <EOL> preexec_func = os . setsid , <EOL> kill_func = kill_process ) <EOL> error = None <EOL> if timed_out : <EOL> error = '<STR_LIT>' % ( self . _timeout ) <EOL> exit_code = - <NUM_LIT:9> <EOL> succeeded = ( exit_code == <NUM_LIT:0> ) <EOL> result = { <EOL> '<STR_LIT>' : not succeeded , <EOL> '<STR_LIT>' : succeeded , <EOL> '<STR_LIT>' : exit_code , <EOL> '<STR_LIT>' : strip_shell_chars ( stdout ) , <EOL> '<STR_LIT>' : strip_shell_chars ( stderr ) <EOL> } <EOL> if error : <EOL> result [ '<STR_LIT:error>' ] = error <EOL> if exit_code == <NUM_LIT:0> : <EOL> status = LIVEACTION_STATUS_SUCCEEDED <EOL> elif timed_out : <EOL> status = LIVEACTION_STATUS_TIMED_OUT <EOL> else : <EOL> status = LIVEACTION_STATUS_FAILED <EOL> return ( status , jsonify . json_loads ( result , LocalShellRunner . KEYS_TO_TRANSFORM ) , None ) </s>
