class Commander(Thread):
def __init__(self, nodes_xml, swarm_xml):
+ Thread.__init__(self)
self.nodes = Nodes(nodes_xml);
self.swarm = Swarm(swarm_xml);
self.tc = TrafficControl("openvz");
self.sshc.load_system_host_keys()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- def send_sshcomm(self, hostname, username, port, comm):
+ def sendSSHComm(self, hostname, username, port, comm):
try:
self.sshc.connect(hostname=hostname, username=username, port=port)
stdin, stdout, stderr = self.sshc.exec_command(comm)
- self.sshc.close()
except Exception as e:
print e
+ finally:
+ self.sshc.close()
- def send_multiple_sshcom(self, hostname, username, port, comms):
+ def sendMultipleSSHComm(self, hostname, username, port, comms):
try:
self.sshc.connect(hostname=hostname, username=username, port=port)
for c in comms:
stdin, stdout, stderr = self.sshc.exec_command(comm)
- self.sshc.close()
except Exception as e:
print e
+ finally:
+ self.sshc.close()
- def send_comm(self, hostname, port, msg_type, config_data):
- self.sock.connect((hostname, port))
- self.send(msg_type)
- response = self.recv_msg()
- if response == ACK_MSG:
- self.send_msg(pickle.dumps(config_data))
+ def sendComm(self, hostname, port, msg_type, config_data):
+ try:
+ self.sock.connect((hostname, port))
+ self.send(msg_type)
response = self.recv_msg()
- self.sock.close()
-
- return response
+ if response == ACK_MSG:
+ self.sendMsg(pickle.dumps(config_data))
+ response = self.recv_msg()
+ print response
+ except Exception as e:
+ print e
+ finally:
+ self.sock.close()
- def send(self, msg):
+ def sendMsg(self, msg):
totalsent = 0
while totalsent < len(msg):
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, "socket connection broken"
totalsent = totalsent + sent
+
+ def recvMsg(self):
+ msg = ''
+ chunk = self.sock.recv(MSGLEN)
+ if chunk == '':
+ raise RuntimeError, "socket connection broken"
+ msg = msg + chunk
+
+ return msg
- def start_daemon(self, node):
- comm = "";
- send_sshcomm(node.public_address, node.username, node.ssh_port, comm)
+ def startDaemon(self, node):
+ comm = "python StartMeUpBaby";
+ self.printDummyCommand(node.public_address, node.public_port, node.ssh_port, comm)
+ #sendSSHComm(node.public_address, node.username, node.ssh_port, comm)
- def apply_tc(self, node):
- si = swarm.getSIByNode(node)
- tc.config(node.public_address, node.public_port, node.public_iface, \
+ def applyTC(self, node):
+ si = self.swarm.getSIByNode(node)
+ self.tc.config(node.public_address, node.public_port, node.public_iface, \
node.private_address, node.private_port, node.private_iface)
- tc.set_upload_limit(si.upload_limit)
- tc.set_download_limit(si.downoad_limit)
+ self.tc.set_upload_limit(si.upload_limit)
+ self.tc.set_download_limit(si.download_limit)
- commands = tc.get_all_commands()
- send_multiple_sshcomm(node.public_address, node.username, node.ssh_port, commands)
-
- def send_start(self, node):
-
- config_data = []
- #~ config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",
- #~ RUN_TYPE:"script",
- #~ INTERPRETER:"python", PREFIX:"PYTHONPATH=.",SUFFIX:"",
- #~ UP_LIMIT_OPTION:"",DL_LIMIT_OPTION:"", PORT_OPTION:"-p",
- #~ LOG_DIR_OPTION:"-l",DL_DIR_OPTION:"-d"}]
- response = send_comm(self, node.public_address, node.public_port, START_MSG, config_data)
+ commands = self.tc.get_all_commands()
+ self.printDummyCommand(node.public_address, node.public_port, node.ssh_port, commands)
+ #sendMultipleSSHComm(node.public_address, node.username, node.ssh_port, commands)
+
+ def sendSIStart(self, si):
+ node = self.nodes.getNodeBySi(si)
+ base_path = node.getClientBaseDir(si.btclient)
+ torrent_file = self.swarm.getTorrentFile()
+ config_data = [{'client':si.btclient,
+ 'base_path':base_path,
+ 'download_dir':si.download_dir,
+ 'log_dir':si.log_dir,
+ 'torrent_file':torrent_file,
+ 'output_dir':si.output_dir, }]
+ self.printDummyCommand(node.public_address, node.public_port, START_MSG, config_data)
+ #sendComm(node.public_address, node.public_port, START_MSG, config_data)
+
+ def printDummyCommand(self, public_address, public_port, option1, data):
+ print "----------------------"
+ print "sending to: [%s:%s] with option %s" %(public_address, public_port, option1)
+ print "this data: %s" %(data)
+ print "----------------------\n"
- def send_stop(self, node):
+
+ def sendSIStop(self, node):
pass
- def send_status(self, node):
+ def sendSIStatus(self, node):
pass
+ def startAll(self):
+ for node in self.nodes.getNodes():
+ self.applyTC(node)
+ self.startDaemon(node)
+
+ for si in self.swarm.getSIs():
+ self.sendSIStart(si)
+
def run(self):
- while(1):
- k = raw_input('enter choice:')
+ self.printUsage()
+ while True:
+ k = raw_input("Choose option:")
if k == '': break
-
- #~ def recv_msg(self):
- #~ msg = ''
- #~ chunk = self.sock.recv(MSGLEN)
- #~ if chunk == '':
- #~ raise RuntimeError, "socket connection broken"
- #~ msg = msg + chunk
- #~ print msg
- #~ return msg
-
-class TestCommander:
- def __init__(self):
- nodes = Nodes("../xml/nodes.xml");
- swarm = Swarm("../xml/swarm.xml");
- #print nodes.getNode("2");
-
-if __name__ == "__main__":
-
- tc = TestCommander()
+ elif k == '1':
+ self.startAll()
-
- #~ # test config
- #~ config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",
- #~ RUN_TYPE:"script",
- #~ INTERPRETER:"python", PREFIX:"PYTHONPATH=.",SUFFIX:"",
- #~ UP_LIMIT_OPTION:"",DL_LIMIT_OPTION:"", PORT_OPTION:"-p",
- #~ LOG_DIR_OPTION:"-l",DL_DIR_OPTION:"-d"}]
-
- #~ response = s.send_command(CONFIG_MSG, config_data)
- #~ print response
+ def printUsage(self):
+ print "=========Commander Options=========="
+ print "1. Start Swarm"
+ print "===================================="
+if __name__ == "__main__":
+ c = Commander("../xml/nodes.xml", "../xml/swarm.xml")
+ c.start()
self.download_limit = list[3];
self.download_dir = list[4];
self.log_dir = list[5];
- self.output_dir = list[6];
+ self.log_file = list[6];
+ self.output_dir = list[7];
+ self.output_file = list[8];
def __str__(self):
return '[%s: %s: %s: %s: %s: %s: %s: %s]' \
return '[%s: %s: %s: %s: %s: %s %s: %s]' \
%(self.id, self.public_address, self.public_port, self.private_address, \
self.private_port, self.ssh_port, self.username, self.clients_base_dir);
+
+ def getClientBaseDir(self, client):
+ return self.clients_base_dir[client]
class Nodes:
def __init__(self, nodes_xml):
try:
tree = etree.parse(nodes_xml)
root = tree.getroot()
- self.nodes = {}
+ self.nodes = []
for elem in root:
id = elem.get("id")
list = [elem2.text for elem2 in elem[:len(elem)-1]]
client_id = elem2.get("id");
client_paths [client_id]=elem2[0].text;
list.append(client_paths);
- self.nodes[id] = Node(id, list)
+ self.nodes.append(Node(id, list))
except IOError as e:
print e
return None
def getNode(self, id):
- return self.nodes[id];
+ for node in self.nodes:
+ if node.id == id:
+ return node
+
+ def getNodeBySi(self, si):
+ for node in self.nodes:
+ if node.id == si.node_id:
+ return node
+
+ def getNodes(self):
+ return self.nodes
+
class Swarm:
def __init__(self, swarm_xml):
try:
tree = etree.parse(swarm_xml)
root = tree.getroot()
- self.swarm = {}
+ self.swarm = []
self.torrent_file = root[0].text
for elem in root[1:]:
id = elem.get("id")
list = [elem2.text for elem2 in elem[:len(elem)-1]]
- self.swarm[id] = SwarmInstance(id, list);
- print self.swarm[id]
+ self.swarm.append(SwarmInstance(id, list))
except IOError as e:
print e
return None
def getSIByNode(self, node):
- for si in self.swarm.values():
+ for si in self.swarm:
if si.node_id == node.id:
return si
-
-#sw = Swarm("swarm.xml");
-#print sw.torrent_file
-#print sw.getSIByNode("1")
+
+ def getTorrentFile(self):
+ return self.torrent_file
+
+ def getSIs(self):
+ return self.swarm
+
+#~ swarm = Swarm("swarm.xml")
+#~ for si in swarm.getSIs():
+ #~ print si
+
+#~ nodes = Nodes("nodes.xml")
+#~ for node in nodes.getNodes():
+ #~ print node
+ #~ print node.getClientBaseDir('libtorrent')
\ No newline at end of file
<torrent_file>cucu</torrent_file>
<instance id="1">
<node>1</node>
- <client>triber</client>
+ <client>tribler</client>
<upload_limit>512</upload_limit>
<download_limit>256</download_limit>
- <download_dir>/this/dir</download_dir>
- <log_dir></log_dir>
- <output_dir></output_dir>
+ <download_dir>/download/dir</download_dir>
+ <log_dir>/log/dir</log_dir>
+ <log_file>log_file</log_file>
+ <output_dir>/output/dir</output_dir>
+ <output_file>output_file</output_file>
<actions>
<action type="start" delay="00:05:00" />
<action type="stop" delay="00:10:00" />
<instance id="2">
<node>2</node>
<client>transmission</client>
- <upload_limit></upload_limit>
- <download_limit></download_limit>
- <download_dir></download_dir>
- <log_dir></log_dir>
- <output_dir></output_dir>
+ <upload_limit>128</upload_limit>
+ <download_limit>64</download_limit>
+ <download_dir>/download/dir</download_dir>
+ <log_dir>/log/dir</log_dir>
+ <log_file>log_file</log_file>
+ <output_dir>/output/dir</output_dir>
+ <output_file>output_file</output_file>
<actions>
<action type="start" delay="00:05:00" />
<action type="stop" delay="01: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>
- <download_dir></download_dir>
- <log_dir></log_dir>
- <output_dir></output_dir>
- <actions>
- <action type="start" delay="00:00:00" />
- <action type="start" delay="end" />
- </actions>
- </instance>
</swarm>