blob: 0bbf44704167cbab90de55a18e064d93a92e0872 [file] [log] [blame]
gstepanove3481cd2015-04-03 21:45:52 +03001import argparse
2import sys
gstepanovf1ff52d2015-04-07 15:54:33 +03003import tempfile
gstepanove3481cd2015-04-03 21:45:52 +03004import os
5import paramiko
6
7from urlparse import urlparse
gstepanovf1ff52d2015-04-07 15:54:33 +03008from nodes.node import Node
9from utils import parse_creds
10from keystone import KeystoneAuth
gstepanove3481cd2015-04-03 21:45:52 +030011
12
gstepanovf1ff52d2015-04-07 15:54:33 +030013from keystone import KeystoneAuth
14
15tmp_file = tempfile.NamedTemporaryFile().name
gstepanove3481cd2015-04-03 21:45:52 +030016
17
18def discover_fuel_nodes(fuel_url, creds, cluster_id):
gstepanovf1ff52d2015-04-07 15:54:33 +030019 username, tenant_name, password = parse_creds(creds)
20 creds = {"username": username,
21 "tenant_name": tenant_name,
22 "password": password}
23
gstepanove3481cd2015-04-03 21:45:52 +030024 admin_ip = urlparse(fuel_url).hostname
25 fuel = KeystoneAuth(fuel_url, creds, headers=None, echo=None,
26 admin_node_ip=admin_ip)
27 nodes = fuel.do("get", path="/api/nodes?cluster_id=" + str(cluster_id))
28 ips = [node["ip"] for node in nodes]
29 roles = [node["roles"] for node in nodes]
30
31 host = urlparse(fuel_url).hostname
32
gstepanovf1ff52d2015-04-07 15:54:33 +030033 nodes, to_clean = run_agent(ips, roles, host, tmp_file)
34 nodes = [Node(node[0], node[1]) for node in nodes]
35
36 return nodes, to_clean
gstepanove3481cd2015-04-03 21:45:52 +030037
38
39def discover_fuel_nodes_clean(fuel_url, ssh_creds, nodes, base_port=12345):
40 admin_ip = urlparse(fuel_url).hostname
41 ssh = paramiko.SSHClient()
42 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
43 ssh.connect(hostname=admin_ip, port=ssh_creds["port"],
44 password=ssh_creds["password"], username=ssh_creds["username"])
45
gstepanov4861d712015-04-09 13:28:02 +030046 command = "python /tmp/agent.py --clean=True --ext_ip=" + \
47 admin_ip + " --base_port=" \
gstepanove3481cd2015-04-03 21:45:52 +030048 + str(base_port) + " --ports"
49
50 for node in nodes:
51 ip = urlparse(node[0]).hostname
52 command += " " + ip
53
54 (stdin, stdout, stderr) = ssh.exec_command(command)
55 for line in stdout.readlines():
56 print line
57
gstepanove3481cd2015-04-03 21:45:52 +030058
gstepanovf1ff52d2015-04-07 15:54:33 +030059def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22,
gstepanove3481cd2015-04-03 21:45:52 +030060 base_port=12345):
61 ssh = paramiko.SSHClient()
62 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
63 ssh.connect(hostname=host, port=port, password=password, username="root")
64 sftp = ssh.open_sftp()
65 sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'),
66 "/tmp/agent.py")
gstepanovf1ff52d2015-04-07 15:54:33 +030067 fuel_id_rsa_path = tmp_name
gstepanove3481cd2015-04-03 21:45:52 +030068 sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path)
gstepanovf1ff52d2015-04-07 15:54:33 +030069 os.chmod(fuel_id_rsa_path, 0o700)
gstepanov4861d712015-04-09 13:28:02 +030070 command = "python /tmp/agent.py --base_port=" + \
71 str(base_port) + " --ext_ip=" \
gstepanovf1ff52d2015-04-07 15:54:33 +030072 + host + " --ports"
gstepanove3481cd2015-04-03 21:45:52 +030073
74 for address in ip_addresses:
75 command += " " + address
76
77 (stdin, stdout, stderr) = ssh.exec_command(command)
78 node_port_mapping = {}
79
80 for line in stdout.readlines():
81 results = line.split(' ')
82
83 if len(results) != 2:
84 continue
85
86 node, port = results
87 node_port_mapping[node] = port
88
89 nodes = []
gstepanovf1ff52d2015-04-07 15:54:33 +030090 nodes_to_clean = []
gstepanove3481cd2015-04-03 21:45:52 +030091
92 for i in range(len(ip_addresses)):
93 ip = ip_addresses[i]
94 role = roles[i]
95 port = node_port_mapping[ip]
96
gstepanov4861d712015-04-09 13:28:02 +030097 nodes_to_clean.append(("ssh://root@" + ip + ":" +
98 port.rstrip('\n')
99 + ":" + fuel_id_rsa_path, role))
gstepanovf1ff52d2015-04-07 15:54:33 +0300100
101 nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n')
102 + ":" + fuel_id_rsa_path, role))
gstepanove3481cd2015-04-03 21:45:52 +0300103
104 ssh.close()
105 print 'Files has been transfered successefully to Fuel node, ' \
106 'agent has been launched'
107 print nodes
108
gstepanovf1ff52d2015-04-07 15:54:33 +0300109 return nodes, nodes_to_clean
gstepanove3481cd2015-04-03 21:45:52 +0300110
111
112def parse_command_line(argv):
113 parser = argparse.ArgumentParser(
114 description="Connect to fuel master and setup ssh agent")
gstepanovf1ff52d2015-04-07 15:54:33 +0300115 parser.add_argument(
116 "--fuel_url", required=True)
117 parser.add_argument(
118 "--cluster_id", required=True)
119 parser.add_argument(
120 "--iface", default="eth1")
121 parser.add_argument(
122 "--creds", default="admin:admin:admin")
123
gstepanove3481cd2015-04-03 21:45:52 +0300124 return parser.parse_args(argv)
125
126
127def main(argv):
128 args = parse_command_line(argv)
gstepanove3481cd2015-04-03 21:45:52 +0300129
gstepanov4861d712015-04-09 13:28:02 +0300130 nodes, to_clean = discover_fuel_nodes(args.fuel_url,
131 args.creds, args.cluster_id)
gstepanove3481cd2015-04-03 21:45:52 +0300132 discover_fuel_nodes_clean(args.fuel_url, {"username": "root",
133 "password": "test37",
gstepanovf1ff52d2015-04-07 15:54:33 +0300134 "port": 22}, to_clean)
gstepanove3481cd2015-04-03 21:45:52 +0300135
136
137if __name__ == "__main__":
138 main(sys.argv[1:])