Cleaned up model option processing
[bootmanager.git] / source / BootManager.py
index a118f66..9df68db 100755 (executable)
@@ -56,13 +56,13 @@ from gzip import GzipFile
 from steps import *
 from Exceptions import *
 import notify_messages
+import BootServerRequest
 
 
 
 # all output is written to this file
 LOG_FILE= "/tmp/bm.log"
-CURL_PATH= "curl"
-UPLOAD_LOG_URL = "http://boot.planet-lab.org/alpina-logs/upload.php"
+UPLOAD_LOG_PATH = "/alpina-logs/upload.php"
 
 # the new contents of PATH when the boot manager is running
 BIN_PATH= ('/usr/local/bin',
@@ -74,6 +74,12 @@ BIN_PATH= ('/usr/local/bin',
            '/usr/local/planetlab/bin')
            
 
+# the set of valid node run states
+NodeRunStates = {'new':None,
+                 'inst':None,
+                 'rins':None,
+                 'boot':None,
+                 'dbg':None}
 
 class log:
 
@@ -119,15 +125,16 @@ class log:
         """
 
         if self.OutputFile is not None:
-            self.LogEntry( "Uploading logs to %s" % UPLOAD_LOG_URL )
+            self.LogEntry( "Uploading logs to %s" % UPLOAD_LOG_PATH )
             
             self.OutputFile.close()
             self.OutputFile= None
-            
-            curl_cmd= "%s -s --connect-timeout 60 --max-time 600 " \
-                      "--form log=@%s %s" % \
-                      (CURL_PATH, self.OutputFilePath, UPLOAD_LOG_URL)
-            os.system( curl_cmd )
+
+            bs_request = BootServerRequest.BootServerRequest()
+            bs_request.MakeRequest(PartialPath = UPLOAD_LOG_PATH,
+                                   GetVars = None, PostVars = None,
+                                   FormData = ["log=@" + self.OutputFilePath],
+                                   DoSSL = True, DoCertCheck = True)
         
     
 
@@ -140,7 +147,10 @@ class BootManager:
     VARS_FILE = "configuration"
 
     
-    def __init__(self, log):
+    def __init__(self, log, forceState):
+        # override machine's current state from the command line
+        self.forceState = forceState
+
         # this contains a set of information used and updated
         # by each step
         self.VARS= {}
@@ -240,7 +250,7 @@ class BootManager:
                 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
                 ChainBootNode.Run( self.VARS, self.LOG )
             else:
-                self._nodeNotInstalled()
+                _nodeNotInstalled()
 
         def _newRun():
             # implements the new install logic, which will first check
@@ -252,7 +262,7 @@ class BootManager:
                 return 0
             self.VARS['BOOT_STATE']= 'rins'
             UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
-            self._rins()
+            _rinsRun()
 
         def _bootRun():
             # implements the boot logic, which consists of first
@@ -266,7 +276,7 @@ class BootManager:
                 SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
                 ChainBootNode.Run( self.VARS, self.LOG )
             else:
-                self._nodeNotInstalled()
+                _nodeNotInstalled()
 
 
         def _debugRun():
@@ -281,19 +291,26 @@ class BootManager:
             UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
             StartDebug.Run( self.VARS, self.LOG )            
 
+        global NodeRunStates
         # setup state -> function hash table
-        states = {'new':_newRun,
-                  'inst':_newRun,
-                  'rins':_rinsRun,
-                  'boot':_bootRun,
-                  'dbg':_debugRun}
+        NodeRunStates['new']  = _newRun
+        NodeRunStates['inst'] = _newRun
+        NodeRunStates['rins'] = _rinsRun
+        NodeRunStates['boot'] = _bootRun
+        NodeRunStates['dbg']  = _debugRun
+
         try:
             InitializeBootManager.Run( self.VARS, self.LOG )
             ReadNodeConfiguration.Run( self.VARS, self.LOG )
             AuthenticateWithPLC.Run( self.VARS, self.LOG )
             GetAndUpdateNodeDetails.Run( self.VARS, self.LOG )
 
-            stateRun = states.get(self.VARS['BOOT_STATE'],_badRun)
+            # override machine's current state from the command line
+            if self.forceState is not None:
+                self.VARS['BOOT_STATE']= self.forceState
+                UpdateBootStateWithPLC.Run( self.VARS, self.LOG )
+
+            stateRun = NodeRunStates.get(self.VARS['BOOT_STATE'],_badRun)
             stateRun()
 
         except KeyError, e:
@@ -315,7 +332,6 @@ class BootManager:
         InstallInit.Run( self.VARS, self.LOG )                    
         InstallPartitionDisks.Run( self.VARS, self.LOG )            
         InstallBootstrapRPM.Run( self.VARS, self.LOG )            
-        InstallBase.Run( self.VARS, self.LOG )            
         InstallWriteConfig.Run( self.VARS, self.LOG )
         InstallBuildVServer.Run( self.VARS, self.LOG )
         InstallNodeInit.Run( self.VARS, self.LOG )
@@ -330,9 +346,8 @@ class BootManager:
         SendHardwareConfigToPLC.Run( self.VARS, self.LOG )
 
     
-    
-if __name__ == "__main__":
-
+def main(argv):
+    global NodeRunStates
     # set to 0 if no error occurred
     error= 1
     
@@ -344,7 +359,28 @@ if __name__ == "__main__":
                   strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
 
     try:
-        bm= BootManager(LOG)
+        forceState = None
+        if len(argv) == 2:
+            fState = argv[1]
+            if NodeRunStates.has_key(fState):
+                forceState = fState
+                error = 0
+            else:
+                LOG.LogEntry("FATAL: cannot force node run state to=%s" % fState)
+    except:
+        traceback.print_exc(file=LOG.OutputFile)
+        traceback.print_exc()
+        
+    if error:
+        LOG.LogEntry( "BootManager finished at: %s" % \
+                      strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
+        LOG.Upload()
+        return error
+    else:
+        error = 1
+
+    try:
+        bm= BootManager(LOG,forceState)
         if bm.CAN_RUN == 0:
             LOG.LogEntry( "Unable to initialize BootManager." )
         else:
@@ -353,16 +389,20 @@ if __name__ == "__main__":
             success= bm.Run()
             if success:
                 LOG.LogEntry( "\nDone!" );
+                error = 0
             else:
                 LOG.LogEntry( "\nError occurred!" );
-
     except:
         traceback.print_exc(file=LOG.OutputFile)
         traceback.print_exc()
 
     LOG.LogEntry( "BootManager finished at: %s" % \
                   strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) )
-
     LOG.Upload()
+
+    return error
+
     
+if __name__ == "__main__":
+    error = main(sys.argv)
     sys.exit(error)