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