remove svn keywords and use %{SCMURL} in spec file
[bootmanager.git] / source / BootManager.py
1 #!/usr/bin/python -u
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 import string
10 import sys, os, traceback
11 import time
12 import gzip
13
14 from steps import *
15 from Exceptions import *
16 import notify_messages
17 import BootServerRequest
18 import utils
19
20 # all output is written to this file
21 BM_NODE_LOG= "/tmp/bm.log"
22 VARS_FILE = "configuration"
23
24 # the new contents of PATH when the boot manager is running
25 BIN_PATH= ('/usr/local/bin',
26            '/usr/local/sbin',
27            '/usr/bin',
28            '/usr/sbin',
29            '/bin',
30            '/sbin')
31
32 def read_configuration_file(filename):
33     # read in and store all variables in VARS_FILE into each line
34     # is in the format name=val (any whitespace around the = is
35     # removed. everything after the = to the end of the line is
36     # the value
37     vars = {}
38     vars_file= file(filename,'r')
39     validConfFile = True
40     for line in vars_file:
41         # if its a comment or a whitespace line, ignore
42         if line[:1] == "#" or string.strip(line) == "":
43             continue
44
45         parts= string.split(line,"=")
46         if len(parts) != 2:
47             validConfFile = False
48             raise Exception( "Invalid line in vars file: %s" % line )
49
50         name= string.strip(parts[0])
51         value= string.strip(parts[1])
52         value= value.replace("'", "")   # remove quotes
53         value= value.replace('"', "")   # remove quotes
54         vars[name]= value
55
56     vars_file.close()
57     if not validConfFile:
58         raise Exception( "Unable to read configuration vars." )
59
60     # find out which directory we are running it, and set a variable
61     # for that. future steps may need to get files out of the bootmanager
62     # directory
63     current_dir= os.getcwd()
64     vars['BM_SOURCE_DIR']= current_dir
65
66     return vars
67
68 ##############################
69 class log:
70
71     format="%H:%M:%S(%Z) "
72
73     def __init__( self, OutputFilePath= None ):
74         try:
75             self.OutputFile= open( OutputFilePath, "w")
76             self.OutputFilePath= OutputFilePath
77         except:
78             print( "bootmanager log : Unable to open output file %r, continuing"%OutputFilePath )
79             self.OutputFile= None
80
81         self.VARS = None
82         try:
83             vars = read_configuration_file(VARS_FILE)
84             self.VARS = vars
85         except Exception, e:
86             self.LogEntry( str(e) )
87             return
88     
89     def LogEntry( self, str, inc_newline= 1, display_screen= 1 ):
90         now=time.strftime(log.format, time.localtime())
91         if self.OutputFile:
92             self.OutputFile.write( now+str )
93         if display_screen:
94             sys.stdout.write( now+str )
95             
96         if inc_newline:
97             if display_screen:
98                 sys.stdout.write( "\n" )
99             if self.OutputFile:
100                 self.OutputFile.write( "\n" )
101
102         if self.OutputFile:
103             self.OutputFile.flush()
104
105     def write( self, str ):
106         """
107         make log behave like a writable file object (for traceback
108         prints)
109         """
110         self.LogEntry( str, 0, 1 )
111     
112     # bm log uploading is available back again, as of nodeconfig-5.0-2
113     def Upload( self, extra_file=None ):
114         """
115         upload the contents of the log to the server
116         """
117         if self.OutputFile is not None:
118             self.OutputFile.flush()
119
120             self.LogEntry( "Uploading logs to %s" % self.VARS['UPLOAD_LOG_SCRIPT'] )
121             
122             self.OutputFile.close()
123             self.OutputFile= None
124
125             hostname= self.VARS['INTERFACE_SETTINGS']['hostname'] + "." + \
126                       self.VARS['INTERFACE_SETTINGS']['domainname']
127             bs_request = BootServerRequest.BootServerRequest(self.VARS)
128             try:
129                 # this was working until f10
130                 bs_request.MakeRequest(PartialPath = self.VARS['UPLOAD_LOG_SCRIPT'],
131                                        GetVars = None, PostVars = None,
132                                        DoSSL = True, DoCertCheck = True,
133                                        FormData = ["log=@" + self.OutputFilePath,
134                                                    "hostname=" + hostname, 
135                                                    "type=bm.log"])
136             except:
137                 # new pycurl
138                 import pycurl
139                 bs_request.MakeRequest(PartialPath = self.VARS['UPLOAD_LOG_SCRIPT'],
140                                        GetVars = None, PostVars = None,
141                                        DoSSL = True, DoCertCheck = True,
142                                        FormData = [('log',(pycurl.FORM_FILE, self.OutputFilePath)),
143                                                    ("hostname",hostname),
144                                                    ("type","bm.log")])
145         if extra_file is not None:
146             # NOTE: for code-reuse, evoke the bash function 'upload_logs'; 
147             # by adding --login, bash reads .bash_profile before execution.
148             # Also, never fail, since this is an optional feature.
149             utils.sysexec_noerr( """bash --login -c "upload_logs %s" """ % extra_file, self)
150
151
152 ##############################
153 class BootManager:
154
155     # file containing initial variables/constants
156
157     # the set of valid node run states
158     NodeRunStates = {'reinstall':None,
159                      'boot':None,
160                      'safeboot':None,
161                      'disabled':None,
162                      }
163     
164     def __init__(self, log, forceState):
165         # override machine's current state from the command line
166         self.forceState = forceState
167
168         # the main logging point
169         self.LOG= log
170
171         # set to 1 if we can run after initialization
172         self.CAN_RUN = 0
173
174         if log.VARS:
175             # this contains a set of information used and updated by each step
176             self.VARS= log.VARS
177         else:
178             return
179              
180         # not sure what the current PATH is set to, replace it with what
181         # we know will work with all the boot cds
182         os.environ['PATH']= string.join(BIN_PATH,":")
183
184         self.CAN_RUN= 1
185
186     def Run(self):
187         """
188         core boot manager logic.
189
190         the way errors are handled is as such: if any particular step
191         cannot continue or unexpectibly fails, an exception is thrown.
192         in this case, the boot manager cannot continue running.
193
194         these step functions can also return a 0/1 depending on whether
195         or not it succeeded. In the case of steps like ConfirmInstallWithUser,
196         a 0 is returned and no exception is thrown if the user chose not
197         to confirm the install. The same goes with the CheckHardwareRequirements.
198         If requriements not met, but tests were succesfull, return 0.
199
200         for steps that run within the installer, they are expected to either
201         complete succesfully and return 1, or throw an execption.
202
203         For exact return values and expected operations, see the comments
204         at the top of each of the invididual step functions.
205         """
206
207         def _nodeNotInstalled(message='MSG_NODE_NOT_INSTALLED'):
208             # called by the _xxxState() functions below upon failure
209             self.VARS['RUN_LEVEL']= 'failboot'
210             notify = getattr(notify_messages, message)
211             self.VARS['STATE_CHANGE_NOTIFY']= 1
212             self.VARS['STATE_CHANGE_NOTIFY_MESSAGE']= notify
213             raise BootManagerException, notify
214
215         def _bootRun():
216             # implements the boot logic, which consists of first
217             # double checking that the node was properly installed,
218             # checking whether someone added or changed disks, and
219             # then finally chain boots.
220
221             # starting the fallback/debug ssh daemon for safety:
222             # if the node install somehow hangs, or if it simply takes ages, 
223             # we can still enter and investigate
224             try:
225                 StartDebug.Run(self.VARS, self.LOG, last_resort = False)
226             except:
227                 pass
228
229             InstallInit.Run( self.VARS, self.LOG )                    
230             ret = ValidateNodeInstall.Run( self.VARS, self.LOG )
231             if ret == 1:
232                 WriteModprobeConfig.Run( self.VARS, self.LOG )
233                 WriteNetworkConfig.Run( self.VARS, self.LOG )
234                 CheckForNewDisks.Run( self.VARS, self.LOG )
235                 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
236                 ChainBootNode.Run( self.VARS, self.LOG )
237             elif ret == -1:
238                 _nodeNotInstalled('MSG_NODE_FILESYSTEM_CORRUPT')
239             elif ret == -2:
240                 _nodeNotInstalled('MSG_NODE_MOUNT_FAILED')
241             elif ret == -3:
242                 _nodeNotInstalled('MSG_NODE_MISSING_KERNEL')
243             else:
244                 _nodeNotInstalled()
245
246         def _reinstallRun():
247
248             # starting the fallback/debug ssh daemon for safety:
249             # if the node install somehow hangs, or if it simply takes ages, 
250             # we can still enter and investigate
251             try:
252                 StartDebug.Run(self.VARS, self.LOG, last_resort = False)
253             except:
254                 pass
255
256             # implements the reinstall logic, which will check whether
257             # the min. hardware requirements are met, install the
258             # software, and upon correct installation will switch too
259             # 'boot' state and chainboot into the production system
260             if not CheckHardwareRequirements.Run( self.VARS, self.LOG ):
261                 self.VARS['RUN_LEVEL']= 'failboot'
262                 raise BootManagerException, "Hardware requirements not met."
263
264             # runinstaller
265             InstallInit.Run( self.VARS, self.LOG )                    
266             InstallPartitionDisks.Run( self.VARS, self.LOG )            
267             InstallBootstrapFS.Run( self.VARS, self.LOG )            
268             InstallWriteConfig.Run( self.VARS, self.LOG )
269             InstallUninitHardware.Run( self.VARS, self.LOG )
270             self.VARS['BOOT_STATE']= 'boot'
271             self.VARS['STATE_CHANGE_NOTIFY']= 1
272             self.VARS['STATE_CHANGE_NOTIFY_MESSAGE']= \
273                  notify_messages.MSG_INSTALL_FINISHED
274             UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
275             _bootRun()
276             
277         def _installRun():
278             # implements the new install logic, which will first check
279             # with the user whether it is ok to install on this
280             # machine, switch to 'reinstall' state and then invoke the reinstall
281             # logic.  See reinstallState logic comments for further
282             # details.
283             if not ConfirmInstallWithUser.Run( self.VARS, self.LOG ):
284                 return 0
285             self.VARS['BOOT_STATE']= 'reinstall'
286             UpdateRunLevelWithPLC.Run( self.VARS, self.LOG )
287             _reinstallRun()
288
289         def _debugRun(state='failboot'):
290             # implements debug logic, which starts the sshd and just waits around
291             self.VARS['RUN_LEVEL']=state
292             UpdateRunLevelWithPLC.Run( self.VARS, self.LOG )
293             StartDebug.Run( self.VARS, self.LOG )
294             # fsck/mount fs if present, and ignore return value if it's not.
295             ValidateNodeInstall.Run( self.VARS, self.LOG )
296
297         def _badstateRun():
298             # should never happen; log event
299             self.LOG.write( "\nInvalid BOOT_STATE = %s\n" % self.VARS['BOOT_STATE'])
300             _debugRun()
301
302         # setup state -> function hash table
303         BootManager.NodeRunStates['reinstall']  = _reinstallRun
304         BootManager.NodeRunStates['boot']       = _bootRun
305         BootManager.NodeRunStates['safeboot']   = lambda : _debugRun('safeboot')
306         BootManager.NodeRunStates['disabled']   = lambda : _debugRun('disabled')
307
308         success = 0
309         try:
310             InitializeBootManager.Run( self.VARS, self.LOG )
311             ReadNodeConfiguration.Run( self.VARS, self.LOG )
312             AuthenticateWithPLC.Run( self.VARS, self.LOG )
313             StartRunlevelAgent.Run( self.VARS, self.LOG )
314             GetAndUpdateNodeDetails.Run( self.VARS, self.LOG )
315
316             # override machine's current state from the command line
317             if self.forceState is not None:
318                 self.VARS['BOOT_STATE']= self.forceState
319                 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
320                 UpdateRunLevelWithPLC.Run( self.VARS, self.LOG )
321
322             stateRun = BootManager.NodeRunStates.get(self.VARS['BOOT_STATE'],_badstateRun)
323             stateRun()
324             success = 1
325
326         except KeyError, e:
327             self.LOG.write( "\n\nKeyError while running: %s\n" % str(e) )
328         except BootManagerException, e:
329             self.LOG.write( "\n\nException while running: %s\n" % str(e) )
330         except BootManagerAuthenticationException, e:
331             self.LOG.write( "\n\nFailed to Authenticate Node: %s\n" % str(e) )
332             # sets /tmp/CANCEL_BOOT flag
333             StartDebug.Run(self.VARS, self.LOG )
334             # Return immediately b/c any other calls to API will fail
335             return success
336         except:
337             self.LOG.write( "\n\nImplementation Error\n")
338             traceback.print_exc(file=self.LOG.OutputFile)
339             traceback.print_exc()
340
341         if not success:
342             try:
343                 _debugRun()
344             except BootManagerException, e:
345                 self.LOG.write( "\n\nException while running: %s\n" % str(e) )
346             except:
347                 self.LOG.write( "\n\nImplementation Error\n")
348                 traceback.print_exc(file=self.LOG.OutputFile)
349                 traceback.print_exc()
350
351         return success
352             
353             
354 def main(argv):
355
356     import utils
357     utils.prompt_for_breakpoint_mode()
358
359     utils.breakpoint ("Entering BootManager::main")
360     
361     # set to 1 if error occurred
362     error= 0
363     
364     # all output goes through this class so we can save it and post
365     # the data back to PlanetLab central
366     LOG= log( BM_NODE_LOG )
367
368     # NOTE: assume CWD is BM's source directory, but never fail
369     utils.sysexec_noerr("./setup_bash_history_scripts.sh", LOG)
370
371     LOG.LogEntry( "BootManager started at: %s" % \
372                   time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) )
373
374     try:
375         forceState = None
376         if len(argv) == 2:
377             fState = argv[1]
378             if BootManager.NodeRunStates.has_key(fState):
379                 forceState = fState
380             else:
381                 LOG.LogEntry("FATAL: cannot force node run state to=%s" % fState)
382                 error = 1
383     except:
384         traceback.print_exc(file=LOG.OutputFile)
385         traceback.print_exc()
386         
387     if error:
388         LOG.LogEntry( "BootManager finished at: %s" % \
389                       time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) )
390         LOG.Upload()
391         return error
392
393     try:
394         bm= BootManager(LOG,forceState)
395         if bm.CAN_RUN == 0:
396             LOG.LogEntry( "Unable to initialize BootManager." )
397         else:
398             LOG.LogEntry( "Running version %s of BootManager." % bm.VARS['VERSION'] )
399             success= bm.Run()
400             if success:
401                 LOG.LogEntry( "\nDone!" );
402             else:
403                 LOG.LogEntry( "\nError occurred!" );
404                 error = 1
405     except:
406         traceback.print_exc(file=LOG.OutputFile)
407         traceback.print_exc()
408
409     LOG.LogEntry( "BootManager finished at: %s" % \
410                   time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) )
411     LOG.Upload()
412
413     return error
414
415     
416 if __name__ == "__main__":
417     error = main(sys.argv)
418     sys.exit(error)