- sql = "DELETE FROM peer_node WHERE peer_id=%d AND node_id=%d" % (peer_id,node_id)
- self.api.db.do(sql)
- self['node_ids'].remove(node_id)
-
- if commit:
- self.api.db.commit()
-
- def refresh_nodes (self, peer_get_nodes):
- """
- refreshes the foreign_nodes and peer_node tables
- expected input is the current list of nodes as returned by GetNodes
-
- returns the number of new nodes on this peer (can be negative)
- """
-
- 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_nodes = ForeignNodes (self.api)
- # new to index it by hostname for searching later
- local_foreign_nodes_index = local_foreign_nodes.dict('hostname')
-
- ### mark entries for this peer outofdate
- old_count=0;
- for foreign_node in local_foreign_nodes:
- if foreign_node['peer_id'] == peer_id:
- foreign_node.uptodate=False
- old_count += 1
-
- ### these fields get copied through
- remote_fields = ['boot_state','model','version','date_created','last_updated']
-
- ### scan the new entries, and mark them uptodate
- for node in peer_get_nodes:
- hostname = node['hostname']
- foreign_id = node ['node_id']
- try:
- foreign_node = local_foreign_nodes_index[hostname]
- if foreign_node['peer_id'] != peer_id:
- ### the node has changed its plc, needs to update peer_node
- old_peer_id = foreign_node['peer_id']
- old_peers=Peers(self.api,[peer_id])
- assert old_peer[0]
- # remove from previous peer
- old_peers[0].manage_node(foreign_node,None,False)
- # add to new peer
- self.manage_node(foreign_node,foreign_id,True)
- foreign_node['peer_id'] = peer_id
- ### update it anyway: copy other relevant fields
- for field in remote_fields:
- foreign_node[field]=node[field]
- # this row is now valid
- foreign_node.uptodate=True
- foreign_node.sync()
- except:
- new_foreign_node = ForeignNode(self.api, {'hostname':hostname})
- for field in remote_fields:
- new_foreign_node[field]=node[field]
- ### need to sync so we get a node_id
- new_foreign_node.sync()
- new_foreign_node.uptodate = True
- self.manage_node(new_foreign_node,foreign_id,True)
- local_foreign_nodes_index[hostname]=new_foreign_node
-
- ### delete entries that are not uptodate
- for foreign_node in local_foreign_nodes:
- if not foreign_node.uptodate:
- foreign_node.delete()
-
- return len(peer_get_nodes)-old_count
-
- def refresh_slices (self, peer_get_slices):
- return 0
-
-