first rough version of an SFA scanner
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 8 Mar 2011 15:44:33 +0000 (16:44 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 8 Mar 2011 15:44:33 +0000 (16:44 +0100)
given one or several entrypoints, draws a graph of the connected bodies

sfa/client/Makefile [new file with mode: 0644]
sfa/client/sfascan.py
sfa/plc/remoteshell.py

diff --git a/sfa/client/Makefile b/sfa/client/Makefile
new file mode 100644 (file)
index 0000000..d9087da
--- /dev/null
@@ -0,0 +1,60 @@
+BUNDLES += http://planet-lab.eu:12345/@sfa-ple-reg 
+BUNDLES += http://planet-lab.eu:12346/@sfa-ple-am
+BUNDLES += http://planet-lab.eu:12347/@sfa-ple-sa
+BUNDLES += http://planet-lab.org:12345/@sfa-plc-reg 
+BUNDLES += http://planet-lab.org:12346/@sfa-plc-am
+BUNDLES += http://planet-lab.org:12347/@sfa-plc-sa
+BUNDLES += http://planet-lab.kr:12345/@sfa-ppk-reg 
+BUNDLES += http://planet-lab.kr:12346/@sfa-ppk-am
+BUNDLES += http://planet-lab.kr:12347/@sfa-ppk-sa
+BUNDLES += http://planet-lab.jp:12345/@sfa-plj-reg 
+BUNDLES += http://planet-lab.jp:12346/@sfa-plj-am
+BUNDLES += http://planet-lab.jp:12347/@sfa-plj-sa
+BUNDLES += http://geni-myvini.umkc.gpeni.net:12345/@sfa-gpeni-reg
+BUNDLES += http://geni-myvini.umkc.gpeni.net:12346/@sfa-gpeni-am
+BUNDLES += http://geni-myvini.umkc.gpeni.net:12347/@sfa-gpeni-sa
+BUNDLES += http://198.248.241.100:12345/@sfa-glc-reg
+BUNDLES += http://198.248.241.100:12346/@sfa-glc-am
+BUNDLES += http://198.248.241.100:12347/@sfa-glc-sa
+
+EXTENSIONS := png svg
+
+define bundle_scan_target
+$(word 2,$(subst @, ,$(1))):
+       ./sfascan.py -e $(word 1,$(subst @, ,$(1))) $(foreach extension,$(EXTENSIONS),-o $(word 2,$(subst @, ,$(1))).$(extension)) >& $(word 2,$(subst @, ,$(1))).out
+.PHONY: $(word 2,$(subst @, ,$(1)))
+endef
+
+ALL := $(foreach bundle,$(BUNDLES),$(word 2,$(subst @, ,$(bundle))))
+
+all: $(ALL)
+
+# the actual targets
+$(foreach bundle,$(BUNDLES),$(eval $(call bundle_scan_target,$(bundle))))
+
+####################
+
+define bundle_version_target
+$(word 2,$(subst @, ,$(1))).version:
+       ./sfi.py -s $(word 1,$(subst @, ,$(1))) version >& $$@
+endef
+
+VERSIONS := $(foreach bundle,$(BUNDLES),$(word 2,$(subst @, ,$(bundle))).version)
+
+versions: $(VERSIONS)
+clean-versions: 
+       rm -f $(VERSIONS)
+
+# the actual targets
+$(foreach bundle,$(BUNDLES),$(eval $(call bundle_version_target,$(bundle))))
+
+
+#################### convenience, for debugging only
+# make +foo : prints the value of $(foo)
+# make ++foo : idem but verbose, i.e. foo=$(foo)
+++%: varname=$(subst +,,$@)
+++%:
+       @echo "$(varname)=$($(varname))"
++%: varname=$(subst +,,$@)
++%:
+       @echo "$($(varname))"
index 0adca4e..639a604 100755 (executable)
@@ -1,16 +1,20 @@
 #!/usr/bin/python
 
+import sys
 import socket
 import re
 
+import pygraphviz
+
+from optparse import OptionParser
+
 from sfa.client.sfi import Sfi
 from sfa.util.sfalogging import sfa_logger,sfa_logger_goes_to_console
 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
 
-m_url_with_proto=re.compile("\w+://(?P<hostname>[\w\-\.]+):(?P<port>[0-9]+)/.*")
+m_url_with_proto=re.compile("\w+://(?P<hostname>[\w\-\.]+):(?P<port>[0-9]+).*")
 m_url_without_proto=re.compile("(?P<hostname>[\w\-\.]+):(?P<port>[0-9]+).*")
 def url_to_hostname_port (url):
-    print 'url',url
     match=m_url_with_proto.match(url)
     if match:
         return (match.group('hostname'),match.group('port'))
@@ -21,9 +25,8 @@ def url_to_hostname_port (url):
 
 ###
 class Interface:
-    def __init__ (self,url,name=None):
-        self.names=[]
-        self.set_name(name)
+
+    def __init__ (self,url):
         try:
             (self.hostname,self.port)=url_to_hostname_port(url)
             self.ip=socket.gethostbyname(self.hostname)
@@ -36,15 +39,11 @@ class Interface:
             self.probed=True
             self._version={}
 
-    def equal (self,against):
-        return (self.ip == against.ip) and (self.port == against.port)
-
-    def set_name (self,name):
-        if name and name not in self.names: 
-            self.names.append(name)
-
     def url(self):
-        return "http://%s:%s"%(self.hostname,self.port)
+        return "http://%s:%s/"%(self.hostname,self.port)
+
+    def uid (self):
+        return "%s:%s"%(self.ip,self.port)
 
     # connect to server and trigger GetVersion
     def get_version(self):
@@ -64,56 +63,119 @@ class Interface:
             sfa_logger().info('issuing get version at %s'%url)
             server=xmlrpcprotocol.get_server(url, key_file, cert_file, options)
             self._version=server.GetVersion()
-#            pdb.set_trace()
+            sfa_logger().info("get_version at %s returned %r"%(url,self._version))
         except:
+            sfa_logger().info("get_version at %s failed"%(url))
             self._version={}
         self.probed=True
         return self._version
 
+    abbrevs = {"registry": "REG", "slicemgr":"SM", "aggregate":"AM"}
+    shapes = {"registry": "diamond", "slicemgr":"ellipse", "aggregate":"box", 'default':'plaintext'}
+
+    def get_name(self):
+        version=self.get_version()
+        if 'hrn' not in version: return self.url()
+        hrn=version['hrn']
+        interface=version['interface']
+        abbrev=Interface.abbrevs.get(interface,"XXX")
+        result="%s %s"%(hrn,abbrev)
+        if 'code_tag' in version: result += " %s"%version['code_tag']
+        return result
+
+    def get_shape(self):
+        default=Interface.shapes['default']
+        try:
+            version=self.get_version()
+            return Interface.shapes.get(version['interface'],default)
+        except:
+            return default
+
 class SfaScan:
 
     # provide the entry points (a list of interfaces)
     def __init__ (self):
         pass
 
+    def graph (self,entry_points):
+        graph=pygraphviz.AGraph(directed=True)
+        self.scan(entry_points,graph)
+        return graph
+    
     # scan from the given interfaces as entry points
-    def scan(self,interfaces):
-        import pdb
-#        pdb.set_trace()
+    def scan(self,interfaces,graph):
         if not isinstance(interfaces,list):
             interfaces=[interfaces]
-        # should add nodes, but with what name ?
+
+        # remember node to interface mapping
+        node2interface={}
+        # add entry points right away using the interface uid's as a key
         to_scan=interfaces
+        for i in interfaces: 
+            sfa_logger().info("adding initial node %s"%i.uid())
+            graph.add_node(i.uid())
+            node2interface[graph.get_node(i.uid())]=i
         scanned=[]
-        def was_scanned (interface):
-            for i in scanned:
-                if interface.equal(i): return i
-            return False
         # keep on looping until we reach a fixed point
+        # don't worry about abels and shapes that will get fixed later on
         while to_scan:
             for interface in to_scan:
+                # performing xmlrpc call
                 version=interface.get_version()
-                if 'peers' in version: 
+                if 'sfa' in version: 
+                    # proceed with neighbours
                     for (next_name,next_url) in version['peers'].items():
-                        # should add edge
                         next_interface=Interface(next_url)
-                        seen_interface=was_scanned(next_interface)
-                        if seen_interface:
-                            # record name
-                            seen_interface.set_name(next_name)
-                        else:
-                            sfa_logger().info('adding %s'%next_interface.url())
+                        # locate or create node in graph
+                        try:
+                            # if found, we're good with this one
+                            next_node=graph.get_node(next_interface.uid())
+                        except:
+                            # otherwise, let's move on with it
+                            graph.add_node(next_interface.uid())
+                            next_node=graph.get_node(next_interface.uid())
+                            node2interface[next_node]=next_interface
                             to_scan.append(next_interface)
-                            
+                        graph.add_edge(interface.uid(),next_interface.uid())
                 scanned.append(interface)
                 to_scan.remove(interface)
+            # we've scanned the whole graph, let's get the labels and shapes right
+            for node in graph.nodes():
+                interface=node2interface.get(node,None)
+                if interface:
+                    node.attr['label']=interface.get_name()
+                    node.attr['shape']=interface.get_shape()
+                else:
+                    sfa_logger().info("MISSED interface with node %s"%node)
     
 
+default_entry_points=["http://www.planet-lab.eu:12345/"]
+default_outfiles=['sfa.png']
+
 def main():
     sfa_logger_goes_to_console()
+    parser=OptionParser()
+    parser.add_option("-e","--entry",action='append',dest='entry_points',default=[],
+                      help="Specify entry points - defaults are %r"%default_entry_points)
+    parser.add_option("-o","--output",action='append',dest='outfiles',default=[],
+                      help="Output filenames - defaults are %r"%default_outfiles)
+    (options,args)=parser.parse_args()
+    if args:
+        parser.print_help()
+        sys.exit(1)
+    if not options.entry_points:
+        options.entry_points=default_entry_points
+    if not options.outfiles:
+        options.outfiles=default_outfiles
     scanner=SfaScan()
-    entry=Interface("http://www.planet-lab.eu:12345/")
-    scanner.scan(entry)
+    entries = [ Interface(entry) for entry in options.entry_points ]
+    g=scanner.graph(entries)
+    sfa_logger().info("creating layout")
+    g.layout(prog='dot')
+    for outfile in options.outfiles:
+        sfa_logger().info("drawing in %s"%outfile)
+        g.draw(outfile)
+    sfa_logger().info("done")
 
 if __name__ == '__main__':
     main()
index 6ec5995..9deceb6 100644 (file)
@@ -6,9 +6,6 @@
 # PLC.Shell.Shell(). It's meant to be a drop in replacement for running
 # SFA on a different machine than PLC.
 
-### $Id$
-### $URL$
-
 import xmlrpclib
 
 class RemoteShell: