def get_user_info_hash(user_name, password): md5_secrethash = hashlib.md5() md5_secrethash.update(password + user_name) secrect_hash = md5_secrethash.hexdigest() md5_userhash = hashlib.md5() md5_userhash.update(load_app_config().SALT_STRING + user_name) user_hash = md5_userhash.hexdigest() return secrect_hash, user_hash
def get_cache_manager(): global cache if not cache: cache = GuldanCache(config={ 'CACHE_TYPE': 'app.cache.backend.codis.codis', 'CACHE_URL': load_app_config().REDIS_URL }) cache.init_app(app) return cache
def get_stats(self): current_time = time.time() qps = self.num_of_queries / (current_time - self.start_time) tps = self.num_of_transactions / (current_time - self.start_time) hostname = socket.gethostname() port = load_app_config().LISTENING_PORT return { 'qps': qps, 'tps': tps, 'hostname': hostname, 'port': port }
def get_kv_manager(): global kv_manager if not kv_manager: redis_url = load_app_config().REDIS_URL try: kv_manager = KVManager(redis_url) except: logger.exception( "exc when init KVManager, kv_url:{}".format(redis_url)) kv_manager = fake_kv_manager threading.Thread(target=try_to_connect_to_kv, args=(redis_url, )).start() return kv_manager
def get_metrics_clent(measurement): try: metrics_client = metrics_clients.get(measurement, None) if not metrics_client: host_and_port = load_app_config().METRICS_URL.split(":") if len(host_and_port) != 2: metrics_client = FakeGroupMetrics() else: tags = {"hostname": HOSTNAME} metrics_client = GroupMetrics( (host_and_port[0], int(host_and_port[1])), measurement, aggregate_count=1000, ring_buffer_capacity=10000, predefined_tags=tags ) metrics_clients[measurement] = metrics_client return metrics_client except: logger.exception("exc when get metrics client") return FakeGroupMetrics()
# -*- coding: utf-8 -*- import functools import logging import app from flask import g from app.api.models.base import Resource from app.api.utils.resource import get_resource_model from app.api.models.privilege import Privilege logger = logging.getLogger(app.load_app_config().APP_NAME + '.audit') def get_resource_type(func_name): if 'org' in func_name: return Resource.Type.ORG elif 'project' in func_name: return Resource.Type.PROJECT elif 'item' in func_name: return Resource.Type.ITEM else: return None def audit(*args, **kwargs): try: resource_type = get_resource_type(kwargs['audit_func_name']) new_value = args[1] resource_id = kwargs.get('resource_id', None)
def create_sessions(self): db_settings = load_app_config().DB_SETTINGS for role, url in db_settings.items(): session_factory = self.create_single_session(url) # event.listen(session_factory, "before_flush", maintainance_check) self.session_map[role] = session_factory
def zk_session_ctx(self): session = None try: session = self._acquire_session() if not session: raise GulDanException().with_code(500).with_message( u"不能连接到zookeeper") yield session finally: self.return_session(session) def is_path_existing(self, path): with self.zk_session_ctx() as session: if session.conn.exists(path): return True return False def close_sessions(self): for s in self.zksessions: s.close() app_config = load_app_config() zk_manager = ZKManager(app_config.ZK_CONN_CONFIG, **app_config.ZK_MANAGER_CONFIG) atexit.register(zk_manager.close_sessions)
# -*- coding: utf-8 -*- from app import app, init, load_app_config from app.serving import GuldanRequestHandler import os current_dir = os.path.dirname(os.path.abspath(__file__)) extra_dirs = [os.path.join(current_dir, "app/templates")] extra_files = extra_dirs[:] for extra_dir in extra_dirs: for dirname, dirs, files in os.walk(extra_dir): for filename in files: filename = os.path.join(dirname, filename) if os.path.isfile(filename): extra_files.append(filename) if __name__ == "__main__": init() print app.url_map app.run(host='0.0.0.0', port=load_app_config().LISTENING_PORT, extra_files=extra_files, request_handler=GuldanRequestHandler)