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 from PLC.Timestamp import *
11 can_update = lambda field_value: field_value[0] in \
12 ['method', 'mac', 'gateway', 'network',
13 'broadcast', 'netmask', 'dns1', 'dns2']
15 class BootUpdateNode(Method):
17 Allows the calling node to update its own record. Only the primary
18 network can be updated, and the node IP cannot be changed.
20 Returns 1 if updated successfully.
25 interface_fields = dict(list(filter(can_update, list(Interface.fields.items()))))
28 Mixed(BootAuth(), SessionAuth()),
29 {'boot_state': Node.fields['boot_state'],
30 'primary_network': interface_fields,
31 ### BEWARE that the expected formerly did not match the native Node field
32 # support both for now
33 'ssh_rsa_key': Node.fields['ssh_rsa_key'],
34 'ssh_host_key': Node.fields['ssh_rsa_key'],
37 returns = Parameter(int, '1 if successful')
39 def call(self, auth, node_fields):
41 if not isinstance(self.caller, Node):
42 raise PLCInvalidArgument("Caller is expected to be a node")
46 # log this event only if a change occured
47 # otherwise the db gets spammed with meaningless entries
50 if 'boot_state' in node_fields:
51 if node['boot_state'] != node_fields['boot_state']: changed_fields.append('boot_state')
52 node['boot_state'] = node_fields['boot_state']
53 ### for legacy BootManager
54 if 'ssh_host_key' in node_fields:
55 if node['ssh_rsa_key'] != node_fields['ssh_host_key']: changed_fields.append('ssh_rsa_key')
56 node['ssh_rsa_key'] = node_fields['ssh_host_key']
57 if 'ssh_rsa_key' in node_fields:
58 if node['ssh_rsa_key'] != node_fields['ssh_rsa_key']: changed_fields.append('ssh_rsa_key')
59 node['ssh_rsa_key'] = node_fields['ssh_rsa_key']
61 # Update primary interface state
62 if 'primary_network' in node_fields:
63 primary_network = node_fields['primary_network']
65 if 'interface_id' not in primary_network:
66 raise PLCInvalidArgument("Interface not specified")
67 if primary_network['interface_id'] not in node['interface_ids']:
68 raise PLCInvalidArgument("Interface not associated with calling node")
70 interfaces = Interfaces(self.api, [primary_network['interface_id']])
72 raise PLCInvalidArgument("No such interface %r"%interface_id)
73 interface = interfaces[0]
75 if not interface['is_primary']:
76 raise PLCInvalidArgument("Not the primary interface on record")
78 interface_fields = dict(list(filter(can_update, list(primary_network.items()))))
79 for field in interface_fields:
80 if interface[field] != primary_network[field] : changed_fields.append('Interface.'+field)
81 interface.update(interface_fields)
82 interface.sync(commit = False)
84 current_time = int(time.time())
86 # ONLY UPDATE ONCE when the boot_state flag and ssh_rsa_key flag are NOT passed
87 if 'boot_state' not in node_fields and 'ssh_rsa_key' not in node_fields:
89 # record times spent on and off line by comparing last_contact with previous value of last_boot
90 if node['last_boot'] and node['last_contact']:
91 # last_boot is when the machine last called this API function.
92 # last_contact is the last time NM or RLA pinged the API.
93 node['last_time_spent_online'] = node['last_contact'] - node['last_boot']
94 node['last_time_spent_offline'] = current_time - Timestamp.cast_long(node['last_contact'])
96 node.update_readonly_int('last_time_spent_online')
97 node.update_readonly_int('last_time_spent_offline')
98 changed_fields.append('last_time_spent_online')
99 changed_fields.append('last_time_spent_offline')
101 # indicate that node has booted & contacted PLC.
102 node.update_last_contact()
103 node.update_last_boot()
105 # if last_pcu_reboot is within 20 minutes of current_time, accept that the PCU is responsible
106 if node['last_pcu_reboot'] and Timestamp.cast_long(node['last_pcu_reboot']) >= current_time - 60*20:
107 node.update_last_pcu_confirmation(commit=False)
109 node.sync(commit = True)
112 self.message = "Boot updated: %s" % ", ".join(changed_fields)
113 self.event_objects = { 'Node' : [node['node_id']] }