| mirabal | 126e787 | 2017-07-06 05:54:49 -0500 | [diff] [blame] | 1 | import yaml |
| 2 | import re |
| 3 | import os |
| 4 | from string import Template |
| 5 | from ssh import * |
| 6 | |
| 7 | |
| 8 | def save_tmp_yaml(name, data): |
| 9 | """ |
| 10 | Save a yaml file into a file to be declare in openvim |
| 11 | :param name: file name |
| 12 | :param data: |
| 13 | :return: |
| 14 | """ |
| 15 | with open(name, "w") as text_file: |
| 16 | text_file.write(data) |
| 17 | |
| 18 | |
| 19 | def delete_tmp_yaml(name): |
| 20 | """ |
| 21 | Delete yaml form Filesystem |
| 22 | :param name: File name |
| 23 | :return: |
| 24 | """ |
| 25 | execute_local('rm {}'.format(name)) |
| 26 | |
| 27 | |
| 28 | def search_host_in_env_var(): |
| 29 | """ |
| 30 | Search for OPENVIM_TEST_HOST_X env var declare by pre_create_host fixture with the host id after creation. |
| garciadeblas | b7970ad | 2019-03-21 09:58:25 +0100 | [diff] [blame] | 31 | :return: All env vars found |
| mirabal | 126e787 | 2017-07-06 05:54:49 -0500 | [diff] [blame] | 32 | """ |
| 33 | return search('OPENVIM_TEST_HOST_') |
| 34 | |
| 35 | |
| 36 | def template_substitute(file_path, values): |
| 37 | """ |
| 38 | Modify a Yaml template with values. |
| 39 | :param file_path: template file |
| 40 | :param values: values to be substituted |
| 41 | :return: a string with the file content modified |
| 42 | """ |
| 43 | with open(file_path, 'r') as server_yaml: |
| 44 | template = Template(server_yaml.read()) |
| 45 | server_yaml = template.safe_substitute(values) |
| 46 | return server_yaml |
| 47 | |
| 48 | |
| 49 | def search(reg_ex): |
| 50 | """ |
| 51 | Search for environment vars. |
| 52 | :param reg_ex: regular expresion to be applied during the search |
| 53 | :return: return results |
| 54 | """ |
| 55 | result = {} |
| 56 | for key in os.environ: |
| 57 | if reg_ex in key: |
| 58 | result[key] = os.environ[key] |
| 59 | return result |
| 60 | |
| 61 | |
| 62 | def parse_uuid(data): |
| 63 | """ |
| 64 | Parse an UUID value from a string given. |
| 65 | :param data: String to be evaluated |
| 66 | :return: the uuid value |
| 67 | """ |
| 68 | match = re.compile(r'[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}', re.I).findall(data) |
| 69 | if match: |
| 70 | data = match[0].replace(' ', '') |
| 71 | data = data.replace('\n', '') |
| 72 | return data |
| 73 | else: |
| 74 | return [] |
| 75 | |
| 76 | |
| 77 | def get_config(data): |
| 78 | """ |
| 79 | Parse test config file |
| 80 | :param data: config file path |
| 81 | :return: config dict |
| 82 | """ |
| 83 | with open(data, 'r') as stream: |
| 84 | try: |
| 85 | return yaml.load(stream) |
| 86 | except yaml.YAMLError as exc: |
| 87 | print(exc) |
| 88 | |
| 89 | |
| 90 | def get_vm_ip(vm_id): |
| 91 | """ |
| 92 | Parse vm id IP from openvim client. |
| 93 | :param vm_id: vm id |
| 94 | :return: IP value |
| 95 | """ |
| 96 | openvim_path = os.path.join(os.environ['OPENVIM_ROOT_FOLDER'], 'openvim') |
| 97 | ip = execute_local("{} vm-list {} -vvv | grep ip_address:".format(openvim_path, vm_id)) |
| 98 | ip = ip.replace('ip_address:', '') |
| 99 | ip = ip.replace(' ', '') |
| 100 | return ip |
| 101 | |
| 102 | |
| 103 | def get_net_status(net_id): |
| 104 | """ |
| 105 | Parse a net status from openvim client |
| 106 | :param net_id: network id |
| 107 | :return: |
| 108 | """ |
| 109 | openvim_path = os.path.join(os.environ['OPENVIM_ROOT_FOLDER'], 'openvim') |
| 110 | net_status = execute_local("{} net-list {} -vvv | grep status:".format(openvim_path, net_id)) |
| 111 | net_status = net_status.replace('status:', '') |
| 112 | net_status = net_status.replace(' ', '') |
| 113 | return net_status |