- be a little more verbose in the returns doc
[plcapi.git] / PLC / Methods / RefreshPeer.py
index 099b737..5ab4bcf 100644 (file)
@@ -2,8 +2,6 @@
 # Thierry Parmentelat - INRIA
 # 
 
-import xmlrpclib
-
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
@@ -11,93 +9,62 @@ from PLC.Auth import Auth
 
 from PLC.Peers import Peer, Peers
 from PLC.Persons import Person, Persons
-from PLC.ForeignNodes import ForeignNode, ForeignNodes
 
+from PLC.Cache import Cache
 
 class RefreshPeer(Method):
     """
-    Query a peer PLC for its list of nodes, and refreshes
-    the local database accordingly
-    
-    Returns None
+    First queries a remote PLC for its name and updates the local peers table
+
+    Then proceeds with fetching objects on the remote PLC, and updates the
+    local database accordingly
+
+    It requires the remote peer to be aware of our own name (as configured in PLC_NAME)
+
+    Returns a dict containing
+    (*) 'peername' :   the peer's name
+    (*) 'new_xxx':     the number of new objects from that peer - may be negative
+    (*) 'timers':      various stats on performance for optimization
+
     """
     
     roles = ['admin']
     
     accepts = [ Auth(),
-               Parameter (int, "Peer id") ]
+                Mixed(Peer.fields['peer_id'],
+                      Peer.fields['peername']),
+                ]
     
-    returns = None
+    returns = {
+        'new_sites': Parameter([dict], "List of new sites"),
+        'new_keys': Parameter([dict], "List of new keys"),
+        'new_nodes': Parameter([dict], "List of new nodes"),
+        'new_persons': Parameter([dict], "List of new users"),
+        'new_slice_attribute_types': Parameter([dict], "List of new slice attribute types"),
+        'new_slices': Parameter([dict], "List of new slices"),
+        'new_slice_attributes': Parameter([dict], "List of new slice attributes"),
+        'timers': Parameter(dict, "(Debug) Timing information"),
+        }
 
-    def call (self, auth, peer_id):
-       
-       ### retrieve peer info
-       peers = Peers (self.api)
-       peer = peers[peer_id]
-       
-       ### retrieve account info
-       person_id = peer['person_id']
-       persons = Persons (self.api,[person_id])
-       person = persons[person_id]
-       
-       ### build up foreign auth
-       auth={ 'Username': person['email'],
-              'AuthMethod' : 'password',
-              'AuthString' : person['password'],
-              'Role' : 'admin' }
+    def call (self, auth, peer_id_or_peername):
+       peers = Peers(self.api, [peer_id_or_peername])
+        if not peers:
+            raise PLCInvalidArgument, "No such peer '%s'" % unicode(peer_id_or_peername)
+        peer = peers[0]
 
-       ## connect to the peer's API
-       apiserver = xmlrpclib.Server (peer['peer_url']+"/PLCAPI/")
-       print 'auth',auth
-       current_peer_nodes = apiserver.GetNodes(auth,[])
-       
-       ## manual feed for tests
-#      n1 = {'hostname': 'n1.plc', 'boot_state': 'inst'}
-#      n2 = {'hostname': 'n2.plc', 'boot_state': 'inst'}
-#      n3 = {'hostname': 'n3.plc', 'boot_state': 'inst'}
-        n11={'session': None, 'slice_ids': [], 'nodegroup_ids': [], 'last_updated': 1162884349, 'version': None, 'nodenetwork_ids': [], 'boot_state': 'inst', 'hostname': 'n11.plc1.org', 'site_id': 1, 'ports': None, 'pcu_ids': [], 'boot_nonce': None, 'node_id': 1, 'root_person_ids': [], 'key': None, 'date_created': 1162884349, 'model': None, 'conf_file_ids': [], 'ssh_rsa_key': None}
-        n12={'session': None, 'slice_ids': [], 'nodegroup_ids': [], 'last_updated': 1162884349, 'version': None, 'nodenetwork_ids': [], 'boot_state': 'inst', 'hostname': 'n12.plc1.org', 'site_id': 1, 'ports': None, 'pcu_ids': [], 'boot_nonce': None, 'node_id': 1, 'root_person_ids': [], 'key': None, 'date_created': 1162884349, 'model': None, 'conf_file_ids': [], 'ssh_rsa_key': None}
-        n21={'session': None, 'slice_ids': [], 'nodegroup_ids': [], 'last_updated': 1162884349, 'version': None, 'nodenetwork_ids': [], 'boot_state': 'boot', 'hostname': 'n21.plc2.org', 'site_id': 1, 'ports': None, 'pcu_ids': [], 'boot_nonce': None, 'node_id': 1, 'root_person_ids': [], 'key': None, 'date_created': 1162884349, 'model': None, 'conf_file_ids': [], 'ssh_rsa_key': None}
-        n22={'session': None, 'slice_ids': [], 'nodegroup_ids': [], 'last_updated': 1162884349, 'version': None, 'nodenetwork_ids': [], 'boot_state': 'boot', 'hostname': 'n22.plc2.org', 'site_id': 1, 'ports': None, 'pcu_ids': [], 'boot_nonce': None, 'node_id': 1, 'root_person_ids': [], 'key': None, 'date_created': 1162884349, 'model': None, 'conf_file_ids': [], 'ssh_rsa_key': None}
+       # Connect to peer API
+        peer.connect()
 
-#        current_peer_nodes = [n21,n22]
+        # Update peer name
+        peername = peer.GetPeerName()
+        if peer['peername'] != peername:
+            peer['peername'] = peername
+            peer.sync()
 
-       ### now to the db
-       # we get the whole table just in case 
-       # a host would have switched from one plc to the other
-       foreign_nodes = ForeignNodes (self.api)
-       
-       ### mark entries for this peer outofdate
-       for foreign_node in foreign_nodes.values():
-           if foreign_node['peer_id'] == peer_id:
-               foreign_node.uptodate=False
+       cache = Cache(self.api, peer['peer_id'], peer)
+        result = cache.refresh_peer()
 
-        ### these fields get copied through
-        remote_fields = ['boot_state','model','version','date_created','date_updated']
-        
-       ### scan the new entries, and mark them uptodate
-       for node in current_peer_nodes:
-           hostname = node['hostname']
-           foreign_node = foreign_nodes.get(hostname)
-           if foreign_node:
-               ### update it anyway
-                foreign_node['cached'] = True
-               foreign_node['peer_id'] = peer_id
-                # copy other relevant fields
-                for field in remote_fields:
-                    foreign_node[field]=node[field]
-                # this row is valid
-               foreign_node.uptodate = True
-           else:
-               foreign_nodes[hostname] = ForeignNode(self.api,
-                                                     {'hostname':hostname,
-                                                       'cached':True,
-                                                      'peer_id':peer_id,})
-                for field in remote_fields:
-                    foreign_nodes[hostname][field]=node[field]
-                    
-           foreign_nodes[hostname].sync()
+        # Add peer name to result set
+        result['peername'] = peername
 
-       ### delete entries that are not uptodate
-       [ x.delete() for x in foreign_nodes.values() if not x.uptodate ]
-       
+        return result