bt_comm: added xml files and client code
authorAdriana Draghici <adriana008@gmail.com>
Tue, 26 Jan 2010 09:38:34 +0000 (11:38 +0200)
committerAdriana Draghici <adriana008@gmail.com>
Tue, 26 Jan 2010 09:38:34 +0000 (11:38 +0200)
13 files changed:
bt_comm/client/ParserConf.py [new file with mode: 0644]
bt_comm/client/SSHCommander.py [new file with mode: 0644]
bt_comm/client/TrafficControl.py [new file with mode: 0644]
bt_comm/client/XMLParser.py [new file with mode: 0644]
bt_comm/xml/.clients.xml.swp [new file with mode: 0644]
bt_comm/xml/.nodes.xml.swp [new file with mode: 0644]
bt_comm/xml/ParserConf.py [new file with mode: 0644]
bt_comm/xml/SSHCommander.py [new file with mode: 0644]
bt_comm/xml/TrafficControl.py [new file with mode: 0644]
bt_comm/xml/XMLParser.py [new file with mode: 0644]
bt_comm/xml/clients.xml [new file with mode: 0644]
bt_comm/xml/nodes.xml [new file with mode: 0644]
bt_comm/xml/swarm.xml [new file with mode: 0644]

diff --git a/bt_comm/client/ParserConf.py b/bt_comm/client/ParserConf.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/bt_comm/client/SSHCommander.py b/bt_comm/client/SSHCommander.py
new file mode 100644 (file)
index 0000000..71cfc41
--- /dev/null
@@ -0,0 +1,21 @@
+import paramiko
+import os
+paramiko.util.log_to_file('/tmp/paramiko.log')
+
+client = paramiko.SSHClient()
+client.load_system_host_keys()
+client.connect(hostname='141.85.37.237',  username='marius', password='marius')
+#client.connect(hostname='p2p-next-07.grid.pub.ro',  username='p2p')
+stdin, stdout, stderr = client.exec_command('touch gugu dudu')
+stdin, stdout, stderr = client.exec_command('ls -l')
+print stdout.readlines()
+client.close()
+#~ host = "p2p-next-09.grid.pub.ro"
+#~ port = 22
+#~ transport = paramiko.Transport((host, port))
+
+#~ privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
+#~ mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
+#~ username = 'p2p'
+#~ transport.connect(username = username, pkey = mykey)
+
diff --git a/bt_comm/client/TrafficControl.py b/bt_comm/client/TrafficControl.py
new file mode 100644 (file)
index 0000000..27d8be2
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+
+"""
+ Traffic control interface for Linux tc
+ 2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+"""
+
+import sys
+import sqlite3
+import os.path
+
+DEBUG = False
+
+class TrafficControl:
+    """
+    Basic class interface to Linux tc
+    """
+    
+    def __init__(self, node_type):
+        self.node_type = node_type
+        self.upload_limit = -1
+        self.download_limit = -1
+
+    def config(self, host_address, host_port, host_iface,
+            vm_address = None, vm_port = -1, vm_iface = None):
+        self.host_address = host_address
+        self.host_port = host_port
+        self.host_iface = host_iface
+        self.vm_address = vm_address
+        self.vm_port = vm_port
+        self.vm_iface = vm_iface
+
+    """
+    Set upload limit. Argument in KB/s (kilobytes/s)
+    """
+    def set_upload_limit(self, upload_limit):
+        self.upload_limit = upload_limit
+
+    """
+    Set download limit. Argument in KB/s (kilobytes/s)
+    """
+    def set_download_limit(self, download_limit):
+        self.download_limit = download_limit
+
+    def get_upload_limit_commands(self):
+        upload_limit_commands = []
+        upload_limit_commands.append("tc qdisc add dev %s root handle 1: htb default 90" % (self.vm_iface))
+        upload_limit_commands.append("tc class add dev %s parent 1: classid 1:1 htb rate %dkb ceil %dkb" % (self.vm_iface, self.upload_limit, self.upload_limit))
+        upload_limit_commands.append("tc qdisc add dev %s parent 1:1 handle 10: sfq perturb 10" % (self.vm_iface))
+        upload_limit_commands.append("tc filter add dev %s parent 1:0 protocol ip u32 match ip dst %s match ip dport %d 0xffff classid 1:1" % (self.vm_iface, self.vm_address, self.vm_port))
+
+        return upload_limit_commands
+
+    def get_download_limit_commands(self):
+        download_limit_commands = []
+        download_limit_commands.append("tc qdisc add dev %s root handle 1: htb default 90" % (self.host_iface))
+        download_limit_commands.append("tc class add dev %s parent 1: classid 1:1 htb rate %dkb ceil %dkb" % (self.host_iface, self.download_limit, self.download_limit))
+        download_limit_commands.append("tc qdisc add dev %s parent 1:1 handle 10: sfq perturb 10" % (self.host_iface))
+        download_limit_commands.append("tc filter add dev %s parent 1:0 protocol ip u32 match ip src %s classid 1:1" % (self.host_iface, self.vm_address))
+
+        return download_limit_commands
+
+    def get_flush_commands(self):
+        flush_commands = []
+        flush_commands.append("tc qdisc del dev %s root handle 1:" % (self.host_iface))
+        flush_commands.append("tc qdisc del dev %s root handle 1:" % (self.vm_iface))
+
+        return flush_commands
+
+
+def print_commands(commands, header):
+    print "\n\t== %s ==\n" % (header)
+    
+    for c in commands:
+        print c
+
+def main():
+
+    """
+    Test case
+    """
+
+    tc = TrafficControl("openvz")
+    tc.config("141.85.224.201", 10150, "eth0",
+            "172.16.10.0", 10150, "venet0")
+    tc.set_upload_limit(512)
+    tc.set_download_limit(256)
+
+    upload_limit_commands = tc.get_upload_limit_commands()
+    download_limit_commands = tc.get_download_limit_commands()
+    flush_commands = tc.get_flush_commands()
+
+    print_commands(upload_limit_commands, "upload limit commands")
+    print_commands(download_limit_commands, "download limit commands")
+    print_commands(flush_commands, "flush commands")
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/bt_comm/client/XMLParser.py b/bt_comm/client/XMLParser.py
new file mode 100644 (file)
index 0000000..1f473b5
--- /dev/null
@@ -0,0 +1,104 @@
+from lxml import etree 
+
+class SwarmInstance:
+       def __init__(self, id, node_id, list):
+               self.id = id;
+               self.node_id = node_id;
+               self.btclient = list[0];
+               self.upload_limit = list[1];
+               self.download_limit = list[2];
+               self.port = list[3];
+               self.download_dir = list[4];
+               self.upload_dir = list[5];
+       
+       def __str__(self):
+               return '[%s: %s: %s: %s: %s: %s: %s: %s]' \
+                       %(self.id, self.node_id, self.btclient, self.upload_limit, \
+                               self.download_limit, self.port, self.upload_dir, self.download_dir);
+       
+class NodeConfig:
+       def __init__(self, list):
+               self.id = list[0];
+               self.public_address = list[1];
+               self.public_port = list[2];
+               self.private_address = list[3];
+               self.private_port = list[4];
+               self.ssh_port= list[5];
+               self.clients_base_dir = list[6];
+       
+       #~ def __str__(self):
+               #~ return '[%s: %s: %s: %s: %s: %s: %s]' \
+                       #~ %(self.id, self.btclient, self.upload_limit, self.download_limit, self.port, self.upload_dir, self.download_dir);
+
+class ClientConfig:
+       def __init__(self, id, node_path, filetype, options):
+               self.id = id;
+               self.filetype = filetype;
+               self.options = options;
+               #~ self.upload_limit_option= list[2];
+               #~ self.download_limit_option = list[3];
+               #~ self.port_option = list[4];
+               #~ self.logging_dir_option = list[5];
+               #~ self.download_dir_option = list[6];
+       
+       #~ def __str__(self):
+               #~ return '[%s: %s: %s: %s: %s: %s: %s]' \
+                       #~ %(self.id, self.btclient, self.upload_limit, self.download_limit, self.port, self.upload_dir, self.download_dir);
+                       
+class FileType:
+       def __init__(self, list):
+               self.type = list[0];
+               self.file = list[1];
+               self.interpreter = list[2];
+               self.prefix = list[3];
+               self.sufix = list[4];
+       
+       def __str__(self):
+               return '[%s: %s: %s: %s: %s: %s: %s]' \
+                       %(self.id, self.btclient, self.upload_limit, self.download_limit, self.port, self.upload_dir, self.download_dir);
+
+try:
+       tree = etree.parse("clients.xml")
+       root = tree.getroot()
+       clients_options = {}
+       for elem in root:
+               id = elem.get("id")
+               options = [elem2.text for elem2 in elem[1:]]
+               filetype =  [elem[0].get("type")]
+               filetype.extend([elem2.text for elem2 in elem[0]])
+               clients_options[id] = [filetype, options]
+       
+       print clients_options
+except IOError as e:
+       print e
+
+try:
+       tree = etree.parse("nodes.xml")
+       root = tree.getroot()
+       nodes = {}
+       for elem in root:
+               id = elem.get("id")
+               list = [elem2.text for elem2 in elem[:len(elem)-1]]
+               client_paths = {}
+               for elem2 in elem[len(elem)-1]:
+                       client_id = elem2.get("id");
+                       client_paths [client_id]=elem2[0].text;
+               nodes[id] = [client_paths, list]
+       print nodes
+       print "=================================="
+except IOError as e:
+       print e
+
+try:
+       tree = etree.parse("swarm.xml")
+       root = tree.getroot()
+       swarm = {}
+       for elem in root:
+               id = elem.get("id")
+               list = [elem2.text for elem2 in elem[:len(elem)-1]]
+               swarm[id] = list
+       print swarm
+       print "=================================="
+except IOError as e:
+       print e
+       
diff --git a/bt_comm/xml/.clients.xml.swp b/bt_comm/xml/.clients.xml.swp
new file mode 100644 (file)
index 0000000..9d64450
Binary files /dev/null and b/bt_comm/xml/.clients.xml.swp differ
diff --git a/bt_comm/xml/.nodes.xml.swp b/bt_comm/xml/.nodes.xml.swp
new file mode 100644 (file)
index 0000000..2fe4261
Binary files /dev/null and b/bt_comm/xml/.nodes.xml.swp differ
diff --git a/bt_comm/xml/ParserConf.py b/bt_comm/xml/ParserConf.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/bt_comm/xml/SSHCommander.py b/bt_comm/xml/SSHCommander.py
new file mode 100644 (file)
index 0000000..71cfc41
--- /dev/null
@@ -0,0 +1,21 @@
+import paramiko
+import os
+paramiko.util.log_to_file('/tmp/paramiko.log')
+
+client = paramiko.SSHClient()
+client.load_system_host_keys()
+client.connect(hostname='141.85.37.237',  username='marius', password='marius')
+#client.connect(hostname='p2p-next-07.grid.pub.ro',  username='p2p')
+stdin, stdout, stderr = client.exec_command('touch gugu dudu')
+stdin, stdout, stderr = client.exec_command('ls -l')
+print stdout.readlines()
+client.close()
+#~ host = "p2p-next-09.grid.pub.ro"
+#~ port = 22
+#~ transport = paramiko.Transport((host, port))
+
+#~ privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
+#~ mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
+#~ username = 'p2p'
+#~ transport.connect(username = username, pkey = mykey)
+
diff --git a/bt_comm/xml/TrafficControl.py b/bt_comm/xml/TrafficControl.py
new file mode 100644 (file)
index 0000000..27d8be2
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+
+"""
+ Traffic control interface for Linux tc
+ 2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+"""
+
+import sys
+import sqlite3
+import os.path
+
+DEBUG = False
+
+class TrafficControl:
+    """
+    Basic class interface to Linux tc
+    """
+    
+    def __init__(self, node_type):
+        self.node_type = node_type
+        self.upload_limit = -1
+        self.download_limit = -1
+
+    def config(self, host_address, host_port, host_iface,
+            vm_address = None, vm_port = -1, vm_iface = None):
+        self.host_address = host_address
+        self.host_port = host_port
+        self.host_iface = host_iface
+        self.vm_address = vm_address
+        self.vm_port = vm_port
+        self.vm_iface = vm_iface
+
+    """
+    Set upload limit. Argument in KB/s (kilobytes/s)
+    """
+    def set_upload_limit(self, upload_limit):
+        self.upload_limit = upload_limit
+
+    """
+    Set download limit. Argument in KB/s (kilobytes/s)
+    """
+    def set_download_limit(self, download_limit):
+        self.download_limit = download_limit
+
+    def get_upload_limit_commands(self):
+        upload_limit_commands = []
+        upload_limit_commands.append("tc qdisc add dev %s root handle 1: htb default 90" % (self.vm_iface))
+        upload_limit_commands.append("tc class add dev %s parent 1: classid 1:1 htb rate %dkb ceil %dkb" % (self.vm_iface, self.upload_limit, self.upload_limit))
+        upload_limit_commands.append("tc qdisc add dev %s parent 1:1 handle 10: sfq perturb 10" % (self.vm_iface))
+        upload_limit_commands.append("tc filter add dev %s parent 1:0 protocol ip u32 match ip dst %s match ip dport %d 0xffff classid 1:1" % (self.vm_iface, self.vm_address, self.vm_port))
+
+        return upload_limit_commands
+
+    def get_download_limit_commands(self):
+        download_limit_commands = []
+        download_limit_commands.append("tc qdisc add dev %s root handle 1: htb default 90" % (self.host_iface))
+        download_limit_commands.append("tc class add dev %s parent 1: classid 1:1 htb rate %dkb ceil %dkb" % (self.host_iface, self.download_limit, self.download_limit))
+        download_limit_commands.append("tc qdisc add dev %s parent 1:1 handle 10: sfq perturb 10" % (self.host_iface))
+        download_limit_commands.append("tc filter add dev %s parent 1:0 protocol ip u32 match ip src %s classid 1:1" % (self.host_iface, self.vm_address))
+
+        return download_limit_commands
+
+    def get_flush_commands(self):
+        flush_commands = []
+        flush_commands.append("tc qdisc del dev %s root handle 1:" % (self.host_iface))
+        flush_commands.append("tc qdisc del dev %s root handle 1:" % (self.vm_iface))
+
+        return flush_commands
+
+
+def print_commands(commands, header):
+    print "\n\t== %s ==\n" % (header)
+    
+    for c in commands:
+        print c
+
+def main():
+
+    """
+    Test case
+    """
+
+    tc = TrafficControl("openvz")
+    tc.config("141.85.224.201", 10150, "eth0",
+            "172.16.10.0", 10150, "venet0")
+    tc.set_upload_limit(512)
+    tc.set_download_limit(256)
+
+    upload_limit_commands = tc.get_upload_limit_commands()
+    download_limit_commands = tc.get_download_limit_commands()
+    flush_commands = tc.get_flush_commands()
+
+    print_commands(upload_limit_commands, "upload limit commands")
+    print_commands(download_limit_commands, "download limit commands")
+    print_commands(flush_commands, "flush commands")
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/bt_comm/xml/XMLParser.py b/bt_comm/xml/XMLParser.py
new file mode 100644 (file)
index 0000000..1f473b5
--- /dev/null
@@ -0,0 +1,104 @@
+from lxml import etree 
+
+class SwarmInstance:
+       def __init__(self, id, node_id, list):
+               self.id = id;
+               self.node_id = node_id;
+               self.btclient = list[0];
+               self.upload_limit = list[1];
+               self.download_limit = list[2];
+               self.port = list[3];
+               self.download_dir = list[4];
+               self.upload_dir = list[5];
+       
+       def __str__(self):
+               return '[%s: %s: %s: %s: %s: %s: %s: %s]' \
+                       %(self.id, self.node_id, self.btclient, self.upload_limit, \
+                               self.download_limit, self.port, self.upload_dir, self.download_dir);
+       
+class NodeConfig:
+       def __init__(self, list):
+               self.id = list[0];
+               self.public_address = list[1];
+               self.public_port = list[2];
+               self.private_address = list[3];
+               self.private_port = list[4];
+               self.ssh_port= list[5];
+               self.clients_base_dir = list[6];
+       
+       #~ def __str__(self):
+               #~ return '[%s: %s: %s: %s: %s: %s: %s]' \
+                       #~ %(self.id, self.btclient, self.upload_limit, self.download_limit, self.port, self.upload_dir, self.download_dir);
+
+class ClientConfig:
+       def __init__(self, id, node_path, filetype, options):
+               self.id = id;
+               self.filetype = filetype;
+               self.options = options;
+               #~ self.upload_limit_option= list[2];
+               #~ self.download_limit_option = list[3];
+               #~ self.port_option = list[4];
+               #~ self.logging_dir_option = list[5];
+               #~ self.download_dir_option = list[6];
+       
+       #~ def __str__(self):
+               #~ return '[%s: %s: %s: %s: %s: %s: %s]' \
+                       #~ %(self.id, self.btclient, self.upload_limit, self.download_limit, self.port, self.upload_dir, self.download_dir);
+                       
+class FileType:
+       def __init__(self, list):
+               self.type = list[0];
+               self.file = list[1];
+               self.interpreter = list[2];
+               self.prefix = list[3];
+               self.sufix = list[4];
+       
+       def __str__(self):
+               return '[%s: %s: %s: %s: %s: %s: %s]' \
+                       %(self.id, self.btclient, self.upload_limit, self.download_limit, self.port, self.upload_dir, self.download_dir);
+
+try:
+       tree = etree.parse("clients.xml")
+       root = tree.getroot()
+       clients_options = {}
+       for elem in root:
+               id = elem.get("id")
+               options = [elem2.text for elem2 in elem[1:]]
+               filetype =  [elem[0].get("type")]
+               filetype.extend([elem2.text for elem2 in elem[0]])
+               clients_options[id] = [filetype, options]
+       
+       print clients_options
+except IOError as e:
+       print e
+
+try:
+       tree = etree.parse("nodes.xml")
+       root = tree.getroot()
+       nodes = {}
+       for elem in root:
+               id = elem.get("id")
+               list = [elem2.text for elem2 in elem[:len(elem)-1]]
+               client_paths = {}
+               for elem2 in elem[len(elem)-1]:
+                       client_id = elem2.get("id");
+                       client_paths [client_id]=elem2[0].text;
+               nodes[id] = [client_paths, list]
+       print nodes
+       print "=================================="
+except IOError as e:
+       print e
+
+try:
+       tree = etree.parse("swarm.xml")
+       root = tree.getroot()
+       swarm = {}
+       for elem in root:
+               id = elem.get("id")
+               list = [elem2.text for elem2 in elem[:len(elem)-1]]
+               swarm[id] = list
+       print swarm
+       print "=================================="
+except IOError as e:
+       print e
+       
diff --git a/bt_comm/xml/clients.xml b/bt_comm/xml/clients.xml
new file mode 100644 (file)
index 0000000..57c96da
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<clients>
+    <client id="tribler">
+       <run type="script">
+           <file>Tribler/Tools/cmdline.py</file>
+           <interpreter>python </interpreter>
+           <prefix>PYTHONPATH=.</prefix>
+           <suffix></suffix>
+       </run>
+       <upload_limit_option></upload_limit_option>
+       <download_limit_option></download_limit_option>
+       <port_option>-p</port_option>
+       <logging_dir_option>-l</logging_dir_option>
+       <download_dir_option>-d</download_dir_option>
+    </client>
+    <client id="libtorrent">
+       <run type="executable">
+           <file>hrktorrent</file>
+           <interpreter></interpreter>
+           <prefix></prefix>
+           <suffix></suffix>
+       </run>
+       <port_option>-p</port_option>
+       <download_dir_option>-d</download_dir_option>
+    </client>
+    <client id="transmission">
+       <run type="executable">
+           <file>src/transmission-cli</file>
+           <interpreter></interpreter>
+           <prefix></prefix>
+           <suffix></suffix>
+       </run>
+       <upload_limit_option>-u</upload_limit_option>
+       <download_limit_option>-d</download_limit_option>
+       <port_option>-p</port_option>
+       <logging_dir_option>-l</logging_dir_option>
+       <download_dir_option>-o</download_dir_option>
+    </client>
+</clients>
diff --git a/bt_comm/xml/nodes.xml b/bt_comm/xml/nodes.xml
new file mode 100644 (file)
index 0000000..def3961
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<nodes>
+    <node id="1">
+       <public_address>141.85.224.201</public_address>
+       <public_port>10150</public_port>
+       <private_address>172.30.10.0</private_address>
+       <private_port>10150</private_port>
+       <ssh_port>10122</ssh_port>
+       <clients>
+           <client id="tribler">
+               <base>/home/p2p/p2p-clients/tribler/</base>
+           </client>
+           <client id="libtorrent">
+               <base>/home/p2p/p2p-clients/libtorrent/</base>
+           </client>
+           <client id="transmission">
+               <base>/home/p2p/p2p-clients/transmission/</base>
+           </client>
+        </clients>
+    </node>
+    <node id="2">
+       <public_address>141.85.224.202</public_address>
+       <public_port>10250</public_port>
+       <private_address>172.30.20.0</private_address>
+       <private_port>10250</private_port>
+       <ssh_port>10222</ssh_port>
+       <clients>
+           <client id="tribler">
+               <base>/home/p2p/p2p-clients/tribler/</base>
+           </client>
+           <client id="libtorrent">
+               <base>/home/p2p/p2p-clients/libtorrent/</base>
+           </client>
+           <client id="transmission">
+               <base>/home/p2p/p2p-clients/transmission/</base>
+           </client>
+       </clients>
+    </node>
+</nodes>
diff --git a/bt_comm/xml/swarm.xml b/bt_comm/xml/swarm.xml
new file mode 100644 (file)
index 0000000..e0ba485
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<swarm>
+    <instance id="1">
+       <node>1</node>
+       <client>triber</client>
+       <upload_limit>512</upload_limit>
+       <download_limit>256</download_limit>
+       <port>9999</port>
+       <download_dir>/this/dir</download_dir>
+       <logging_dir>/this/dir</logging_dir>
+       <actions>
+           <action type="start" delay="00:05:00" />
+           <action type="stop" delay="00:10:00" />
+           <action type="start" delay="00:15:00" />
+           <action type="stop" delay="00:20:00" />
+           <action type="start" delay="00:25:00" />
+           <action type="start" delay="end" />
+       </actions>
+    </instance>
+    <instance id="2">
+       <node>2</node>
+       <client>transmission</client>
+       <upload_limit></upload_limit>
+       <download_limit></download_limit>
+       <port></port>
+       <download_dir></download_dir>
+       <logging_dir></logging_dir>
+       <actions>
+           <action type="start" delay="00:05:00" />
+           <action type="stop" delay="01:00:00" />
+           <action type="start" delay="02:00:00" />
+           <action type="start" delay="end" />
+       </actions>
+    </instance>
+    <instance id="3">
+       <node>3</node>
+       <client>libtorrent</client>
+       <upload_limit></upload_limit>
+       <download_limit></download_limit>
+       <port></port>
+       <download_dir></download_dir>
+       <logging_dir></logging_dir>
+       <actions>
+           <action type="start" delay="00:00:00" />
+           <action type="start" delay="end" />
+       </actions>
+    </instance>
+</swarm>