savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | """ |
| 2 | Module to handle interaction with salt |
| 3 | """ |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 4 | import json |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 5 | import os |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 6 | import time |
| 7 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 8 | import requests |
| 9 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 10 | from cfg_checker.common import logger, logger_cli |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 11 | from cfg_checker.common.exception import InvalidReturnException, SaltException |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 12 | from cfg_checker.common.other import shell |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 13 | from cfg_checker.common.ssh_utils import ssh_shell_p |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 14 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 15 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 16 | def _extract_salt_password(_raw): |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 17 | if not isinstance(_raw, str): |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 18 | raise InvalidReturnException(_raw) |
| 19 | else: |
| 20 | try: |
| 21 | _json = json.loads(_raw) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 22 | except ValueError: |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 23 | raise SaltException( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 24 | "# Return value is not a json: '{}'".format(_raw) |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 25 | ) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 26 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 27 | return _json["local"] |
| 28 | |
| 29 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 30 | def get_remote_salt_env_password(config): |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 31 | """Uses ssh call with configured options to get password from salt master |
| 32 | |
| 33 | :return: password string |
| 34 | """ |
| 35 | _salt_cmd = "salt-call --out=json pillar.get _param:salt_api_password" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 36 | logger_cli.debug("... calling salt using ssh: '{}'".format(_salt_cmd)) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 37 | try: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 38 | _result = ssh_shell_p( |
| 39 | _salt_cmd, |
| 40 | config.ssh_host, |
| 41 | username=config.ssh_user, |
| 42 | keypath=config.ssh_key, |
| 43 | piped=False, |
| 44 | use_sudo=config.ssh_uses_sudo, |
| 45 | silent=True |
| 46 | ) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 47 | if len(_result) < 1: |
| 48 | raise InvalidReturnException( |
| 49 | "# Empty value returned for '{}".format( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 50 | _salt_cmd |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 51 | ) |
| 52 | ) |
| 53 | else: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 54 | return _extract_salt_password(_result) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 55 | except OSError as e: |
| 56 | raise SaltException( |
| 57 | "Salt error calling '{}': '{}'\n" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 58 | "\nConsider checking 'MCP_ENV' " |
| 59 | "and '<pkg>/etc/<env>.env' files".format(_salt_cmd, e.strerror) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 60 | ) |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 61 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 62 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 63 | def get_salt_local_password(config): |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 64 | """Calls salt locally to get password from the pillar |
| 65 | |
| 66 | :return: password string |
| 67 | """ |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 68 | _cmd = [] |
| 69 | if config.ssh_uses_sudo: |
| 70 | _cmd = ["sudo"] |
| 71 | # salt commands |
| 72 | _cmd.append("salt-call") |
| 73 | _cmd.append("--out=json pillar.get _param:salt_api_password") |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 74 | try: |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 75 | _result = shell(" ".join(_cmd)) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 76 | except OSError as e: |
| 77 | raise SaltException( |
| 78 | "Salt error calling '{}': '{}'\n" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 79 | "\nConsider checking 'MCP_ENV' " |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 80 | "and '<pkg>/etc/<env>.env' files".format(_cmd, e.strerror) |
| 81 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 82 | return _extract_salt_password(_result) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def list_to_target_string(node_list, separator): |
| 86 | result = '' |
| 87 | for node in node_list: |
| 88 | result += node + ' ' + separator + ' ' |
| 89 | return result[:-(len(separator)+2)] |
| 90 | |
| 91 | |
| 92 | class SaltRest(object): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 93 | _auth = {} |
| 94 | |
| 95 | default_headers = { |
| 96 | 'Accept': 'application/json', |
| 97 | 'Content-Type': 'application/json', |
| 98 | 'X-Auth-Token': None |
| 99 | } |
| 100 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 101 | def __init__(self, config): |
| 102 | self.config = config |
| 103 | |
| 104 | self._host = config.mcp_host |
| 105 | self._port = config.salt_port |
| 106 | self.uri = "http://" + config.mcp_host + ":" + config.salt_port |
| 107 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 108 | self._token = self._login() |
| 109 | self.last_response = None |
| 110 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 111 | def get( |
| 112 | self, |
| 113 | path='', |
| 114 | headers=default_headers, |
| 115 | cookies=None, |
| 116 | timeout=None |
| 117 | ): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 118 | _path = os.path.join(self.uri, path) |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 119 | logger.debug("# GET '{}'\nHeaders: '{}'\nCookies: {}".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 120 | _path, |
| 121 | headers, |
| 122 | cookies |
| 123 | )) |
| 124 | return requests.get( |
| 125 | _path, |
| 126 | headers=headers, |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 127 | cookies=cookies, |
| 128 | timeout=timeout |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 129 | ) |
| 130 | |
| 131 | def post(self, data, path='', headers=default_headers, cookies=None): |
| 132 | if data is None: |
| 133 | data = {} |
| 134 | _path = os.path.join(self.uri, path) |
| 135 | if path == 'login': |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 136 | _data = str(data).replace(self._pass, "*****") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 137 | else: |
| 138 | _data = data |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 139 | logger.debug( |
| 140 | "# POST '{}'\nHeaders: '{}'\nCookies: {}\nBody: {}".format( |
| 141 | _path, |
| 142 | headers, |
| 143 | cookies, |
| 144 | _data |
| 145 | ) |
| 146 | ) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 147 | return requests.post( |
| 148 | os.path.join(self.uri, path), |
| 149 | headers=headers, |
| 150 | json=data, |
| 151 | cookies=cookies |
| 152 | ) |
| 153 | |
| 154 | def _login(self): |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 155 | # if there is no password - try to get local, if this available |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 156 | if self.config.env_name == "local": |
| 157 | _pass = get_salt_local_password(self.config) |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 158 | else: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 159 | _pass = get_remote_salt_env_password(self.config) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 160 | login_payload = { |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 161 | 'username': self.config.salt_user, |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 162 | 'password': _pass, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 163 | 'eauth': 'pam' |
| 164 | } |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 165 | self._pass = _pass |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 166 | logger.debug("# Logging in to salt master...") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 167 | _response = self.post(login_payload, path='login') |
| 168 | |
| 169 | if _response.ok: |
| 170 | self._auth['response'] = _response.json()['return'][0] |
| 171 | self._auth['cookies'] = _response.cookies |
| 172 | self.default_headers['X-Auth-Token'] = \ |
| 173 | self._auth['response']['token'] |
| 174 | return self._auth['response']['token'] |
| 175 | else: |
| 176 | raise EnvironmentError( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 177 | "# HTTP:{}, Not authorized?".format(_response.status_code) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 178 | ) |
| 179 | |
| 180 | def salt_request(self, fn, *args, **kwargs): |
| 181 | # if token will expire in 5 min, re-login |
| 182 | if self._auth['response']['expire'] < time.time() + 300: |
| 183 | self._auth['response']['X-Auth-Token'] = self._login() |
| 184 | |
| 185 | _method = getattr(self, fn) |
| 186 | _response = _method(*args, **kwargs) |
| 187 | self.last_response = _response |
| 188 | _content = "..." |
| 189 | _len = len(_response.content) |
| 190 | if _len < 1024: |
| 191 | _content = _response.content |
| 192 | logger.debug( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 193 | "# Response (HTTP {}/{}), {}: {}".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 194 | _response.status_code, |
| 195 | _response.reason, |
| 196 | _len, |
| 197 | _content |
| 198 | ) |
| 199 | ) |
| 200 | if _response.ok: |
| 201 | return _response.json()['return'] |
| 202 | else: |
| 203 | raise EnvironmentError( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 204 | "# Salt Error: HTTP:{}, '{}'".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 205 | _response.status_code, |
| 206 | _response.reason |
| 207 | ) |
| 208 | ) |
| 209 | |
| 210 | |
| 211 | class SaltRemote(SaltRest): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 212 | master_node = "" |
| 213 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 214 | def __init__(self, config): |
| 215 | super(SaltRemote, self).__init__(config) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 216 | |
| 217 | def cmd( |
| 218 | self, |
| 219 | tgt, |
| 220 | fun, |
| 221 | param=None, |
| 222 | client='local', |
| 223 | kwarg=None, |
| 224 | expr_form=None, |
| 225 | tgt_type=None, |
| 226 | timeout=None |
| 227 | ): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 228 | _timeout = timeout if timeout is not None else self.config.salt_timeout |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 229 | _payload = { |
| 230 | 'fun': fun, |
| 231 | 'tgt': tgt, |
| 232 | 'client': client, |
| 233 | 'timeout': _timeout |
| 234 | } |
| 235 | |
| 236 | if expr_form: |
| 237 | _payload['expr_form'] = expr_form |
| 238 | if tgt_type: |
| 239 | _payload['tgt_type'] = tgt_type |
| 240 | if param: |
| 241 | _payload['arg'] = param |
| 242 | if kwarg: |
| 243 | _payload['kwarg'] = kwarg |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 244 | logger_cli.debug("SaltRequest: POST '{}'".format(_payload)) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 245 | _response = self.salt_request('post', [_payload]) |
| 246 | if isinstance(_response, list): |
| 247 | return _response[0] |
| 248 | else: |
| 249 | raise EnvironmentError( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 250 | "# Unexpected response from from salt-api/LocalClient: " |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 251 | "{}".format(_response) |
| 252 | ) |
| 253 | |
| 254 | def run(self, fun, kwarg=None): |
| 255 | _payload = { |
| 256 | 'client': 'runner', |
| 257 | 'fun': fun, |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 258 | 'timeout': self.config.salt_timeout |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if kwarg: |
| 262 | _payload['kwarg'] = kwarg |
| 263 | |
| 264 | _response = self.salt_request('post', [_payload]) |
| 265 | if isinstance(_response, list): |
| 266 | return _response[0] |
| 267 | else: |
| 268 | raise EnvironmentError( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 269 | "# Unexpected response from from salt-api/RunnerClient: " |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 270 | "{}".format(_response) |
| 271 | ) |
| 272 | |
| 273 | def wheel(self, fun, arg=None, kwarg=None): |
| 274 | _payload = { |
| 275 | 'client': 'wheel', |
| 276 | 'fun': fun, |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 277 | 'timeout': self.config.salt_timeout |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if arg: |
| 281 | _payload['arg'] = arg |
| 282 | if kwarg: |
| 283 | _payload['kwarg'] = kwarg |
| 284 | |
| 285 | _response = self.salt_request('post', _payload)['data'] |
| 286 | if _response['success']: |
| 287 | return _response |
| 288 | else: |
| 289 | raise EnvironmentError( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 290 | "# Salt Error: '{}'".format(_response['return'])) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 291 | |
| 292 | def pillar_request(self, node_target, pillar_submodule, argument): |
| 293 | # example cli: 'salt "ctl01*" pillar.keys rsyslog' |
| 294 | _type = "compound" |
| 295 | if isinstance(node_target, list): |
| 296 | _type = "list" |
| 297 | return self.cmd( |
| 298 | node_target, |
| 299 | "pillar." + pillar_submodule, |
| 300 | argument, |
| 301 | expr_form=_type |
| 302 | ) |
| 303 | |
| 304 | def pillar_keys(self, node_target, argument): |
| 305 | return self.pillar_request(node_target, 'keys', argument) |
| 306 | |
| 307 | def pillar_get(self, node_target, argument): |
| 308 | return self.pillar_request(node_target, 'get', argument) |
| 309 | |
| 310 | def pillar_data(self, node_target, argument): |
| 311 | return self.pillar_request(node_target, 'data', argument) |
| 312 | |
| 313 | def pillar_raw(self, node_target, argument): |
| 314 | return self.pillar_request(node_target, 'raw', argument) |
| 315 | |
| 316 | def list_minions(self): |
| 317 | """ |
| 318 | Fails in salt version 2016.3.8 |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 319 | Works starting from 2017.7.7 |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 320 | api returns dict of minions with grains |
| 321 | """ |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 322 | try: |
| 323 | _r = self.salt_request('get', 'minions', timeout=10) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 324 | except requests.exceptions.ReadTimeout: |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 325 | logger_cli.debug("... timeout waiting list minions from Salt API") |
| 326 | _r = None |
| 327 | return _r[0] if _r else None |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 328 | |
| 329 | def list_keys(self): |
| 330 | """ |
| 331 | Fails in salt version 2016.3.8 |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 332 | Works starting from 2017.7.7 |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 333 | api should return dict: |
| 334 | { |
| 335 | 'local': [], |
| 336 | 'minions': [], |
| 337 | 'minions_denied': [], |
| 338 | 'minions_pre': [], |
| 339 | 'minions_rejected': [], |
| 340 | } |
| 341 | """ |
| 342 | return self.salt_request('get', path='keys') |
| 343 | |
| 344 | def get_status(self): |
| 345 | """ |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 346 | Fails in salt version 2017.7.7 |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 347 | 'runner' client is the equivalent of 'salt-run' |
| 348 | Returns the |
| 349 | """ |
| 350 | return self.run( |
| 351 | 'manage.status', |
| 352 | kwarg={'timeout': 10} |
| 353 | ) |
| 354 | |
| 355 | def get_active_nodes(self): |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 356 | """Used when other minion list metods fail |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 357 | |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 358 | :return: json result from salt test.ping |
| 359 | """ |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 360 | if self.config.skip_nodes: |
| 361 | logger.info( |
| 362 | "# Nodes to be skipped: {0}".format(self.config.skip_nodes) |
| 363 | ) |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 364 | _r = self.cmd( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 365 | '* and not ' + list_to_target_string( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 366 | self.config.skip_nodes, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 367 | 'and not' |
| 368 | ), |
| 369 | 'test.ping', |
| 370 | expr_form='compound') |
| 371 | else: |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 372 | _r = self.cmd('*', 'test.ping') |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 373 | # Return all nodes that responded |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 374 | return [node for node in _r.keys() if _r[node]] |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 375 | |
| 376 | def get_monitoring_ip(self, param_name): |
| 377 | salt_output = self.cmd( |
| 378 | 'docker:client:stack:monitoring', |
| 379 | 'pillar.get', |
| 380 | param=param_name, |
| 381 | expr_form='pillar') |
| 382 | return salt_output[salt_output.keys()[0]] |
| 383 | |
| 384 | def f_touch_master(self, path, makedirs=True): |
| 385 | _kwarg = { |
| 386 | "makedirs": makedirs |
| 387 | } |
| 388 | salt_output = self.cmd( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 389 | self.master_node, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 390 | "file.touch", |
| 391 | param=path, |
| 392 | kwarg=_kwarg |
| 393 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 394 | return [*salt_output.values()][0] |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 395 | |
| 396 | def f_append_master(self, path, strings_list, makedirs=True): |
| 397 | _kwarg = { |
| 398 | "makedirs": makedirs |
| 399 | } |
| 400 | _args = [path] |
| 401 | _args.extend(strings_list) |
| 402 | salt_output = self.cmd( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 403 | self.master_node, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 404 | "file.write", |
| 405 | param=_args, |
| 406 | kwarg=_kwarg |
| 407 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 408 | return [*salt_output.values()][0] |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 409 | |
| 410 | def mkdir(self, target, path, tgt_type=None): |
| 411 | salt_output = self.cmd( |
| 412 | target, |
| 413 | "file.mkdir", |
| 414 | param=path, |
| 415 | expr_form=tgt_type |
| 416 | ) |
| 417 | return salt_output |
| 418 | |
| 419 | def f_manage_file(self, target_path, source, |
| 420 | sfn='', ret='{}', |
| 421 | source_hash={}, |
| 422 | user='root', group='root', backup_mode='755', |
| 423 | show_diff='base', |
| 424 | contents='', makedirs=True): |
| 425 | """ |
| 426 | REST variation of file.get_managed |
| 427 | CLI execution goes like this (10 agrs): |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 428 | salt cfg01\\* file.manage_file /root/test_scripts/pkg_versions.py |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 429 | '' '{}' /root/diff_pkg_version.py |
| 430 | '{hash_type: 'md5', 'hsum': <md5sum>}' root root '755' base '' |
| 431 | makedirs=True |
| 432 | param: name - target file placement when managed |
| 433 | param: source - source for the file |
| 434 | """ |
| 435 | _source_hash = { |
| 436 | "hash_type": "md5", |
| 437 | "hsum": 000 |
| 438 | } |
| 439 | _arg = [ |
| 440 | target_path, |
| 441 | sfn, |
| 442 | ret, |
| 443 | source, |
| 444 | _source_hash, |
| 445 | user, |
| 446 | group, |
| 447 | backup_mode, |
| 448 | show_diff, |
| 449 | contents |
| 450 | ] |
| 451 | _kwarg = { |
| 452 | "makedirs": makedirs |
| 453 | } |
| 454 | salt_output = self.cmd( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 455 | self.master_node, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 456 | "file.manage_file", |
| 457 | param=_arg, |
| 458 | kwarg=_kwarg |
| 459 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 460 | return [*salt_output.values()][0] |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 461 | |
| 462 | def cache_file(self, target, source_path): |
| 463 | salt_output = self.cmd( |
| 464 | target, |
| 465 | "cp.cache_file", |
| 466 | param=source_path |
| 467 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 468 | return [*salt_output.values()][0] |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 469 | |
| 470 | def get_file(self, target, source_path, target_path, tgt_type=None): |
| 471 | return self.cmd( |
| 472 | target, |
| 473 | "cp.get_file", |
| 474 | param=[source_path, target_path], |
| 475 | expr_form=tgt_type |
| 476 | ) |
| 477 | |
| 478 | @staticmethod |
| 479 | def compound_string_from_list(nodes_list): |
| 480 | return " or ".join(nodes_list) |