Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import json |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 2 | import os |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 3 | from copy import deepcopy |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 4 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 5 | from cfg_checker.clients import get_salt_remote, salt |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 6 | from cfg_checker.common import config |
| 7 | from cfg_checker.common.const import all_roles_map |
| 8 | from cfg_checker.common.const import NODE_UP, NODE_DOWN, NODE_SKIP |
Alex | 7c9494e | 2019-04-22 10:40:59 -0500 | [diff] [blame] | 9 | from cfg_checker.common import logger, logger_cli |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 10 | from cfg_checker.common import utils |
| 11 | from cfg_checker.common.exception import SaltException |
Alex | 7c9494e | 2019-04-22 10:40:59 -0500 | [diff] [blame] | 12 | from cfg_checker.common.settings import pkg_dir |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 13 | |
| 14 | node_tmpl = { |
| 15 | 'role': '', |
| 16 | 'node_group': '', |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 17 | 'status': NODE_DOWN, |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 18 | 'pillars': {}, |
| 19 | 'grains': {} |
| 20 | } |
| 21 | |
| 22 | |
| 23 | class SaltNodes(object): |
| 24 | def __init__(self): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 25 | logger_cli.info("# Gathering environment information") |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 26 | # simple salt rest client |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 27 | self.salt = salt |
| 28 | self.nodes = None |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 29 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 30 | def gather_node_info(self, skip_list, skip_list_file): |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 31 | # Keys for all nodes |
| 32 | # this is not working in scope of 2016.8.3, will overide with list |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 33 | logger_cli.debug("... collecting node names existing in the cloud") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 34 | if not self.salt: |
| 35 | self.salt = get_salt_remote(config) |
| 36 | |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 37 | try: |
| 38 | _keys = self.salt.list_keys() |
| 39 | _str = [] |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 40 | for _k, _v in _keys.items(): |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 41 | _str.append("{}: {}".format(_k, len(_v))) |
| 42 | logger_cli.info("-> keys collected: {}".format(", ".join(_str))) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 43 | |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 44 | self.node_keys = { |
| 45 | 'minions': _keys['minions'] |
| 46 | } |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 47 | except Exception: |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 48 | _keys = None |
| 49 | self.node_keys = None |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 50 | |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 51 | # List of minions with grains |
| 52 | _minions = self.salt.list_minions() |
| 53 | if _minions: |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 54 | logger_cli.info( |
| 55 | "-> api reported {} active minions".format(len(_minions)) |
| 56 | ) |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 57 | elif not self.node_keys: |
| 58 | # this is the last resort |
| 59 | _minions = config.load_nodes_list() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 60 | logger_cli.info( |
| 61 | "-> {} nodes loaded from list file".format(len(_minions)) |
| 62 | ) |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 63 | else: |
| 64 | _minions = self.node_keys['minions'] |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 65 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 66 | # Skip nodes if needed |
| 67 | _skipped_minions = [] |
| 68 | # skip list file |
| 69 | if skip_list_file: |
| 70 | _valid, _invalid = utils.get_nodes_list(skip_list_file) |
| 71 | logger_cli.info( |
| 72 | "\n# WARNING: Detected invalid entries " |
| 73 | "in nodes skip list:\n".format( |
| 74 | "\n".join(_invalid) |
| 75 | ) |
| 76 | ) |
| 77 | _skipped_minions.extend(_valid) |
| 78 | # process wildcard, create node list out of mask |
| 79 | if skip_list: |
| 80 | _list = [] |
| 81 | _invalid = [] |
| 82 | for _item in skip_list: |
| 83 | if '*' in _item: |
| 84 | _str = _item[:_item.index('*')] |
| 85 | _nodes = [_m for _m in _minions if _m.startswith(_str)] |
| 86 | if not _nodes: |
| 87 | logger_cli.warn( |
| 88 | "# WARNING: No nodes found for {}".format(_item) |
| 89 | ) |
| 90 | _list.extend(_nodes) |
| 91 | else: |
| 92 | if _item in _minions: |
| 93 | _list += _item |
| 94 | else: |
| 95 | logger_cli.warn( |
| 96 | "# WARNING: No node found for {}".format(_item) |
| 97 | ) |
| 98 | # removing duplicates |
| 99 | _list = list(set(_list)) |
| 100 | _skipped_minions.extend(_list) |
| 101 | |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 102 | # in case API not listed minions, we need all that answer ping |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 103 | _active = self.salt.get_active_nodes() |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 104 | logger_cli.info("-> nodes responded: {}".format(len(_active))) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 105 | # iterate through all accepted nodes and create a dict for it |
| 106 | self.nodes = {} |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 107 | self.skip_list = [] |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 108 | _domains = set() |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 109 | for _name in _minions: |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 110 | _nc = utils.get_node_code(_name) |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 111 | _rmap = all_roles_map |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 112 | _role = _rmap[_nc] if _nc in _rmap else 'unknown' |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 113 | if _name in _skipped_minions: |
| 114 | _status = NODE_SKIP |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 115 | self.skip_list.append(_name) |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 116 | else: |
| 117 | _status = NODE_UP if _name in _active else NODE_DOWN |
| 118 | if _status == NODE_DOWN: |
| 119 | self.skip_list.append(_name) |
| 120 | logger_cli.info( |
| 121 | "-> '{}' is down, " |
| 122 | "added to skip list".format( |
| 123 | _name |
| 124 | ) |
| 125 | ) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 126 | self.nodes[_name] = deepcopy(node_tmpl) |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 127 | self.nodes[_name]['shortname'] = _name.split(".", 1)[0] |
| 128 | _domains.add(_name.split(".", 1)[1]) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 129 | self.nodes[_name]['node_group'] = _nc |
| 130 | self.nodes[_name]['role'] = _role |
| 131 | self.nodes[_name]['status'] = _status |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 132 | _domains = list(_domains) |
| 133 | if len(_domains) > 1: |
| 134 | logger_cli.warning( |
| 135 | "Multiple domains detected: {}".format(",".join(_domains)) |
| 136 | ) |
| 137 | else: |
| 138 | self.domain = _domains[0] |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 139 | logger_cli.info("-> {} nodes inactive".format(len(self.skip_list))) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 140 | logger_cli.info("-> {} nodes collected".format(len(self.nodes))) |
| 141 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 142 | # form an all nodes compound string to use in salt |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 143 | self.active_nodes_compound = self.salt.compound_string_from_list( |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 144 | filter( |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 145 | lambda nd: self.nodes[nd]['status'] == NODE_UP, |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 146 | self.nodes |
| 147 | ) |
| 148 | ) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 149 | # get master node fqdn |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 150 | # _filtered = filter( |
| 151 | # lambda nd: self.nodes[nd]['role'] == const.all_roles_map['cfg'], |
| 152 | # self.nodes |
| 153 | # ) |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 154 | _role = all_roles_map['cfg'] |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 155 | _filtered = [n for n, v in self.nodes.items() if v['role'] == _role] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 156 | if len(_filtered) < 1: |
| 157 | raise SaltException( |
| 158 | "No master node detected! Check/Update node role map." |
| 159 | ) |
| 160 | else: |
| 161 | self.salt.master_node = _filtered[0] |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 162 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 163 | # OpenStack versions |
| 164 | self.mcp_release = self.salt.pillar_get( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 165 | self.salt.master_node, |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 166 | "_param:apt_mk_version" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 167 | )[self.salt.master_node] |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 168 | self.openstack_release = self.salt.pillar_get( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 169 | self.salt.master_node, |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 170 | "_param:openstack_version" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 171 | )[self.salt.master_node] |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 172 | # Preload codenames |
| 173 | # do additional queries to get linux codename and arch for each node |
| 174 | self.get_specific_pillar_for_nodes("_param:linux_system_codename") |
| 175 | self.get_specific_pillar_for_nodes("_param:linux_system_architecture") |
| 176 | for _name in self.nodes.keys(): |
Alex | e9547d8 | 2019-06-03 15:22:50 -0500 | [diff] [blame] | 177 | _n = self.nodes[_name] |
| 178 | if _name not in self.skip_list: |
| 179 | _p = _n['pillars']['_param'] |
| 180 | _n['linux_codename'] = _p['linux_system_codename'] |
| 181 | _n['linux_arch'] = _p['linux_system_architecture'] |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 182 | |
Alex Savatieiev | a1f6f8c | 2019-03-18 17:13:55 -0500 | [diff] [blame] | 183 | def skip_node(self, node): |
| 184 | # Add node to skip list |
| 185 | # Fro example if it is fails to comply with the rules |
| 186 | |
| 187 | # check if we know such node |
| 188 | if node in self.nodes.keys() and node not in self.skip_list: |
| 189 | # yes, add it |
| 190 | self.skip_list.append(node) |
| 191 | return True |
| 192 | else: |
| 193 | return False |
| 194 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 195 | def get_nodes(self, skip_list=None, skip_list_file=None): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 196 | if not self.nodes: |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 197 | if not skip_list and config.skip_nodes: |
| 198 | self.gather_node_info(config.skip_nodes, skip_list_file) |
| 199 | else: |
| 200 | self.gather_node_info(skip_list, skip_list_file) |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 201 | return self.nodes |
| 202 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 203 | def get_info(self): |
| 204 | _info = { |
| 205 | 'mcp_release': self.mcp_release, |
| 206 | 'openstack_release': self.openstack_release |
| 207 | } |
| 208 | return _info |
| 209 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 210 | def get_cmd_for_nodes(self, cmd, target_key, target_dict=None, nodes=None): |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 211 | """Function runs. cmd.run and parses result into place |
| 212 | or into dict structure provided |
| 213 | |
| 214 | :return: no return value, data pulished internally |
| 215 | """ |
| 216 | logger_cli.debug( |
| 217 | "... collecting results for '{}'".format(cmd) |
| 218 | ) |
| 219 | if target_dict: |
| 220 | _nodes = target_dict |
| 221 | else: |
| 222 | _nodes = self.nodes |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 223 | _result = self.execute_cmd_on_active_nodes(cmd, nodes=nodes) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 224 | for node, data in _nodes.items(): |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 225 | |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 226 | if node in self.skip_list: |
| 227 | logger_cli.debug( |
| 228 | "... '{}' skipped while collecting '{}'".format( |
| 229 | node, |
| 230 | cmd |
| 231 | ) |
| 232 | ) |
| 233 | continue |
| 234 | # Prepare target key |
| 235 | if target_key not in data: |
| 236 | data[target_key] = None |
| 237 | # Save data |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 238 | if data['status'] in [NODE_DOWN, NODE_SKIP]: |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 239 | data[target_key] = None |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 240 | elif node not in _result: |
| 241 | continue |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 242 | elif not _result[node]: |
| 243 | logger_cli.debug( |
| 244 | "... '{}' not responded after '{}'".format( |
| 245 | node, |
| 246 | config.salt_timeout |
| 247 | ) |
| 248 | ) |
| 249 | data[target_key] = None |
| 250 | else: |
| 251 | data[target_key] = _result[node] |
| 252 | |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 253 | def get_specific_pillar_for_nodes(self, pillar_path): |
| 254 | """Function gets pillars on given path for all nodes |
| 255 | |
| 256 | :return: no return value, data pulished internally |
| 257 | """ |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 258 | logger_cli.debug( |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 259 | "... collecting node pillars for '{}'".format(pillar_path) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 260 | ) |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 261 | _result = self.salt.pillar_get(self.active_nodes_compound, pillar_path) |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 262 | self.not_responded = [] |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 263 | for node, data in self.nodes.items(): |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 264 | if node in self.skip_list: |
| 265 | logger_cli.debug( |
| 266 | "... '{}' skipped while collecting '{}'".format( |
| 267 | node, |
| 268 | pillar_path |
| 269 | ) |
| 270 | ) |
| 271 | continue |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 272 | _pillar_keys = pillar_path.split(':') |
| 273 | _data = data['pillars'] |
| 274 | # pre-create nested dict |
| 275 | for idx in range(0, len(_pillar_keys)-1): |
| 276 | _key = _pillar_keys[idx] |
| 277 | if _key not in _data: |
| 278 | _data[_key] = {} |
| 279 | _data = _data[_key] |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 280 | if data['status'] in [NODE_DOWN, NODE_SKIP]: |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 281 | _data[_pillar_keys[-1]] = None |
| 282 | elif not _result[node]: |
| 283 | logger_cli.debug( |
| 284 | "... '{}' not responded after '{}'".format( |
| 285 | node, |
| 286 | config.salt_timeout |
| 287 | ) |
| 288 | ) |
| 289 | _data[_pillar_keys[-1]] = None |
| 290 | self.not_responded.append(node) |
| 291 | else: |
| 292 | _data[_pillar_keys[-1]] = _result[node] |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 293 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 294 | def prepare_json_on_node(self, node, _dict, filename): |
| 295 | # this function assumes that all folders are created |
| 296 | _dumps = json.dumps(_dict, indent=2).splitlines() |
| 297 | _storage_path = os.path.join( |
| 298 | config.salt_file_root, config.salt_scripts_folder |
| 299 | ) |
| 300 | logger_cli.debug( |
| 301 | "... uploading data as '{}' " |
| 302 | "to master's file cache folder: '{}'".format( |
| 303 | filename, |
| 304 | _storage_path |
| 305 | ) |
| 306 | ) |
| 307 | _cache_path = os.path.join(_storage_path, filename) |
| 308 | _source_path = os.path.join( |
| 309 | 'salt://', |
| 310 | config.salt_scripts_folder, |
| 311 | filename |
| 312 | ) |
| 313 | _target_path = os.path.join( |
| 314 | '/root', |
| 315 | config.salt_scripts_folder, |
| 316 | filename |
| 317 | ) |
| 318 | |
| 319 | logger_cli.debug("... creating file in cache '{}'".format(_cache_path)) |
| 320 | self.salt.f_touch_master(_cache_path) |
| 321 | self.salt.f_append_master(_cache_path, _dumps) |
| 322 | logger.debug("... syncing file to '{}'".format(node)) |
| 323 | self.salt.get_file( |
| 324 | node, |
| 325 | _source_path, |
| 326 | _target_path, |
| 327 | tgt_type="compound" |
| 328 | ) |
| 329 | return _target_path |
| 330 | |
| 331 | def prepare_script_on_active_nodes(self, script_filename): |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 332 | # Prepare script |
| 333 | _p = os.path.join(pkg_dir, 'scripts', script_filename) |
| 334 | with open(_p, 'rt') as fd: |
| 335 | _script = fd.read().splitlines() |
| 336 | _storage_path = os.path.join( |
| 337 | config.salt_file_root, config.salt_scripts_folder |
| 338 | ) |
| 339 | logger_cli.debug( |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 340 | "... uploading script {} " |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 341 | "to master's file cache folder: '{}'".format( |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 342 | script_filename, |
| 343 | _storage_path |
| 344 | ) |
| 345 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 346 | self.salt.mkdir(self.salt.master_node, _storage_path) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 347 | # Form cache, source and target path |
| 348 | _cache_path = os.path.join(_storage_path, script_filename) |
| 349 | _source_path = os.path.join( |
| 350 | 'salt://', |
| 351 | config.salt_scripts_folder, |
| 352 | script_filename |
| 353 | ) |
| 354 | _target_path = os.path.join( |
| 355 | '/root', |
| 356 | config.salt_scripts_folder, |
| 357 | script_filename |
| 358 | ) |
| 359 | |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 360 | logger_cli.debug("... creating file in cache '{}'".format(_cache_path)) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 361 | self.salt.f_touch_master(_cache_path) |
| 362 | self.salt.f_append_master(_cache_path, _script) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 363 | # command salt to copy file to minions |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 364 | logger_cli.debug( |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 365 | "... creating script target folder '{}'".format( |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 366 | _cache_path |
| 367 | ) |
| 368 | ) |
| 369 | self.salt.mkdir( |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 370 | self.active_nodes_compound, |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 371 | os.path.join( |
| 372 | '/root', |
| 373 | config.salt_scripts_folder |
| 374 | ), |
| 375 | tgt_type="compound" |
| 376 | ) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 377 | logger.debug("... syncing file to nodes") |
| 378 | self.salt.get_file( |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 379 | self.active_nodes_compound, |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 380 | _source_path, |
| 381 | _target_path, |
| 382 | tgt_type="compound" |
| 383 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 384 | # return path on nodes, just in case |
| 385 | return _target_path |
| 386 | |
| 387 | def execute_script_on_node(self, node, script_filename, args=[]): |
| 388 | # Prepare path |
| 389 | _target_path = os.path.join( |
| 390 | '/root', |
| 391 | config.salt_scripts_folder, |
| 392 | script_filename |
| 393 | ) |
| 394 | |
| 395 | # execute script |
| 396 | logger.debug("... running script on '{}'".format(node)) |
| 397 | # handle results for each node |
| 398 | _script_arguments = " ".join(args) if args else "" |
| 399 | self.not_responded = [] |
| 400 | _r = self.salt.cmd( |
| 401 | node, |
| 402 | 'cmd.run', |
| 403 | param='python {} {}'.format(_target_path, _script_arguments), |
| 404 | expr_form="compound" |
| 405 | ) |
| 406 | |
| 407 | # all false returns means that there is no response |
| 408 | self.not_responded = [_n for _n in _r.keys() if not _r[_n]] |
| 409 | return _r |
| 410 | |
| 411 | def execute_script_on_active_nodes(self, script_filename, args=[]): |
| 412 | # Prepare path |
| 413 | _target_path = os.path.join( |
| 414 | '/root', |
| 415 | config.salt_scripts_folder, |
| 416 | script_filename |
| 417 | ) |
| 418 | |
| 419 | # execute script |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 420 | logger_cli.debug("... running script") |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 421 | # handle results for each node |
| 422 | _script_arguments = " ".join(args) if args else "" |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 423 | self.not_responded = [] |
| 424 | _r = self.salt.cmd( |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 425 | self.active_nodes_compound, |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 426 | 'cmd.run', |
| 427 | param='python {} {}'.format(_target_path, _script_arguments), |
| 428 | expr_form="compound" |
| 429 | ) |
| 430 | |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 431 | # all false returns means that there is no response |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 432 | self.not_responded = [_n for _n in _r.keys() if not _r[_n]] |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 433 | return _r |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 434 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 435 | def execute_cmd_on_active_nodes(self, cmd, nodes=None): |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 436 | # execute cmd |
| 437 | self.not_responded = [] |
| 438 | _r = self.salt.cmd( |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 439 | nodes if nodes else self.active_nodes_compound, |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 440 | 'cmd.run', |
| 441 | param=cmd, |
| 442 | expr_form="compound" |
| 443 | ) |
| 444 | |
| 445 | # all false returns means that there is no response |
| 446 | self.not_responded = [_n for _n in _r.keys() if not _r[_n]] |
| 447 | return _r |
| 448 | |
Alex Savatieiev | efa79c4 | 2019-03-14 19:14:04 -0500 | [diff] [blame] | 449 | def is_node_available(self, node, log=True): |
| 450 | if node in self.skip_list: |
| 451 | if log: |
| 452 | logger_cli.info("-> node '{}' not active".format(node)) |
| 453 | return False |
| 454 | elif node in self.not_responded: |
| 455 | if log: |
| 456 | logger_cli.info("-> node '{}' not responded".format(node)) |
| 457 | return False |
| 458 | else: |
| 459 | return True |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 460 | |
| 461 | |
| 462 | salt_master = SaltNodes() |