# Thierry Parmentelat - INRIA
#
-import xmlrpclib
-
from PLC.Faults import *
from PLC.Method import Method
from PLC.Parameter import Parameter, Mixed
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 the number of new nodes from that peer - may be negative
+ 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 = Parameter(int, "Delta in number of foreign nodes attached to that peer")
+ 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_id])
+ def call (self, auth, peer_id_or_peername):
+ peers = Peers(self.api, [peer_id_or_peername])
if not peers:
- raise PLCInvalidArgument,'no such peer_id:%d'%peer_id
- peer=peers[0]
-
- ### retrieve account info
- person_id = peer['person_id']
- persons = Persons (self.api,[person_id])
- if not persons:
- raise PLCInvalidArgument,'no such person_id:%d'%person_id
- person = persons[0]
-
- ### build up foreign auth
- auth={ 'Username': person['email'],
- 'AuthMethod' : 'password',
- 'AuthString' : person['password'],
- 'Role' : 'admin' }
+ raise PLCInvalidArgument, "No such peer '%s'" % unicode(peer_id_or_peername)
+ peer = peers[0]
- ## connect to the peer's API
- url=peer['peer_url']+"/PLCAPI/"
- print 'url=',url
- apiserver = xmlrpclib.Server (url)
- print 'auth=',auth
- current_peer_nodes = apiserver.GetNodes(auth)
- print 'current_peer_nodes',current_peer_nodes
+ # Connect to peer API
+ peer.connect()
- ## manual feed for tests
- 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}
+ # Update peer name
+ peername = peer.GetPeerName()
+ if peer['peername'] != peername:
+ peer['peername'] = peername
+ peer.sync()
-# current_peer_nodes = []
-# print 'current_peer_nodes',current_peer_nodes
+ cache = Cache(self.api, peer['peer_id'], peer)
+ result = cache.refresh_peer()
- nb_new_nodes = peer.refresh_nodes(current_peer_nodes)
+ # Add peer name to result set
+ result['peername'] = peername
- return nb_new_nodes
+ return result