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