gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 1 | import os |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 2 | import sys |
| 3 | import logging |
| 4 | import argparse |
| 5 | import tempfile |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 6 | import paramiko |
| 7 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 8 | import fuel_rest_api |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 9 | from nodes.node import Node |
| 10 | from utils import parse_creds |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 11 | from urlparse import urlparse |
| 12 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 13 | |
| 14 | tmp_file = tempfile.NamedTemporaryFile().name |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 15 | openrc_path = tempfile.NamedTemporaryFile().name |
| 16 | logger = logging.getLogger("io-perf-tool") |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 17 | |
| 18 | |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 19 | def discover_fuel_nodes(fuel_url, creds, cluster_name): |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 20 | username, tenant_name, password = parse_creds(creds) |
| 21 | creds = {"username": username, |
| 22 | "tenant_name": tenant_name, |
| 23 | "password": password} |
| 24 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 25 | conn = fuel_rest_api.KeystoneAuth(fuel_url, creds, headers=None) |
| 26 | cluster_id = fuel_rest_api.get_cluster_id(conn, cluster_name) |
| 27 | cluster = fuel_rest_api.reflect_cluster(conn, cluster_id) |
| 28 | |
| 29 | nodes = list(cluster.get_nodes()) |
| 30 | ips = [node.get_ip('admin') for node in nodes] |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 31 | roles = [node["roles"] for node in nodes] |
| 32 | |
| 33 | host = urlparse(fuel_url).hostname |
| 34 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 35 | nodes, to_clean = run_agent(ips, roles, host, tmp_file) |
| 36 | nodes = [Node(node[0], node[1]) for node in nodes] |
| 37 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 38 | openrc_dict = cluster.get_openrc() |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 39 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 40 | logger.debug("Found %s fuel nodes for env %r" % (len(nodes), cluster_name)) |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 41 | return nodes, to_clean, openrc_dict |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def discover_fuel_nodes_clean(fuel_url, ssh_creds, nodes, base_port=12345): |
| 45 | admin_ip = urlparse(fuel_url).hostname |
| 46 | ssh = paramiko.SSHClient() |
| 47 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 48 | ssh.connect(hostname=admin_ip, port=ssh_creds["port"], |
| 49 | password=ssh_creds["password"], username=ssh_creds["username"]) |
| 50 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 51 | command = "python /tmp/agent.py --clean=True --ext_ip=" + \ |
| 52 | admin_ip + " --base_port=" \ |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 53 | + str(base_port) + " --ports" |
| 54 | |
| 55 | for node in nodes: |
| 56 | ip = urlparse(node[0]).hostname |
| 57 | command += " " + ip |
| 58 | |
| 59 | (stdin, stdout, stderr) = ssh.exec_command(command) |
| 60 | for line in stdout.readlines(): |
| 61 | print line |
| 62 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 63 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 64 | def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22, |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 65 | base_port=12345): |
| 66 | ssh = paramiko.SSHClient() |
| 67 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 68 | ssh.connect(hostname=host, port=port, password=password, username="root") |
| 69 | sftp = ssh.open_sftp() |
| 70 | sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'), |
| 71 | "/tmp/agent.py") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 72 | fuel_id_rsa_path = tmp_name |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 73 | sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 74 | os.chmod(fuel_id_rsa_path, 0o700) |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 75 | command = "python /tmp/agent.py --base_port=" + \ |
| 76 | str(base_port) + " --ext_ip=" \ |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 77 | + host + " --ports" |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 78 | |
| 79 | for address in ip_addresses: |
| 80 | command += " " + address |
| 81 | |
| 82 | (stdin, stdout, stderr) = ssh.exec_command(command) |
| 83 | node_port_mapping = {} |
| 84 | |
| 85 | for line in stdout.readlines(): |
| 86 | results = line.split(' ') |
| 87 | |
| 88 | if len(results) != 2: |
| 89 | continue |
| 90 | |
| 91 | node, port = results |
| 92 | node_port_mapping[node] = port |
| 93 | |
| 94 | nodes = [] |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 95 | nodes_to_clean = [] |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 96 | |
| 97 | for i in range(len(ip_addresses)): |
| 98 | ip = ip_addresses[i] |
| 99 | role = roles[i] |
| 100 | port = node_port_mapping[ip] |
| 101 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame] | 102 | nodes_to_clean.append(("ssh://root@" + ip + ":" + |
| 103 | port.rstrip('\n') |
| 104 | + ":" + fuel_id_rsa_path, role)) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 105 | |
| 106 | nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n') |
| 107 | + ":" + fuel_id_rsa_path, role)) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 108 | |
| 109 | ssh.close() |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 110 | logger.info('Files has been transferred successfully to Fuel node, ' + |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 111 | 'agent has been launched') |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 112 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 113 | return nodes, nodes_to_clean |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def parse_command_line(argv): |
| 117 | parser = argparse.ArgumentParser( |
| 118 | description="Connect to fuel master and setup ssh agent") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 119 | parser.add_argument( |
| 120 | "--fuel_url", required=True) |
| 121 | parser.add_argument( |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 122 | "--cluster_name", required=True) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 123 | parser.add_argument( |
| 124 | "--iface", default="eth1") |
| 125 | parser.add_argument( |
gstepanov | 82489e7 | 2015-04-10 16:18:03 +0300 | [diff] [blame] | 126 | "--creds", default="admin:admin@admin") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 127 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 128 | return parser.parse_args(argv) |
| 129 | |
| 130 | |
| 131 | def main(argv): |
| 132 | args = parse_command_line(argv) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 133 | |
gstepanov | 1fbb71d | 2015-04-14 19:56:23 +0300 | [diff] [blame] | 134 | nodes, to_clean, _ = discover_fuel_nodes(args.fuel_url, |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 135 | args.creds, |
| 136 | args.cluster_name) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 137 | discover_fuel_nodes_clean(args.fuel_url, {"username": "root", |
| 138 | "password": "test37", |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 139 | "port": 22}, to_clean) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | main(sys.argv[1:]) |