blob: 74af967fb7126b29d04a6acf72ed197b7d2e1f89 [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
8class salt_remote:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00009 def cmd(self, tgt, fun, param=None, expr_form=None, tgt_type=None):
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000010 config = get_configuration()
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050011 url = config['SALT_URL']
12 proxies = {"http": None, "https": None}
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000013 headers = {'Accept': 'application/json'}
14 login_payload = {'username': config['SALT_USERNAME'],
15 'password': config['SALT_PASSWORD'], 'eauth': 'pam'}
16 accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local',
Oleksii Zhurba7a671112018-01-12 11:50:56 -060017 'expr_form': expr_form, 'tgt_type': tgt_type,
18 'timeout': config['salt_timeout']}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000019 if param:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000020 accept_key_payload['arg'] = param
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000021
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050022 try:
23 login_request = requests.post(os.path.join(url, 'login'),
24 headers=headers, data=login_payload,
25 proxies=proxies)
26 if login_request.ok:
27 request = requests.post(url, headers=headers,
28 data=accept_key_payload,
29 cookies=login_request.cookies,
30 proxies=proxies)
31 return request.json()['return'][0]
32 except Exception:
33 print "\033[91m\nConnection to SaltMaster " \
34 "was not established.\n" \
35 "Please make sure that you " \
36 "provided correct credentials.\033[0m\n"
37 traceback.print_exc(file=sys.stdout)
38 sys.exit()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000039
40
41def init_salt_client():
42 local = salt_remote()
43 return local
44
45
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000046def list_to_target_string(node_list, separator):
47 result = ''
48 for node in node_list:
49 result += node + ' ' + separator + ' '
Oleksii Zhurba26a3a1c2018-01-16 19:10:20 +000050 return result[:-(len(separator)+2)]
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000051
52
Oleksii Zhurba020fab42017-11-01 20:13:28 +000053def get_monitoring_ip(param_name):
54 local_salt_client = init_salt_client()
55 salt_output = local_salt_client.cmd(
56 'docker:client:stack:monitoring',
57 'pillar.get',
58 ['_param:{}'.format(param_name)],
59 expr_form='pillar')
60 return salt_output[salt_output.keys()[0]]
61
62
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000063def get_active_nodes(test=None):
64 config = get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000065 local_salt_client = init_salt_client()
66
67 skipped_nodes = config.get('skipped_nodes') or []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000068 if test:
69 testname = test.split('.')[0]
70 if 'skipped_nodes' in config.get(testname).keys():
71 skipped_nodes += config.get(testname)['skipped_nodes'] or []
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000072 if skipped_nodes != ['']:
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000073 print "\nNotice: {0} nodes will be skipped".format(skipped_nodes)
74 nodes = local_salt_client.cmd(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000075 '* and not ' + list_to_target_string(skipped_nodes, 'and not'),
76 'test.ping',
77 expr_form='compound')
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000078 else:
79 nodes = local_salt_client.cmd('*', 'test.ping')
80 return nodes
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000081
82
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000083def calculate_groups():
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000084 config = get_configuration()
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000085 local_salt_client = init_salt_client()
Oleksii Zhurba30122e12018-03-29 14:01:50 -050086 node_groups = {}
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000087 nodes_names = set ()
88 expr_form = ''
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050089 all_nodes = set(local_salt_client.cmd('*', 'test.ping'))
90 if 'groups' in config.keys() and 'PB_GROUPS' in os.environ.keys() and \
91 os.environ['PB_GROUPS'].lower() != 'false':
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000092 nodes_names.update(config['groups'].keys())
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050093 expr_form = 'compound'
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000094 else:
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050095 for node in all_nodes:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000096 index = re.search('[0-9]{1,3}$', node.split('.')[0])
97 if index:
98 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000099 else:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000100 nodes_names.add(node)
101 expr_form = 'pcre'
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000102
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500103 gluster_nodes = local_salt_client.cmd('I@salt:control and '
104 'I@glusterfs:server',
105 'test.ping', expr_form='compound')
106 kvm_nodes = local_salt_client.cmd('I@salt:control and not '
107 'I@glusterfs:server',
108 'test.ping', expr_form='compound')
109
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000110 for node_name in nodes_names:
111 skipped_groups = config.get('skipped_groups') or []
112 if node_name in skipped_groups:
113 continue
114 if expr_form == 'pcre':
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500115 nodes = local_salt_client.cmd('{}[0-9]{{1,3}}'.format(node_name),
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000116 'test.ping',
117 expr_form=expr_form)
118 else:
119 nodes = local_salt_client.cmd(config['groups'][node_name],
120 'test.ping',
121 expr_form=expr_form)
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500122 if nodes == {}:
123 continue
124
125 node_groups[node_name]=[x for x in nodes
126 if x not in config['skipped_nodes']
127 if x not in gluster_nodes.keys()
128 if x not in kvm_nodes.keys()]
129 all_nodes = set(all_nodes - set(node_groups[node_name]))
130 if node_groups[node_name] == []:
131 del node_groups[node_name]
132 if kvm_nodes:
133 node_groups['kvm'] = kvm_nodes.keys()
134 node_groups['kvm_gluster'] = gluster_nodes.keys()
135 all_nodes = set(all_nodes - set(kvm_nodes.keys()))
136 all_nodes = set(all_nodes - set(gluster_nodes.keys()))
137 if all_nodes:
138 print ("These nodes were not collected {0}. Check config (groups section)".format(all_nodes))
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -0500139 return node_groups
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000140
141
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000142def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000143 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000144 and for test if it's specified"""
145 global_config_file = os.path.join(
146 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
147 with open(global_config_file, 'r') as file:
148 global_config = yaml.load(file)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000149 for param in global_config.keys():
150 if param in os.environ.keys():
151 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000152 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000153 for item in os.environ[param].split(','):
154 global_config[param].append(item)
155 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000156 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000157
158 return global_config