koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 1 | import re |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 2 | import socket |
| 3 | import logging |
| 4 | from urlparse import urlparse |
| 5 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 6 | import sshtunnel |
| 7 | from paramiko import AuthenticationException |
| 8 | |
| 9 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 10 | from wally.fuel_rest_api import (KeystoneAuth, get_cluster_id, |
| 11 | reflect_cluster, FuelInfo) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 12 | from wally.utils import (parse_creds, check_input_param, StopTestError, |
| 13 | clean_resource, get_ip_for_target) |
| 14 | from wally.ssh_utils import (run_over_ssh, connect, set_key_for_node, |
| 15 | read_from_remote) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 16 | |
| 17 | from .node import Node |
| 18 | |
| 19 | |
| 20 | logger = logging.getLogger("wally.discover") |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 21 | BASE_PF_PORT = 44006 |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 22 | |
| 23 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 24 | def discover_fuel_nodes(fuel_data, var_dir, discover_nodes=True): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 25 | username, tenant_name, password = parse_creds(fuel_data['creds']) |
| 26 | creds = {"username": username, |
| 27 | "tenant_name": tenant_name, |
| 28 | "password": password} |
| 29 | |
| 30 | conn = KeystoneAuth(fuel_data['url'], creds, headers=None) |
| 31 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 32 | msg = "openstack_env should be provided in fuel config" |
| 33 | check_input_param('openstack_env' in fuel_data, msg) |
| 34 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 35 | cluster_id = get_cluster_id(conn, fuel_data['openstack_env']) |
| 36 | cluster = reflect_cluster(conn, cluster_id) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 37 | |
| 38 | if not discover_nodes: |
| 39 | logger.warning("Skip fuel cluster discovery") |
| 40 | return ([], None, cluster.get_openrc()) |
| 41 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 42 | version = FuelInfo(conn).get_version() |
| 43 | |
| 44 | fuel_nodes = list(cluster.get_nodes()) |
| 45 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 46 | logger.info("Found FUEL {0}".format(".".join(map(str, version)))) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 47 | |
| 48 | network = 'fuelweb_admin' if version >= [6, 0] else 'admin' |
| 49 | |
| 50 | ssh_creds = fuel_data['ssh_creds'] |
| 51 | |
| 52 | fuel_host = urlparse(fuel_data['url']).hostname |
| 53 | fuel_ip = socket.gethostbyname(fuel_host) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 54 | |
| 55 | try: |
| 56 | ssh_conn = connect("{0}@{1}".format(ssh_creds, fuel_host)) |
| 57 | except AuthenticationException: |
| 58 | raise StopTestError("Wrong fuel credentials") |
| 59 | except Exception: |
| 60 | logger.exception("While connection to FUEL") |
| 61 | raise StopTestError("Failed to connect to FUEL") |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 62 | |
| 63 | fuel_ext_iface = get_external_interface(ssh_conn, fuel_ip) |
| 64 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 65 | logger.debug("Downloading fuel master key") |
| 66 | fuel_key = download_master_key(ssh_conn) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 67 | |
| 68 | nodes = [] |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 69 | ips_ports = [] |
| 70 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame^] | 71 | logger.info("Forwarding ssh ports from FUEL nodes to localhost") |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 72 | fuel_usr, fuel_passwd = ssh_creds.split(":", 1) |
| 73 | ips = [str(fuel_node.get_ip(network)) for fuel_node in fuel_nodes] |
| 74 | port_fw = forward_ssh_ports(fuel_host, fuel_usr, fuel_passwd, ips) |
| 75 | listen_ip = get_ip_for_target(fuel_host) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 76 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 77 | for port, fuel_node, ip in zip(port_fw, fuel_nodes, ips): |
| 78 | logger.debug( |
| 79 | "SSH port forwarding {0} => localhost:{1}".format(ip, port)) |
| 80 | |
| 81 | conn_url = "ssh://root@127.0.0.1:{0}".format(port) |
| 82 | set_key_for_node(('127.0.0.1', port), fuel_key) |
| 83 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 84 | node = Node(conn_url, fuel_node['roles']) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 85 | node.monitor_ip = listen_ip |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 86 | nodes.append(node) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 87 | ips_ports.append((ip, port)) |
| 88 | |
| 89 | logger.debug("Found %s fuel nodes for env %r" % |
| 90 | (len(nodes), fuel_data['openstack_env'])) |
| 91 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 92 | return (nodes, |
| 93 | (ssh_conn, fuel_ext_iface, ips_ports), |
| 94 | cluster.get_openrc()) |
| 95 | |
| 96 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 97 | def download_master_key(conn): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 98 | # download master key |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 99 | with conn.open_sftp() as sftp: |
| 100 | return read_from_remote(sftp, '/root/.ssh/id_rsa') |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def get_external_interface(conn, ip): |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 104 | data = run_over_ssh(conn, "ip a", node='fuel-master', nolog=True) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 105 | curr_iface = None |
| 106 | for line in data.split("\n"): |
| 107 | |
| 108 | match1 = re.match(r"\d+:\s+(?P<name>.*?):\s\<", line) |
| 109 | if match1 is not None: |
| 110 | curr_iface = match1.group('name') |
| 111 | |
| 112 | match2 = re.match(r"\s+inet\s+(?P<ip>[0-9.]+)/", line) |
| 113 | if match2 is not None: |
| 114 | if match2.group('ip') == ip: |
| 115 | assert curr_iface is not None |
| 116 | return curr_iface |
| 117 | raise KeyError("Can't found interface for ip {0}".format(ip)) |
| 118 | |
| 119 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 120 | def forward_ssh_ports(proxy_ip, proxy_user, proxy_passwd, ips): |
| 121 | for ip in ips: |
| 122 | tunnel = sshtunnel.open( |
| 123 | (proxy_ip, 22), |
| 124 | ssh_username=proxy_user, |
| 125 | ssh_password=proxy_passwd, |
| 126 | threaded=True, |
| 127 | remote_bind_address=(ip, 22)) |
| 128 | tunnel.__enter__() |
| 129 | clean_resource(tunnel.__exit__, None, None, None) |
| 130 | yield tunnel.local_bind_port |