BootUpdateNode logs event only when a change occurs to avoid spamming the DB
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 1 Apr 2010 11:00:58 +0000 (11:00 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 1 Apr 2010 11:00:58 +0000 (11:00 +0000)
PLC/Methods/BootUpdateNode.py

index 7d8949c..d69f162 100644 (file)
@@ -27,21 +27,35 @@ class BootUpdateNode(Method):
         Mixed(BootAuth(), SessionAuth()),
         {'boot_state': Node.fields['boot_state'],
          'primary_network': interface_fields,
-         'ssh_host_key': Node.fields['ssh_rsa_key']}
-        ]
+         ### BEWARE that the expected formerly did not match the native Node field
+         # support both for now
+         'ssh_rsa_key': Node.fields['ssh_rsa_key'],
+         'ssh_host_key': Node.fields['ssh_rsa_key'],
+         }]
 
     returns = Parameter(int, '1 if successful')
 
-    # xxx this method is a bit spamming the events log
-    #    todo : log only when a change occurs
-    # also this seems to expect the user-provided node_fields to optionally 
-    #    contain the 'primary_network' key that should be renamed into 'primary_interface'
     def call(self, auth, node_fields):
+
+        if not isinstance(self.caller, Node):
+            raise PLCInvalidArgument,"Caller is expected to be a node"
+
+        node = self.caller
+
+        # log this event only if a change occured
+        # otherwise the db gets spammed with meaningless entries
+        changed_fields = []
         # Update node state
         if node_fields.has_key('boot_state'):
-            self.caller['boot_state'] = node_fields['boot_state']
+            if node['boot_state'] != node_fields['boot_state']: changed_fields.append('boot_state')
+            node['boot_state'] = node_fields['boot_state']
+        ### for legacy BootManager
         if node_fields.has_key('ssh_host_key'):
-            self.caller['ssh_rsa_key'] = node_fields['ssh_host_key']
+            if node['ssh_rsa_key'] != node_fields['ssh_host_key']: changed_fields.append('ssh_rsa_key')
+            node['ssh_rsa_key'] = node_fields['ssh_host_key']
+        if node_fields.has_key('ssh_rsa_key'):
+            if node['ssh_rsa_key'] != node_fields['ssh_rsa_key']: changed_fields.append('ssh_rsa_key')
+            node['ssh_rsa_key'] = node_fields['ssh_rsa_key']
 
         # Update primary interface state
         if node_fields.has_key('primary_network'):
@@ -49,7 +63,7 @@ class BootUpdateNode(Method):
 
             if 'interface_id' not in primary_network:
                 raise PLCInvalidArgument, "Interface not specified"
-            if primary_network['interface_id'] not in self.caller['interface_ids']:
+            if primary_network['interface_id'] not in node['interface_ids']:
                 raise PLCInvalidArgument, "Interface not associated with calling node"
 
             interfaces = Interfaces(self.api, [primary_network['interface_id']])
@@ -61,15 +75,18 @@ class BootUpdateNode(Method):
                 raise PLCInvalidArgument, "Not the primary interface on record"
 
             interface_fields = dict(filter(can_update, primary_network.items()))
+            for field in interface_fields:
+                if interface[field] != primary_network[field] : changed_fields.append('Interface.'+field)
             interface.update(interface_fields)
             interface.sync(commit = False)
 
         # indicate that node has booted & contacted PLC.
-        if isinstance(self.caller, Node):
-            node = self.caller
-            node.update_last_contact()
+        node.update_last_contact()
+
+        node.sync(commit = True)
 
-        self.caller.sync(commit = True)
-        self.message = "Node updated: %s" % ", ".join(node_fields.keys())
+        if changed_fields:
+            self.message = "Boot updated: %s" % ", ".join(changed_fields)
+            self.event_objects = { 'Node' : [node['node_id']] }
 
         return 1