blob: ca72ddc8ba3a6e38f3f3d0400535262aed42ad7c [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
gstepanovf1ff52d2015-04-07 15:54:33 +030046 command = "python /tmp/agent.py --clean=True --ext_ip=" + admin_ip + " --base_port=" \
gstepanove3481cd2015-04-03 21:45:52 +030047 + str(base_port) + " --ports"
48
49 for node in nodes:
50 ip = urlparse(node[0]).hostname
51 command += " " + ip
52
53 (stdin, stdout, stderr) = ssh.exec_command(command)
54 for line in stdout.readlines():
55 print line
56
gstepanove3481cd2015-04-03 21:45:52 +030057
gstepanovf1ff52d2015-04-07 15:54:33 +030058def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22,
gstepanove3481cd2015-04-03 21:45:52 +030059 base_port=12345):
60 ssh = paramiko.SSHClient()
61 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
62 ssh.connect(hostname=host, port=port, password=password, username="root")
63 sftp = ssh.open_sftp()
64 sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'),
65 "/tmp/agent.py")
gstepanovf1ff52d2015-04-07 15:54:33 +030066 fuel_id_rsa_path = tmp_name
gstepanove3481cd2015-04-03 21:45:52 +030067 sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path)
gstepanovf1ff52d2015-04-07 15:54:33 +030068 os.chmod(fuel_id_rsa_path, 0o700)
69 command = "python /tmp/agent.py --base_port=" + str(base_port) + " --ext_ip=" \
70 + host + " --ports"
gstepanove3481cd2015-04-03 21:45:52 +030071
72 for address in ip_addresses:
73 command += " " + address
74
75 (stdin, stdout, stderr) = ssh.exec_command(command)
76 node_port_mapping = {}
77
78 for line in stdout.readlines():
79 results = line.split(' ')
80
81 if len(results) != 2:
82 continue
83
84 node, port = results
85 node_port_mapping[node] = port
86
87 nodes = []
gstepanovf1ff52d2015-04-07 15:54:33 +030088 nodes_to_clean = []
gstepanove3481cd2015-04-03 21:45:52 +030089
90 for i in range(len(ip_addresses)):
91 ip = ip_addresses[i]
92 role = roles[i]
93 port = node_port_mapping[ip]
94
gstepanovf1ff52d2015-04-07 15:54:33 +030095 nodes_to_clean.append(("ssh://root@" + ip + ":" + port.rstrip('\n')
96 + ":" + fuel_id_rsa_path, role))
97
98 nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n')
99 + ":" + fuel_id_rsa_path, role))
gstepanove3481cd2015-04-03 21:45:52 +0300100
101 ssh.close()
102 print 'Files has been transfered successefully to Fuel node, ' \
103 'agent has been launched'
104 print nodes
105
gstepanovf1ff52d2015-04-07 15:54:33 +0300106 return nodes, nodes_to_clean
gstepanove3481cd2015-04-03 21:45:52 +0300107
108
109def parse_command_line(argv):
110 parser = argparse.ArgumentParser(
111 description="Connect to fuel master and setup ssh agent")
gstepanovf1ff52d2015-04-07 15:54:33 +0300112 parser.add_argument(
113 "--fuel_url", required=True)
114 parser.add_argument(
115 "--cluster_id", required=True)
116 parser.add_argument(
117 "--iface", default="eth1")
118 parser.add_argument(
119 "--creds", default="admin:admin:admin")
120
121
gstepanove3481cd2015-04-03 21:45:52 +0300122 return parser.parse_args(argv)
123
124
125def main(argv):
126 args = parse_command_line(argv)
gstepanove3481cd2015-04-03 21:45:52 +0300127
gstepanovf1ff52d2015-04-07 15:54:33 +0300128 nodes, to_clean = discover_fuel_nodes(args.fuel_url, args.creds, args.cluster_id)
gstepanove3481cd2015-04-03 21:45:52 +0300129 discover_fuel_nodes_clean(args.fuel_url, {"username": "root",
130 "password": "test37",
gstepanovf1ff52d2015-04-07 15:54:33 +0300131 "port": 22}, to_clean)
gstepanove3481cd2015-04-03 21:45:52 +0300132
133
134if __name__ == "__main__":
135 main(sys.argv[1:])