3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
10 import sys, os, traceback
11 from time import gmtime, strftime
12 from gzip import GzipFile
15 from Exceptions import *
16 import notify_messages
17 import BootServerRequest
19 # all output is written to this file
20 LOG_FILE= "/tmp/bm.log"
21 UPLOAD_LOG_PATH = "/alpina-logs/upload.php"
23 # the new contents of PATH when the boot manager is running
24 BIN_PATH= ('/usr/local/bin',
30 '/usr/local/planetlab/bin')
33 # the set of valid node run states
38 def __init__( self, OutputFilePath= None ):
41 self.OutputFilePath= OutputFilePath
42 self.OutputFile= GzipFile( OutputFilePath, "w", 9 )
44 print( "Unable to open output file for log, continuing" )
48 def LogEntry( self, str, inc_newline= 1, display_screen= 1 ):
50 self.OutputFile.write( str )
52 sys.stdout.write( str )
56 sys.stdout.write( "\n" )
58 self.OutputFile.write( "\n" )
61 self.OutputFile.flush()
65 def write( self, str ):
67 make log behave like a writable file object (for traceback
70 self.LogEntry( str, 0, 1 )
76 upload the contents of the log to the server
79 if self.OutputFile is not None:
80 self.LogEntry( "Uploading logs to %s" % UPLOAD_LOG_PATH )
82 self.OutputFile.close()
85 bs_request = BootServerRequest.BootServerRequest()
86 bs_request.MakeRequest(PartialPath = UPLOAD_LOG_PATH,
87 GetVars = None, PostVars = None,
88 FormData = ["log=@" + self.OutputFilePath],
89 DoSSL = True, DoCertCheck = True)
98 # file containing initial variables/constants
99 VARS_FILE = "configuration"
102 def __init__(self, log, forceState):
103 # override machine's current state from the command line
104 self.forceState = forceState
106 # the main logging point
109 # set to 1 if we can run after initialization
112 # read in and store all variables in VARS_FILE into each line
113 # is in the format name=val (any whitespace around the = is
114 # removed. everything after the = to the end of the line is
117 vars_file= file(self.VARS_FILE,'r')
119 for line in vars_file:
120 # if its a comment or a whitespace line, ignore
121 if line[:1] == "#" or string.strip(line) == "":
124 parts= string.split(line,"=")
126 self.LOG.LogEntry( "Invalid line in vars file: %s" % line )
127 validConfFile = False
130 name= string.strip(parts[0])
131 value= string.strip(parts[1])
135 if not validConfFile:
136 self.LOG.LogEntry( "Unable to read configuration vars." )
139 # find out which directory we are running it, and set a variable
140 # for that. future steps may need to get files out of the bootmanager
142 current_dir= os.getcwd()
143 vars['BM_SOURCE_DIR']= current_dir
145 # not sure what the current PATH is set to, replace it with what
146 # we know will work with all the boot cds
147 os.environ['PATH']= string.join(BIN_PATH,":")
149 # this contains a set of information used and updated
157 core boot manager logic.
159 the way errors are handled is as such: if any particular step
160 cannot continue or unexpectibly fails, an exception is thrown.
161 in this case, the boot manager cannot continue running.
163 these step functions can also return a 0/1 depending on whether
164 or not it succeeded. In the case of steps like ConfirmInstallWithUser,
165 a 0 is returned and no exception is thrown if the user chose not
166 to confirm the install. The same goes with the CheckHardwareRequirements.
167 If requriements not met, but tests were succesfull, return 0.
169 for steps that run within the installer, they are expected to either
170 complete succesfully and return 1, or throw an execption.
172 For exact return values and expected operations, see the comments
173 at the top of each of the invididual step functions.
176 def _nodeNotInstalled():
177 # called by the _xxxState() functions below upon failure
178 self.VARS['BOOT_STATE']= 'dbg'
179 self.VARS['STATE_CHANGE_NOTIFY']= 1
180 self.VARS['STATE_CHANGE_NOTIFY_MESSAGE']= \
181 notify_messages.MSG_NODE_NOT_INSTALLED
182 raise BootManagerException, \
183 notify_messages.MSG_NODE_NOT_INSTALLED
186 # implements the boot logic, which consists of first
187 # double checking that the node was properly installed,
188 # checking whether someone added or changed disks, and
189 # then finally chain boots.
191 InstallInit.Run( self.VARS, self.LOG )
192 if ValidateNodeInstall.Run( self.VARS, self.LOG ):
193 WriteModprobeConfig.Run( self.VARS, self.LOG )
194 WriteNetworkConfig.Run( self.VARS, self.LOG )
195 # the following step should be done by NM
196 UpdateNodeConfiguration.Run( self.VARS, self.LOG )
197 CheckForNewDisks.Run( self.VARS, self.LOG )
198 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
199 ChainBootNode.Run( self.VARS, self.LOG )
204 # implements the reinstall logic, which will check whether
205 # the min. hardware requirements are met, install the
206 # software, and upon correct installation will switch too
207 # 'boot' state and chainboot into the production system
208 if not CheckHardwareRequirements.Run( self.VARS, self.LOG ):
209 self.VARS['BOOT_STATE']= 'dbg'
210 raise BootManagerException, "Hardware requirements not met."
213 InstallInit.Run( self.VARS, self.LOG )
214 InstallPartitionDisks.Run( self.VARS, self.LOG )
215 InstallBootstrapRPM.Run( self.VARS, self.LOG )
216 InstallWriteConfig.Run( self.VARS, self.LOG )
217 InstallBuildVServer.Run( self.VARS, self.LOG )
218 InstallNodeInit.Run( self.VARS, self.LOG )
219 InstallUninitHardware.Run( self.VARS, self.LOG )
220 self.VARS['BOOT_STATE']= 'boot'
221 self.VARS['STATE_CHANGE_NOTIFY']= 1
222 self.VARS['STATE_CHANGE_NOTIFY_MESSAGE']= \
223 notify_messages.MSG_INSTALL_FINISHED
224 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
228 # implements the new install logic, which will first check
229 # with the user whether it is ok to install on this
230 # machine, switch to 'rins' state and then invoke the rins
231 # logic. See rinsState logic comments for further
233 if not ConfirmInstallWithUser.Run( self.VARS, self.LOG ):
235 self.VARS['BOOT_STATE']= 'rins'
236 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
240 # implements debug logic, which just starts the sshd
241 # and just waits around
242 self.VARS['BOOT_STATE']='dbg'
243 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
244 StartDebug.Run( self.VARS, self.LOG )
247 # should never happen; log event
248 self.LOG.write( "\nInvalid BOOT_STATE = %s\n" % self.VARS['BOOT_STATE'])
252 # setup state -> function hash table
253 NodeRunStates['new'] = _newRun
254 NodeRunStates['inst'] = _newRun
255 NodeRunStates['rins'] = _rinsRun
256 NodeRunStates['boot'] = _bootRun
257 NodeRunStates['dbg'] = _debugRun
261 InitializeBootManager.Run( self.VARS, self.LOG )
262 ReadNodeConfiguration.Run( self.VARS, self.LOG )
263 AuthenticateWithPLC.Run( self.VARS, self.LOG )
264 GetAndUpdateNodeDetails.Run( self.VARS, self.LOG )
266 # override machine's current state from the command line
267 if self.forceState is not None:
268 self.VARS['BOOT_STATE']= self.forceState
269 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
271 stateRun = NodeRunStates.get(self.VARS['BOOT_STATE'],_badRun)
276 self.LOG.write( "\n\nKeyError while running: %s\n" % str(e) )
277 except BootManagerException, e:
278 self.LOG.write( "\n\nException while running: %s\n" % str(e) )
280 self.LOG.write( "\n\nImplementation Error\n")
281 traceback.print_exc(file=self.LOG.OutputFile)
282 traceback.print_exc()
287 except BootManagerException, e:
288 self.LOG.write( "\n\nException while running: %s\n" % str(e) )
290 self.LOG.write( "\n\nImplementation Error\n")
291 traceback.print_exc(file=self.LOG.OutputFile)
292 traceback.print_exc()
299 NodeRunStates = {'new':None,
305 # set to 1 if error occurred
308 # all output goes through this class so we can save it and post
309 # the data back to PlanetLab central
312 LOG.LogEntry( "BootManager started at: %s" % \
313 strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
319 if NodeRunStates.has_key(fState):
322 LOG.LogEntry("FATAL: cannot force node run state to=%s" % fState)
325 traceback.print_exc(file=LOG.OutputFile)
326 traceback.print_exc()
329 LOG.LogEntry( "BootManager finished at: %s" % \
330 strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
335 bm= BootManager(LOG,forceState)
337 LOG.LogEntry( "Unable to initialize BootManager." )
339 LOG.LogEntry( "Running version %s of BootManager." %
343 LOG.LogEntry( "\nDone!" );
345 LOG.LogEntry( "\nError occurred!" );
348 traceback.print_exc(file=LOG.OutputFile)
349 traceback.print_exc()
351 LOG.LogEntry( "BootManager finished at: %s" % \
352 strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
358 if __name__ == "__main__":
359 error = main(sys.argv)