<s> import uuid <EOL> import os <EOL> import tempfile <EOL> import yaml <EOL> from oslo_config import cfg <EOL> from eventlet . green import subprocess <EOL> from st2common . util . green . shell import run_command <EOL> from st2common . util . shell import kill_process <EOL> from st2common . util . shell import quote_unix <EOL> from st2common import log as logging <EOL> from st2actions . runners import ActionRunner <EOL> from st2common . constants . action import LIVEACTION_STATUS_SUCCEEDED <EOL> from st2common . constants . action import LIVEACTION_STATUS_FAILED <EOL> from st2common . constants . runners import LOCAL_RUNNER_DEFAULT_ACTION_TIMEOUT <EOL> import st2common . util . jsonify as jsonify <EOL> LOG = logging . getLogger ( __name__ ) <EOL> RUNNER_INPUTS = '<STR_LIT>' <EOL> RUNNER_TIMEOUT = '<STR_LIT>' <EOL> def get_runner ( ) : <EOL> return CloudSlangRunner ( str ( uuid . uuid4 ( ) ) ) <EOL> class CloudSlangRunner ( ActionRunner ) : <EOL> """<STR_LIT>""" <EOL> KEYS_TO_TRANSFORM = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , runner_id ) : <EOL> super ( CloudSlangRunner , self ) . __init__ ( runner_id = runner_id ) <EOL> def pre_run ( self ) : <EOL> self . _flow_path = self . entry_point <EOL> self . _user = cfg . CONF . system_user . user <EOL> self . _cloudslang_home = cfg . CONF . cloudslang . home_dir <EOL> self . _inputs = self . runner_parameters . get ( RUNNER_INPUTS , None ) <EOL> self . _timeout = self . runner_parameters . get ( RUNNER_TIMEOUT , <EOL> LOCAL_RUNNER_DEFAULT_ACTION_TIMEOUT ) <EOL> def run ( self , action_parameters ) : <EOL> if self . _inputs : <EOL> inputs = self . _inputs <EOL> elif action_parameters : <EOL> inputs = action_parameters <EOL> else : <EOL> inputs = None <EOL> inputs_file_path = self . _write_inputs_to_a_temp_file ( inputs = inputs ) <EOL> has_inputs = ( inputs_file_path is not None ) <EOL> try : <EOL> command = self . _prepare_command ( has_inputs = has_inputs , <EOL> inputs_file_path = inputs_file_path ) <EOL> result , status = self . _run_cli_command ( command ) <EOL> return ( status , jsonify . json_loads ( result , CloudSlangRunner . KEYS_TO_TRANSFORM ) , None ) <EOL> finally : <EOL> if inputs_file_path and os . path . isfile ( inputs_file_path ) : <EOL> os . remove ( inputs_file_path ) <EOL> def _write_inputs_to_a_temp_file ( self , inputs ) : <EOL> """<STR_LIT>""" <EOL> if not inputs : <EOL> return None <EOL> LOG . debug ( '<STR_LIT>' , inputs ) <EOL> inputs_file = tempfile . NamedTemporaryFile ( delete = False ) <EOL> inputs_file_path = inputs_file . name <EOL> yaml_inputs = yaml . safe_dump ( inputs , default_flow_style = False ) <EOL> with open ( inputs_file_path , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( yaml_inputs ) <EOL> LOG . debug ( '<STR_LIT>' , yaml_inputs ) <EOL> return inputs_file_path <EOL> def _prepare_command ( self , has_inputs , inputs_file_path ) : <EOL> LOG . debug ( '<STR_LIT>' , self . _cloudslang_home ) <EOL> cloudslang_binary = os . path . join ( self . _cloudslang_home , '<STR_LIT>' ) <EOL> LOG . debug ( '<STR_LIT>' , cloudslang_binary ) <EOL> command_args = [ '<STR_LIT>' , self . _flow_path , <EOL> '<STR_LIT>' , self . _cloudslang_home ] <EOL> if has_inputs : <EOL> command_args += [ '<STR_LIT>' , inputs_file_path ] <EOL> command = cloudslang_binary + "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( [ quote_unix ( arg ) for arg in command_args ] ) <EOL> LOG . info ( '<STR_LIT>' , self . runner_id ) <EOL> LOG . debug ( '<STR_LIT>' , command ) <EOL> return command <EOL> def _run_cli_command ( self , command ) : <EOL> exit_code , stdout , stderr , timed_out = run_command ( <EOL> cmd = command , stdin = None , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> shell = True , timeout = self . _timeout , 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>' : stdout , <EOL> '<STR_LIT>' : stderr <EOL> } <EOL> if error : <EOL> result [ '<STR_LIT:error>' ] = error <EOL> status = LIVEACTION_STATUS_SUCCEEDED if succeeded else LIVEACTION_STATUS_FAILED <EOL> return result , status </s>
