blob: 2620d05db6409096720e829dec79e0334b998c86 [file] [log] [blame]
savex4448e132018-04-25 15:51:14 +02001import os
2import re
Alex Savatieiev63576832019-02-27 15:46:26 -06003import subprocess
savex4448e132018-04-25 15:51:14 +02004
Alexe0c5b9e2019-04-23 18:51:23 -05005from cfg_checker.common.const import all_roles_map, uknown_code
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
Alex Savatieiev63576832019-02-27 15:46:26 -060014def shell(command):
15 _ps = subprocess.Popen(
16 command.split(),
17 stdout=subprocess.PIPE
18 ).communicate()[0].decode()
19
20 return _ps
21
22
savex4448e132018-04-25 15:51:14 +020023class 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 Savatieievf808cd22019-03-01 13:17:59 -060035 _message = "# Validation passed"
savex4448e132018-04-25 15:51:14 +020036
37 def _result():
38 return (True, _message) if message else True
39
40 # node role code checks
Alexd0391d42019-05-21 18:48:55 -050041 _code = re.findall(r"[a-zA-Z]+", fqdn.split('.')[0])
savex4448e132018-04-25 15:51:14 +020042 if len(_code) > 0:
43 if _code[0] in all_roles_map:
44 return _result()
45 else:
46 # log warning here
Alex Savatieievf808cd22019-03-01 13:17:59 -060047 _message = "# Node code is unknown, '{}'. " \
48 "# Please, update map".format(_code)
savex4448e132018-04-25 15:51:14 +020049 else:
50 # log warning here
Alex Savatieievf808cd22019-03-01 13:17:59 -060051 _message = "# Node name is invalid, '{}'".format(fqdn)
savex4448e132018-04-25 15:51:14 +020052
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)
Alexd0391d42019-05-21 18:48:55 -050073 _code = re.findall(r"[a-zA-Z]+?(?=(?:[0-9]+$)|$)", fqdn.split('.')[0])
savex4448e132018-04-25 15:51:14 +020074 # check if it is valid and raise if not
75 if _isvalid:
Alexe0c5b9e2019-04-23 18:51:23 -050076 # try to match it with ones in map
77 _c = _code[0]
78 match = any([r in _c for r in all_roles_map.keys()])
79 if match:
80 # no match, try to find it
81 match = False
82 for r in all_roles_map.keys():
83 _idx = _c.find(r)
84 if _idx > -1:
85 _c = _c[_idx:]
86 match = True
87 break
88 if match:
89 return _c
90 else:
91 return uknown_code
92 else:
93 return uknown_code
savex4448e132018-04-25 15:51:14 +020094 else:
95 raise ConfigException(_message)
96
savexce010ba2018-04-27 09:49:23 +020097 def get_nodes_list(self, env, nodes_list):
savex4448e132018-04-25 15:51:14 +020098 _list = []
99 if env is None:
100 # nothing supplied, use the one in repo
101 try:
Alex Savatieievd48994d2018-12-13 12:13:00 +0100102 if not nodes_list:
103 return []
Alex Savatieiev5118de02019-02-20 15:50:42 -0600104 with open(os.path.join(pkg_dir, nodes_list)) as _f:
savex4448e132018-04-25 15:51:14 +0200105 _list.extend(_f.read().splitlines())
106 except IOError as e:
Alex Savatieievf808cd22019-03-01 13:17:59 -0600107 raise ConfigException("# Error while loading file, '{}': "
savex4448e132018-04-25 15:51:14 +0200108 "{}".format(e.filename, e.strerror))
109 else:
110 _list.extend(self.node_string_to_list(env))
111
112 # validate names
113 _invalid = []
114 _valid = []
115 for idx in range(len(_list)):
116 _name = _list[idx]
117 if not self.validate_name(_name):
118 _invalid.append(_name)
119 else:
120 _valid.append(_name)
121
122 return _valid
123
124
125utils = Utils()