3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Auth import Auth
8 from PLC.Nodes import Node, Nodes
9 from PLC.PCUs import PCU, PCUs
12 from pcucontrol import reboot
13 external_dependency = True
15 external_dependency = False
17 class RebootNodeWithPCU(Method):
19 Uses the associated PCU to attempt to reboot the given Node.
21 Admins can reboot any node. Techs and PIs can only reboot nodes at
24 Returns 1 if the reboot proceeded without error (Note: this does not guarantee
25 that the reboot is successful).
26 Returns -1 if external dependencies for this call are not available.
27 Returns "error string" if the reboot failed with a specific message.
30 roles = ['admin', 'pi', 'tech']
34 Mixed(Node.fields['node_id'],
35 Node.fields['hostname']),
36 Parameter(bool, "Run as a test, or as a real reboot", nullok = True)
39 returns = Parameter(int, '1 if successful')
41 def call(self, auth, node_id_or_hostname, testrun=None):
42 # Get account information
43 nodes = Nodes(self.api, [node_id_or_hostname])
45 raise PLCInvalidArgument, "No such node"
52 # Authenticated function
53 assert self.caller is not None
55 # If we are not an admin, make sure that the caller is a
56 # member of the site at which the node is located.
57 if 'admin' not in self.caller['roles']:
58 if node['site_id'] not in self.caller['site_ids']:
59 raise PLCPermissionDenied, "Not allowed to reboot nodes from specified site"
61 # Verify that the node has pcus associated with it.
62 pcus = PCUs(self.api, {'pcu_id' : node['pcu_ids']} )
64 raise PLCInvalidArgument, "No PCUs associated with Node"
68 if not external_dependency:
69 raise PLCNotImplemented, "Could not load external module to attempt reboot"
71 # model, hostname, port,
72 # i = pcu['node_ids'].index(node['node_id'])
74 ret = reboot.reboot_api(node, pcu, testrun)
76 node.update_last_pcu_reboot(commit=True) # commits new timestamp to node
78 self.event_objects = {'Node': [node['node_id']]}
79 self.message = "RebootNodeWithPCU %s with %s returned %s" % (node['node_id'], pcu['pcu_id'], ret)