MOL 319, connector code integrated in run_test.py
diff --git a/config.yaml b/config.yaml
index da94a8a..040c49e 100644
--- a/config.yaml
+++ b/config.yaml
@@ -1,7 +1,12 @@
 clouds:
-    ceph: local
+     fuel:
+         id: 3
+         url: http://172.16.52.112:8000
+         creds: admin:admin@admin
+         ssh_creds: root:test37
+         openstack_env: test
 
-discover: ceph
+discover: fuel
 
 explicit_nodes:
     "ssh://192.168.152.43": testnode
@@ -20,7 +25,7 @@
 
 
 # # nodes to be started/detected
-# clouds:
+#clouds:
 #     # openstack: file:///tmp/openrc
 #     # openstack: user:passwd:tenant, http://......
 #     # ceph: ssh://root@172.16.53.2::/home/koder/.ssh/id_rsa.pub
diff --git a/nodes/discover.py b/nodes/discover.py
index 62da457..769f263 100644
--- a/nodes/discover.py
+++ b/nodes/discover.py
@@ -2,10 +2,10 @@
 import urlparse
 
 import ceph
-import fuel
 import openstack
 
-from disk_perf_test_tool.utils import parse_creds
+from utils import parse_creds
+from scripts import connector
 
 logger = logging.getLogger("io-perf-tool")
 
@@ -39,13 +39,13 @@
 
         elif cluster == "fuel" or cluster == "fuel+openstack":
             cluster_info = clusters_info['fuel']
+            cluster_id = cluster_info['id']
             url = cluster_info['url']
             creds = cluster_info['creds']
             ssh_creds = cluster_info['ssh_creds']
             # if user:password format us used
             if not ssh_creds.startswith("ssh://"):
                 ip_port = urlparse.urlparse(url).netloc
-
                 if ':' in ip_port:
                     ip = ip_port.split(":")[0]
                 else:
@@ -54,8 +54,7 @@
                 ssh_creds = "ssh://{0}@{1}".format(ssh_creds, ip)
 
             env = cluster_info['openstack_env']
-
-            nodes_to_run.extend(fuel.discover_fuel_nodes(url, creds, env))
+            nodes_to_run.extend(connector.discover_fuel_nodes(url, creds, cluster_id)[0])
 
         elif cluster == "ceph":
             cluster_info = clusters_info["ceph"]
diff --git a/scripts/agent.py b/scripts/agent.py
index 2873cb5..ec73736 100644
--- a/scripts/agent.py
+++ b/scripts/agent.py
@@ -1,9 +1,51 @@
 import argparse
 import subprocess
 import sys
+import socket
+import fcntl
+import struct
+import array
 
 
-def make_tunnels(ips, base_port=12345, delete=False):
+def all_interfaces():
+    max_possible = 128  # arbitrary. raise if needed.
+    bytes = max_possible * 32
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    names = array.array('B', '\0' * bytes)
+    outbytes = struct.unpack('iL', fcntl.ioctl(
+        s.fileno(),
+        0x8912,  # SIOCGIFCONF
+        struct.pack('iL', bytes, names.buffer_info()[0])
+    ))[0]
+    namestr = names.tostring()
+    lst = []
+    for i in range(0, outbytes, 40):
+        name = namestr[i:i+16].split('\0', 1)[0]
+        ip = namestr[i+20:i+24]
+        lst.append((name, ip))
+    return lst
+
+
+def format_ip(addr):
+    return str(ord(addr[0])) + '.' + \
+           str(ord(addr[1])) + '.' + \
+           str(ord(addr[2])) + '.' + \
+           str(ord(addr[3]))
+
+
+def find_interface_by_ip(ext_ip):
+    ifs = all_interfaces()
+    for i in ifs:
+        ip = format_ip(i[1])
+
+        if ip == ext_ip:
+            return str(i[0])
+
+    print "External ip doesnt corresponds to any of available interfaces"
+    return None
+
+
+def make_tunnels(ips, ext_ip, base_port=12345, delete=False):
     node_port = {}
 
     if delete is True:
@@ -11,9 +53,11 @@
     else:
         mode = "-A"
 
