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