blob: 97030bbc9e05d2a595c90a2ccb6211b3319ef66b [file] [log] [blame]
savex4448e132018-04-25 15:51:14 +02001import os
2import re
3
Alex Savatieiev5118de02019-02-20 15:50:42 -06004from cfg_checker.common.const import all_roles_map
savex4448e132018-04-25 15:51:14 +02005
Alex Savatieiev5118de02019-02-20 15:50:42 -06006from cfg_checker.common.exception import ConfigException
savex4448e132018-04-25 15:51:14 +02007
Alex Savatieiev5118de02019-02-20 15:50:42 -06008pkg_dir = os.path.dirname(__file__)
9pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir)
10pkg_dir = os.path.normpath(pkg_dir)
11pkg_dir = os.path.abspath(pkg_dir)
savex4448e132018-04-25 15:51:14 +020012
13
14class 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
savexce010ba2018-04-27 09:49:23 +020071 def get_nodes_list(self, env, nodes_list):
savex4448e132018-04-25 15:51:14 +020072 _list = []
73 if env is None:
74 # nothing supplied, use the one in repo
75 try:
Alex Savatieievd48994d2018-12-13 12:13:00 +010076 if not nodes_list:
77 return []
Alex Savatieiev5118de02019-02-20 15:50:42 -060078 with open(os.path.join(pkg_dir, nodes_list)) as _f:
savex4448e132018-04-25 15:51:14 +020079 _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
99utils = Utils()