Esempio n. 1
0
    def run():
        global DeviceClientInstance

        if not LogRotation.isRotationNeeded():
            logger.debug("Log rotation not needed at this time")
            return

        logger.info("Running log rotation")

        ProcessExecutor.stopBluetooth()
        ProcessExecutor.stopLighttpd()

        try:
            newProps = SystemWriter.writeLogProperties(None, False)

            try:
                logger.info(
                    "Rotating bluetooth log, new file is %s" %
                    newProps[ClientURISettings.BLUETOOTH_TRANSFER_PROP])
                logger.info("Rotating wi-fi log, new file is %s" %
                            newProps[ClientURISettings.WIFI_TRANSFER_PROP])
            except:
                pass  # don't care

            filesToKeep = newProps.values()

            workingDir = ClientURISettings.LOG_WORKING
            for file in os.listdir(workingDir):
                if file not in filesToKeep:
                    sourceFile = ClientURISettings.LOG_WORKING + os.sep + file
                    destFile = ClientURISettings.LOG_QUEUE + os.sep + file
                    logger.info("Moving %s to queue" % sourceFile)
                    shutil.move(sourceFile, destFile)
                else:
                    logger.info("%s is active" % file)

            campaignHandler = DeviceClientInstance.campaignHandler
            if (campaignHandler != None):
                activeBTCampaign = campaignHandler.getActiveCampaign(
                    ClientURISettings.MESSAGE_BLUETOOTH)
                activeWifiCampaign = campaignHandler.getActiveCampaign(
                    ClientURISettings.MESSAGE_WIFI)

                # we write some additional things here because it also runs at startup
                SystemWriter.writeHostAPDConfFile(activeWifiCampaign, False)
                SystemWriter.makeLighttpdConfFile(activeWifiCampaign, False)
                SystemWriter.writeObexSenderConfig(activeBTCampaign)
                SystemWriter.writeBluetoothConfig(activeBTCampaign)

        except Exception as err:
            logger.error(err)

        try:
            ProcessExecutor.startBluetooth()
            ProcessExecutor.startLighttpd()
        except Exception as err:
            logger.error(err)
 def run():
     global DeviceClientInstance
     
     if not LogRotation.isRotationNeeded():
         logger.debug("Log rotation not needed at this time")
         return
     
     logger.info("Running log rotation")
     
     ProcessExecutor.stopBluetooth()
     ProcessExecutor.stopLighttpd()
     
     try:
         newProps = SystemWriter.writeLogProperties(None, False)
         
         try:
             logger.info("Rotating bluetooth log, new file is %s" % newProps[ClientURISettings.BLUETOOTH_TRANSFER_PROP])
             logger.info("Rotating wi-fi log, new file is %s" % newProps[ClientURISettings.WIFI_TRANSFER_PROP])
         except:
             pass # don't care
         
         filesToKeep = newProps.values()
         
         workingDir = ClientURISettings.LOG_WORKING
         for file in os.listdir(workingDir):
             if file not in filesToKeep:
                 sourceFile = ClientURISettings.LOG_WORKING + os.sep + file
                 destFile = ClientURISettings.LOG_QUEUE + os.sep + file
                 logger.info("Moving %s to queue" % sourceFile)
                 shutil.move(sourceFile, destFile)
             else:
                 logger.info("%s is active" % file)
  
         campaignHandler = DeviceClientInstance.campaignHandler
         if ( campaignHandler != None):
             activeBTCampaign = campaignHandler.getActiveCampaign(ClientURISettings.MESSAGE_BLUETOOTH)
             activeWifiCampaign = campaignHandler.getActiveCampaign(ClientURISettings.MESSAGE_WIFI)
             
             # we write some additional things here because it also runs at startup
             SystemWriter.writeHostAPDConfFile(activeWifiCampaign, False)
             SystemWriter.makeLighttpdConfFile(activeWifiCampaign, False)
             SystemWriter.writeObexSenderConfig(activeBTCampaign) 
             SystemWriter.writeBluetoothConfig(activeBTCampaign)    
         
     except Exception as err:
         logger.error(err)
     
     try:
         ProcessExecutor.startBluetooth()
         ProcessExecutor.startLighttpd()
     except Exception as err:
         logger.error(err)
         
