| Conditions | 5 |
| Total Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 92 | def run(self, action_parameters): |
||
| 93 | env_vars = self._env |
||
| 94 | |||
| 95 | if not self.entry_point: |
||
| 96 | script_action = False |
||
| 97 | command = self.runner_parameters.get(RUNNER_COMMAND, None) |
||
| 98 | action = ShellCommandAction(name=self.action_name, |
||
| 99 | action_exec_id=str(self.liveaction_id), |
||
| 100 | command=command, |
||
| 101 | user=self._user, |
||
| 102 | env_vars=env_vars, |
||
| 103 | sudo=self._sudo, |
||
| 104 | timeout=self._timeout) |
||
| 105 | else: |
||
| 106 | script_action = True |
||
| 107 | script_local_path_abs = self.entry_point |
||
| 108 | positional_args, named_args = self._get_script_args(action_parameters) |
||
| 109 | named_args = self._transform_named_args(named_args) |
||
| 110 | |||
| 111 | action = ShellScriptAction(name=self.action_name, |
||
| 112 | action_exec_id=str(self.liveaction_id), |
||
| 113 | script_local_path_abs=script_local_path_abs, |
||
| 114 | named_args=named_args, |
||
| 115 | positional_args=positional_args, |
||
| 116 | user=self._user, |
||
| 117 | env_vars=env_vars, |
||
| 118 | sudo=self._sudo, |
||
| 119 | timeout=self._timeout, |
||
| 120 | cwd=self._cwd) |
||
| 121 | |||
| 122 | args = action.get_full_command_string() |
||
| 123 | |||
| 124 | # For consistency with the old Fabric based runner, make sure the file is executable |
||
| 125 | if script_action: |
||
| 126 | args = 'chmod +x %s ; %s' % (script_local_path_abs, args) |
||
| 127 | |||
| 128 | env = os.environ.copy() |
||
| 129 | |||
| 130 | # Include user provided env vars (if any) |
||
| 131 | env.update(env_vars) |
||
| 132 | |||
| 133 | # Include common st2 env vars |
||
| 134 | st2_env_vars = self._get_common_action_env_variables() |
||
| 135 | env.update(st2_env_vars) |
||
| 136 | |||
| 137 | LOG.info('Executing action via LocalRunner: %s', self.runner_id) |
||
| 138 | LOG.info('[Action info] name: %s, Id: %s, command: %s, user: %s, sudo: %s' % |
||
| 139 | (action.name, action.action_exec_id, args, action.user, action.sudo)) |
||
| 140 | |||
| 141 | # Make sure os.setsid is called on each spawned process so that all processes |
||
| 142 | # are in the same group. |
||
| 143 | |||
| 144 | # Process is started as sudo -u {{system_user}} -- bash -c {{command}}. Introduction of the |
||
| 145 | # bash means that multiple independent processes are spawned without them being |
||
| 146 | # children of the process we have access to and this requires use of pkill. |
||
| 147 | # Ideally os.killpg should have done the trick but for some reason that failed. |
||
| 148 | # Note: pkill will set the returncode to 143 so we don't need to explicitly set |
||
| 149 | # it to some non-zero value. |
||
| 150 | exit_code, stdout, stderr, timed_out = shell.run_command(cmd=args, stdin=None, |
||
| 151 | stdout=subprocess.PIPE, |
||
| 152 | stderr=subprocess.PIPE, |
||
| 153 | shell=True, |
||
| 154 | cwd=self._cwd, |
||
| 155 | env=env, |
||
| 156 | timeout=self._timeout, |
||
| 157 | preexec_func=os.setsid, |
||
| 158 | kill_func=kill_process) |
||
| 159 | |||
| 160 | error = None |
||
| 161 | |||
| 162 | if timed_out: |
||
| 163 | error = 'Action failed to complete in %s seconds' % (self._timeout) |
||
| 164 | exit_code = -1 * exit_code_constants.SIGKILL_EXIT_CODE |
||
| 165 | |||
| 166 | succeeded = (exit_code == exit_code_constants.SUCCESS_EXIT_CODE) |
||
| 167 | |||
| 168 | result = { |
||
| 169 | 'failed': not succeeded, |
||
| 170 | 'succeeded': succeeded, |
||
| 171 | 'return_code': exit_code, |
||
| 172 | 'stdout': strip_shell_chars(stdout), |
||
| 173 | 'stderr': strip_shell_chars(stderr) |
||
| 174 | } |
||
| 175 | |||
| 176 | if error: |
||
| 177 | result['error'] = error |
||
| 178 | |||
| 179 | status = PROC_EXIT_CODE_TO_LIVEACTION_STATUS_MAP.get( |
||
| 180 | str(exit_code), |
||
| 181 | action_constants.LIVEACTION_STATUS_FAILED |
||
| 182 | ) |
||
| 183 | |||
| 184 | return (status, jsonify.json_loads(result, LocalShellRunner.KEYS_TO_TRANSFORM), None) |
||
| 185 |