gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 1 | import argparse |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 2 | import logging |
| 3 | import re |
| 4 | import string |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 5 | import sys |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 6 | import tempfile |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 7 | import os |
| 8 | import paramiko |
| 9 | |
| 10 | from urlparse import urlparse |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 11 | from nodes.node import Node |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 12 | from ssh_utils import ssh_connect, ssh_copy_file, connect |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 13 | from utils import parse_creds |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 14 | from fuel_rest_api import KeystoneAuth |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 15 | |
| 16 | tmp_file = tempfile.NamedTemporaryFile().name |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 17 | openrc_path = tempfile.NamedTemporaryFile().name |
| 18 | logger = logging.getLogger("io-perf-tool") |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 19 | |
| 20 | |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 21 | def get_cluster_id(cluster_name, conn): |
| 22 | clusters = conn.do("get", path="/api/clusters") |
| 23 | for cluster in clusters: |
| 24 | if cluster['name'] == cluster_name: |
| 25 | return cluster['id'] |
| 26 | |
| 27 | |
| 28 | def get_openrc_data(file_name): |
| 29 | openrc_dict = {} |
| 30 | |
| 31 | with open(file_name) as f: |
| 32 | for line in f.readlines(): |
| 33 | if len(line.split(" ")) > 1: |
| 34 | line = line.split(' ')[1] |
| 35 | key, value = line.split('=') |
| 36 | |
| 37 | if key in ['OS_AUTH_URL', 'OS_PASSWORD', |
| 38 | 'OS_TENANT_NAME', 'OS_USERNAME']: |
| 39 | openrc_dict[key] = value[1: len(value) - 2] |
| 40 | |
| 41 | return openrc_dict |
| 42 | |
| 43 | |
| 44 | def get_openrc(nodes): |
| 45 | controller = None |
| 46 | |
| 47 | for node in nodes: |
| 48 | if 'controller' in node.roles: |
| 49 | controller = node |
| 50 | break |
| 51 | |
| 52 | url = controller.conn_url[6:] |
| 53 | ssh = connect(url) |
| 54 | sftp = ssh.open_sftp() |
| 55 | sftp.get('/root/openrc', openrc_path) |
| 56 | sftp.close() |
| 57 | |
| 58 | return get_openrc_data(openrc_path) |
| 59 | |
| 60 | |
| 61 | def discover_fuel_nodes(fuel_url, creds, cluster_name): |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 62 | username, tenant_name, password = parse_creds(creds) |
| 63 | creds = {"username": username, |
| 64 | "tenant_name": tenant_name, |
| 65 | "password": password} |
| 66 | |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 67 | fuel = KeystoneAuth(fuel_url, creds, headers=None, echo=None,) |
| 68 | cluster_id = get_cluster_id(cluster_name, fuel) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 69 | nodes = fuel.do("get", path="/api/nodes?cluster_id=" + str(cluster_id)) |
| 70 | ips = [node["ip"] for node in nodes] |
| 71 | roles = [node["roles"] for node in nodes] |
| 72 | |
| 73 | host = urlparse(fuel_url).hostname |
| 74 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 75 | nodes, to_clean = run_agent(ips, roles, host, tmp_file) |
| 76 | nodes = [Node(node[0], node[1]) for node in nodes] |
| 77 | |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 78 | openrc_dict = get_openrc(nodes) |
| 79 | |
| 80 | return nodes, to_clean, openrc_dict |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def discover_fuel_nodes_clean(fuel_url, ssh_creds, nodes, base_port=12345): |
| 84 | admin_ip = urlparse(fuel_url).hostname |
| 85 | ssh = paramiko.SSHClient() |
| 86 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 87 | ssh.connect(hostname=admin_ip, port=ssh_creds["port"], |
| 88 | password=ssh_creds["password"], username=ssh_creds["username"]) |
| 89 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 90 | command = "python /tmp/agent.py --clean=True --ext_ip=" + \ |
| 91 | admin_ip + " --base_port=" \ |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 92 | + str(base_port) + " --ports" |
| 93 | |
| 94 | for node in nodes: |
| 95 | ip = urlparse(node[0]).hostname |
| 96 | command += " " + ip |
| 97 | |
| 98 | (stdin, stdout, stderr) = ssh.exec_command(command) |
| 99 | for line in stdout.readlines(): |
| 100 | print line |
| 101 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 102 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 103 | def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22, |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 104 | base_port=12345): |
| 105 | ssh = paramiko.SSHClient() |
| 106 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 107 | ssh.connect(hostname=host, port=port, password=password, username="root") |
| 108 | sftp = ssh.open_sftp() |
| 109 | sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'), |
| 110 | "/tmp/agent.py") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 111 | fuel_id_rsa_path = tmp_name |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 112 | sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 113 | os.chmod(fuel_id_rsa_path, 0o700) |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 114 | command = "python /tmp/agent.py --base_port=" + \ |
| 115 | str(base_port) + " --ext_ip=" \ |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 116 | + host + " --ports" |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 117 | |
| 118 | for address in ip_addresses: |
| 119 | command += " " + address |
| 120 | |
| 121 | (stdin, stdout, stderr) = ssh.exec_command(command) |
| 122 | node_port_mapping = {} |
| 123 | |
| 124 | for line in stdout.readlines(): |
| 125 | results = line.split(' ') |
| 126 | |
| 127 | if len(results) != 2: |
| 128 | continue |
| 129 | |
| 130 | node, port = results |
| 131 | node_port_mapping[node] = port |
| 132 | |
| 133 | nodes = [] |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 134 | nodes_to_clean = [] |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 135 | |
| 136 | for i in range(len(ip_addresses)): |
| 137 | ip = ip_addresses[i] |
| 138 | role = roles[i] |
| 139 | port = node_port_mapping[ip] |
| 140 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 141 | nodes_to_clean.append(("ssh://root@" + ip + ":" + |
| 142 | port.rstrip('\n') |
| 143 | + ":" + fuel_id_rsa_path, role)) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 144 | |
| 145 | nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n') |
| 146 | + ":" + fuel_id_rsa_path, role)) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 147 | |
| 148 | ssh.close() |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 149 | logger.info('Files has been transferred successfully to Fuel node, ' \ |
| 150 | 'agent has been launched') |
| 151 | logger.info("Nodes : " + str(nodes)) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 152 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 153 | return nodes, nodes_to_clean |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def parse_command_line(argv): |
| 157 | parser = argparse.ArgumentParser( |
| 158 | description="Connect to fuel master and setup ssh agent") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 159 | parser.add_argument( |
| 160 | "--fuel_url", required=True) |
| 161 | parser.add_argument( |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 162 | "--cluster_name", required=True) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 163 | parser.add_argument( |
| 164 | "--iface", default="eth1") |
| 165 | parser.add_argument( |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 166 | "--creds", default="admin:admin@admin") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 167 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 168 | return parser.parse_args(argv) |
| 169 | |
| 170 | |
| 171 | def main(argv): |
| 172 | args = parse_command_line(argv) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 173 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 174 | nodes, to_clean = discover_fuel_nodes(args.fuel_url, |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 175 | args.creds, args.cluster_name) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 176 | discover_fuel_nodes_clean(args.fuel_url, {"username": "root", |
| 177 | "password": "test37", |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 178 | "port": 22}, to_clean) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 179 | |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | main(sys.argv[1:]) |