4e81845ec0c63854fecd74b7587b58aeaa8c4e9a
[plcapi.git] / PLC / Methods / BootUpdateNode.py
1 # $Id$
2 # $URL$
3 import time
4
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 *
12
13 can_update = lambda (field, value): field in \
14              ['method', 'mac', 'gateway', 'network',
15               'broadcast', 'netmask', 'dns1', 'dns2']
16
17 class BootUpdateNode(Method):
18     """
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.
21
22     Returns 1 if updated successfully.
23     """
24
25     roles = ['node']
26
27     interface_fields = dict(filter(can_update, Interface.fields.items()))
28
29     accepts = [
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'],
37          }]
38
39     returns = Parameter(int, '1 if successful')
40
41     def call(self, auth, node_fields):
42
43         if not isinstance(self.caller, Node):
44             raise PLCInvalidArgument,"Caller is expected to be a node"
45
46         node = self.caller
47
48         # log this event only if a change occured
49         # otherwise the db gets spammed with meaningless entries
50         changed_fields = []
51         # Update node state
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']
62
63         # Update primary interface state
64         if node_fields.has_key('primary_network'):
65             primary_network = node_fields['primary_network']
66
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"
71
72             interfaces = Interfaces(self.api, [primary_network['interface_id']])
73             if not interfaces:
74                 raise PLCInvalidArgument, "No such interface %r"%interface_id
75             interface = interfaces[0]
76
77             if not interface['is_primary']:
78                 raise PLCInvalidArgument, "Not the primary interface on record"
79
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)
85
86         # indicate that node has booted & contacted PLC.
87         node.update_last_contact()
88         node.update_last_boot()
89
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)
94
95         node.sync(commit = True)
96
97         if changed_fields:
98             self.message = "Boot updated: %s" % ", ".join(changed_fields)
99             self.event_objects = { 'Node' : [node['node_id']] }
100
101         return 1