blob: aeb4cd896cd2cb12cf8a799f73bd25c93ed66047 [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import os
2import yaml
3import requests
4import re
Oleksii Zhurbae592ed12018-06-21 18:01:09 -05005import sys, traceback
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00006
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00007
Mikhail Chernikc492a682018-08-27 22:41:17 +02008class AuthenticationError(Exception):
9 pass
10
11
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000012class salt_remote:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000013 def cmd(self, tgt, fun, param=None, expr_form=None, tgt_type=None):
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000014 config = get_configuration()
Valentyn Khalin81dd23d2018-09-20 15:39:07 +030015 url = config['SALT_URL'].strip()
16 if not re.match("^(http|https)://", url):
17 raise AuthenticationError("Salt URL should start \
18 with http or https, given - {}".format(url))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050019 proxies = {"http": None, "https": None}
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000020 headers = {'Accept': 'application/json'}
21 login_payload = {'username': config['SALT_USERNAME'],
22 'password': config['SALT_PASSWORD'], 'eauth': 'pam'}
23 accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local',
Oleksii Zhurba7a671112018-01-12 11:50:56 -060024 'expr_form': expr_form, 'tgt_type': tgt_type,
25 'timeout': config['salt_timeout']}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000026 if param:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000027 accept_key_payload['arg'] = param
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000028
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050029 try:
30 login_request = requests.post(os.path.join(url, 'login'),
31 headers=headers, data=login_payload,
32 proxies=proxies)
Mikhail Chernikc492a682018-08-27 22:41:17 +020033 if not login_request.ok:
34 raise AuthenticationError("Authentication to SaltMaster failed")
35
36 request = requests.post(url, headers=headers,
37 data=accept_key_payload,
38 cookies=login_request.cookies,
39 proxies=proxies)
40
41 response = request.json()['return'][0]
Mikhail Chernikc492a682018-08-27 22:41:17 +020042 return response
43
44 except Exception as e:
45 print ("\033[91m\nConnection to SaltMaster "
46 "was not established.\n"
47 "Please make sure that you "
48 "provided correct credentials.\n"
49 "Error message: {}\033[0m\n".format(e.message or e)
50 )
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050051 traceback.print_exc(file=sys.stdout)
52 sys.exit()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000053
54
55def init_salt_client():
56 local = salt_remote()
57 return local
58
59
Mikhail Chernik714596e2018-08-10 21:19:07 +020060def list_to_target_string(node_list, separator, add_spaces=True):
61 if add_spaces:
62 separator = ' ' + separator.strip() + ' '
63 return separator.join(node_list)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000064
65
Oleksii Zhurba020fab42017-11-01 20:13:28 +000066def get_monitoring_ip(param_name):
67 local_salt_client = init_salt_client()
68 salt_output = local_salt_client.cmd(
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030069 'salt:master',
Oleksii Zhurba020fab42017-11-01 20:13:28 +000070 'pillar.get',
71 ['_param:{}'.format(param_name)],
72 expr_form='pillar')
73 return salt_output[salt_output.keys()[0]]
74
75
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000076def get_active_nodes(test=None):
77 config = get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000078 local_salt_client = init_salt_client()
79
80 skipped_nodes = config.get('skipped_nodes') or []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000081 if test:
82 testname = test.split('.')[0]
83 if 'skipped_nodes' in config.get(testname).keys():
84 skipped_nodes += config.get(testname)['skipped_nodes'] or []
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000085 if skipped_nodes != ['']:
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000086 print "\nNotice: {0} nodes will be skipped".format(skipped_nodes)
87 nodes = local_salt_client.cmd(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000088 '* and not ' + list_to_target_string(skipped_nodes, 'and not'),
89 'test.ping',
90 expr_form='compound')
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000091 else:
92 nodes = local_salt_client.cmd('*', 'test.ping')
93 return nodes
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000094
95
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000096def calculate_groups():
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000097 config = get_configuration()
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000098 local_salt_client = init_salt_client()
Oleksii Zhurba30122e12018-03-29 14:01:50 -050099 node_groups = {}
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000100 nodes_names = set ()
101 expr_form = ''
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500102 all_nodes = set(local_salt_client.cmd('*', 'test.ping'))
103 if 'groups' in config.keys() and 'PB_GROUPS' in os.environ.keys() and \
104 os.environ['PB_GROUPS'].lower() != 'false':
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000105 nodes_names.update(config['groups'].keys())
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500106 expr_form = 'compound'
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000107 else:
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500108 for node in all_nodes:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000109 index = re.search('[0-9]{1,3}$', node.split('.')[0])
110 if index:
111 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000112 else:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000113 nodes_names.add(node)
114 expr_form = 'pcre'
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000115
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500116 gluster_nodes = local_salt_client.cmd('I@salt:control and '
117 'I@glusterfs:server',
118 'test.ping', expr_form='compound')
119 kvm_nodes = local_salt_client.cmd('I@salt:control and not '
120 'I@glusterfs:server',
121 'test.ping', expr_form='compound')
122
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000123 for node_name in nodes_names:
124 skipped_groups = config.get('skipped_groups') or []
125 if node_name in skipped_groups:
126 continue
127 if expr_form == 'pcre':
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500128 nodes = local_salt_client.cmd('{}[0-9]{{1,3}}'.format(node_name),
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000129 'test.ping',
130 expr_form=expr_form)
131 else:
132 nodes = local_salt_client.cmd(config['groups'][node_name],
133 'test.ping',
134 expr_form=expr_form)
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500135 if nodes == {}:
136 continue
137
138 node_groups[node_name]=[x for x in nodes
139 if x not in config['skipped_nodes']
140 if x not in gluster_nodes.keys()
141 if x not in kvm_nodes.keys()]
142 all_nodes = set(all_nodes - set(node_groups[node_name]))
143 if node_groups[node_name] == []:
144 del node_groups[node_name]
145 if kvm_nodes:
146 node_groups['kvm'] = kvm_nodes.keys()
147 node_groups['kvm_gluster'] = gluster_nodes.keys()
148 all_nodes = set(all_nodes - set(kvm_nodes.keys()))
149 all_nodes = set(all_nodes - set(gluster_nodes.keys()))
150 if all_nodes:
151 print ("These nodes were not collected {0}. Check config (groups section)".format(all_nodes))
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -0500152 return node_groups
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000153
154
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000155def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000156 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000157 and for test if it's specified"""
158 global_config_file = os.path.join(
159 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
160 with open(global_config_file, 'r') as file:
161 global_config = yaml.load(file)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000162 for param in global_config.keys():
163 if param in os.environ.keys():
164 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000165 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000166 for item in os.environ[param].split(','):
167 global_config[param].append(item)
168 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000169 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000170
171 return global_config