def logcrit(message,context_id=0): """ Wrapper to make sure (at least my modules) contain a context_id """ logging.crit("%s:%s" % (context_id, message))
def run(cmd, exit=True, cwd=None): debg(cmd) if subprocess.Popen(cmd.split(), cwd=cwd).wait() != 0: if exit: crit('Failed!') sys.exit(1) else: eror('Ignoring failure.')
pollFile = 'currentpoll.yaml' movieFile = 'movielist.yaml' configFile = 'botconfig.yaml' required_configs = [ "bot_discord_token", "guild" ] if os.path.exists(configFile): botConfig = storage.load(configFile)["config"] logging.debug("This is what botConfit holds %s", botConfig) else: logging.critical("Unable to find config file, please create %s as described in the README", configFile) sys.exit(1) for entry in required_configs: if entry not in botConfig: logging.crit("Required config entry %s not found in configuration.", entry) sys.exit(1) TOKEN = botConfig['bot_discord_token'] GUILD = botConfig['guild'] if os.path.exists(pollFile): #Code to read file and save it as "pollData" logging.debug("Found file '%s'", pollFile) pollData = storage.load(pollFile) #Instantiate poll, passing it data and updating the isActive flag because we know a poll was already started. Poor bot probably died. logging.info("Restarting saved poll.") logging.debug("Using data %s", pollData) currentPoll = Polls.Poll(suggestion = pollData, isActive = True) else: #Instantiate poll, without passing it data
def putSecret(self, clientIP, secret, not_before=None, not_after=None, tries=0): """ store a secret for clientIP @param clientIP : client ip address @param secret : radius secret for client @param tries : internal parameter to constrain recursion depth for self-calls @type clientIP string @type secret example usage: from radiussecrets import * rs=ClientRadiusSecrets(encryption_key='someencryptionkey', aws_keys=AWSKeys('myaccesskey','mysecretkey'),table_name='qradius_secrets') ValidationException rs.putSecret('1.2.3.4','shhdonottellanyone') """ now = time.time() if not_before is None: not_before = now if not_after is None: not_after = now + DEFAULT_KEY_LIFE if not isinstance(not_before, (int, float, long)) or not_before < 0: raise ValueError( "not_before must be a number representing seconds since epoch") if not isinstance(not_after, (int, float, long)) or not_after < 0: raise ValueError( "not_before must be a number representing seconds since epoch") if len(secret) > MAX_SECRET_LENGTH: raise ValueError("length of secret may not exceed %d bytes" % MAX_SECRET_LENGTH) result = None try: result = self._secret_table.put_item( data={ 'ip_address': clientIP, 'not_before': not_before, 'not_after': not_after, 'secret': self.encryptSecret(secret) }) except boto.dynamodb2.exceptions.ConditionalCheckFailedException as e: tries += 1 if tries > 5: logging.crit( 'pk violation for client %s not_before %d after %d tries at incrementing' % (clientIP, not_before, tries)) raise e #increment not_before to avoid pk violation not_before += 1 logging.warn( 'pk violation for client %s not_before %d; retrying with higher not_before ' % (clientIP, not_before)) result = self.putSecret(clientIP, secret, not_before=not_before, not_after=not_after, tries=tries) return result
def putSecret(self, clientIP, secret, not_before=None,not_after=None,tries=0): """ store a secret for clientIP @param clientIP : client ip address @param secret : radius secret for client @param tries : internal parameter to constrain recursion depth for self-calls @type clientIP string @type secret example usage: from radiussecrets import * rs=ClientRadiusSecrets(encryption_key='someencryptionkey', aws_keys=AWSKeys('myaccesskey','mysecretkey'),table_name='qradius_secrets') ValidationException rs.putSecret('1.2.3.4','shhdonottellanyone') """ now = time.time() if not_before is None: not_before = now if not_after is None: not_after = now + DEFAULT_KEY_LIFE if not isinstance(not_before,(int,float,long)) or not_before < 0: raise ValueError("not_before must be a number representing seconds since epoch") if not isinstance(not_after,(int,float,long)) or not_after < 0: raise ValueError("not_before must be a number representing seconds since epoch") if len(secret) > MAX_SECRET_LENGTH: raise ValueError("length of secret may not exceed %d bytes" % MAX_SECRET_LENGTH) result = None try: result = self._secret_table.put_item(data={ 'ip_address': clientIP, 'not_before': not_before, 'not_after': not_after, 'secret': self.encryptSecret(secret) }) except boto.dynamodb2.exceptions.ConditionalCheckFailedException as e: tries += 1 if tries > 5: logging.crit('pk violation for client %s not_before %d after %d tries at incrementing' % (clientIP,not_before,tries)) raise e #increment not_before to avoid pk violation not_before += 1 logging.warn('pk violation for client %s not_before %d; retrying with higher not_before ' % (clientIP,not_before)) result = self.putSecret(clientIP, secret, not_before=not_before,not_after=not_after,tries=tries) return result