savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import os |
| 2 | import re |
| 3 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 4 | from cfg_checker.common.const import all_roles_map |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 5 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 6 | from cfg_checker.common.exception import ConfigException |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 7 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 8 | pkg_dir = os.path.dirname(__file__) |
| 9 | pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) |
| 10 | pkg_dir = os.path.normpath(pkg_dir) |
| 11 | pkg_dir = os.path.abspath(pkg_dir) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class Utils(object): |
| 15 | @staticmethod |
| 16 | def validate_name(fqdn, message=False): |
| 17 | """ |
| 18 | Function that tries to validate node name. |
| 19 | Checks if code contains letters, has '.' in it, |
| 20 | roles map contains code's role |
| 21 | |
| 22 | :param fqdn: node FQDN name to supply for the check |
| 23 | :param message: True if validate should return error check message |
| 24 | :return: False if checks failed, True if all checks passed |
| 25 | """ |
| 26 | _message = "Validation passed" |
| 27 | |
| 28 | def _result(): |
| 29 | return (True, _message) if message else True |
| 30 | |
| 31 | # node role code checks |
| 32 | _code = re.findall("[a-zA-Z]+", fqdn.split('.')[0]) |
| 33 | if len(_code) > 0: |
| 34 | if _code[0] in all_roles_map: |
| 35 | return _result() |
| 36 | else: |
| 37 | # log warning here |
| 38 | _message = "Node code is unknown, '{}'. " \ |
| 39 | "Please, update map".format(_code) |
| 40 | else: |
| 41 | # log warning here |
| 42 | _message = "Node name is invalid, '{}'".format(fqdn) |
| 43 | |
| 44 | # put other checks here |
| 45 | |
| 46 | # output result |
| 47 | return _result() |
| 48 | |
| 49 | @staticmethod |
| 50 | def node_string_to_list(node_string): |
| 51 | # supplied var should contain node list |
| 52 | # if there is no ',' -> use space as a delimiter |
| 53 | if node_string is not None: |
| 54 | if node_string.find(',') < 0: |
| 55 | return node_string.split(' ') |
| 56 | else: |
| 57 | return node_string.split(',') |
| 58 | else: |
| 59 | return [] |
| 60 | |
| 61 | def get_node_code(self, fqdn): |
| 62 | # validate |
| 63 | _isvalid, _message = self.validate_name(fqdn, message=True) |
| 64 | _code = re.findall("[a-zA-Z]+", fqdn.split('.')[0]) |
| 65 | # check if it is valid and raise if not |
| 66 | if _isvalid: |
| 67 | return _code[0] |
| 68 | else: |
| 69 | raise ConfigException(_message) |
| 70 | |
savex | ce010ba | 2018-04-27 09:49:23 +0200 | [diff] [blame] | 71 | def get_nodes_list(self, env, nodes_list): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 72 | _list = [] |
| 73 | if env is None: |
| 74 | # nothing supplied, use the one in repo |
| 75 | try: |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 76 | if not nodes_list: |
| 77 | return [] |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 78 | with open(os.path.join(pkg_dir, nodes_list)) as _f: |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 79 | _list.extend(_f.read().splitlines()) |
| 80 | except IOError as e: |
| 81 | raise ConfigException("Error while loading file, '{}': " |
| 82 | "{}".format(e.filename, e.strerror)) |
| 83 | else: |
| 84 | _list.extend(self.node_string_to_list(env)) |
| 85 | |
| 86 | # validate names |
| 87 | _invalid = [] |
| 88 | _valid = [] |
| 89 | for idx in range(len(_list)): |
| 90 | _name = _list[idx] |
| 91 | if not self.validate_name(_name): |
| 92 | _invalid.append(_name) |
| 93 | else: |
| 94 | _valid.append(_name) |
| 95 | |
| 96 | return _valid |
| 97 | |
| 98 | |
| 99 | utils = Utils() |