5d083d31303f57df0355f8eefb58e3b7d189134c
[bootmanager.git] / source / steps / UpdateRunLevelWithPLC.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8
9 from Exceptions import *
10 import BootAPI
11 import notify_messages
12
13
14 def Run( vars, log ):
15     """
16     Change this nodes run level at PLC.
17
18     Replaces the behavior of UpdateBootStateWithPLC.  Where previously, the
19     boot_state of a node would be altered by the BM, now the run_level is
20     updated, and the boot_state is preserved as a record of a User's
21     preference.
22
23     The current value of the RUN_LEVEL key in vars is used.
24     Optionally, notify the contacts of the run level change.
25     If this is the case, the following keys/values
26     should be set in vars before calling this step:
27     STATE_CHANGE_NOTIFY= 1
28     STATE_CHANGE_NOTIFY_MESSAGE= "<notify message>"
29     The second value is a message to send the users from notify_messages.py
30
31     Return 1 if succesfull, a BootManagerException otherwise.
32     """
33
34     log.write( "\n\nStep: Updating node run level at PLC.\n" )
35
36     update_vals= {}
37     # translate boot_state values to run_level value
38     if vars['RUN_LEVEL'] in ['diag', 'diagnose', 'disabled', 'disable']:
39         vars['RUN_LEVEL']='safeboot'
40     update_vals['run_level']=vars['RUN_LEVEL']
41     try:
42         BootAPI.call_api_function( vars, "ReportRunlevel", (update_vals,) )
43         log.write( "Successfully updated run level for this node at PLC\n" )
44     except BootManagerException, e:
45         log.write( "Unable to update run level for this node at PLC: %s.\n" % e )
46
47     notify = vars.get("STATE_CHANGE_NOTIFY",0)
48
49     if notify:
50         message= vars['STATE_CHANGE_NOTIFY_MESSAGE']
51         include_pis= 0
52         include_techs= 1
53         include_support= 0
54
55         sent= 0
56         try:
57             sent= BootAPI.call_api_function( vars, "BootNotifyOwners",
58                                              (message,
59                                               include_pis,
60                                               include_techs,
61                                               include_support) )
62         except BootManagerException, e:
63             log.write( "Call to BootNotifyOwners failed: %s.\n" % e )
64
65         if sent == 0:
66             log.write( "Unable to notify site contacts of state change.\n" )
67
68     return 1