(*) Peer has new fields person_ids and site_ids
[plcapi.git] / PLC / Methods / RefreshPeer.py
index 099b737..4d5d114 100644 (file)
@@ -11,93 +11,90 @@ 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 = Parameter(dict, 
+                        'new_sites '
+                        'new_keys '
+                        'new_nodes '
+                        'new_persons '
+                        'new_slice_attribute_types '
+                        'new_slices '
+                        'new_slice_attributes '
+                        'timers ')
 
-    def call (self, auth, peer_id):
+    def call (self, auth, peer_id_or_peername):
        
        ### retrieve peer info
-       peers = Peers (self.api)
-       peer = peers[peer_id]
+       peers = Peers (self.api,[peer_id_or_peername])
+        try:
+            peer=peers[0]
+        except:
+            raise PLCInvalidArgument,'RefreshPeer: no such peer:%r'%peer_id_or_peername
        
        ### retrieve account info
-       person_id = peer['person_id']
-       persons = Persons (self.api,[person_id])
-       person = persons[person_id]
+       auth_person_id = peer['auth_person_id']
+       persons = Persons (self.api,[auth_person_id])
+        try:
+            person = persons[0]
+        except:
+            raise PLCInvalidArgument,'RefreshPeer: no such person_id:%d'%auth_person_id
        
+       ## connect to the peer's API
+        url=peer['peer_url']
+       apiserver = xmlrpclib.ServerProxy (url,allow_none=True)
+
        ### build up foreign auth
        auth={ 'Username': person['email'],
               'AuthMethod' : 'password',
               'AuthString' : person['password'],
-              'Role' : 'admin' }
-
-       ## 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}
-
-#        current_peer_nodes = [n21,n22]
-
-       ### 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
+              'Role' : 'admin' ,
+               }
 
-        ### these fields get copied through
-        remote_fields = ['boot_state','model','version','date_created','date_updated']
+        # xxx
+        # right now we *need* the remote peer to know our name
+        # (this is used in the GetPeerData that we issue)
+        # in general this will be true
+        # however if anyone decides to change its own plc name things can get wrong
+        # doing this should ensure things become right again after some iterations
+        # that is, hopefully
+        # might wish to change this policy once we have peer authentication, maybe
         
-       ### 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()
-
-       ### delete entries that are not uptodate
-       [ x.delete() for x in foreign_nodes.values() if not x.uptodate ]
-       
+        # make sure we have the right name for that peer
+        peername = apiserver.GetPeerName (auth)
+        peer.update_name(peername)
+
+        # we need a peer_id from there on
+        peer_id = peer['peer_id']
+
+        print 'Got peer_id',peer_id
+
+       cache = Cache (self.api, peer_id, apiserver, auth)
+        result = cache.refresh_peer ()
+        result['peername']=peername
+
+        return result