Esempio n. 3
0
    def run():
        global DeviceClientInstance
        
        # See which campaign is active now, if it's not right, 
        # the right way to see which Wi-Fi is active is to read the lighttpd.conf
        # and look at the 'server.document-root'
        logger.debug("WifiActiveCampaignCron running...")
        
        currentCampaignId = None
        
        try:
            if (os.path.exists(ClientURISettings.LIGHTTPD_FILE)):
                inf = open(ClientURISettings.LIGHTTPD_FILE, "r")
                
                for line in inf.readlines():
                    #  """server.document-root = "%s"\n""" % doc_root
                    if line.find("server.document-root") == 0:
                        try:
                            dirs = line.split('"')[1].split("/")
                            if len(dirs) == 4:
                                # no active campaign
                                pass
                            else:
                                currentCampaignId = dirs[4]
                            break
                        except Exception as err:
                            logger.info(err)
                inf.close()
            
                if (currentCampaignId == ClientURISettings.MESSAGE_NOACTIVE):
                    currentCampaignId = None
            else:
                # lighttpd doesn't exist, so we should ensure below that one gets written
                SystemWriter.makeLighttpdConfFile(None, True)
                SystemWriter.writeCaptivePortalConf(None)
                
            correctActiveCampaign = DeviceClientInstance.campaignHandler.getActiveCampaign(ClientURISettings.MESSAGE_WIFI)
            
            # Check to see if the Channel is correct, if not, that needs to happen too
            currentChannel = None
            currentAccessPointInterface = None
            currentSSID = None
            
            if (Platform.getPlatform() == Platform.PLATFORM_LINUX_DDWRT):
                hostAPDFile = ClientURISettings.HOSTAPD_CONF_FILE_DDWRT
            else:
                hostAPDFile = ClientURISettings.HOSTAPD_CONF_FILE
           
            
            if (os.path.exists(hostAPDFile)):
                inf = open(hostAPDFile, "r")
                for line in inf.readlines():
                    #  """server.document-root = "%s"\n""" % doc_root
                    if line.find("channel") == 0:
                        try:
                            currentChannel = line.split("=")[1].strip()
                        except Exception as err:
                            logger.info(err)
                    elif line.find("interface") == 0:
                        try:
                            currentAccessPointInterface = line.split("=")[1].strip()
                        except Exception as err:
                            logger.info(err)
                    elif line.find("ssid") == 0:
                        try:
                            currentSSID = line.split("=")[1].strip()
                        except Exception as err:
                            logger.info(err)
                inf.close()
            
            correctChannel = DeviceClientInstance.clientConfig.channel
            
            correctAccessPointInterface = SystemWriter.getCurrentAccessPointInterface()
            
            try:
                correctSSID = correctActiveCampaign.wifiCampaign.networkName
            except:
                correctSSID = ClientURISettings.MESSAGE_DEFAULT_WIFI
            
            needHostAPDUpdate = False
            if (correctChannel != currentChannel):
                logger.info("Current channel: %s, correct channel is %s" % (currentChannel, correctChannel))
                needHostAPDUpdate = True
            
            if (correctSSID != currentSSID):
                logger.info("Current SSID: %s, correct SSID is %s" % (currentSSID, correctSSID))
                needHostAPDUpdate = True
                
            if (correctAccessPointInterface != currentAccessPointInterface):
                logger.info("Current interface: %s, correct interface is %s" % (currentAccessPointInterface, correctAccessPointInterface))
                needHostAPDUpdate = True
            

            needCaptivePortalConfUpdate = False 
            #correct
            correctBackhaul = SystemWriter.getCorrectExtBackhaulInterface()
            #current
            currentBackhaul = SystemWriter.getCurrentExtBackhaulInterface()
           
            
            if (currentBackhaul != correctBackhaul):
                logger.info("current (%s) != correct (%s)" % (str(currentBackhaul), str(correctBackhaul)))
                needCaptivePortalConfUpdate = True


            # Make sure the hotspot domains are correct
            WifiActiveCampaignCron.ensureDNSMasqCorrect(correctActiveCampaign)
            
            
            """
            Possibilites:
                1) No campaign is active and none should be  -- RETURN
                2) A campaign is active, and it's the right one -- RETURN
                3) A campaign is active, but none should be -- REDO AS DEFAULT CONFIG & RESTART
                4) No campaign is active and one should be -- REDO CONFIG & RESTART
                5) A campaign is active, but it's the wrong one  -- REDO CONFIG & RESTART
                
                For the Wi-Fi Channel, if it is incorrect, writing hostapd.conf and restarting lighttpd will fix it.  
                
            """
            logger.debug("Need HOSTAPD Update: %s " % str(needHostAPDUpdate))
            logger.debug("Need CaptivePortalConf Update: %s" % str(needCaptivePortalConfUpdate))
            
            if ((currentCampaignId == None) and (correctActiveCampaign == None)):
                if (needHostAPDUpdate):
                    SystemWriter.writeHostAPDConfFile(None, True)
                if (needCaptivePortalConfUpdate):
                    SystemWriter.writeCaptivePortalConf(currentCampaignId)
                    
                logger.debug("No campaign changes necessary, no campaign active")
                
            elif ((currentCampaignId != None) and (correctActiveCampaign != None) and ( int(currentCampaignId) == int(correctActiveCampaign.id) ) ):
                if (needHostAPDUpdate):
                    SystemWriter.writeHostAPDConfFile(correctActiveCampaign, True)
                if (needCaptivePortalConfUpdate):
                    SystemWriter.writeCaptivePortalConf(correctActiveCampaign)
                    
                logger.debug("No campaign changes necessary, campaign %s active" % str(correctActiveCampaign.id))
                
            elif (correctActiveCampaign == None):
                logger.info("No campaign should be active, deactivating")
                SystemWriter.setLogProperty(ClientURISettings.WIFI_TRANSFER_PROP, SystemWriter.getFileName(ClientURISettings.MESSAGE_WIFI, None))
                SystemWriter.writeCaptivePortalConf(None)
                SystemWriter.makeLighttpdConfFile(None, False)
                SystemWriter.writeHostAPDConfFile(None, True)
                
                if (Platform.getPlatform() == Platform.PLATFORM_LINUX_DREAMPLUG):
                    ProcessExecutor.openCaptiveOrLimitedPortal()
                
            else:
                logger.info("Campaign changes needed, updating and restarting with from campaign [%s] to active [%s]" % (str(currentCampaignId), (str(correctActiveCampaign.id))))
                SystemWriter.setLogProperty(ClientURISettings.WIFI_TRANSFER_PROP, SystemWriter.getFileName(ClientURISettings.MESSAGE_WIFI, correctActiveCampaign.id))
                SystemWriter.writeCaptivePortalConf(correctActiveCampaign)
                SystemWriter.makeLighttpdConfFile(correctActiveCampaign, False)
                SystemWriter.writeHostAPDConfFile(correctActiveCampaign, True)
                #CAMPAIGN_NO_INTERNET = 1
                #CAMPAIGN_LIMITED = 2
                #CAMPAIGN_HOTSPOT = 3
                #CAMPAIGN_MOBILOZOPHY = 4
                #CAMPAIGN_FACEBOOK = 5
                if (Platform.getPlatform() == Platform.PLATFORM_LINUX_DREAMPLUG):
                    if (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_HOTSPOT)):
                        ProcessExecutor.openHotspotPortal()
                    elif (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_NO_INTERNET)):
                        ProcessExecutor.openCaptiveOrLimitedPortal()
                    elif (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_LIMITED)):
                        ProcessExecutor.openCaptiveOrLimitedPortal()
                    elif (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_FACEBOOK)):
                        ProcessExecutor.openFacebookPortal()                    
                    else:
                        logger.error("Not sure what to do with wifi campaign of type %s" % correctActiveCampaign.wifiCampaign.hotspotMode)
                else:
                    logger.debug("Alternate campaign modes not set up for this platform")
            
        except Exception as err:
            logger.error(err)
            
               
    def run():
        global DeviceClientInstance
        
        # See which campaign is active now, if it's not right, 
        # the right way to see which Wi-Fi is active is to read the lighttpd.conf
        # and look at the 'server.document-root'
        logger.debug("WifiActiveCampaignCron running...")
        
        currentCampaignId = None
        
        try:
            if (os.path.exists(ClientURISettings.LIGHTTPD_FILE)):
                inf = open(ClientURISettings.LIGHTTPD_FILE, "r")
                
                for line in inf.readlines():
                    #  """server.document-root = "%s"\n""" % doc_root
                    if line.find("server.document-root") == 0:
                        try:
                            dirs = line.split('"')[1].split("/")
                            if len(dirs) == 4:
                                # no active campaign
                                pass
                            else:
                                currentCampaignId = dirs[4]
                            break
                        except Exception as err:
                            logger.info(err)
                inf.close()
            
                if (currentCampaignId == ClientURISettings.MESSAGE_NOACTIVE):
                    currentCampaignId = None
            else:
                # lighttpd doesn't exist, so we should ensure below that one gets written
                SystemWriter.makeLighttpdConfFile(None, True)
                SystemWriter.writeCaptivePortalConf(None)
                
            correctActiveCampaign = DeviceClientInstance.campaignHandler.getActiveCampaign(ClientURISettings.MESSAGE_WIFI)
            
            # Check to see if the Channel is correct, if not, that needs to happen too
            currentChannel = None
            currentAccessPointInterface = None
            currentSSID = None
            
            if (Platform.getPlatform() == Platform.PLATFORM_LINUX_DDWRT):
                hostAPDFile = ClientURISettings.HOSTAPD_CONF_FILE_DDWRT
            else:
                hostAPDFile = ClientURISettings.HOSTAPD_CONF_FILE
           
            
            if (os.path.exists(hostAPDFile)):
                inf = open(hostAPDFile, "r")
                for line in inf.readlines():
                    #  """server.document-root = "%s"\n""" % doc_root
                    if line.find("channel") == 0:
                        try:
                            currentChannel = line.split("=")[1].strip()
                        except Exception as err:
                            logger.info(err)
                    elif line.find("interface") == 0:
                        try:
                            currentAccessPointInterface = line.split("=")[1].strip()
                        except Exception as err:
                            logger.info(err)
                    elif line.find("ssid") == 0:
                        try:
                            currentSSID = line.split("=")[1].strip()
                        except Exception as err:
                            logger.info(err)
                inf.close()
            
            correctChannel = DeviceClientInstance.clientConfig.channel
            
            correctAccessPointInterface = SystemWriter.getCurrentAccessPointInterface()
            
            try:
                correctSSID = correctActiveCampaign.wifiCampaign.networkName
            except:
                correctSSID = ClientURISettings.MESSAGE_DEFAULT_WIFI
            
            needHostAPDUpdate = False
            if (correctChannel != currentChannel):
                logger.info("Current channel: %s, correct channel is %s" % (currentChannel, correctChannel))
                needHostAPDUpdate = True
            
            if (correctSSID != currentSSID):
                logger.info("Current SSID: %s, correct SSID is %s" % (currentSSID, correctSSID))
                needHostAPDUpdate = True
                
            if (correctAccessPointInterface != currentAccessPointInterface):
                logger.info("Current interface: %s, correct interface is %s" % (currentAccessPointInterface, correctAccessPointInterface))
                needHostAPDUpdate = True
            

            needCaptivePortalConfUpdate = False 
            #correct
            correctBackhaul = SystemWriter.getCorrectExtBackhaulInterface()
            #current
            currentBackhaul = SystemWriter.getCurrentExtBackhaulInterface()
           
            
            if (currentBackhaul != correctBackhaul):
                logger.info("current (%s) != correct (%s)" % (str(currentBackhaul), str(correctBackhaul)))
                needCaptivePortalConfUpdate = True


            # Make sure the hotspot domains are correct
            WifiActiveCampaignCron.ensureDNSMasqCorrect(correctActiveCampaign)
            
            
            """
            Possibilites:
                1) No campaign is active and none should be  -- RETURN
                2) A campaign is active, and it's the right one -- RETURN
                3) A campaign is active, but none should be -- REDO AS DEFAULT CONFIG & RESTART
                4) No campaign is active and one should be -- REDO CONFIG & RESTART
                5) A campaign is active, but it's the wrong one  -- REDO CONFIG & RESTART
                
                For the Wi-Fi Channel, if it is incorrect, writing hostapd.conf and restarting lighttpd will fix it.  
                
            """
            logger.debug("Need HOSTAPD Update: %s " % str(needHostAPDUpdate))
            logger.debug("Need CaptivePortalConf Update: %s" % str(needCaptivePortalConfUpdate))
            
            if ((currentCampaignId == None) and (correctActiveCampaign == None)):
                if (needHostAPDUpdate):
                    SystemWriter.writeHostAPDConfFile(None, True)
                if (needCaptivePortalConfUpdate):
                    SystemWriter.writeCaptivePortalConf(currentCampaignId)
                    
                logger.debug("No campaign changes necessary, no campaign active")
                
            elif ((currentCampaignId != None) and (correctActiveCampaign != None) and ( int(currentCampaignId) == int(correctActiveCampaign.id) ) ):
                if (needHostAPDUpdate):
                    SystemWriter.writeHostAPDConfFile(correctActiveCampaign, True)
                if (needCaptivePortalConfUpdate):
                    SystemWriter.writeCaptivePortalConf(correctActiveCampaign)
                    
                logger.debug("No campaign changes necessary, campaign %s active" % str(correctActiveCampaign.id))
                
            elif (correctActiveCampaign == None):
                logger.info("No campaign should be active, deactivating")
                SystemWriter.setLogProperty(ClientURISettings.WIFI_TRANSFER_PROP, SystemWriter.getFileName(ClientURISettings.MESSAGE_WIFI, None))
                SystemWriter.writeCaptivePortalConf(None)
                SystemWriter.makeLighttpdConfFile(None, False)
                SystemWriter.writeHostAPDConfFile(None, True)
                
                if (Platform.getPlatform() == Platform.PLATFORM_LINUX_DREAMPLUG):
                    ProcessExecutor.openCaptiveOrLimitedPortal()
                
            else:
                logger.info("Campaign changes needed, updating and restarting with from campaign [%s] to active [%s]" % (str(currentCampaignId), (str(correctActiveCampaign.id))))
                SystemWriter.setLogProperty(ClientURISettings.WIFI_TRANSFER_PROP, SystemWriter.getFileName(ClientURISettings.MESSAGE_WIFI, correctActiveCampaign.id))
                SystemWriter.writeCaptivePortalConf(correctActiveCampaign)
                SystemWriter.makeLighttpdConfFile(correctActiveCampaign, False)
                SystemWriter.writeHostAPDConfFile(correctActiveCampaign, True)
                #CAMPAIGN_NO_INTERNET = 1
                #CAMPAIGN_LIMITED = 2
                #CAMPAIGN_HOTSPOT = 3
                #CAMPAIGN_MOBILOZOPHY = 4
                #CAMPAIGN_FACEBOOK = 5
                if (Platform.getPlatform() == Platform.PLATFORM_LINUX_DREAMPLUG):
                    if (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_HOTSPOT)):
                        ProcessExecutor.openHotspotPortal()
                    elif (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_NO_INTERNET)):
                        ProcessExecutor.openCaptiveOrLimitedPortal()
                    elif (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_LIMITED)):
                        ProcessExecutor.openCaptiveOrLimitedPortal()
                    elif (int(correctActiveCampaign.wifiCampaign.hotspotMode) == int(ClientURISettings.CAMPAIGN_FACEBOOK)):
                        ProcessExecutor.openFacebookPortal()                    
                    else:
                        logger.error("Not sure what to do with wifi campaign of type %s" % correctActiveCampaign.wifiCampaign.hotspotMode)
                else:
                    logger.debug("Alternate campaign modes not set up for this platform")
            
        except Exception as err:
            logger.error(err)