3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Nodes import Node, Nodes
7 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
8 from PLC.Auth import Auth
9 from PLC.POD import udp_pod
11 class RebootNode(Method):
13 Sends the specified node a specially formatted UDP packet which
14 should cause it to reboot immediately.
16 Admins can reboot any node. Techs and PIs can only reboot nodes at
19 Returns 1 if the packet was successfully sent (which only whether
20 the packet was sent, not whether the reboot was successful).
23 roles = ['admin', 'pi', 'tech']
27 Mixed(Node.fields['node_id'],
28 Node.fields['hostname'])
31 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 nodenetworks = NodeNetworks(self.api, node['nodenetwork_ids'])
61 for nodenetwork in nodenetworks:
62 if nodenetwork['is_primary'] == 1:
63 host = nodenetwork['ip']
67 udp_pod(host, session)
68 except socket.error, e:
69 # Ignore socket errors
72 self.message = "RebootNode called"