Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / PLC / Methods / BootUpdateNode.py
1 import time
2
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 *
10
11 can_update = lambda (field, value): field in \
12              ['method', 'mac', 'gateway', 'network',
13               'broadcast', 'netmask', 'dns1', 'dns2']
14
15 class BootUpdateNode(Method):
16     """
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.
19
20     Returns 1 if updated successfully.
21     """
22
23     roles = ['node']
24
25     interface_fields = dict(filter(can_update, Interface.fields.items()))
26
27     accepts = [
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'],
35          }]
36
37     returns = Parameter(int, '1 if successful')
38
39     def call(self, auth, node_fields):
40
41         if not isinstance(self.caller, Node):
42             raise PLCInvalidArgument,"Caller is expected to be a node"
43
44         node = self.caller
45
46         # log this event only if a change occured
47         # otherwise the db gets spammed with meaningless entries
48         changed_fields = []
49         # Update node state
50         if node_fields.has_key('boot_state'):
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 node_fields.has_key('ssh_host_key'):
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 node_fields.has_key('ssh_rsa_key'):
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']
60
61         # Update primary interface state
62         if node_fields.has_key('primary_network'):
63             primary_network = node_fields['primary_network']
64
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"
69
70             interfaces = Interfaces(self.api, [primary_network['interface_id']])
71             if not interfaces:
72                 raise PLCInvalidArgument, "No such interface %r"%interface_id
73             interface = interfaces[0]
74
75             if not interface['is_primary']:
76                 raise PLCInvalidArgument, "Not the primary interface on record"
77
78             interface_fields = dict(filter(can_update, 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)
83
84         current_time = int(time.time())
85
86         # ONLY UPDATE ONCE when the boot_state flag and ssh_rsa_key flag are NOT passed
87         if not node_fields.has_key('boot_state') and not node_fields.has_key('ssh_rsa_key'):
88
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'])
95
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')
100
101             # indicate that node has booted & contacted PLC.
102             node.update_last_contact()
103             node.update_last_boot()
104
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)
108
109         node.sync(commit = True)
110
111         if changed_fields:
112             self.message = "Boot updated: %s" % ", ".join(changed_fields)
113             self.event_objects = { 'Node' : [node['node_id']] }
114
115         return 1