def checkStatus(self): try: clientConfig = self.mainParent.clientConfig logger.debug("Connecting to status API") response = self.rest.POSTRequest(uri=ClientURISettings.getStatusUri(), content=clientConfig.getConfigurationXml()) self.handleStatusResponse(response, self.mainParent) except Exception as err: logger.error("Error: " + str(err))
def register(self): rest = RESTClient() if (not self.mac): self.mac = ProcessExecutor.getMACeth0() if (not self.serialNumber): self.serialNumber = ProcessExecutor.getSerialNumber() while not self.isRegistered(): try: logger.debug("Starting REGISTER Request, MAC %s, serial %s" % (self.mac, self.serialNumber)) formparams = {} formparams["mac"] = self.mac formparams["serialNumber"] = self.serialNumber formparams[ "major"] = self.mainParent.clientConfig.software.major formparams[ "minor"] = self.mainParent.clientConfig.software.minor formparams[ "build"] = self.mainParent.clientConfig.software.build formparams["kernel"] = ProcessExecutor.getKernelVersion() formparams["platform"] = "BGAX4" r = rest.PUTRequest(uri=ClientURISettings.getRegistrationUri(), content=formparams) if (r == None): logger.error("No response from server, check connectivity") elif (r.status_code == HttpStatus.SC_FORBIDDEN): logger.error( "Device %s already registered in database, please remedy" % self.mac) elif (r.status_code == HttpStatus.SC_OK): xmlNode = XMLUtils.stringToElement(r.content) token = XMLParser.getTokenFromRegisterResponse(xmlNode) self.mainParent.clientConfig.setToken(token) self.mainParent.clientConfig.authentication.setToken(token) self.mainParent.clientConfig.saveConfiguration() logger.info("Successful registration, token %s received" % token) return else: logger.error("Status: %d, content: %s" % (r.status_code, r.content)) except Exception as err: logger.error(err) # If registration failed sleep 60 seconds before retrying logger.debug("Registration failed, sleeping %d s" % TimeConstants.ONE_MINUTE_SECONDS) time.sleep(TimeConstants.ONE_MINUTE_SECONDS)
def checkStatus(self): try: clientConfig = self.mainParent.clientConfig logger.debug("Connecting to status API") response = self.rest.POSTRequest( uri=ClientURISettings.getStatusUri(), content=clientConfig.getConfigurationXml()) self.handleStatusResponse(response, self.mainParent) except Exception as err: logger.error("Error: " + str(err))
def uploadFile(self, fullPath): if ( os.path.exists(fullPath) and os.path.isfile(fullPath)): fileName = os.path.basename(fullPath) form = {"metadata": fileName} files = {'body': (fileName, open(fullPath, 'rb'))} r = self.rest.POSTFiles(ClientURISettings.getUploadUri(), data=form, files=files) if ( r.status_code == HttpStatus.SC_OK ): logger.info("%s uploaded" % fileName) else: logger.error("%s upload failed, %s: " % (fileName, r.content)) return r
def register(self): rest = RESTClient() if ( not self.mac ): self.mac = ProcessExecutor.getMACeth0() if ( not self.serialNumber ): self.serialNumber = ProcessExecutor.getSerialNumber() while not self.isRegistered(): try: logger.debug("Starting REGISTER Request, MAC %s, serial %s" % (self.mac, self.serialNumber)) formparams = {} formparams["mac"] = self.mac formparams["serialNumber"] = self.serialNumber formparams["major"] = self.mainParent.clientConfig.software.major formparams["minor"] = self.mainParent.clientConfig.software.minor formparams["build"] = self.mainParent.clientConfig.software.build formparams["kernel"] = ProcessExecutor.getKernelVersion() formparams["platform"] = "BGAX4" r = rest.PUTRequest(uri=ClientURISettings.getRegistrationUri(), content=formparams) if ( r == None ): logger.error("No response from server, check connectivity") elif ( r.status_code == HttpStatus.SC_FORBIDDEN ): logger.error("Device %s already registered in database, please remedy" % self.mac) elif ( r.status_code == HttpStatus.SC_OK ): xmlNode = XMLUtils.stringToElement(r.content) token = XMLParser.getTokenFromRegisterResponse(xmlNode) self.mainParent.clientConfig.setToken(token) self.mainParent.clientConfig.authentication.setToken(token) self.mainParent.clientConfig.saveConfiguration() logger.info("Successful registration, token %s received" % token) return else: logger.error("Status: %d, content: %s" % (r.status_code, r.content)) except Exception as err: logger.error(err) # If registration failed sleep 60 seconds before retrying logger.debug("Registration failed, sleeping %d s" % TimeConstants.ONE_MINUTE_SECONDS) time.sleep(TimeConstants.ONE_MINUTE_SECONDS)
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 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)