blob: 83b867d88a4a169e26389da096c680168bec6527 [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 + ' '
Oleksii Zhurba26a3a1c2018-01-16 19:10:20 +000043 return result[:-(len(separator)+2)]
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000044
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 []
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000065 if skipped_nodes != ['']:
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000066 print "\nNotice: {0} nodes will be skipped".format(skipped_nodes)
67 nodes = local_salt_client.cmd(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000068 '* and not ' + list_to_target_string(skipped_nodes, 'and not'),
69 'test.ping',
70 expr_form='compound')
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000071 else:
72 nodes = local_salt_client.cmd('*', 'test.ping')
73 return nodes
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000074
75
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000076def calculate_groups():
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000077 config = get_configuration()
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000078 local_salt_client = init_salt_client()
79 nodes_names = set ()
80 expr_form = ''
81 if 'groups' in config.keys():
82 nodes_names.update(config['groups'].keys())
83 expr_form = 'pillar'
84 else:
85 nodes = local_salt_client.cmd('*', 'test.ping')
86 for node in nodes:
87 index = re.search('[0-9]{1,3}$', node.split('.')[0])
88 if index:
89 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000090 else:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000091 nodes_names.add(node)
92 expr_form = 'pcre'
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000093
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000094 for node_name in nodes_names:
95 skipped_groups = config.get('skipped_groups') or []
96 if node_name in skipped_groups:
97 continue
98 if expr_form == 'pcre':
99 nodes = local_salt_client.cmd(node_name,
100 'test.ping',
101 expr_form=expr_form)
102 else:
103 nodes = local_salt_client.cmd(config['groups'][node_name],
104 'test.ping',
105 expr_form=expr_form)
106 node_groups[node_name]=[x for x in nodes if x not in config['skipped_nodes']]
107
108
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000109def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000110 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000111 and for test if it's specified"""
112 global_config_file = os.path.join(
113 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
114 with open(global_config_file, 'r') as file:
115 global_config = yaml.load(file)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000116 for param in global_config.keys():
117 if param in os.environ.keys():
118 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000119 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000120 for item in os.environ[param].split(','):
121 global_config[param].append(item)
122 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000123 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000124
125 return global_config
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000126
127
128calculate_groups()