+    iface = find_interface_by_ip(ext_ip)
+
     for ip in ips:
         p = subprocess.Popen(["iptables -t nat " + mode + " PREROUTING " +
-                              "-p tcp -i eth1 --dport " + str(base_port) +
+                              "-p tcp -i " + iface + "  --dport " + str(base_port) +
                               " -j DNAT --to " + str(ip) + ":22"],
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
@@ -35,12 +79,20 @@
 
 
 def parse_command_line(argv):
-    parser = argparse.ArgumentParser(description="Connect to fuel master " +
+    parser = argparse.ArgumentParser(description=
+                                     "Connect to fuel master "
                                      "and setup ssh agent")
-    parser.add_argument("--base_port", type=int, required=True)
-    # To do: fix clean to be False when string is False
-    parser.add_argument("--clean", type=bool, default=False)
-    parser.add_argument("--ports", type=str, nargs='+')
+    parser.add_argument(
+        "--base_port", type=int, required=True)
+
+    parser.add_argument(
+        "--ext_ip", type=str, required=True)
+
+    parser.add_argument(
+        "--clean", type=bool, default=False)
+
+    parser.add_argument(
+        "--ports", type=str, nargs='+')
 
     return parser.parse_args(argv)
 
@@ -48,6 +100,7 @@
 def main(argv):
     arg_object = parse_command_line(argv)
     mapping = make_tunnels(arg_object.ports,
+                           ext_ip=arg_object.ext_ip,
                            base_port=arg_object.base_port,
                            delete=arg_object.clean)
 
diff --git a/scripts/connector.py b/scripts/connector.py
index 15bc538..ca72ddc 100644
--- a/scripts/connector.py
+++ b/scripts/connector.py
@@ -1,15 +1,26 @@
 import argparse
 import sys
+import tempfile
 import os
 import paramiko
 
 from urlparse import urlparse
+from nodes.node import Node
+from utils import parse_creds
+from keystone import KeystoneAuth
 
 
-from disk_perf_test_tool.keystone import KeystoneAuth
+from keystone import KeystoneAuth
+
+tmp_file = tempfile.NamedTemporaryFile().name
 
 
 def discover_fuel_nodes(fuel_url, creds, cluster_id):
+    username, tenant_name, password = parse_creds(creds)
+    creds = {"username": username,
+             "tenant_name": tenant_name,
+             "password": password}
+
     admin_ip = urlparse(fuel_url).hostname
     fuel = KeystoneAuth(fuel_url, creds, headers=None, echo=None,
                         admin_node_ip=admin_ip)
@@ -19,7 +30,10 @@
 
     host = urlparse(fuel_url).hostname
 
-    return run_agent(ips, roles, host)
+    nodes, to_clean = run_agent(ips, roles, host, tmp_file)
+    nodes = [Node(node[0], node[1]) for node in nodes]
+
+    return nodes, to_clean
 
 
 def discover_fuel_nodes_clean(fuel_url, ssh_creds, nodes, base_port=12345):
@@ -29,7 +43,7 @@
     ssh.connect(hostname=admin_ip, port=ssh_creds["port"],
                 password=ssh_creds["password"], username=ssh_creds["username"])
 
-    command = "python /tmp/agent.py --clean=True --base_port=" \
+    command = "python /tmp/agent.py --clean=True --ext_ip=" + admin_ip + " --base_port=" \
               + str(base_port) + " --ports"
 
     for node in nodes:
@@ -40,10 +54,8 @@
     for line in stdout.readlines():
         print line
 
-    os.remove('/tmp/fuel_id_rsa')
 
-
-def run_agent(ip_addresses, roles, host, password="test37", port=22,
+def run_agent(ip_addresses, roles, host, tmp_name, password="test37", port=22,
               base_port=12345):
     ssh = paramiko.SSHClient()
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
@@ -51,10 +63,11 @@
     sftp = ssh.open_sftp()
     sftp.put(os.path.join(os.path.dirname(__file__), 'agent.py'),
              "/tmp/agent.py")
-    fuel_id_rsa_path = '/tmp/fuel_id_rsa'
+    fuel_id_rsa_path = tmp_name
     sftp.get('/root/.ssh/id_rsa', fuel_id_rsa_path)
-    os.chmod(fuel_id_rsa_path, 700)
-    command = "python /tmp/agent.py --base_port=" + str(base_port) + " --ports"
+    os.chmod(fuel_id_rsa_path, 0o700)
+    command = "python /tmp/agent.py --base_port=" + str(base_port) + " --ext_ip=" \
+              + host + " --ports"
 
     for address in ip_addresses:
         command += " " + address
@@ -72,46 +85,50 @@
         node_port_mapping[node] = port
 
     nodes = []
+    nodes_to_clean = []
 
     for i in range(len(ip_addresses)):
         ip = ip_addresses[i]
         role = roles[i]
         port = node_port_mapping[ip]
 
-        nodes.append(("ssh://root@" + ip + ":" + port +
-                      ":/tmp/fuel_id_rsa", role))
+        nodes_to_clean.append(("ssh://root@" + ip + ":" + port.rstrip('\n')
+                                + ":" + fuel_id_rsa_path, role))
+
+        nodes.append(("ssh://root@" + host + ":" + port.rstrip('\n')
+                      + ":" + fuel_id_rsa_path, role))
 
     ssh.close()
     print 'Files has been transfered successefully to Fuel node, ' \
           'agent has been launched'
     print nodes
 
-    return nodes
+    return nodes, nodes_to_clean
 
 
 def parse_command_line(argv):
     parser = argparse.ArgumentParser(
         description="Connect to fuel master and setup ssh agent")
-    parser.add_argument("--fuel-url", required=True)
-    parser.add_argument("--cluster-id", required=True)
-    parser.add_argument("--username", default="admin")
-    parser.add_argument("--tenantname", default="admin")
-    parser.add_argument("--password", default="admin")
+    parser.add_argument(
+        "--fuel_url", required=True)
+    parser.add_argument(
+        "--cluster_id", required=True)
+    parser.add_argument(
+        "--iface", default="eth1")
+    parser.add_argument(
+        "--creds", default="admin:admin:admin")
+
+
     return parser.parse_args(argv)
 
 
 def main(argv):
     args = parse_command_line(argv)
-    creds = {"username": args.username,
-             "tenant_name": args.tenantname,
-             "password": args.password}
 
-    nodes = discover_fuel_nodes(args.fuel_url, creds, args.cluster_id)
-    print "Ready", nodes
-    sys.stdin.readline()
+    nodes, to_clean = discover_fuel_nodes(args.fuel_url, args.creds, args.cluster_id)
     discover_fuel_nodes_clean(args.fuel_url, {"username": "root",
                                               "password": "test37",
-                                              "port": 22}, nodes)
+                                              "port": 22}, to_clean)
 
 
 if __name__ == "__main__":