3 # ------------------------------------------------------------------------
4 # THIS file used to be named alpina.py, from the node installer. Since then
5 # the installer has been expanded to include all the functions of the boot
6 # manager as well, hence the new name for this file.
7 # ------------------------------------------------------------------------
9 # Copyright (c) 2003 Intel Corporation
10 # All rights reserved.
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions are
16 # * Redistributions of source code must retain the above copyright
17 # notice, this list of conditions and the following disclaimer.
19 # * Redistributions in binary form must reproduce the above
20 # copyright notice, this list of conditions and the following
21 # disclaimer in the documentation and/or other materials provided
22 # with the distribution.
24 # * Neither the name of the Intel Corporation nor the names of its
25 # contributors may be used to endorse or promote products derived
26 # from this software without specific prior written permission.
28 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
32 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
41 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
42 # export regulations applicable in licensee's jurisdiction. Under
43 # CURRENT (May 2000) U.S. export regulations this software is eligible
44 # for export from the U.S. and can be downloaded by or otherwise
45 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
46 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
47 # Afghanistan and any other country to which the U.S. has embargoed
52 import sys, os, traceback
53 from time import gmtime, strftime
54 from gzip import GzipFile
57 from Exceptions import *
58 import notify_messages
62 # all output is written to this file
63 LOG_FILE= "/tmp/bm.log"
65 UPLOAD_LOG_URL = "http://boot.planet-lab.org/alpina-logs/upload.php"
67 # the new contents of PATH when the boot manager is running
68 BIN_PATH= ('/usr/local/bin',
74 '/usr/local/planetlab/bin')
80 def __init__( self, OutputFilePath= None ):
83 self.OutputFilePath= OutputFilePath
84 self.OutputFile= GzipFile( OutputFilePath, "w", 9 )
86 print( "Unable to open output file for log, continuing" )
90 def LogEntry( self, str, inc_newline= 1, display_screen= 1 ):
92 self.OutputFile.write( str )
94 sys.stdout.write( str )
98 sys.stdout.write( "\n" )
100 self.OutputFile.write( "\n" )
103 self.OutputFile.flush()
107 def write( self, str ):
109 make log behave like a writable file object (for traceback
112 self.LogEntry( str, 0, 1 )
118 upload the contents of the log to the server
121 if self.OutputFile is not None:
122 self.LogEntry( "Uploading logs to %s" % UPLOAD_LOG_URL )
124 self.OutputFile.close()
125 self.OutputFile= None
127 curl_cmd= "%s -s --connect-timeout 60 --max-time 600 " \
128 "--form log=@%s %s" % \
129 (CURL_PATH, self.OutputFilePath, UPLOAD_LOG_URL)
130 os.system( curl_cmd )
139 # file containing initial variables/constants
140 VARS_FILE = "configuration"
143 def __init__(self, log):
144 # this contains a set of information used and updated
148 # the main logging point
151 # set to 1 if we can run after initialization
154 if not self.ReadBMConf():
155 self.LOG.LogEntry( "Unable to read configuration vars." )
158 # find out which directory we are running it, and set a variable
159 # for that. future steps may need to get files out of the bootmanager
161 current_dir= os.getcwd()
162 self.VARS['BM_SOURCE_DIR']= current_dir
164 # not sure what the current PATH is set to, replace it with what
165 # we know will work with all the boot cds
166 os.environ['PATH']= string.join(BIN_PATH,":")
170 def ReadBMConf(self):
172 read in and store all variables in VARS_FILE into
175 each line is in the format name=val (any whitespace around
176 the = is removed. everything after the = to the end of
177 the line is the value
180 vars_file= file(self.VARS_FILE,'r')
181 for line in vars_file:
182 # if its a comment or a whitespace line, ignore
183 if line[:1] == "#" or string.strip(line) == "":
186 parts= string.split(line,"=")
188 self.LOG.LogEntry( "Invalid line in vars file: %s" % line )
191 name= string.strip(parts[0])
192 value= string.strip(parts[1])
194 self.VARS[name]= value
200 core boot manager logic.
202 the way errors are handled is as such: if any particular step
203 cannot continue or unexpectibly fails, an exception is thrown.
204 in this case, the boot manager cannot continue running.
206 these step functions can also return a 0/1 depending on whether
207 or not it succeeded. In the case of steps like ConfirmInstallWithUser,
208 a 0 is returned and no exception is thrown if the user chose not
209 to confirm the install. The same goes with the CheckHardwareRequirements.
210 If requriements not met, but tests were succesfull, return 0.
212 for steps that run within the installer, they are expected to either
213 complete succesfully and return 1, or throw an execption.
215 For exact return values and expected operations, see the comments
216 at the top of each of the invididual step functions.
219 def _nodeNotInstalled():
220 # called by the _xxxState() functions below upon failure
221 self.VARS['BOOT_STATE']= 'dbg'
222 self.VARS['STATE_CHANGE_NOTIFY']= 1
223 self.VARS['STATE_CHANGE_NOTIFY_MESSAGE']= \
224 notify_messages.MSG_NODE_NOT_INSTALLED
225 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
228 # implements the reinstall logic, which will check whether
229 # the min. hardware requirements are met, install the
230 # software, and upon correct installation will switch too
231 # 'boot' state and chainboot into the production system
232 if not CheckHardwareRequirements.Run( self.VARS, self.LOG ):
233 self.VARS['BOOT_STATE']= 'dbg'
234 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
235 raise BootManagerException, "Hardware requirements not met."
239 if ValidateNodeInstall.Run( self.VARS, self.LOG ):
240 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
241 ChainBootNode.Run( self.VARS, self.LOG )
246 # implements the new install logic, which will first check
247 # with the user whether it is ok to install on this
248 # machine, switch to 'rins' state and then invoke the rins
249 # logic. See rinsState logic comments for further
251 if not ConfirmInstallWithUser.Run( self.VARS, self.LOG ):
253 self.VARS['BOOT_STATE']= 'rins'
254 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
258 # implements the boot logic, which consists of first
259 # double checking that the node was properly installed,
260 # checking whether someone added or changed disks, and
261 # then finally chain boots.
263 if ValidateNodeInstall.Run( self.VARS, self.LOG ):
264 UpdateNodeConfiguration.Run( self.VARS, self.LOG )
265 CheckForNewDisks.Run( self.VARS, self.LOG )
266 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
267 ChainBootNode.Run( self.VARS, self.LOG )
273 # implements debug logic, which just starts the sshd
274 # and just waits around
275 StartDebug.Run( self.VARS, self.LOG )
278 # should never happen; log event
279 self.LOG.write( "\nInvalid BOOT_STATE = %s\n" % self.VARS['BOOT_STATE'])
280 self.VARS['BOOT_STATE']= 'dbg'
281 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
282 StartDebug.Run( self.VARS, self.LOG )
284 # setup state -> function hash table
285 states = {'new':_newRun,
291 InitializeBootManager.Run( self.VARS, self.LOG )
292 ReadNodeConfiguration.Run( self.VARS, self.LOG )
293 AuthenticateWithPLC.Run( self.VARS, self.LOG )
294 GetAndUpdateNodeDetails.Run( self.VARS, self.LOG )
296 stateRun = states.get(self.VARS['BOOT_STATE'],_badRun)
300 self.LOG.write( "\n\nKeyError while running: %s\n" % str(e) )
301 except BootManagerException, e:
302 self.LOG.write( "\n\nException while running: %s\n" % str(e) )
308 def RunInstaller(self):
310 since the installer can be invoked at more than one place
311 in the boot manager logic, seperate the steps necessary
315 InstallInit.Run( self.VARS, self.LOG )
316 InstallPartitionDisks.Run( self.VARS, self.LOG )
317 InstallBootstrapRPM.Run( self.VARS, self.LOG )
318 InstallBase.Run( self.VARS, self.LOG )
319 InstallWriteConfig.Run( self.VARS, self.LOG )
320 InstallBuildVServer.Run( self.VARS, self.LOG )
321 InstallNodeInit.Run( self.VARS, self.LOG )
322 InstallUninitHardware.Run( self.VARS, self.LOG )
324 self.VARS['BOOT_STATE']= 'boot'
325 self.VARS['STATE_CHANGE_NOTIFY']= 1
326 self.VARS['STATE_CHANGE_NOTIFY_MESSAGE']= \
327 notify_messages.MSG_INSTALL_FINISHED
328 UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
330 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
334 if __name__ == "__main__":
336 # set to 0 if no error occurred
339 # all output goes through this class so we can save it and post
340 # the data back to PlanetLab central
343 LOG.LogEntry( "BootManager started at: %s" % \
344 strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
349 LOG.LogEntry( "Unable to initialize BootManager." )
351 LOG.LogEntry( "Running version %s of BootManager." %
355 LOG.LogEntry( "\nDone!" );
357 LOG.LogEntry( "\nError occurred!" );
360 traceback.print_exc(file=LOG.OutputFile)
361 traceback.print_exc()
363 LOG.LogEntry( "BootManager finished at: %s" % \
364 strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )