Conditions | 20 |
Total Lines | 104 |
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:
Complex classes like BaseParallelSSHRunner.pre_run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
83 | def pre_run(self): |
||
84 | super(BaseParallelSSHRunner, self).pre_run() |
||
85 | |||
86 | LOG.debug('Entering BaseParallelSSHRunner.pre_run() for liveaction_id="%s"', |
||
87 | self.liveaction_id) |
||
88 | hosts = self.runner_parameters.get(RUNNER_HOSTS, '').split(',') |
||
89 | self._hosts = [h.strip() for h in hosts if len(h) > 0] |
||
90 | if len(self._hosts) < 1: |
||
91 | raise ActionRunnerPreRunError('No hosts specified to run action for action %s.', |
||
92 | self.liveaction_id) |
||
93 | self._username = self.runner_parameters.get(RUNNER_USERNAME, None) |
||
94 | self._password = self.runner_parameters.get(RUNNER_PASSWORD, None) |
||
95 | self._private_key = self.runner_parameters.get(RUNNER_PRIVATE_KEY, None) |
||
96 | self._passphrase = self.runner_parameters.get(RUNNER_PASSPHRASE, None) |
||
97 | |||
98 | self._ssh_port = self.runner_parameters.get(RUNNER_SSH_PORT, None) |
||
99 | self._ssh_key_file = self._private_key |
||
100 | self._parallel = self.runner_parameters.get(RUNNER_PARALLEL, True) |
||
101 | self._sudo = self.runner_parameters.get(RUNNER_SUDO, False) |
||
102 | self._sudo = self._sudo if self._sudo else False |
||
103 | self._sudo_password = self.runner_parameters.get(RUNNER_SUDO_PASSWORD, None) |
||
104 | |||
105 | if self.context: |
||
106 | self._on_behalf_user = self.context.get(RUNNER_ON_BEHALF_USER, self._on_behalf_user) |
||
107 | |||
108 | self._cwd = self.runner_parameters.get(RUNNER_CWD, None) |
||
109 | self._env = self.runner_parameters.get(RUNNER_ENV, {}) |
||
110 | self._kwarg_op = self.runner_parameters.get(RUNNER_KWARG_OP, '--') |
||
111 | self._timeout = self.runner_parameters.get(RUNNER_TIMEOUT, |
||
112 | REMOTE_RUNNER_DEFAULT_ACTION_TIMEOUT) |
||
113 | self._bastion_host = self.runner_parameters.get(RUNNER_BASTION_HOST, None) |
||
114 | |||
115 | LOG.info('[BaseParallelSSHRunner="%s", liveaction_id="%s"] Finished pre_run.', |
||
116 | self.runner_id, self.liveaction_id) |
||
117 | |||
118 | concurrency = int(len(self._hosts) / 3) + 1 if self._parallel else 1 |
||
119 | if concurrency > self._max_concurrency: |
||
120 | LOG.debug('Limiting parallel SSH concurrency to %d.', concurrency) |
||
121 | concurrency = self._max_concurrency |
||
122 | |||
123 | client_kwargs = { |
||
124 | 'hosts': self._hosts, |
||
125 | 'user': self._username, |
||
126 | 'port': self._ssh_port, |
||
127 | 'concurrency': concurrency, |
||
128 | 'bastion_host': self._bastion_host, |
||
129 | 'raise_on_any_error': False, |
||
130 | 'connect': True |
||
131 | } |
||
132 | |||
133 | def make_store_stdout_line_func(execution_db, action_db): |
||
134 | def store_stdout_line(line): |
||
135 | if cfg.CONF.actionrunner.stream_output: |
||
136 | store_execution_output_data(execution_db=execution_db, action_db=action_db, |
||
137 | data=line, output_type='stdout') |
||
138 | |||
139 | return store_stdout_line |
||
140 | |||
141 | def make_store_stderr_line_func(execution_db, action_db): |
||
142 | def store_stderr_line(line): |
||
143 | if cfg.CONF.actionrunner.stream_output: |
||
144 | store_execution_output_data(execution_db=execution_db, action_db=action_db, |
||
145 | data=line, output_type='stderr') |
||
146 | |||
147 | return store_stderr_line |
||
148 | |||
149 | handle_stdout_line_func = make_store_stdout_line_func(execution_db=self.execution, |
||
150 | action_db=self.action) |
||
151 | handle_stderr_line_func = make_store_stderr_line_func(execution_db=self.execution, |
||
152 | action_db=self.action) |
||
153 | |||
154 | if len(self._hosts) == 1: |
||
155 | # We only support streaming output when running action on one host. That is because |
||
156 | # the action output is tied to a particulat execution. User can still achieve output |
||
157 | # streaming for multiple hosts by running one execution per host. |
||
158 | client_kwargs['handle_stdout_line_func'] = handle_stdout_line_func |
||
159 | client_kwargs['handle_stderr_line_func'] = handle_stderr_line_func |
||
160 | else: |
||
161 | LOG.debug('Real-time action output streaming is disabled, because action is running ' |
||
162 | 'on more than one host') |
||
163 | |||
164 | if self._password: |
||
165 | client_kwargs['password'] = self._password |
||
166 | elif self._private_key: |
||
167 | # Determine if the private_key is a path to the key file or the raw key material |
||
168 | is_key_material = self._is_private_key_material(private_key=self._private_key) |
||
169 | |||
170 | if is_key_material: |
||
171 | # Raw key material |
||
172 | client_kwargs['pkey_material'] = self._private_key |
||
173 | else: |
||
174 | # Assume it's a path to the key file, verify the file exists |
||
175 | client_kwargs['pkey_file'] = self._private_key |
||
176 | |||
177 | if self._passphrase: |
||
178 | client_kwargs['passphrase'] = self._passphrase |
||
179 | else: |
||
180 | # Default to stanley key file specified in the config |
||
181 | client_kwargs['pkey_file'] = self._ssh_key_file |
||
182 | |||
183 | if self._sudo_password: |
||
184 | client_kwargs['sudo_password'] = True |
||
185 | |||
186 | self._parallel_ssh_client = ParallelSSHClient(**client_kwargs) |
||
187 | |||
258 |