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