Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 1 | import os |
| 2 | import yaml |
| 3 | import requests |
| 4 | import re |
| 5 | import sys, traceback |
| 6 | import itertools |
| 7 | import helpers |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 8 | from utils import os_client |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class salt_remote: |
| 12 | def cmd(self, tgt, fun, param=None, expr_form=None, tgt_type=None): |
| 13 | config = get_configuration() |
| 14 | url = config['SALT_URL'] |
| 15 | proxies = {"http": None, "https": None} |
| 16 | headers = {'Accept': 'application/json'} |
| 17 | login_payload = {'username': config['SALT_USERNAME'], |
| 18 | 'password': config['SALT_PASSWORD'], 'eauth': 'pam'} |
| 19 | accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local', |
| 20 | 'expr_form': expr_form, 'tgt_type': tgt_type, |
| 21 | 'timeout': config['salt_timeout']} |
| 22 | if param: |
| 23 | accept_key_payload['arg'] = param |
| 24 | |
| 25 | try: |
| 26 | login_request = requests.post(os.path.join(url, 'login'), |
| 27 | headers=headers, data=login_payload, |
| 28 | proxies=proxies) |
| 29 | if login_request.ok: |
| 30 | request = requests.post(url, headers=headers, |
| 31 | data=accept_key_payload, |
| 32 | cookies=login_request.cookies, |
| 33 | proxies=proxies) |
| 34 | return request.json()['return'][0] |
| 35 | except Exception: |
| 36 | print "\033[91m\nConnection to SaltMaster " \ |
| 37 | "was not established.\n" \ |
| 38 | "Please make sure that you " \ |
| 39 | "provided correct credentials.\033[0m\n" |
| 40 | traceback.print_exc(file=sys.stdout) |
| 41 | sys.exit() |
| 42 | |
| 43 | |
| 44 | def init_salt_client(): |
| 45 | local = salt_remote() |
| 46 | return local |
| 47 | |
| 48 | |
| 49 | def compile_pairs (nodes): |
| 50 | result = {} |
| 51 | if len(nodes) %2 != 0: |
| 52 | nodes.pop(1) |
| 53 | pairs = zip(*[iter(nodes)]*2) |
| 54 | for pair in pairs: |
| 55 | result[pair[0]+'<>'+pair[1]] = pair |
| 56 | return result |
| 57 | |
| 58 | |
| 59 | def get_pairs(): |
| 60 | # TODO |
| 61 | # maybe collect cmp from nova service-list |
| 62 | config = get_configuration() |
| 63 | local_salt_client = init_salt_client() |
| 64 | cmp_hosts = config.get('CMP_HOSTS') or [] |
| 65 | skipped_nodes = config.get('skipped_nodes') or [] |
| 66 | if skipped_nodes: |
| 67 | print "Notice: {0} nodes will be skipped for vm2vm test".format(skipped_nodes) |
| 68 | if not cmp_hosts: |
| 69 | nodes = local_salt_client.cmd( |
| 70 | 'I@nova:compute', |
| 71 | 'test.ping', |
| 72 | expr_form='compound') |
| 73 | cmp_hosts = [node.split('.')[0] for node in nodes.keys() if node not in skipped_nodes] |
| 74 | return compile_pairs(cmp_hosts) |
| 75 | |
| 76 | |
| 77 | def get_hw_pairs(): |
| 78 | config = get_configuration() |
| 79 | local_salt_client = init_salt_client() |
| 80 | hw_nodes = config.get('HW_NODES') or [] |
| 81 | skipped_nodes = config.get('skipped_nodes') or [] |
| 82 | if skipped_nodes: |
| 83 | print "Notice: {0} nodes will be skipped for hw2hw test".format(skipped_nodes) |
| 84 | if not hw_nodes: |
| 85 | nodes = local_salt_client.cmd( |
| 86 | 'I@salt:control or I@nova:compute', |
| 87 | 'test.ping', |
| 88 | expr_form='compound') |
| 89 | hw_nodes = [node for node in nodes.keys() if node not in skipped_nodes] |
| 90 | print local_salt_client.cmd(expr_form='compound', tgt="L@"+','.join(hw_nodes), |
| 91 | fun='pkg.install', param=['iperf']) |
| 92 | return compile_pairs(hw_nodes) |
| 93 | |
| 94 | def get_configuration(): |
| 95 | """function returns configuration for environment |
| 96 | and for test if it's specified""" |
| 97 | |
| 98 | global_config_file = os.path.join( |
| 99 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 100 | with open(global_config_file, 'r') as file: |
| 101 | global_config = yaml.load(file) |
| 102 | for param in global_config.keys(): |
| 103 | if param in os.environ.keys(): |
| 104 | if ',' in os.environ[param]: |
| 105 | global_config[param] = [] |
| 106 | for item in os.environ[param].split(','): |
| 107 | global_config[param].append(item) |
| 108 | else: |
| 109 | global_config[param] = os.environ[param] |
| 110 | |
| 111 | return global_config |