def init_crud_clients(): config = nest_config.generate_config(ProjectEnv.knoweng_instance(), RunLevel.development_instance()) init_token_maker(config['JWT_SECRET'], config['JWT_ISSUER'], config['JWT_AUDIENCES']) global SCHEMA_REGISTRY SCHEMA_REGISTRY = knoweng_schemas.get_schemas() global CLIENT_REGISTRY CLIENT_REGISTRY = dict() #make db clients #TODO: knoweng should declare the db it's using, but for now #the default postgres container on localhost is all there is, #which is what the global db engine defaults to. engine = nest_db.get_global_sqlalchemy_engine() sqla_md = nest_db.get_global_sqlalchemy_metadata() db_client_makers = knoweng_db.get_sqla_makers() for name in DB_COLLECTIONS: cm = db_client_makers[name] client = cm.get_db_client(engine, sqla_md) CLIENT_REGISTRY[name] = client #make api clients http_client = get_http_client() api_client_makers = knoweng_api_clients.get_api_client_makers() for name in API_COLLECTIONS: cm = api_client_makers[name] crud_client = cm.get_crud_client(http_client) CLIENT_REGISTRY[name] = crud_client return
def register_nest_endpoints(flask_app, project_env, authenticator): db_engine = nest_db.get_global_sqlalchemy_engine() sqla_md = nest_db.get_global_sqlalchemy_metadata() if ProjectEnv.hello_world_instance() == project_env: import nest_py.hello_world.flask.hw_flask as hw_flask nest_endpoints = hw_flask.get_nest_endpoints(db_engine, sqla_md, authenticator) elif ProjectEnv.mmbdb_instance() == project_env: import nest_py.omix.flask.omix_flask as omix_flask nest_endpoints = omix_flask.get_nest_endpoints(db_engine, sqla_md, authenticator) elif ProjectEnv.knoweng_instance() == project_env: import nest_py.knoweng.flask.knoweng_flask as knoweng_flask nest_endpoints = knoweng_flask.get_nest_endpoints( db_engine, sqla_md, authenticator) else: raise Exception("Unknown project when registering endpoints") for flask_ep in nest_endpoints.get_flask_endpoints(): nest_ep = nest_endpoints.get_endpoint(flask_ep) relative_flask_rule = nest_ep.get_flask_rule() rule = API_PREFIX + relative_flask_rule print('registering flask rule: ' + str(rule)) flask_ep = nest_ep.get_flask_endpoint() renderer = nest_ep.handle_request flask_app.add_url_rule(rule, flask_ep, view_func=renderer, methods=['GET', 'POST', 'PATCH', 'DELETE']) return
def _run_seed_script(project_env, target_site, flavor_name, subsample): """ runs a seeding job. logs the final result and returns a 0/1 exit code based on whether all walkthroughs worked. """ http_client = target_site.build_http_client() data_projects_dir = '/code_live/data/projects/' if project_env == ProjectEnv.hello_world_instance(): data_dir = data_projects_dir + 'hello_world/' exit_code = hello_world_seed_job.run(http_client, data_dir) elif project_env == ProjectEnv.knoweng_instance(): db_engine = _make_db_engine(project_env) data_dir = data_projects_dir + 'knoweng/' exit_code = knoweng_seed_job.run(http_client, db_engine, data_dir, subsample, flavor_name) elif project_env == ProjectEnv.mmbdb_instance(): data_dir = data_projects_dir + 'mmbdb/' db_engine = _make_db_engine(project_env) exit_code = mmbdb_seed_job.run(http_client, db_engine, data_dir, subsample, flavor_name) pass else: raise Exception("Project's seed job not implemented") return exit_code
def test_dev_config(): run_level = RunLevel.development_instance() project_env = ProjectEnv.knoweng_instance() config = nest_config.generate_config(project_env, run_level) app = create_app(config, project_env, run_level) if app is None: assert False, "create_app returned None" assert app.config['ENV'] == 'dev' assert app.config['DEBUG'] is True return
def test_production_config(): run_level = RunLevel.production_instance() project_env = ProjectEnv.knoweng_instance() config = nest_config.generate_config(project_env, run_level) app = create_app(config, project_env, run_level) if app is None: assert False, "create_app returned None" assert app.config['ENV'] == 'prod' assert app.config['DEBUG'] is False assert app.config['DEBUG_TB_ENABLED'] is False return
def run_project_scripts(project_env, http_client): """ runs the smoke test for the current project. """ if project_env == ProjectEnv.hello_world_instance(): smoke_res = hw_api_clients.run_all_smoke_tests(http_client) elif project_env == ProjectEnv.knoweng_instance(): smoke_res = knoweng_api_clients.run_all_smoke_tests(http_client) elif project_env == ProjectEnv.mmbdb_instance(): smoke_res = omix_api_clients.run_all_smoke_tests(http_client) else: raise Exception("Project smoke scripts not implemented") return smoke_res
def _generate_project_params(project_env, runlevel): if project_env == ProjectEnv.hello_world_instance(): import nest_py.hello_world.hw_config as hw_config project_params = hw_config.generate_project_params(runlevel) elif project_env == ProjectEnv.knoweng_instance(): import nest_py.knoweng.knoweng_config as knoweng_config project_params = knoweng_config.generate_project_params(runlevel) elif project_env == ProjectEnv.mmbdb_instance(): import nest_py.omix.omix_config as omix_config project_params = omix_config.generate_project_params(runlevel) else: raise Exception("Unsupported project env: " + str(project_env)) return project_params
def app(): from nest_py.core.flask.app2 import create_app run_level = RunLevel.development_instance() project_env = ProjectEnv.knoweng_instance() config = nest_config.generate_config(run_level, project_env) #force localhost as the db server host = NestSite.localhost_instance().get_server_ip_address() config['host'] = host _app = create_app(config, project_env, run_level) ctx = _app.test_request_context() ctx.push() yield _app ctx.pop()
def test_project_instances(): knoweng_env = ProjectEnv.knoweng_instance() mmbdb_env = ProjectEnv.mmbdb_instance() knoweng_rt = ProjectEnv(knoweng_env.get_project_name()) mmbdb_rt = ProjectEnv(mmbdb_env.get_project_name()) assert knoweng_env == knoweng_rt assert knoweng_env == knoweng_env assert not knoweng_env == mmbdb_env assert mmbdb_env == mmbdb_rt s = (str(knoweng_env)) print(s) assert s.endswith('knoweng') assert knoweng_env == ProjectEnv.from_string('KnowEng') return
def _run_wipe(project_env, target_site): """ runs a seeding job. logs the final result and returns a 0/1 exit code based on whether all walkthroughs worked. """ http_client = target_site.build_http_client() jobs_auth.login_jobs_user(http_client) if project_env == ProjectEnv.hello_world_instance(): cms = hw_api_clients.get_api_client_makers() elif project_env == ProjectEnv.knoweng_instance(): # TODO add option to govern handling of user data cms = knoweng_api_clients.get_api_client_makers() elif project_env == ProjectEnv.mmbdb_instance(): cms = omix_api_clients.get_api_client_makers() else: raise Exception("Project's wipe job not implemented") exit_code = wipe_by_api_clients(cms, http_client) return exit_code
def build_authenticator(flask_app, project_env, runlevel): users_sqla_maker = core_db.get_nest_users_sqla_maker() db_engine = nest_db.get_global_sqlalchemy_engine() md = nest_db.get_global_sqlalchemy_metadata() users_client = users_sqla_maker.get_db_client(db_engine, md) #the authenticator will interact with the local db as the master system_user auth_user = core_db.get_system_user() users_client.set_requesting_user(auth_user) #knoweng uses hubzero to look up user accounts in production #all other situations will use user accounts stored in the local db use_hubzero = (ProjectEnv.knoweng_instance() == project_env and RunLevel.production_instance() == runlevel) if use_hubzero: print('registering Hubzero authenticator') from nest_py.knoweng.flask.accounts.knoweng_authentication import HubzeroAuthenticationStrategy authenticator = HubzeroAuthenticationStrategy(flask_app, users_client) else: from nest_py.core.flask.accounts.authentication import NativeAuthenticationStrategy authenticator = NativeAuthenticationStrategy(flask_app, users_client) return authenticator
def test_knoweng_seed_users(): project_env = ProjectEnv.knoweng_instance() _test_seed_users_for_project(project_env) return