Detangled steps. No step makes calls into another step.
[bootmanager.git] / source / steps / AuthenticateWithPLC.py
1 #!/usr/bin/python2
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
10 import os
11
12 from Exceptions import *
13 import BootAPI
14
15
16 AUTH_FAILURE_COUNT_FILE= "/tmp/authfailurecount"
17
18
19 def Run( vars, log ):
20     """
21     Authenticate this node with PLC. This ensures that the node can operate
22     as normal, and that our management authority has authorized it.
23
24     For this, just call the PLC api function BootCheckAuthentication
25
26     Return 1 if authorized, a BootManagerException if not or the
27     call fails entirely.
28
29     If there are two consecutive authentication failures, put the node
30     into debug mode and exit the bootmanager.
31
32     Expect the following variables from the store:
33     NUM_AUTH_FAILURES_BEFORE_DEBUG    How many failures before debug
34     """
35
36     log.write( "\n\nStep: Authenticating node with PLC.\n" )
37
38     # make sure we have the variables we need
39     try:
40         NUM_AUTH_FAILURES_BEFORE_DEBUG= int(vars["NUM_AUTH_FAILURES_BEFORE_DEBUG"])
41     except KeyError, var:
42         raise BootManagerException, "Missing variable in vars: %s\n" % var
43     except ValueError, var:
44         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
45
46     try:
47         authorized= BootAPI.call_api_function( vars, "BootCheckAuthentication", () )
48         if authorized == 1:
49             log.write( "Authentication successful.\n" )
50
51             try:
52                 os.unlink( AUTH_FAILURE_COUNT_FILE )
53             except OSError, e:
54                 pass
55             
56             return 1
57     except BootManagerException, e:
58         log.write( "Authentication failed: %s.\n" % e )
59
60     # increment auth failure
61     auth_failure_count= 0
62     try:
63         auth_failure_count= int(file(AUTH_FAILURE_COUNT_FILE,"r").read().strip())
64     except IOError:
65         pass
66     except ValueError:
67         pass
68
69     auth_failure_count += 1
70
71     try:
72         fail_file= file(AUTH_FAILURE_COUNT_FILE,"w")
73         fail_file.write( str(auth_failure_count) )
74         fail_file.close()
75     except IOError:
76         pass
77
78     if auth_failure_count >= NUM_AUTH_FAILURES_BEFORE_DEBUG:
79         log.write( "Maximum number of authentication failures reached.\n" )
80         log.write( "Canceling boot process and going into debug mode.\n" )
81
82     raise BootManagerException, "Unable to authenticate node."
83     
84