5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Auth import Auth, BootAuth, SessionAuth
9 from PLC.Nodes import Node, Nodes
10 from PLC.Interfaces import Interface, Interfaces
11 from PLC.Timestamp import *
13 can_update = lambda (field, value): field in \
14 ['method', 'mac', 'gateway', 'network',
15 'broadcast', 'netmask', 'dns1', 'dns2']
17 class BootUpdateNode(Method):
19 Allows the calling node to update its own record. Only the primary
20 network can be updated, and the node IP cannot be changed.
22 Returns 1 if updated successfully.
27 interface_fields = dict(filter(can_update, Interface.fields.items()))
30 Mixed(BootAuth(), SessionAuth()),
31 {'boot_state': Node.fields['boot_state'],
32 'primary_network': interface_fields,
33 ### BEWARE that the expected formerly did not match the native Node field
34 # support both for now
35 'ssh_rsa_key': Node.fields['ssh_rsa_key'],
36 'ssh_host_key': Node.fields['ssh_rsa_key'],
39 returns = Parameter(int, '1 if successful')
41 def call(self, auth, node_fields):
43 if not isinstance(self.caller, Node):
44 raise PLCInvalidArgument,"Caller is expected to be a node"
48 # log this event only if a change occured
49 # otherwise the db gets spammed with meaningless entries
52 if node_fields.has_key('boot_state'):
53 if node['boot_state'] != node_fields['boot_state']: changed_fields.append('boot_state')
54 node['boot_state'] = node_fields['boot_state']
55 ### for legacy BootManager
56 if node_fields.has_key('ssh_host_key'):
57 if node['ssh_rsa_key'] != node_fields['ssh_host_key']: changed_fields.append('ssh_rsa_key')
58 node['ssh_rsa_key'] = node_fields['ssh_host_key']
59 if node_fields.has_key('ssh_rsa_key'):
60 if node['ssh_rsa_key'] != node_fields['ssh_rsa_key']: changed_fields.append('ssh_rsa_key')
61 node['ssh_rsa_key'] = node_fields['ssh_rsa_key']
63 # Update primary interface state
64 if node_fields.has_key('primary_network'):
65 primary_network = node_fields['primary_network']
67 if 'interface_id' not in primary_network:
68 raise PLCInvalidArgument, "Interface not specified"
69 if primary_network['interface_id'] not in node['interface_ids']:
70 raise PLCInvalidArgument, "Interface not associated with calling node"
72 interfaces = Interfaces(self.api, [primary_network['interface_id']])
74 raise PLCInvalidArgument, "No such interface %r"%interface_id
75 interface = interfaces[0]
77 if not interface['is_primary']:
78 raise PLCInvalidArgument, "Not the primary interface on record"
80 interface_fields = dict(filter(can_update, primary_network.items()))
81 for field in interface_fields:
82 if interface[field] != primary_network[field] : changed_fields.append('Interface.'+field)
83 interface.update(interface_fields)
84 interface.sync(commit = False)
86 # indicate that node has booted & contacted PLC.
87 node.update_last_contact()
88 node.update_last_boot()
90 current_time = int(time.time())
91 # if last_pcu_reboot is within 20 minutes of current_time, accept that the PCU is responsible
92 if node['last_pcu_reboot'] and Timestamp.cast_long(node['last_pcu_reboot']) >= current_time - 60*20:
93 node.update_last_pcu_confirmation(commit=False)
95 node.sync(commit = True)
98 self.message = "Boot updated: %s" % ", ".join(changed_fields)
99 self.event_objects = { 'Node' : [node['node_id']] }