5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Nodes import Node, Nodes
9 from PLC.Interfaces import Interface, Interfaces
10 from PLC.Auth import Auth
11 from PLC.POD import udp_pod
13 class RebootNode(Method):
15 Sends the specified node a specially formatted UDP packet which
16 should cause it to reboot immediately.
18 Admins can reboot any node. Techs and PIs can only reboot nodes at
21 Returns 1 if the packet was successfully sent (which only whether
22 the packet was sent, not whether the reboot was successful).
25 roles = ['admin', 'pi', 'tech']
29 Mixed(Node.fields['node_id'],
30 Node.fields['hostname'])
33 returns = Parameter(int, '1 if successful')
35 def call(self, auth, node_id_or_hostname):
36 # Get account information
37 nodes = Nodes(self.api, [node_id_or_hostname])
39 raise PLCInvalidArgument, "No such node"
43 # Authenticated function
44 assert self.caller is not None
46 # If we are not an admin, make sure that the caller is a
47 # member of the site at which the node is located.
48 if 'admin' not in self.caller['roles']:
49 if node['site_id'] not in self.caller['site_ids']:
50 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
52 session = node['session']
54 raise PLCInvalidArgument, "No session key on record for that node (i.e., has never successfully booted)"
55 session = session.strip()
57 # Only use the hostname as a backup, try to use the primary ID
59 host = node['hostname']
60 interfaces = Interfaces(self.api, node['interface_ids'])
61 for interface in interfaces:
62 if interface['is_primary'] == 1:
63 host = interface['ip']
67 udp_pod(host, session)
68 except socket.error, e:
69 # Ignore socket errors
72 self.event_objects = {'Node': [node['node_id']]}
73 self.message = "RebootNode called"