d69f1623c3443587d137ffc469281859afb1478c
[plcapi.git] / PLC / Methods / BootUpdateNode.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Auth import Auth, BootAuth, SessionAuth
7 from PLC.Nodes import Node, Nodes
8 from PLC.Interfaces import Interface, Interfaces
9
10 can_update = lambda (field, value): field in \
11              ['method', 'mac', 'gateway', 'network',
12               'broadcast', 'netmask', 'dns1', 'dns2']
13
14 class BootUpdateNode(Method):
15     """
16     Allows the calling node to update its own record. Only the primary
17     network can be updated, and the node IP cannot be changed.
18
19     Returns 1 if updated successfully.
20     """
21
22     roles = ['node']
23
24     interface_fields = dict(filter(can_update, Interface.fields.items()))
25
26     accepts = [
27         Mixed(BootAuth(), SessionAuth()),
28         {'boot_state': Node.fields['boot_state'],
29          'primary_network': interface_fields,
30          ### BEWARE that the expected formerly did not match the native Node field
31          # support both for now
32          'ssh_rsa_key': Node.fields['ssh_rsa_key'],
33          'ssh_host_key': Node.fields['ssh_rsa_key'],
34          }]
35
36     returns = Parameter(int, '1 if successful')
37
38     def call(self, auth, node_fields):
39
40         if not isinstance(self.caller, Node):
41             raise PLCInvalidArgument,"Caller is expected to be a node"
42
43         node = self.caller
44
45         # log this event only if a change occured
46         # otherwise the db gets spammed with meaningless entries
47         changed_fields = []
48         # Update node state
49         if node_fields.has_key('boot_state'):
50             if node['boot_state'] != node_fields['boot_state']: changed_fields.append('boot_state')
51             node['boot_state'] = node_fields['boot_state']
52         ### for legacy BootManager
53         if node_fields.has_key('ssh_host_key'):
54             if node['ssh_rsa_key'] != node_fields['ssh_host_key']: changed_fields.append('ssh_rsa_key')
55             node['ssh_rsa_key'] = node_fields['ssh_host_key']
56         if node_fields.has_key('ssh_rsa_key'):
57             if node['ssh_rsa_key'] != node_fields['ssh_rsa_key']: changed_fields.append('ssh_rsa_key')
58             node['ssh_rsa_key'] = node_fields['ssh_rsa_key']
59
60         # Update primary interface state
61         if node_fields.has_key('primary_network'):
62             primary_network = node_fields['primary_network'] 
63
64             if 'interface_id' not in primary_network:
65                 raise PLCInvalidArgument, "Interface not specified"
66             if primary_network['interface_id'] not in node['interface_ids']:
67                 raise PLCInvalidArgument, "Interface not associated with calling node"
68
69             interfaces = Interfaces(self.api, [primary_network['interface_id']])
70             if not interfaces:
71                 raise PLCInvalidArgument, "No such interface %r"%interface_id
72             interface = interfaces[0]
73
74             if not interface['is_primary']:
75                 raise PLCInvalidArgument, "Not the primary interface on record"
76
77             interface_fields = dict(filter(can_update, primary_network.items()))
78             for field in interface_fields:
79                 if interface[field] != primary_network[field] : changed_fields.append('Interface.'+field)
80             interface.update(interface_fields)
81             interface.sync(commit = False)
82
83         # indicate that node has booted & contacted PLC.
84         node.update_last_contact()
85
86         node.sync(commit = True)
87
88         if changed_fields:
89             self.message = "Boot updated: %s" % ", ".join(changed_fields)
90             self.event_objects = { 'Node' : [node['node_id']] }
91
92         return 1