- peer_id = self['peer_id']
-
- # we get the whole table just in case
- # a host would have switched from one plc to the other
- local_foreign_slices = Slices (self.api,{'~peer_id':None})
- # index it by name for searching later
- local_foreign_slices_index = local_foreign_slices.dict('name')
-
- ### mark entries for this peer outofdate
- old_count=0;
- for foreign_slice in local_foreign_slices:
- if foreign_slice['peer_id'] == peer_id:
- foreign_slice.uptodate=False
- old_count += 1
-
- ### these fields get copied through
- remote_fields = ['instantiation', 'url', 'description',
- 'max_nodes', 'created', 'expires']
-
- ### scan the new entries, and mark them uptodate
- new_count=0
- for slice in peer_get_slices:
-
- ### ignore system-wide slices
- if slice['creator_person_id'] == 1:
- continue
-
- name = slice['name']
-
- # create or update
- try:
- foreign_slice = local_foreign_slices_index[name]
- if foreign_slice['peer_id'] != peer_id:
- # more suspucious ? - the slice moved on another peer
- foreign_slice['peer_id'] = peer_id;
- except:
- foreign_slice = Slice(self.api, {'name':name})
- foreign_slice['peer_id']=self['peer_id']
-# ### xxx temporary
-# foreign_slice['site_id']=1
- ### need to sync so we get a slice_id
- foreign_slice.sync()
- # insert in index
- local_foreign_slices_index[name]=foreign_slice
-
- # go on with update
- for field in remote_fields:
- foreign_slice[field]=slice[field]
- # this row is now valid
- foreign_slice.uptodate=True
- new_count += 1
- foreign_slice.sync()
-
- ### handle node_ids
- # in slice we get a set of node_ids
- # but these ids are RELATIVE TO THE PEER
- # so we need to figure the local node_id for these nodes
- # we do this through peer_foreign_nodes
- # dictify once
- peer_foreign_nodes_dict = {}
- for foreign_node in peer_foreign_nodes:
- peer_foreign_nodes_dict[foreign_node['node_id']]=foreign_node
- updated_node_ids = []
- for alien_node_id in slice['node_ids']:
- try:
- local_node_id=self.locate_alien_node_id_in_foreign_nodes(peer_foreign_nodes_dict,
- alien_node_id)
- updated_node_ids.append(local_node_id)
- except:
- # this node_id is not in our scope
- pass
- foreign_slice.update_slice_nodes (updated_node_ids)
-
- ### delete entries that are not uptodate
- for foreign_slice in local_foreign_slices:
- if not foreign_slice.uptodate:
- foreign_slice.delete()
-
- return new_count-old_count
+ add = Row.add_object(Node, 'peer_node')
+ add(self, node,
+ {'peer_id': self['peer_id'],
+ 'node_id': node['node_id'],
+ 'peer_node_id': peer_node_id},
+ commit = commit)
+
+ # calling UpdateNode with the node's hostname
+ # will force the 'hrn' tag to be updated with the
+ # correct root auth
+ shell = Shell()
+ UpdateNode = self.api.callable('UpdateNode')
+ UpdateNode(shell.auth, node['node_id'], {'hostname': node['hostname']})
+
+ def remove_node(self, node, commit = True):
+ """
+ Unassociate a node with this peer.
+ """
+
+ remove = Row.remove_object(Node, 'peer_node')
+ remove(self, node, commit)
+
+ # calling UpdateNode with the node's hostname
+ # will force the 'hrn' tag to be updated with the
+ # correct root auth
+ shell = Shell()
+ UpdateNode = self.api.callable('UpdateNode')
+ UpdateNode(shell.auth, node['node_id'], {'hostname': node['hostname']})
+
+ def add_slice(self, slice, peer_slice_id, commit = True):
+ """
+ Associate a local slice entry with this peer.
+ """
+
+ add = Row.add_object(Slice, 'peer_slice')
+ add(self, slice,
+ {'peer_id': self['peer_id'],
+ 'slice_id': slice['slice_id'],
+ 'peer_slice_id': peer_slice_id},
+ commit = commit)
+
+ def remove_slice(self, slice, commit = True):
+ """
+ Unassociate a slice with this peer.
+ """
+
+ remove = Row.remove_object(Slice, 'peer_slice')
+ remove(self, slice, commit)
+
+ def connect(self, **kwds):
+ """
+ Connect to this peer via XML-RPC.
+ """
+
+ import xmlrpclib
+ from PLC.PyCurl import PyCurlTransport
+ self.server = xmlrpclib.ServerProxy(self['peer_url'],
+ PyCurlTransport(self['peer_url'], self['cacert']),
+ allow_none = 1, **kwds)
+
+ def add_auth(self, function, methodname, **kwds):
+ """
+ Sign the specified XML-RPC call and add an auth struct as the
+ first argument of the call.
+ """
+
+ def wrapper(*args, **kwds):
+ from PLC.GPG import gpg_sign
+ signature = gpg_sign(args,
+ self.api.config.PLC_ROOT_GPG_KEY,
+ self.api.config.PLC_ROOT_GPG_KEY_PUB,
+ methodname)
+
+ auth = {'AuthMethod': "gpg",
+ 'name': self.api.config.PLC_NAME,
+ 'signature': signature}
+
+ # Automagically add auth struct to every call
+ args = (auth,) + args
+
+ return function(*args)
+
+ return wrapper
+
+ def __getattr__(self, attr):
+ """
+ Returns a callable API function if attr is the name of a
+ PLCAPI function; otherwise, returns the specified attribute.
+ """
+
+ try:
+ # Figure out if the specified attribute is the name of a
+ # PLCAPI function. If so and the function requires an
+ # authentication structure as its first argument, return a
+ # callable that automagically adds an auth struct to the
+ # call.
+ methodname = attr
+ api_function = self.api.callable(methodname)
+ if api_function.accepts and \
+ (isinstance(api_function.accepts[0], PLC.Auth.Auth) or \
+ (isinstance(api_function.accepts[0], Mixed) and \
+ filter(lambda param: isinstance(param, Auth), api_function.accepts[0]))):
+ function = getattr(self.server, methodname)
+ return self.add_auth(function, methodname)
+ except Exception, err:
+ pass
+
+ if hasattr(self, attr):
+ return getattr(self, attr)
+ else:
+ raise AttributeError, "type object 'Peer' has no attribute '%s'" % attr