def notify (event_type, description, content=None): """Send Notifications""" global last_notification, notification_count # Throttle notifications to 15 a minute if (((time.time() - last_notification) < 60) and notification_count > 15): notification_count += 1 return elif ((time.time() - last_notification) > 60): notification_count = 0 #Log entry log_notify(event_type, description) #XMPP Configured? if (config.has_option('dbAlerter', 'xmpp_to')): xmpp_notify (event_type, description, content) #SMTP Configured? if (config.has_option('dbAlerter', 'smtp_to')): smtp_notify (event_type, description, content) notification_count += 1 last_notification = time.time()
def _extract_params(): params = {'storage_duration': '12H'} for name, default_value in params.items(): if not config.has_option('database', name): service_logger.warning("Not found '{name}' in database section, using default value: '{default_value}'." .format(name=name, default_value=default_value)) value = default_value else: value = config.get('database', name) params[name] = value params['database_dir'] = config.get('database', 'database_dir') params['listen_host'] = config.get('server', 'listen_host') params['listen_port'] = config.getint('server', 'listen_port') params['white_host'] = config.get('server', 'white_host') params['white_port'] = config.get('server', 'white_port') # Https configures. if config.has_option('security', 'tls') and config.getboolean('security', 'tls'): params['server_cert'] = config.get('security', 'server_cert') params['server_key'] = config.get('security', 'server_key') params['ca'] = config.get('security', 'ca') with open(os.path.join(global_vars.CURRENT_DIRNAME, 'certificate/pwf'), mode='r') as f: params['cert_pwd'] = f.read().strip() return params
def notify(event_type, description, content=None): """Send Notifications""" global last_notification, notification_count # Throttle notifications to 15 a minute if (((time.time() - last_notification) < 60) and notification_count > 15): notification_count += 1 return elif ((time.time() - last_notification) > 60): notification_count = 0 #Log entry log_notify(event_type, description) #XMPP Configured? if (config.has_option('dbAlerter', 'xmpp_to')): xmpp_notify(event_type, description, content) #SMTP Configured? if (config.has_option('dbAlerter', 'smtp_to')): smtp_notify(event_type, description, content) notification_count += 1 last_notification = time.time()
def show(section='', options='', config=None, deep=False): section, options = get_config_str(section, options, config) try: if section != '' and options == '': if config.has_section(section): print('''[{0}]'''.format(section)) for option in config[section]: print('\t{0}={1}'.format(option, config[section][option])) else: print('{0}属性未找到'.format(section)) elif section != '' and options != '': if config.has_option(section, options): print('{0}={1}'.format(options, config[section][options])) else: print('[{0}][{1}]属性未找到'.format(section, options)) elif section != '' and options != '' and ( not config.has_section(section) or not config.has_option(section, option)): print('属性不存在。') elif deep: for line in config: print('[{0}]'.format(line)) if deep and type(config) is configparser.ConfigParser: for row in config[line]: print('\t{0}={1}'.format(row, config[line][row])) else: print('超出预期') except configparser.InterpolationSyntaxError as e: print(e) print('特殊字符,请使用configparser.RawConfigParser,暂时不处理')
def check_params(): if not config.has_section('database'): service_logger.error("Not found 'database' section in config file.") else: if not config.has_option('database', 'database_path'): service_logger.error("Not found 'database_path' in database section.") if not config.has_section('server'): service_logger.error("Not found 'database' section in config file.") else: if not config.has_option('server', 'listen_host') or not config.has_option('server', 'listen_port'): service_logger.error("Not found 'listen_host' or 'listen_port' in server section.")
def Admin(self, handler, query): #Read config file new each time in case there was any outside edits config = ConfigParser.ConfigParser() config.read(config_file_path) shares_data = [] for section in config.sections(): if not (section.startswith('_tivo_') or section.startswith('Server')): if not (config.has_option(section, 'type')): shares_data.append( (section, dict(config.items(section, raw=True)))) elif config.get(section, 'type').lower() != 'admin': shares_data.append( (section, dict(config.items(section, raw=True)))) subcname = query['Container'][0] cname = subcname.split('/')[0] handler.send_response(200) handler.end_headers() t = Template( file=os.path.join(SCRIPTDIR, 'templates', 'settings.tmpl')) t.container = cname t.server_data = dict(config.items('Server', raw=True)) t.server_known = buildhelp.getknown('server') t.shares_data = shares_data t.shares_known = buildhelp.getknown('shares') t.tivos_data = [ (section, dict(config.items(section, raw=True))) for section in config.sections() \ if section.startswith('_tivo_')] t.tivos_known = buildhelp.getknown('tivos') t.help_list = buildhelp.gethelp() handler.wfile.write(t)
def Admin(self, handler, query): #Read config file new each time in case there was any outside edits config = ConfigParser.ConfigParser() config.read(config_file_path) shares_data = [] for section in config.sections(): if not(section.startswith('_tivo_') or section.startswith('Server')): if not(config.has_option(section,'type')): shares_data.append((section, dict(config.items(section, raw=True)))) elif config.get(section,'type').lower() != 'admin': shares_data.append((section, dict(config.items(section, raw=True)))) subcname = query['Container'][0] cname = subcname.split('/')[0] handler.send_response(200) handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'settings.tmpl')) t.container = cname t.server_data = dict(config.items('Server', raw=True)) t.server_known = buildhelp.getknown('server') t.shares_data = shares_data t.shares_known = buildhelp.getknown('shares') t.tivos_data = [ (section, dict(config.items(section, raw=True))) for section in config.sections() \ if section.startswith('_tivo_')] t.tivos_known = buildhelp.getknown('tivos') t.help_list = buildhelp.gethelp() handler.wfile.write(t)
def shutdown(): """Perform any cleanup code before we cease to exist""" notify.log_notify('Shutdown') #Remove pidfile if exists if (config.has_option('dbAlerter', 'pid_file')): pidfile = config.get('dbAlerter', 'pid_file') if os.path.exists(pidfile): os.remove(pidfile) sys.exit(0)
def _extract_params(): """ Check if the agent parameter is valid, if the parameter is valid, then return parameters dict, otherwise exit process. :return: agent parameters dict. """ params = {} host = config.get('server', 'host') listen_port = config.get('server', 'listen_port') params['host'] = host params['listen_port'] = listen_port default_params = { 'sink_timer_interval': '10S', 'source_timer_interval': '10S', 'channel_capacity': 1000 } for parameter, default in default_params.items(): try: if parameter == 'channel_capacity': agent_parameter_value = config.getint('agent', parameter) params[parameter] = agent_parameter_value else: agent_parameter_value = config.get('agent', parameter) params[parameter] = TimeString( agent_parameter_value).to_second() except Exception as e: agent_logger.error( "An error ({error}) occurs when acquiring {parameter}," " using default value: {default_value}.".format( parameter=parameter, error=e, default_value=default)) params[parameter] = default_params[parameter] params['db_host'] = config.get('agent', 'db_host') params['db_port'] = config.get('agent', 'db_port') params['db_type'] = config.get('agent', 'db_type') # Https configures. if config.has_option('security', 'tls') and config.getboolean( 'security', 'tls'): params['agent_cert'] = os.path.realpath( config.get('security', 'agent_cert')) params['agent_key'] = os.path.realpath( config.get('security', 'agent_key')) params['ca'] = os.path.realpath(config.get('security', 'ca')) pwd_file = os.path.join(global_vars.CURRENT_DIRNAME, 'certificate/pwf') with open(pwd_file, mode='r') as f: params['cert_pwd'] = f.read().strip() return params
def bootup (): try: opts, args = getopt.getopt (sys.argv[1:], "hc:p:", ["config=", "pid-file="]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) verbose = False config_file = None pidfile = None for o, a in opts: if o == "-v": verbose = True if o in ("-h", "--help"): usage() sys.exit() if o in ("-c", "--config"): config_file = a if o in ("-p", "--pid-file"): pidfile = a if config_file == None: # print help information and exit: usage() sys.exit(2) config.initialise(config_file) if pidfile is not None: #Command line override config.set('dbAlerter', 'pid_file', pidfile) if (config.has_option('dbAlerter', 'pid_file')): pidfile = config.get('dbAlerter', 'pid_file') try: pf = file(pidfile,'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid: message = "pidfile %s already exists. dbAlerter may already be running,\ncheck the process / task list and dbAlerter log file for any\nouststanding issues before removing the existing pidfile.\n" sys.stderr.write(message % pidfile) sys.exit(1) daemonize('/dev/null', config.get('dbAlerter','dbalerter_log'), config.get('dbAlerter','dbalerter_log')) main()
def get_or_none(config, section, value): if config.has_option(section, value): return config.get(section, value) else: return None
config = ConfigParser.RawConfigParser() config.read(configfile) print "Read config from file: %s" % configfile man = module_manager.ModuleManager() couch_engine = dict(config.items("server")).get("couch_engine", "couchdb") couch_config = config.items(couch_engine) man.set_database( twentyc.database.ClientFromConfig( couch_engine, couch_config, "modules" ) ) if config.has_option("xbahn", "username") and config.has_option("xbahn", "password"): xbahn_user = config.get("xbahn", "username") xbahn_pass = config.get("xbahn", "password") else: xbahn_user = None xbahn_pass = None if config.has_section("xbahn") and config.get("xbahn", "host"): xb = xbahn.xBahn( config.get("xbahn", "host"), int(config.get("xbahn", "port")), config.get("xbahn", "exchange"), None, None, username = xbahn_user, password = xbahn_pass
def get_or_none(config, section, value): """Return the value of a config option if it exists, none otherwise.""" if config.has_option(section, value): return config.get(section, value) else: return None
def _select(req,name): # Create a working area in request object if it # doesn't exist already. if not hasattr(req,"vampire"): req.vampire = {} # Try and find entry in appropriate config. options = req.get_options() file = ".vampire" if options.has_key("VampireHandlersConfig"): file = options["VampireHandlersConfig"] config = _configCache.loadConfig(req,file) section = "Handlers" if options.has_key("VampireHandlersSection"): section = options["VampireHandlersSection"] handler = None if config.has_section(section): if config.has_option(section,name): handler = config.get(section,name) # If there is no entry or it is empty, skip it. if not handler: return apache.OK # Ensure handler is a Python module file. if os.path.splitext(handler)[1] != ".py": return apache.HTTP_INTERNAL_SERVER_ERROR # The handler is supposed to be the actual # file name of the module to load. The actual # handler within it must be matching name for # the directive, it can't be something else. directory = os.path.dirname(handler) stub = os.path.splitext(os.path.basename(handler))[0] module = _moduleCache.importModule(stub,directory,req) if not module: raise ImportError("No file named %s"%handler) # Determine the appropriate content handler. function = None if hasattr(module,name): function = getattr(module,name) if function == None: raise ImportError("Cannot import %s from %s"%(name,handler)) # Execute the actual handler function. result = function(req) if result is None: return apache.OK return result
def _handler(req): # Create a working area in request object if it # doesn't exist already. if not hasattr(req,"vampire"): req.vampire = {} # Record in request object which handler is servicing # the request. This can be used by a handler to # accomodate being able to be called as a handler or # publisher type method. req.vampire["handler"] = "vampire::handler" # Translate a request against a directory to a request # against a specified index file. if req.path_info == "" and os.path.isdir(req.filename): options = req.get_options() if options.has_key("VampireDirectoryIndex"): value = options["VampireDirectoryIndex"] if value != ".": if req.args: value = "%s?%s" % (value,req.args) if hasattr(req,"internal_redirect"): req.internal_redirect(req.uri+value) else: req.headers_out["location"] = "%s" % value req.status = apache.HTTP_MOVED_TEMPORARILY return apache.OK # Determine type of file based on extension. stub,extn = os.path.splitext(req.filename) # Forbid requests against Python code files or # anything which may be generated from them. if extn in [".py",".pyc",".pyo"]: if os.path.exists(req.filename): return apache.HTTP_NOT_FOUND # Determine name of the content handler to be used. if extn != "": method = "handler_%s" % extn[1:] else: method = "handler" # Search for handler in associated python code. file = stub + ".py" module = _import(req,file) # If we have already found a valid module, first check # to see if it provides an appropriate handler. objects = [] status = apache.HTTP_NOT_FOUND rules = _handler_rules if module: if not hasattr(module,"__handle__") or extn in module.__handle__: status,traverse,execute,access,objects = _resolve( req,module,[method],rules) # Look for any default handlers. req.vampire["__login__"] = None req.vampire["defaults"] = None options = req.get_options() if options.has_key("VampireDefaultHandlers"): if options["VampireDefaultHandlers"] in ["On","on"]: file = ".vampire" if options.has_key("VampireHandlersConfig"): file = options["VampireHandlersConfig"] config = _configCache.loadConfig(req,file) section = "Handlers" if options.has_key("VampireHandlersSection"): section = options["VampireHandlersSection"] # Section defined for default handlers. if config.has_section(section): # Look for module of default handlers. file = None if config.has_option(section,"defaults"): file = config.get(section,"defaults") if file != None: if os.path.splitext(file)[1] != ".py": return apache.HTTP_INTERNAL_SERVER_ERROR req.vampire["defaults"] = _import(req,file) if not req.vampire["defaults"]: raise ImportError("No file named %s"%file) # Look for default login handler in module of # default handlers to override the inbuilt basic # authentication login handler. if req.vampire["defaults"]: module = req.vampire["defaults"] if hasattr(module,"loginhandler"): req.vampire["__login__"] = getattr(module,"loginhandler") # Look for explicitly defined default login # handler. These can still be overridden by # "__login__" function present within objects # which are traversed. file = None if config.has_option(section,"loginhandler"): file = config.get(section,"loginhandler") if file != None: if os.path.splitext(file)[1] != ".py": return apache.HTTP_INTERNAL_SERVER_ERROR module = _import(req,file) if module: if hasattr(module,"loginhandler"): req.vampire["__login__"] = getattr(module,"loginhandler") else: raise ImportError("Cannot import loginhandler from %s"%file) else: raise ImportError("No file named %s"%file) # If a specific content handler wasn't already # found for the actual request, see if default # content handler has been specified. if status != apache.OK: # First look in module of default handlers. if req.vampire["defaults"]: status,traverse,execute,access,objects = _resolve( req,req.vampire["defaults"],[method],rules) # Now check for an explicitly defined handler. if len(objects) <= 1: file = None if config.has_option(section,method): file = config.get(section,method) if file != None: if os.path.splitext(file)[1] != ".py": return apache.HTTP_INTERNAL_SERVER_ERROR module = _import(req,file) if module: status,traverse,execute,access,objects = _resolve( req,module,[method],rules) if status != apache.OK: raise ImportError("Cannot import %s from %s"%(method,file)) else: raise ImportError("No file named %s"%file) req.vampire["objects"] = objects # Perform authentication even if we did not find an # acceptable handler. _authenticate(req) # Return control to Apache if we were unable to find # an acceptable handler to execute. if status != apache.OK: return apache.DECLINED # Execute the content handler which was found. result = _execute(req,objects[-1],lazy=True) # To try and make standard content handlers and # publisher style handlers interchangeable, allow a # return value of "None" to be interchangeable with # returning "apache.OK". if result is None: return apache.OK return result
import services from servicesexceptions import ServiceException import traceback import convertible import config config = config.read_redirect_configs() app = Flask(__name__) app.config['SECRET_KEY'] = config.get('redirect', 'auth_secret_key') login_manager = LoginManager() login_manager.init_app(app) # This is to host static html - should be done on proper web server in prod host_static_files = config.has_option('redirect', 'static_files_path') if host_static_files: static_files_path = config.get('redirect', 'static_files_path') @app.route('/<path:filename>') def static_file(filename): return send_from_directory(static_files_path, filename) # End of hosting static html class UserFlask: def __init__(self, user): self.user = user def is_authenticated(self):
def find_module(self, fullname, path=None): # Raise an exception so as to determine the stack # frame of the parent frame which has requested the # module be imported. try: raise Exception except: parent = sys.exc_info()[2].tb_frame.f_back # Only consider using import caching mechanism if # request object is present as "__req__", filename # is defined by "__file__" and use of import hooks # has been enabled. globals = parent.f_globals if not globals.has_key("__req__"): return None if not globals.has_key("__file__"): return None options = {} req = globals["__req__"] if req is None: return None options = req.get_options() if not options.has_key("VampireImportHooks"): return None if options["VampireImportHooks"] not in ["On", "on"]: return None # Check directory in which parent is located. file = None ispkg = False directory = os.path.dirname(globals["__file__"]) target = os.path.join(directory, fullname) if os.path.isdir(target): target = os.path.join(target, "__init__.py") if os.path.exists(target): file = target ispkg = True if not file: target = os.path.join(directory, fullname) + ".py" if os.path.exists(target): file = target # If not in the parents own directory, check # along the Vampire module search path. def _search(name, path, req): for directory in path: target = os.path.join(directory, name) + ".py" if os.path.exists(target): return target if not file: name = ".vampire" if options.has_key("VampireHandlersConfig"): name = options["VampireHandlersConfig"] config = _configCache.loadConfig(req, name) section = "Modules" if options.has_key("VampireModulesSection"): section = options["VampireModulesSection"] path = None if config.has_option(section, "path"): path = config.get(section, "path").split(':') if path: file = _search(fullname, path, req) if not file: return None if ispkg: directory = os.path.dirname(os.path.dirname(file)) else: directory = os.path.dirname(file) return ModuleLoader(directory, req)
os.umask(0) os.setsid() # Do second fork. try: pid = os.fork() if pid > 0: sys.exit(0) # Exit second parent. except OSError, e: sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror) ) sys.exit(1) # Now I am a daemon! #Write pidfile if (config.has_option('dbAlerter', 'pid_file')): file(config.get('dbAlerter', 'pid_file'),'w+').write(str(os.getpid())) # Redirect standard file descriptors. si = file(stdin, 'r') so = file(stdout, 'a+') se = file(stderr, 'a+', 0) os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) # Handle SIGTERM signal.signal(signal.SIGTERM, sighandler) def usage ():