| gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame^] | 1 | import argparse | 
|  | 2 | import sys | 
|  | 3 | import os | 
|  | 4 | import paramiko | 
|  | 5 |  | 
|  | 6 | from urlparse import urlparse | 
|  | 7 |  | 
|  | 8 |  | 
|  | 9 | from keystone import KeystoneAuth | 
|  | 10 |  | 
|  | 11 |  | 
|  | 12 | def discover_fuel_nodes(fuel_url, creds, cluster_id): | 
|  | 13 | admin_ip = urlparse(fuel_url).hostname | 
|  | 14 | fuel = KeystoneAuth(fuel_url, creds, headers=None, echo=None, | 
|  | 15 | admin_node_ip=admin_ip) | 
|  | 16 | nodes = fuel.do("get", path="/api/nodes?cluster_id=" + str(cluster_id)) | 
|  | 17 | ips = [node["ip"] for node in nodes] | 
|  | 18 | roles = [node["roles"] for node in nodes] | 
|  | 19 |  | 
|  | 20 | host = urlparse(fuel_url).hostname | 
|  | 21 |  | 
|  | 22 | return run_agent(ips, roles, host) | 
|  | 23 |  | 
|  | 24 |  | 
|  | 25 | def discover_fuel_nodes_clean(fuel_url, ssh_creds, nodes, base_port=12345): | 
|  | 26 | admin_ip = urlparse(fuel_url).hostname | 
|  | 27 | ssh = paramiko.SSHClient() | 
|  | 28 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | 
|  | 29 | ssh.connect(hostname=admin_ip, port=ssh_creds["port"], | 
|  | 30 | password=ssh_creds["password"], username=ssh_creds["username"]) | 
|  | 31 |  | 
|  | 32 | command = "python /tmp/agent.py --clean=True --base_port=" \ | 
|  | 33 | + str(base_port) + " --ports" | 
|  | 34 |  | 
|  | 35 | for node in nodes: | 
|  | 36 | ip = urlparse(node[0]).hostname | 
|  | 37 | command += " " + ip | 
|  | 38 |  | 
|  | 39 | (stdin, stdout, stderr) = ssh.exec_command(command) | 
|  | 40 | for line in stdout.readlines(): | 
|  | 41 | print line | 
|  | 42 |  | 
|  | 43 | os.remove('/tmp/fuel_id_rsa') | 
|  | 44 |  | 
|  | 45 |  | 
|  | 46 | def run_agent(ip_addresses, roles, host, password="test37", port=22, | 
|  | 47 | base_port=12345): | 
|  | 48 | ssh = paramiko.SSHClient() | 
|  | 49 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | 
|  | 50 | ssh.connect(hostname=host, port=port, password=password, username="root") | 
|  | 51 | sftp = ssh.open_sftp() | 
|  | 52 | sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'), | 
|  | 53 | "/tmp/agent.py") | 
|  | 54 | fuel_id_rsa_path = '/tmp/fuel_id_rsa' | 
|  | 55 | sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path) | 
|  | 56 | os.chmod(fuel_id_rsa_path, 700) | 
|  | 57 | command = "python /tmp/agent.py --base_port=" + str(base_port) + " --ports" | 
|  | 58 |  | 
|  | 59 | for address in ip_addresses: | 
|  | 60 | command += " " + address | 
|  | 61 |  | 
|  | 62 | (stdin, stdout, stderr) = ssh.exec_command(command) | 
|  | 63 | node_port_mapping = {} | 
|  | 64 |  | 
|  | 65 | for line in stdout.readlines(): | 
|  | 66 | results = line.split(' ') | 
|  | 67 |  | 
|  | 68 | if len(results) != 2: | 
|  | 69 | continue | 
|  | 70 |  | 
|  | 71 | node, port = results | 
|  | 72 | node_port_mapping[node] = port | 
|  | 73 |  | 
|  | 74 | nodes = [] | 
|  | 75 |  | 
|  | 76 | for i in range(len(ip_addresses)): | 
|  | 77 | ip = ip_addresses[i] | 
|  | 78 | role = roles[i] | 
|  | 79 | port = node_port_mapping[ip] | 
|  | 80 |  | 
|  | 81 | nodes.append(("ssh://root@" + ip + ":" + port + | 
|  | 82 | ":/tmp/fuel_id_rsa", role)) | 
|  | 83 |  | 
|  | 84 | ssh.close() | 
|  | 85 | print 'Files has been transfered successefully to Fuel node, ' \ | 
|  | 86 | 'agent has been launched' | 
|  | 87 | print nodes | 
|  | 88 |  | 
|  | 89 | return nodes | 
|  | 90 |  | 
|  | 91 |  | 
|  | 92 | def parse_command_line(argv): | 
|  | 93 | parser = argparse.ArgumentParser( | 
|  | 94 | description="Connect to fuel master and setup ssh agent") | 
|  | 95 | parser.add_argument( | 
|  | 96 | "--fuel_url", required=True) | 
|  | 97 | parser.add_argument( | 
|  | 98 | "--cluster_id", required=True) | 
|  | 99 | parser.add_argument( | 
|  | 100 | "--username", default="admin") | 
|  | 101 | parser.add_argument( | 
|  | 102 | "--tenantname", default="admin") | 
|  | 103 | parser.add_argument( | 
|  | 104 | "--password", default="admin") | 
|  | 105 |  | 
|  | 106 | return parser.parse_args(argv) | 
|  | 107 |  | 
|  | 108 |  | 
|  | 109 | def main(argv): | 
|  | 110 | args = parse_command_line(argv) | 
|  | 111 | creds = {"username": args.username, | 
|  | 112 | "tenant_name": args.tenantname, | 
|  | 113 | "password": args.password} | 
|  | 114 |  | 
|  | 115 | nodes = discover_fuel_nodes(args.fuel_url, creds, args.cluster_id) | 
|  | 116 | discover_fuel_nodes_clean(args.fuel_url, {"username": "root", | 
|  | 117 | "password": "test37", | 
|  | 118 | "port": 22}, nodes) | 
|  | 119 |  | 
|  | 120 |  | 
|  | 121 | if __name__ == "__main__": | 
|  | 122 | main(sys.argv[1:]) |