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