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 | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame^] | 46 | command = "python /tmp/agent.py --clean=True --ext_ip=" + \ |
| 47 | admin_ip + " --base_port=" \ |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 48 | + 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 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 58 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 59 | def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22, |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 60 | 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") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 67 | fuel_id_rsa_path = tmp_name |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 68 | sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 69 | os.chmod(fuel_id_rsa_path, 0o700) |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame^] | 70 | command = "python /tmp/agent.py --base_port=" + \ |
| 71 | str(base_port) + " --ext_ip=" \ |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 72 | + host + " --ports" |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 73 | |
| 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 = [] |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 90 | nodes_to_clean = [] |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 91 | |
| 92 | for i in range(len(ip_addresses)): |
| 93 | ip = ip_addresses[i] |
| 94 | role = roles[i] |
| 95 | port = node_port_mapping[ip] |
| 96 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame^] | 97 | nodes_to_clean.append(("ssh://root@" + ip + ":" + |
| 98 | port.rstrip('\n') |
| 99 | + ":" + fuel_id_rsa_path, role)) |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 100 | |
| 101 | nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n') |
| 102 | + ":" + fuel_id_rsa_path, role)) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 103 | |
| 104 | ssh.close() |
| 105 | print 'Files has been transfered successefully to Fuel node, ' \ |
| 106 | 'agent has been launched' |
| 107 | print nodes |
| 108 | |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 109 | return nodes, nodes_to_clean |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def parse_command_line(argv): |
| 113 | parser = argparse.ArgumentParser( |
| 114 | description="Connect to fuel master and setup ssh agent") |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 115 | 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 | |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 124 | return parser.parse_args(argv) |
| 125 | |
| 126 | |
| 127 | def main(argv): |
| 128 | args = parse_command_line(argv) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 129 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame^] | 130 | nodes, to_clean = discover_fuel_nodes(args.fuel_url, |
| 131 | args.creds, args.cluster_id) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 132 | discover_fuel_nodes_clean(args.fuel_url, {"username": "root", |
| 133 | "password": "test37", |
gstepanov | f1ff52d | 2015-04-07 15:54:33 +0300 | [diff] [blame] | 134 | "port": 22}, to_clean) |
gstepanov | e3481cd | 2015-04-03 21:45:52 +0300 | [diff] [blame] | 135 | |
| 136 | |
| 137 | if __name__ == "__main__": |
| 138 | main(sys.argv[1:]) |