Conditions | 12 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
79 | def pre_run(self): |
||
80 | super(BaseParallelSSHRunner, self).pre_run() |
||
81 | |||
82 | LOG.debug('Entering BaseParallelSSHRunner.pre_run() for liveaction_id="%s"', |
||
83 | self.liveaction_id) |
||
84 | hosts = self.runner_parameters.get(RUNNER_HOSTS, '').split(',') |
||
85 | self._hosts = [h.strip() for h in hosts if len(h) > 0] |
||
86 | if len(self._hosts) < 1: |
||
87 | raise ActionRunnerPreRunError('No hosts specified to run action for action %s.', |
||
88 | self.liveaction_id) |
||
89 | self._username = self.runner_parameters.get(RUNNER_USERNAME, None) |
||
90 | self._password = self.runner_parameters.get(RUNNER_PASSWORD, None) |
||
91 | self._private_key = self.runner_parameters.get(RUNNER_PRIVATE_KEY, None) |
||
92 | self._passphrase = self.runner_parameters.get(RUNNER_PASSPHRASE, None) |
||
93 | |||
94 | self._ssh_port = self.runner_parameters.get(RUNNER_SSH_PORT, None) |
||
95 | self._ssh_key_file = self._private_key |
||
96 | self._parallel = self.runner_parameters.get(RUNNER_PARALLEL, True) |
||
97 | self._sudo = self.runner_parameters.get(RUNNER_SUDO, False) |
||
98 | self._sudo = self._sudo if self._sudo else False |
||
99 | if self.context: |
||
100 | self._on_behalf_user = self.context.get(RUNNER_ON_BEHALF_USER, self._on_behalf_user) |
||
101 | self._cwd = self.runner_parameters.get(RUNNER_CWD, None) |
||
102 | self._env = self.runner_parameters.get(RUNNER_ENV, {}) |
||
103 | self._kwarg_op = self.runner_parameters.get(RUNNER_KWARG_OP, '--') |
||
104 | self._timeout = self.runner_parameters.get(RUNNER_TIMEOUT, |
||
105 | REMOTE_RUNNER_DEFAULT_ACTION_TIMEOUT) |
||
106 | self._bastion_host = self.runner_parameters.get(RUNNER_BASTION_HOST, None) |
||
107 | |||
108 | LOG.info('[BaseParallelSSHRunner="%s", liveaction_id="%s"] Finished pre_run.', |
||
109 | self.runner_id, self.liveaction_id) |
||
110 | |||
111 | concurrency = int(len(self._hosts) / 3) + 1 if self._parallel else 1 |
||
112 | if concurrency > self._max_concurrency: |
||
113 | LOG.debug('Limiting parallel SSH concurrency to %d.', concurrency) |
||
114 | concurrency = self._max_concurrency |
||
115 | |||
116 | client_kwargs = { |
||
117 | 'hosts': self._hosts, |
||
118 | 'user': self._username, |
||
119 | 'port': self._ssh_port, |
||
120 | 'concurrency': concurrency, |
||
121 | 'bastion_host': self._bastion_host, |
||
122 | 'raise_on_any_error': False, |
||
123 | 'connect': True |
||
124 | } |
||
125 | |||
126 | if self._password: |
||
127 | client_kwargs['password'] = self._password |
||
128 | elif self._private_key: |
||
129 | # Determine if the private_key is a path to the key file or the raw key material |
||
130 | is_key_material = self._is_private_key_material(private_key=self._private_key) |
||
131 | |||
132 | if is_key_material: |
||
133 | # Raw key material |
||
134 | client_kwargs['pkey_material'] = self._private_key |
||
135 | else: |
||
136 | # Assume it's a path to the key file, verify the file exists |
||
137 | client_kwargs['pkey_file'] = self._private_key |
||
138 | |||
139 | if self._passphrase: |
||
140 | client_kwargs['passphrase'] = self._passphrase |
||
141 | else: |
||
142 | # Default to stanley key file specified in the config |
||
143 | client_kwargs['pkey_file'] = self._ssh_key_file |
||
144 | |||
145 | self._parallel_ssh_client = ParallelSSHClient(**client_kwargs) |
||
146 | |||
210 |