blob: 28a4a40274f71090e5c5ecb5e3ea8d8f72d45b47 [file] [log] [blame]
gstepanove3481cd2015-04-03 21:45:52 +03001import argparse
gstepanov82489e72015-04-10 16:18:03 +03002import logging
3import re
4import string
gstepanove3481cd2015-04-03 21:45:52 +03005import sys
gstepanovf1ff52d2015-04-07 15:54:33 +03006import tempfile
gstepanove3481cd2015-04-03 21:45:52 +03007import os
8import paramiko
9
10from urlparse import urlparse
gstepanovf1ff52d2015-04-07 15:54:33 +030011from nodes.node import Node
gstepanov82489e72015-04-10 16:18:03 +030012from ssh_utils import ssh_connect, ssh_copy_file, connect
gstepanovf1ff52d2015-04-07 15:54:33 +030013from utils import parse_creds
gstepanov82489e72015-04-10 16:18:03 +030014from fuel_rest_api import KeystoneAuth
gstepanovf1ff52d2015-04-07 15:54:33 +030015
16tmp_file = tempfile.NamedTemporaryFile().name
gstepanov82489e72015-04-10 16:18:03 +030017openrc_path = tempfile.NamedTemporaryFile().name
18logger = logging.getLogger("io-perf-tool")
gstepanove3481cd2015-04-03 21:45:52 +030019
20
gstepanov82489e72015-04-10 16:18:03 +030021def 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
28def 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
44def 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
61def discover_fuel_nodes(fuel_url, creds, cluster_name):
gstepanovf1ff52d2015-04-07 15:54:33 +030062 username, tenant_name, password = parse_creds(creds)
63 creds = {"username": username,
64 "tenant_name": tenant_name,
65 "password": password}
66
gstepanov82489e72015-04-10 16:18:03 +030067 fuel = KeystoneAuth(fuel_url, creds, headers=None, echo=None,)
68 cluster_id = get_cluster_id(cluster_name, fuel)
gstepanove3481cd2015-04-03 21:45:52 +030069 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
gstepanovf1ff52d2015-04-07 15:54:33 +030075 nodes, to_clean = run_agent(ips, roles, host, tmp_file)
76 nodes = [Node(node[0], node[1]) for node in nodes]
77
gstepanov82489e72015-04-10 16:18:03 +030078 openrc_dict = get_openrc(nodes)
79
80 return nodes, to_clean, openrc_dict
gstepanove3481cd2015-04-03 21:45:52 +030081
82
83def 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
gstepanov4861d712015-04-09 13:28:02 +030090 command = "python /tmp/agent.py --clean=True --ext_ip=" + \
91 admin_ip + " --base_port=" \
gstepanove3481cd2015-04-03 21:45:52 +030092 + 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
gstepanove3481cd2015-04-03 21:45:52 +0300102
gstepanovf1ff52d2015-04-07 15:54:33 +0300103def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22,
gstepanove3481cd2015-04-03 21:45:52 +0300104 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")
gstepanovf1ff52d2015-04-07 15:54:33 +0300111 fuel_id_rsa_path = tmp_name
gstepanove3481cd2015-04-03 21:45:52 +0300112 sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path)
gstepanovf1ff52d2015-04-07 15:54:33 +0300113 os.chmod(fuel_id_rsa_path, 0o700)
gstepanov4861d712015-04-09 13:28:02 +0300114 command = "python /tmp/agent.py --base_port=" + \
115 str(base_port) + " --ext_ip=" \
gstepanovf1ff52d2015-04-07 15:54:33 +0300116 + host + " --ports"
gstepanove3481cd2015-04-03 21:45:52 +0300117
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 = []
gstepanovf1ff52d2015-04-07 15:54:33 +0300134 nodes_to_clean = []
gstepanove3481cd2015-04-03 21:45:52 +0300135
136 for i in range(len(ip_addresses)):
137 ip = ip_addresses[i]
138 role = roles[i]
139 port = node_port_mapping[ip]
140
gstepanov4861d712015-04-09 13:28:02 +0300141 nodes_to_clean.append(("ssh://root@" + ip + ":" +
142 port.rstrip('\n')
143 + ":" + fuel_id_rsa_path, role))
gstepanovf1ff52d2015-04-07 15:54:33 +0300144
145 nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n')
146 + ":" + fuel_id_rsa_path, role))
gstepanove3481cd2015-04-03 21:45:52 +0300147
148 ssh.close()
gstepanov82489e72015-04-10 16:18:03 +0300149 logger.info('Files has been transferred successfully to Fuel node, ' \
150 'agent has been launched')
151 logger.info("Nodes : " + str(nodes))
gstepanove3481cd2015-04-03 21:45:52 +0300152
gstepanovf1ff52d2015-04-07 15:54:33 +0300153 return nodes, nodes_to_clean
gstepanove3481cd2015-04-03 21:45:52 +0300154
155
156def parse_command_line(argv):
157 parser = argparse.ArgumentParser(
158 description="Connect to fuel master and setup ssh agent")
gstepanovf1ff52d2015-04-07 15:54:33 +0300159 parser.add_argument(
160 "--fuel_url", required=True)
161 parser.add_argument(
gstepanov82489e72015-04-10 16:18:03 +0300162 "--cluster_name", required=True)
gstepanovf1ff52d2015-04-07 15:54:33 +0300163 parser.add_argument(
164 "--iface", default="eth1")
165 parser.add_argument(
gstepanov82489e72015-04-10 16:18:03 +0300166 "--creds", default="admin:admin@admin")
gstepanovf1ff52d2015-04-07 15:54:33 +0300167
gstepanove3481cd2015-04-03 21:45:52 +0300168 return parser.parse_args(argv)
169
170
171def main(argv):
172 args = parse_command_line(argv)
gstepanove3481cd2015-04-03 21:45:52 +0300173
gstepanov4861d712015-04-09 13:28:02 +0300174 nodes, to_clean = discover_fuel_nodes(args.fuel_url,
gstepanov82489e72015-04-10 16:18:03 +0300175 args.creds, args.cluster_name)
gstepanove3481cd2015-04-03 21:45:52 +0300176 discover_fuel_nodes_clean(args.fuel_url, {"username": "root",
177 "password": "test37",
gstepanovf1ff52d2015-04-07 15:54:33 +0300178 "port": 22}, to_clean)
gstepanove3481cd2015-04-03 21:45:52 +0300179
180
181if __name__ == "__main__":
182 main(sys.argv[1:])