blob: 3cf40cc5ae35d822f699e0134069f158a1cf01a1 [file] [log] [blame]
gstepanove3481cd2015-04-03 21:45:52 +03001import argparse
gstepanov82489e72015-04-10 16:18:03 +03002import logging
gstepanove3481cd2015-04-03 21:45:52 +03003import sys
gstepanovf1ff52d2015-04-07 15:54:33 +03004import tempfile
gstepanove3481cd2015-04-03 21:45:52 +03005import os
6import paramiko
7
8from urlparse import urlparse
gstepanovf1ff52d2015-04-07 15:54:33 +03009from nodes.node import Node
gstepanov82489e72015-04-10 16:18:03 +030010from ssh_utils import ssh_connect, ssh_copy_file, connect
gstepanovf1ff52d2015-04-07 15:54:33 +030011from utils import parse_creds
gstepanov82489e72015-04-10 16:18:03 +030012from fuel_rest_api import KeystoneAuth
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 get_cluster_id(cluster_name, conn):
20 clusters = conn.do("get", path="/api/clusters")
21 for cluster in clusters:
22 if cluster['name'] == cluster_name:
23 return cluster['id']
24
25
26def get_openrc_data(file_name):
27 openrc_dict = {}
28
29 with open(file_name) as f:
30 for line in f.readlines():
31 if len(line.split(" ")) > 1:
32 line = line.split(' ')[1]
33 key, value = line.split('=')
34
35 if key in ['OS_AUTH_URL', 'OS_PASSWORD',
36 'OS_TENANT_NAME', 'OS_USERNAME']:
37 openrc_dict[key] = value[1: len(value) - 2]
38
39 return openrc_dict
40
41
42def get_openrc(nodes):
43 controller = None
44
45 for node in nodes:
46 if 'controller' in node.roles:
47 controller = node
48 break
49
50 url = controller.conn_url[6:]
51 ssh = connect(url)
52 sftp = ssh.open_sftp()
53 sftp.get('/root/openrc', openrc_path)
54 sftp.close()
55
56 return get_openrc_data(openrc_path)
57
58
59def discover_fuel_nodes(fuel_url, creds, cluster_name):
gstepanovf1ff52d2015-04-07 15:54:33 +030060 username, tenant_name, password = parse_creds(creds)
61 creds = {"username": username,
62 "tenant_name": tenant_name,
63 "password": password}
64
gstepanov82489e72015-04-10 16:18:03 +030065 fuel = KeystoneAuth(fuel_url, creds, headers=None, echo=None,)
66 cluster_id = get_cluster_id(cluster_name, fuel)
gstepanove3481cd2015-04-03 21:45:52 +030067 nodes = fuel.do("get", path="/api/nodes?cluster_id=" + str(cluster_id))
68 ips = [node["ip"] for node in nodes]
69 roles = [node["roles"] for node in nodes]
70
71 host = urlparse(fuel_url).hostname
72
gstepanovf1ff52d2015-04-07 15:54:33 +030073 nodes, to_clean = run_agent(ips, roles, host, tmp_file)
74 nodes = [Node(node[0], node[1]) for node in nodes]
75
gstepanov82489e72015-04-10 16:18:03 +030076 openrc_dict = get_openrc(nodes)
77
78 return nodes, to_clean, openrc_dict
gstepanove3481cd2015-04-03 21:45:52 +030079
80
81def discover_fuel_nodes_clean(fuel_url, ssh_creds, nodes, base_port=12345):
82 admin_ip = urlparse(fuel_url).hostname
83 ssh = paramiko.SSHClient()
84 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
85 ssh.connect(hostname=admin_ip, port=ssh_creds["port"],
86 password=ssh_creds["password"], username=ssh_creds["username"])
87
gstepanov4861d712015-04-09 13:28:02 +030088 command = "python /tmp/agent.py --clean=True --ext_ip=" + \
89 admin_ip + " --base_port=" \
gstepanove3481cd2015-04-03 21:45:52 +030090 + str(base_port) + " --ports"
91
92 for node in nodes:
93 ip = urlparse(node[0]).hostname
94 command += " " + ip
95
96 (stdin, stdout, stderr) = ssh.exec_command(command)
97 for line in stdout.readlines():
98 print line
99
gstepanove3481cd2015-04-03 21:45:52 +0300100
gstepanovf1ff52d2015-04-07 15:54:33 +0300101def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22,
gstepanove3481cd2015-04-03 21:45:52 +0300102 base_port=12345):
103 ssh = paramiko.SSHClient()
104 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
105 ssh.connect(hostname=host, port=port, password=password, username="root")
106 sftp = ssh.open_sftp()
107 sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'),
108 "/tmp/agent.py")
gstepanovf1ff52d2015-04-07 15:54:33 +0300109 fuel_id_rsa_path = tmp_name
gstepanove3481cd2015-04-03 21:45:52 +0300110 sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path)
gstepanovf1ff52d2015-04-07 15:54:33 +0300111 os.chmod(fuel_id_rsa_path, 0o700)
gstepanov4861d712015-04-09 13:28:02 +0300112 command = "python /tmp/agent.py --base_port=" + \
113 str(base_port) + " --ext_ip=" \
gstepanovf1ff52d2015-04-07 15:54:33 +0300114 + host + " --ports"
gstepanove3481cd2015-04-03 21:45:52 +0300115
116 for address in ip_addresses:
117 command += " " + address
118
119 (stdin, stdout, stderr) = ssh.exec_command(command)
120 node_port_mapping = {}
121
122 for line in stdout.readlines():
123 results = line.split(' ')
124
125 if len(results) != 2:
126 continue
127
128 node, port = results
129 node_port_mapping[node] = port
130
131 nodes = []
gstepanovf1ff52d2015-04-07 15:54:33 +0300132 nodes_to_clean = []
gstepanove3481cd2015-04-03 21:45:52 +0300133
134 for i in range(len(ip_addresses)):
135 ip = ip_addresses[i]
136 role = roles[i]
137 port = node_port_mapping[ip]
138
gstepanov4861d712015-04-09 13:28:02 +0300139 nodes_to_clean.append(("ssh://root@" + ip + ":" +
140 port.rstrip('\n')
141 + ":" + fuel_id_rsa_path, role))
gstepanovf1ff52d2015-04-07 15:54:33 +0300142
143 nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n')
144 + ":" + fuel_id_rsa_path, role))
gstepanove3481cd2015-04-03 21:45:52 +0300145
146 ssh.close()
gstepanov82489e72015-04-10 16:18:03 +0300147 logger.info('Files has been transferred successfully to Fuel node, ' \
148 'agent has been launched')
149 logger.info("Nodes : " + str(nodes))
gstepanove3481cd2015-04-03 21:45:52 +0300150
gstepanovf1ff52d2015-04-07 15:54:33 +0300151 return nodes, nodes_to_clean
gstepanove3481cd2015-04-03 21:45:52 +0300152
153
154def parse_command_line(argv):
155 parser = argparse.ArgumentParser(
156 description="Connect to fuel master and setup ssh agent")
gstepanovf1ff52d2015-04-07 15:54:33 +0300157 parser.add_argument(
158 "--fuel_url", required=True)
159 parser.add_argument(
gstepanov82489e72015-04-10 16:18:03 +0300160 "--cluster_name", required=True)
gstepanovf1ff52d2015-04-07 15:54:33 +0300161 parser.add_argument(
162 "--iface", default="eth1")
163 parser.add_argument(
gstepanov82489e72015-04-10 16:18:03 +0300164 "--creds", default="admin:admin@admin")
gstepanovf1ff52d2015-04-07 15:54:33 +0300165
gstepanove3481cd2015-04-03 21:45:52 +0300166 return parser.parse_args(argv)
167
168
169def main(argv):
170 args = parse_command_line(argv)
gstepanove3481cd2015-04-03 21:45:52 +0300171
gstepanov1fbb71d2015-04-14 19:56:23 +0300172 nodes, to_clean, _ = discover_fuel_nodes(args.fuel_url,
gstepanov82489e72015-04-10 16:18:03 +0300173 args.creds, args.cluster_name)
gstepanove3481cd2015-04-03 21:45:52 +0300174 discover_fuel_nodes_clean(args.fuel_url, {"username": "root",
175 "password": "test37",
gstepanovf1ff52d2015-04-07 15:54:33 +0300176 "port": 22}, to_clean)
gstepanove3481cd2015-04-03 21:45:52 +0300177
178
179if __name__ == "__main__":
180 main(sys.argv[1:])