def makeDefaultIndexPage():
     try:
         html = "<html><head><title>Proximus Mobility - Proximus Mobility, LLC.</title></head>"
         html += "<body><h1>Proximus Mobility</h1><div id=\"footer-address\"><p>75 Fifth Street NW, Atlanta, GA 30308</p>"
         html += "<p>Toll Free : 1.888.665.2527 <br> Direct : 404.477.3310 <br> Fax : 775.269.0387<br>Email : [email protected]</p>"
         html += "</div></body></html>"
         FileSystemChecker.initDir(ClientURISettings.CAMPAIGNS_ROOT_DIR)
         filePath = ClientURISettings.CAMPAIGNS_ROOT_DIR + os.sep + "index.html"
         SystemWriter.writeFile(filePath, html)
         
     except Exception as err:
             logger.error(err)
 def makeErrorsPage():
     try:
         FileSystemChecker.initDir(ClientURISettings.ERROR_ROOT_DIR)
         filename = ClientURISettings.ERROR_ROOT_DIR + os.sep + "404.php"
         writer = open(filename, "w")
         writer.write("<?php")
         writer.write("\n")
         writer.write("header(\"Location: http://%s/\")" % ClientURISettings.ACCESS_POINT)
         writer.write("\n")
         writer.write("?>")
         writer.write("\n")
         writer.close()
     except Exception as err:
         logger.error(err)
 def initFileSystem(self):
     # Filesystem setup
     dirs = []
     # Config Dir
     dirs.append(ClientURISettings.CONFIG_ROOT)
     dirs.append(ClientURISettings.DNSMASQ_CONFIG_FOLDER)
     dirs.append(ClientURISettings.BIN_DIR)
     dirs.append(ClientURISettings.CAPTIVEPORTAL_CONFIG_DIR)
   
     # Campaigns Dir
     dirs.append(ClientURISettings.CAMPAIGNS_ROOT_DIR)
     # Logs Dirs
     dirs.append(ClientURISettings.LOG_ROOT)
     dirs.append(ClientURISettings.LOG_COMPLETED)
     dirs.append(ClientURISettings.LOG_QUEUE)
     dirs.append(ClientURISettings.LOG_WORKING)
     
     FileSystemChecker.initDirs(dirs)
     # Setting up the default WEB CONTENT and config properties
     SystemWriter.makeDefaultIndexPage()
     SystemWriter.makeErrorsPage()
     ProcessExecutor.giveLogPermission()
    def handle(self, action):
        logging.info("Got software update action")
        path = XMLUtils.getAttributeSafe(action.actionNode, "path", None)
        if ( path != None ):
            # download the file
            restClient = RESTClient()
            
            url =  ClientURISettings.getSoftwareUpdateURI()
            if path.find("/") != 0:
                url += "/"
            url += path
            
            filename = os.path.basename(path)
            
            FileSystemChecker.initDir(ClientURISettings.SWUPDATE_ROOT_DIR )
            downloadPath = ClientURISettings.SWUPDATE_ROOT_DIR + os.sep + filename

            if ( os.path.exists(downloadPath )):
                success = True
            else:
                success = restClient.GETFile(url, downloadPath)
            
            if ( not success ):
                logger.error("Failed to download software update at URL '%s' to file '%s'" % (url, downloadPath))
            elif ( success and not os.path.exists(downloadPath)):
                success = False
                logger.error("Failure in saving downloaded wpk file to %s" % downloadPath)  
            else:
                # success, move to /tmp/obex to make it auto-install the WPK file
                logger.info("Installation of %s beginning" % downloadPath)
                fileName = os.path.basename(downloadPath)
                FileSystemChecker.initDir("/tmp/obex")
                destination = "/tmp/obex/%s" % fileName
                shutil.copy(downloadPath, destination)
                
                
                
    def run(self):
        for campaign in self.campaignList:
            success = False
            
            campaignFolder = ClientURISettings.CAMPAIGNS_ROOT_DIR + os.sep + campaign.id + os.sep + self.campaignType
            downloadFolder = ClientURISettings.CAMPAIGNS_ROOT_DIR + os.sep + campaign.id + os.sep + "download"
            FileSystemChecker.initDir(campaignFolder)
            FileSystemChecker.initDir(downloadFolder)

            # PATH contains the checksum so if the campaign changes, it will be re-downloaded
            if campaign.content.has_key(self.campaignType):
                
                if ( self.campaignType == ClientURISettings.MESSAGE_WIFI ):
                    checksum = campaign.wifiCampaign.checksum
                elif ( self.campaignType == ClientURISettings.MESSAGE_BLUETOOTH ):
                    checksum = campaign.bluetoothCampaign.checksum
                else:
                    checksum = "0"
                
                zipfileName = re.sub("[^0-9a-zA-Z_-]", "", "%s_%s_%s" % (checksum, self.campaignType, campaign.name)) + ".zip"
                zipFilePath = campaignFolder + os.sep + zipfileName
                zipFileDownloadPath = downloadFolder + os.sep + zipfileName
        
                if ( os.path.exists(zipFilePath)):
                    logger.info("Campaign %s (%s) already downloaded" % (str(campaign.id), self.campaignType))
                    continue
                
                else:
                    restClient = RESTClient()
                    while not success:
                        try:
                            url =  ClientURISettings.getDownloadUri() 
                            url += "/" + campaign.id 
                            url += "/" + self.campaignType
                            url += "/" + zipfileName
                            success = restClient.GETFile(url, zipFileDownloadPath)
                            
                            if ( not success ):
                                logger.error("Failure downloading file %s, sleeping 1 min" % url)
                                time.sleep(60)
                                continue
                                
                            elif ( success and not os.path.exists(zipFileDownloadPath)):
                                success = False
                                logger.error("Failure in saving downloaded zip file to %s" % zipFileDownloadPath)
                                if ( os.path.exists(zipFilePath)):
                                    logger.info("BUT zip file was downloaded at some point to %s" % zipFilePath)

                            if success:
                                try:
                                    campaign_zip = ZipFile(zipFileDownloadPath, 'r')
                                    fullCampaignPath = os.path.abspath(campaignFolder)
                                    # remove anything that was there
                                    shutil.rmtree(fullCampaignPath, ignore_errors=True)
                                    FileSystemChecker.initDir(fullCampaignPath)
                                    campaign_zip.extractall(fullCampaignPath)
                                    shutil.move(zipFileDownloadPath, zipFilePath)
                                    
                                    logger.info("Campaign ZIP %s extracted to %s" % ( zipfileName, campaignFolder))
                                except Exception as err:
                                    logger.error("Could not extract zip file %s" % (zipFilePath))
                                    logger.error(err)
                                    os.remove(zipFilePath)
                                    success = False
                                    time.sleep(60)
                                    
                        except Exception as err:
                            logger.error("Could not download campaign content: " + str(err))
                            time.sleep(60)
                    # Here indicates success
                    self.validateInstalledCampaign(campaignFolder)
 def initLogging():
     FileSystemChecker.initDir("logs")
     logging.config.fileConfig("conf" + os.sep + "logging.conf")
     logger.info("Logging initialized")
 def initLogging():
     FileSystemChecker.initDir("logs")
     logging.config.fileConfig("conf" + os.sep + "logging.conf")
     logger.info("Logging initialized")