blob: 746b5bfc5c71420b4e6940726b253541ef02bb4a [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import os
2import yaml
3import requests
4import re
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00005
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00006
7class salt_remote:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00008 def cmd(self, tgt, fun, param=None, expr_form=None, tgt_type=None):
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +00009 config = get_configuration()
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000010 headers = {'Accept': 'application/json'}
11 login_payload = {'username': config['SALT_USERNAME'],
12 'password': config['SALT_PASSWORD'], 'eauth': 'pam'}
13 accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local',
Oleksii Zhurba7a671112018-01-12 11:50:56 -060014 'expr_form': expr_form, 'tgt_type': tgt_type,
15 'timeout': config['salt_timeout']}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000016 if param:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000017 accept_key_payload['arg'] = param
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000018
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000019 login_request = requests.post(os.path.join(config['SALT_URL'],
20 'login'),
21 headers=headers, data=login_payload)
vkhalin8b402672017-12-05 17:47:57 +020022 if login_request.ok:
23 request = requests.post(config['SALT_URL'], headers=headers,
24 data=accept_key_payload,
25 cookies=login_request.cookies)
26 return request.json()['return'][0]
27 else:
28 raise EnvironmentError("401 Not authorized.")
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000029
30
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000031node_groups = {}
32
33
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000034def init_salt_client():
35 local = salt_remote()
36 return local
37
38
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000039def list_to_target_string(node_list, separator):
40 result = ''
41 for node in node_list:
42 result += node + ' ' + separator + ' '
43 return result.strip(' ' + separator + ' ')
44
45
Oleksii Zhurba020fab42017-11-01 20:13:28 +000046def get_monitoring_ip(param_name):
47 local_salt_client = init_salt_client()
48 salt_output = local_salt_client.cmd(
49 'docker:client:stack:monitoring',
50 'pillar.get',
51 ['_param:{}'.format(param_name)],
52 expr_form='pillar')
53 return salt_output[salt_output.keys()[0]]
54
55
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000056def get_active_nodes(test=None):
57 config = get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000058 local_salt_client = init_salt_client()
59
60 skipped_nodes = config.get('skipped_nodes') or []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000061 if test:
62 testname = test.split('.')[0]
63 if 'skipped_nodes' in config.get(testname).keys():
64 skipped_nodes += config.get(testname)['skipped_nodes'] or []
65
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000066 if skipped_nodes != ['']:
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000067 print "\nNotice: {0} nodes will be skipped".format(skipped_nodes)
68 nodes = local_salt_client.cmd(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000069 '* and not ' + list_to_target_string(skipped_nodes, 'and not'),
70 'test.ping',
71 expr_form='compound')
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000072 else:
73 nodes = local_salt_client.cmd('*', 'test.ping')
74 return nodes
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000075
76
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000077def calculate_groups():
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000078 config = get_configuration()
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000079 local_salt_client = init_salt_client()
80 nodes_names = set ()
81 expr_form = ''
82 if 'groups' in config.keys():
83 nodes_names.update(config['groups'].keys())
84 expr_form = 'pillar'
85 else:
86 nodes = local_salt_client.cmd('*', 'test.ping')
87 for node in nodes:
88 index = re.search('[0-9]{1,3}$', node.split('.')[0])
89 if index:
90 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000091 else:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000092 nodes_names.add(node)
93 expr_form = 'pcre'
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000094
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000095 for node_name in nodes_names:
96 skipped_groups = config.get('skipped_groups') or []
97 if node_name in skipped_groups:
98 continue
99 if expr_form == 'pcre':
100 nodes = local_salt_client.cmd(node_name,
101 'test.ping',
102 expr_form=expr_form)
103 else:
104 nodes = local_salt_client.cmd(config['groups'][node_name],
105 'test.ping',
106 expr_form=expr_form)
107 node_groups[node_name]=[x for x in nodes if x not in config['skipped_nodes']]
108
109
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000110def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000111 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000112 and for test if it's specified"""
113 global_config_file = os.path.join(
114 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
115 with open(global_config_file, 'r') as file:
116 global_config = yaml.load(file)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000117 for param in global_config.keys():
118 if param in os.environ.keys():
119 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000120 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000121 for item in os.environ[param].split(','):
122 global_config[param].append(item)
123 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000124 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000125
126 return global_config
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000127
128
129calculate_groups()