From 52659b72c3ac9e35f2ab6a68a3a80f78d26a4671 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Tue, 6 Jan 2009 23:35:56 +0000 Subject: [PATCH] add these functions to the 4.2 branch, to make the features available right away. --- PLC/Methods/RebootNodeWithPCU.py | 73 ++++++++++++++++++++++++++++++++ PLC/Methods/__init__.py | 1 + 2 files changed, 74 insertions(+) create mode 100644 PLC/Methods/RebootNodeWithPCU.py diff --git a/PLC/Methods/RebootNodeWithPCU.py b/PLC/Methods/RebootNodeWithPCU.py new file mode 100644 index 00000000..cf32da02 --- /dev/null +++ b/PLC/Methods/RebootNodeWithPCU.py @@ -0,0 +1,73 @@ +# $Id$ +import socket + +from PLC.Faults import * +from PLC.Method import Method +from PLC.Parameter import Parameter, Mixed +from PLC.Auth import Auth + +from PLC.Nodes import Node, Nodes +from PLC.PCUs import PCU, PCUs + +try: + from pcucontrol import reboot + external_dependency = True +except: + external_dependency = False + +class RebootNodeWithPCU(Method): + """ + Uses the associated PCU to attempt to reboot the given Node. + + Admins can reboot any node. Techs and PIs can only reboot nodes at + their site. + + Returns 1 if the reboot proceeded without error (Note: this does not guarantee + that the reboot is successful). + Returns "error string" if the reboot failed with a specific message. + Raises exception if external dependencies for this call are not available. + """ + + roles = ['admin', 'pi', 'tech'] + + accepts = [ + Auth(), + Mixed(Node.fields['node_id'], + Node.fields['hostname']) + ] + + returns = Parameter(int, '1 if successful') + + def call(self, auth, node_id_or_hostname): + # Get account information + nodes = Nodes(self.api, [node_id_or_hostname]) + if not nodes: + raise PLCInvalidArgument, "No such node" + + node = nodes[0] + + # Authenticated function + assert self.caller is not None + + # If we are not an admin, make sure that the caller is a + # member of the site at which the node is located. + if 'admin' not in self.caller['roles']: + if node['site_id'] not in self.caller['site_ids']: + raise PLCPermissionDenied, "Not allowed to reboot nodes from specified site" + + # Verify that the node has pcus associated with it. + pcus = PCUs(self.api, {'pcu_id' : node['pcu_ids']} ) + if not pcus: + raise PLCInvalidArgument, "No PCUs associated with Node" + + pcu = pcus[0] + + if not external_dependency: + raise PLCNotImplemented, "Could not load external module to attempt reboot" + + ret = reboot.reboot_api(node, pcu) + + self.event_objects = {'Node': [node['node_id']]} + self.message = "RebootNodeWithPCU called" + + return ret diff --git a/PLC/Methods/__init__.py b/PLC/Methods/__init__.py index 45c92284..8e5a3346 100644 --- a/PLC/Methods/__init__.py +++ b/PLC/Methods/__init__.py @@ -184,6 +184,7 @@ GetWhitelist NotifyPersons NotifySupport RebootNode +RebootNodeWithPCU RefreshPeer ResetPassword SetPersonPrimarySite -- 2.47.0