gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 1 | import argparse |
| 2 | import sys |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 3 | import tempfile |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 4 | import os |
| 5 | import paramiko |
| 6 | |
| 7 | from urlparse import urlparse |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 8 | from nodes.node import Node |
| 9 | from utils import parse_creds |
| 10 | from keystone import KeystoneAuth |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 11 | |
| 12 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 13 | from keystone import KeystoneAuth |
| 14 | |
| 15 | tmp_file = tempfile.NamedTemporaryFile().name |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def discover_fuel_nodes(fuel_url, creds, cluster_id): |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 19 | username, tenant_name, password = parse_creds(creds) |
| 20 | creds = {"username": username, |
| 21 | "tenant_name": tenant_name, |
| 22 | "password": password} |
| 23 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 24 | 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 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 33 | 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 |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def 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 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 46 | command = "python /tmp/agent.py --clean=True --ext_ip=" + admin_ip + " --base_port=" \ |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 47 | + 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 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 57 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 58 | def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22, |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 59 | 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") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 66 | fuel_id_rsa_path = tmp_name |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 67 | sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 68 | os.chmod(fuel_id_rsa_path, 0o700) |
| 69 | command = "python /tmp/agent.py --base_port=" + str(base_port) + " --ext_ip=" \ |
| 70 | + host + " --ports" |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 71 | |
| 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 = [] |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 88 | nodes_to_clean = [] |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 89 | |
| 90 | for i in range(len(ip_addresses)): |
| 91 | ip = ip_addresses[i] |
| 92 | role = roles[i] |
| 93 | port = node_port_mapping[ip] |
| 94 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 95 | 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)) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 100 | |
| 101 | ssh.close() |
| 102 | print 'Files has been transfered successefully to Fuel node, ' \ |
| 103 | 'agent has been launched' |
| 104 | print nodes |
| 105 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 106 | return nodes, nodes_to_clean |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def parse_command_line(argv): |
| 110 | parser = argparse.ArgumentParser( |
| 111 | description="Connect to fuel master and setup ssh agent") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 112 | 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 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 122 | return parser.parse_args(argv) |
| 123 | |
| 124 | |
| 125 | def main(argv): |
| 126 | args = parse_command_line(argv) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 127 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 128 | nodes, to_clean = discover_fuel_nodes(args.fuel_url, args.creds, args.cluster_id) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 129 | discover_fuel_nodes_clean(args.fuel_url, {"username": "root", |
| 130 | "password": "test37", |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame^] | 131 | "port": 22}, to_clean) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 132 | |
| 133 | |
| 134 | if __name__ == "__main__": |
| 135 | main(sys.argv[1:]) |