46c8c84c08e9c325e17a3f23807c7f465f9e478a
[plcapi.git] / PLC / Methods / RebootNodeWithPCU.py
1 import socket
2
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
10
11 try:
12         from pcucontrol import reboot
13         external_dependency = True
14 except:
15         external_dependency = False
16
17 class RebootNodeWithPCU(Method):
18     """
19         Uses the associated PCU to attempt to reboot the given Node.
20
21     Admins can reboot any node. Techs and PIs can only reboot nodes at
22     their site.
23
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.
28     """
29
30     roles = ['admin', 'pi', 'tech']
31
32     accepts = [
33         Auth(),
34         Mixed(Node.fields['node_id'],
35               Node.fields['hostname'])
36         ]
37
38     returns = Parameter(int, '1 if successful')
39
40     def call(self, auth, node_id_or_hostname):
41         # Get account information
42         nodes = Nodes(self.api, [node_id_or_hostname])
43         if not nodes:
44             raise PLCInvalidArgument, "No such node"
45
46         node = nodes[0]
47
48         # Authenticated function
49         assert self.caller is not None
50
51         # If we are not an admin, make sure that the caller is a
52         # member of the site at which the node is located.
53         if 'admin' not in self.caller['roles']:
54             if node['site_id'] not in self.caller['site_ids']:
55                 raise PLCPermissionDenied, "Not allowed to reboot nodes from specified site"
56
57                 # Verify that the node has pcus associated with it.
58                 pcus = PCUs(self.api, {'pcu_id' : node['pcu_ids']} )
59         if not pcus:
60             raise PLCInvalidArgument, "No PCUs associated with Node"
61
62                 pcu = pcus[0]
63
64                 if not external_dependency:
65             raise PLCNotImplemented, "Could not load external module to attempt reboot"
66
67                 # model, hostname, port, 
68                 # i = pcu['node_ids'].index(node['node_id'])
69                 # p = pcu['ports'][i]
70                 ret = reboot.reboot_api(node, pcu)
71
72         self.event_objects = {'Node': [node['node_id']]}
73         self.message = "RebootNodeWithPCU called"
74                 
75         return ret