blob: 9762e8e3f24696a7d470bbde1d47156e45f8f344 [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import os
2import yaml
3import requests
4import re
5
6
7class salt_remote:
8 def cmd(self, tgt, fun, param=None,expr_form=None):
9 config = get_configuration(__file__)
10 for salt_cred in ['SALT_USERNAME', 'SALT_PASSWORD', 'SALT_URL']:
11 if os.environ.get(salt_cred):
12 config[salt_cred] = os.environ[salt_cred]
13 headers = {'Accept':'application/json'}
14 login_payload = {'username':config['SALT_USERNAME'],'password':config['SALT_PASSWORD'],'eauth':'pam'}
15 accept_key_payload = {'fun': fun,'tgt':tgt,'client':'local','expr_form':expr_form}
16 if param:
17 accept_key_payload['arg']=param
18
19 login_request = requests.post(os.path.join(config['SALT_URL'],'login'),headers=headers,data=login_payload)
20 request = requests.post(config['SALT_URL'],headers=headers,data=accept_key_payload,cookies=login_request.cookies)
21 return request.json()['return'][0]
22
23
24def init_salt_client():
25 local = salt_remote()
26 return local
27
28
29def get_active_nodes(config):
30 local_salt_client = init_salt_client()
31
32 skipped_nodes = config.get('skipped_nodes') or []
33 # TODO add skipped nodes to cmd command instead of filtering
34 nodes = local_salt_client.cmd('*', 'test.ping')
35 active_nodes = [
36 node_name for node_name in nodes
37 if nodes[node_name] and node_name not in skipped_nodes
38 ]
39 return active_nodes
40
41
42def get_groups(config):
43 # assume that node name is like <name>.domain
44 # last 1-3 digits of name are index, e.g. 001 in cpu001
45 # name doesn't contain dots
46 active_nodes = get_active_nodes(config)
47 skipped_group = config.get('skipped_group') or []
48 groups = []
49
50 for node in active_nodes:
51 index = re.search('[0-9]{1,3}$', node.split('.')[0])
52 if index:
53 group_name = node.split('.')[0][:-len(index.group(0))]
54 else:
55 group_name = node
56 if group_name not in skipped_group and group_name not in groups:
57 groups.append(group_name)
58 test_groups = []
59 groups_from_config = config.get('groups')
60 # check if config.yaml contains `groups` key
61 if groups_from_config is not None:
62 invalid_groups = []
63 for group in groups_from_config:
64 # check if group name from config
65 # is substring of one of the groups
66 grp = [x for x in groups if group in x]
67 if grp:
68 test_groups.append(grp[0])
69 else:
70 invalid_groups.append(group)
71 if invalid_groups:
72 raise ValueError('Config file contains'
73 ' invalid groups name: {}'.format(invalid_groups))
74
75 groups = test_groups if test_groups else groups
76
77 # For splitting Ceph nodes
78 local_salt_client = init_salt_client()
79
80 if "ceph*" in groups:
81 groups.remove("ceph*")
82
83 ceph_status = local_salt_client.cmd(
84 'ceph*', "cmd.run", ["ps aux | grep ceph-mon | grep -v grep"])
85
86 mon = []
87 ceph = []
88 for node in ceph_status:
89 if ceph_status[node] != '':
90 mon.append(node.split('.')[0])
91 else:
92 ceph.append(node.split('.')[0])
93
94 mon_regex = "({0}.*)".format(".*|".join(mon))
95 groups.append(mon_regex)
96
97 ceph_regex = "({0}.*)".format(".*|".join(ceph))
98 groups.append(ceph_regex)
99
100 return groups
101
102
103def get_configuration(path_to_test):
104 """function returns configuration for environment
105
106 and for test if it's specified"""
107 global_config_file = os.path.join(
108 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
109 with open(global_config_file, 'r') as file:
110 global_config = yaml.load(file)
111
112 config_file = os.path.join(
113 os.path.dirname(os.path.abspath(path_to_test)), "config.yaml")
114
115 if os.path.exists(config_file):
116 with open(config_file, 'r') as file:
117 global_config.update(yaml.load(file))
118
119 return global_config