blob: 1d34776b19fa8969a769392613f270143d04ebe1 [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
Alex Savatieiev5118de02019-02-20 15:50:42 -06005from cfg_checker.common.const import all_roles_map
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
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 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)
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
savexce010ba2018-04-27 09:49:23 +020080 def get_nodes_list(self, env, nodes_list):
savex4448e132018-04-25 15:51:14 +020081 _list = []
82 if env is None:
83 # nothing supplied, use the one in repo
84 try:
Alex Savatieievd48994d2018-12-13 12:13:00 +010085 if not nodes_list:
86 return []
Alex Savatieiev5118de02019-02-20 15:50:42 -060087 with open(os.path.join(pkg_dir, nodes_list)) as _f:
savex4448e132018-04-25 15:51:14 +020088 _list.extend(_f.read().splitlines())
89 except IOError as e:
Alex Savatieievf808cd22019-03-01 13:17:59 -060090 raise ConfigException("# Error while loading file, '{}': "
savex4448e132018-04-25 15:51:14 +020091 "{}".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
108utils = Utils()