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
10 can_update = lambda (field, value): field in \
11 ['method', 'mac', 'gateway', 'network',
12 'broadcast', 'netmask', 'dns1', 'dns2']
14 class BootUpdateNode(Method):
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.
19 Returns 1 if updated successfully.
24 interface_fields = dict(filter(can_update, Interface.fields.items()))
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'],
36 returns = Parameter(int, '1 if successful')
38 def call(self, auth, node_fields):
40 if not isinstance(self.caller, Node):
41 raise PLCInvalidArgument,"Caller is expected to be a node"
45 # log this event only if a change occured
46 # otherwise the db gets spammed with meaningless entries
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']
60 # Update primary interface state
61 if node_fields.has_key('primary_network'):
62 primary_network = node_fields['primary_network']
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"
69 interfaces = Interfaces(self.api, [primary_network['interface_id']])
71 raise PLCInvalidArgument, "No such interface %r"%interface_id
72 interface = interfaces[0]
74 if not interface['is_primary']:
75 raise PLCInvalidArgument, "Not the primary interface on record"
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)
83 # indicate that node has booted & contacted PLC.
84 node.update_last_contact()
86 node.sync(commit = True)
89 self.message = "Boot updated: %s" % ", ".join(changed_fields)
90 self.event_objects = { 'Node' : [node['node_id']] }