blob: 6f0f74464a5863ea9708920d957a68f861d129b4 [file] [log] [blame]
gstepanove3481cd2015-04-03 21:45:52 +03001import os
koder aka kdanilovcee43342015-04-14 22:52:53 +03002import sys
3import logging
4import argparse
5import tempfile
gstepanove3481cd2015-04-03 21:45:52 +03006import paramiko
7
koder aka kdanilovcee43342015-04-14 22:52:53 +03008import fuel_rest_api
gstepanovf1ff52d2015-04-07 15:54:33 +03009from nodes.node import Node
10from utils import parse_creds
koder aka kdanilovcee43342015-04-14 22:52:53 +030011from urlparse import urlparse
12
gstepanovf1ff52d2015-04-07 15:54:33 +030013
14tmp_file = tempfile.NamedTemporaryFile().name
gstepanov82489e72015-04-10 16:18:03 +030015openrc_path = tempfile.NamedTemporaryFile().name
16logger = logging.getLogger("io-perf-tool")
gstepanove3481cd2015-04-03 21:45:52 +030017
18
gstepanov82489e72015-04-10 16:18:03 +030019def discover_fuel_nodes(fuel_url, creds, cluster_name):
gstepanovf1ff52d2015-04-07 15:54:33 +030020 username, tenant_name, password = parse_creds(creds)
21 creds = {"username": username,
22 "tenant_name": tenant_name,
23 "password": password}
24
koder aka kdanilovcee43342015-04-14 22:52:53 +030025 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]
gstepanove3481cd2015-04-03 21:45:52 +030031 roles = [node["roles"] for node in nodes]
32
33 host = urlparse(fuel_url).hostname
34
gstepanovf1ff52d2015-04-07 15:54:33 +030035 nodes, to_clean = run_agent(ips, roles, host, tmp_file)
36 nodes = [Node(node[0], node[1]) for node in nodes]
37
koder aka kdanilovcee43342015-04-14 22:52:53 +030038 openrc_dict = cluster.get_openrc()
gstepanov82489e72015-04-10 16:18:03 +030039
koder aka kdanilovcee43342015-04-14 22:52:53 +030040 logger.debug("Found %s fuel nodes for env %r" % (len(nodes), cluster_name))
gstepanov82489e72015-04-10 16:18:03 +030041 return nodes, to_clean, openrc_dict
gstepanove3481cd2015-04-03 21:45:52 +030042
43
44def 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
gstepanov4861d712015-04-09 13:28:02 +030051 command = "python /tmp/agent.py --clean=True --ext_ip=" + \
52 admin_ip + " --base_port=" \
gstepanove3481cd2015-04-03 21:45:52 +030053 + 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
gstepanove3481cd2015-04-03 21:45:52 +030063
gstepanovf1ff52d2015-04-07 15:54:33 +030064def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22,
gstepanove3481cd2015-04-03 21:45:52 +030065 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")
gstepanovf1ff52d2015-04-07 15:54:33 +030072 fuel_id_rsa_path = tmp_name
gstepanove3481cd2015-04-03 21:45:52 +030073 sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path)
gstepanovf1ff52d2015-04-07 15:54:33 +030074 os.chmod(fuel_id_rsa_path, 0o700)
gstepanov4861d712015-04-09 13:28:02 +030075 command = "python /tmp/agent.py --base_port=" + \
76 str(base_port) + " --ext_ip=" \
gstepanovf1ff52d2015-04-07 15:54:33 +030077 + host + " --ports"
gstepanove3481cd2015-04-03 21:45:52 +030078
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 = []
gstepanovf1ff52d2015-04-07 15:54:33 +030095 nodes_to_clean = []
gstepanove3481cd2015-04-03 21:45:52 +030096
97 for i in range(len(ip_addresses)):
98 ip = ip_addresses[i]
99 role = roles[i]
100 port = node_port_mapping[ip]
101
gstepanov4861d712015-04-09 13:28:02 +0300102 nodes_to_clean.append(("ssh://root@" + ip + ":" +
103 port.rstrip('\n')
104 + ":" + fuel_id_rsa_path, role))
gstepanovf1ff52d2015-04-07 15:54:33 +0300105
106 nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n')
107 + ":" + fuel_id_rsa_path, role))
gstepanove3481cd2015-04-03 21:45:52 +0300108
109 ssh.close()
koder aka kdanilovcee43342015-04-14 22:52:53 +0300110 logger.info('Files has been transferred successfully to Fuel node, ' +
gstepanov82489e72015-04-10 16:18:03 +0300111 'agent has been launched')
gstepanove3481cd2015-04-03 21:45:52 +0300112
gstepanovf1ff52d2015-04-07 15:54:33 +0300113 return nodes, nodes_to_clean
gstepanove3481cd2015-04-03 21:45:52 +0300114
115
116def parse_command_line(argv):
117 parser = argparse.ArgumentParser(
118 description="Connect to fuel master and setup ssh agent")
gstepanovf1ff52d2015-04-07 15:54:33 +0300119 parser.add_argument(
120 "--fuel_url", required=True)
121 parser.add_argument(
gstepanov82489e72015-04-10 16:18:03 +0300122 "--cluster_name", required=True)
gstepanovf1ff52d2015-04-07 15:54:33 +0300123 parser.add_argument(
124 "--iface", default="eth1")
125 parser.add_argument(
gstepanov82489e72015-04-10 16:18:03 +0300126 "--creds", default="admin:admin@admin")
gstepanovf1ff52d2015-04-07 15:54:33 +0300127
gstepanove3481cd2015-04-03 21:45:52 +0300128 return parser.parse_args(argv)
129
130
131def main(argv):
132 args = parse_command_line(argv)
gstepanove3481cd2015-04-03 21:45:52 +0300133
gstepanov1fbb71d2015-04-14 19:56:23 +0300134 nodes, to_clean, _ = discover_fuel_nodes(args.fuel_url,
koder aka kdanilovcee43342015-04-14 22:52:53 +0300135 args.creds,
136 args.cluster_name)
gstepanove3481cd2015-04-03 21:45:52 +0300137 discover_fuel_nodes_clean(args.fuel_url, {"username": "root",
138 "password": "test37",
gstepanovf1ff52d2015-04-07 15:54:33 +0300139 "port": 22}, to_clean)
gstepanove3481cd2015-04-03 21:45:52 +0300140
141
142if __name__ == "__main__":
143 main(sys.argv[1:])