__author__ = 'rcj1492' __created__ = '2017.04' __license__ = '©2017 Collective Acuity' # inject environmental variables from os import environ from server.utils import inject_cred system_environment = environ.get('SYSTEM_ENVIRONMENT', 'dev') inject_cred(system_environment) # retrieve system configurations from labpack.records.settings import ingest_environ bot_config = ingest_environ('models/envvar/bot.json') # construct flask app object from flask import Flask flask_kwargs = { 'import_name': __name__, 'static_folder': 'public', 'template_folder': 'views' } flask_app = Flask(**flask_kwargs) # declare flask configurations # http://flask.pocoo.org/docs/0.12/config/ # http://flask.pocoo.org/docs/0.12/api/#sessions from datetime import timedelta class flaskDev(object): ASSETS_DEBUG = False
def retrieve_oauth2_configs(folder_path=''): ''' a method to retrieve oauth2 configuration details from files or envvar ''' # define oauth2 model oauth2_fields = { "schema": { "oauth2_app_name": "My App", "oauth2_developer_name": "Collective Acuity", "oauth2_service_name": "moves", "oauth2_auth_endpoint": "https://api.moves-app.com/oauth/v1/authorize", "oauth2_token_endpoint": "https://api.moves-app.com/oauth/v1/access_token", "oauth2_client_id": "ABC-DEF1234ghijkl-567MNOPQRST890uvwxyz", "oauth2_client_secret": "abcdefgh01234456789_IJKLMNOPQrstuv-wxyz", "oauth2_redirect_uri": "https://collectiveacuity.com/authorize/moves", "oauth2_service_scope": "activity location", "oauth2_response_type": "code", "oauth2_request_mimetype": "", "oauth2_service_logo": "https://pbs.twimg.com/profile_images/3/d_400x400.png", "oauth2_service_description": "", "oauth2_service_setup": 0.0 } } # retrieve keys, value pairs from config files in cred folder if folder_path: envvar_details = {} import os from labpack.records.settings import load_settings file_list = [] for suffix in ['.yaml', '.yml', '.json']: for file_name in os.listdir(folder_path): file_path = os.path.join(folder_path, file_name) if os.path.isfile(file_path): if file_name.find(suffix) > -1: file_list.append(file_path) for file_path in file_list: file_details = load_settings(file_path) envvar_details.update(**file_details) # or ingest environmental variables else: from labpack.records.settings import ingest_environ envvar_details = ingest_environ() # map oauth2 variables import re oauth2_map = {} for key in oauth2_fields['schema'].keys(): key_pattern = '%s$' % key[6:] key_regex = re.compile(key_pattern) for k, v in envvar_details.items(): if key_regex.findall(k.lower()): service_name = key_regex.sub('',k.lower()) if not service_name in oauth2_map.keys(): oauth2_map[service_name] = {} oauth2_map[service_name][key] = v # ingest models from jsonmodel.validators import jsonModel oauth2_model = jsonModel(oauth2_fields) oauth2_services = {} for key, value in oauth2_map.items(): valid_oauth2 = {} try: valid_oauth2 = oauth2_model.validate(value) except: pass if valid_oauth2: oauth2_services[key] = valid_oauth2 return oauth2_services
if max_instances: scheduler_job_defaults['max_instances'] = max_instances if scheduler_job_defaults: scheduler_configs['SCHEDULER_JOB_DEFAULTS'] = scheduler_job_defaults # adjust executor settings # scheduler_executors = {} # if scheduler_settings['scheduler_executors_type']: # scheduler_executors['type'] = scheduler_settings['scheduler_executors_type'] # if scheduler_settings['scheduler_executors_max_workers']: # scheduler_executors['max_workers'] = scheduler_settings['scheduler_executors_max_workers'] # if scheduler_executors: # scheduler_configs['SCHEDULER_EXECUTORS'] = scheduler_executors return scheduler_configs if __name__ == '__main__': # test config scheduler import os os.environ['SCHEDULER_JOB_STORE_PASS'] = '******' model_path = '../models/scheduler-model.json' from labpack.records.settings import load_settings, ingest_environ settings_model = load_settings(model_path) assert settings_model['schema']['scheduler_job_store_user'] == 'postgres' env_settings = ingest_environ(model_path) assert env_settings['scheduler_job_store_pass'] == 'test_pass' example_settings = settings_model['schema'] scheduler_config = config_scheduler(example_settings) assert scheduler_config['SCHEDULER_JOB_DEFAULTS']['coalesce']
# create init path to sibling folders import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # initialize flask and configuration objects from server.init import flask_app, bot_config, oauth2_configs, request_models from flask import request, jsonify, url_for, render_template # initialize job scheduling from pytz import utc from server.utils import config_scheduler from labpack.records.settings import ingest_environ from apscheduler.schedulers.gevent import GeventScheduler scheduler_kwargs = { 'timezone': utc } scheduler_config = ingest_environ('models/envvar/scheduler.json') scheduler_update = config_scheduler(scheduler_config) scheduler_kwargs.update(**scheduler_update) flask_scheduler = GeventScheduler(**scheduler_kwargs) flask_scheduler.start() # import authorization methods from labpack.handlers.requests import handle_requests from labpack.authentication.oauth2 import oauth2Client from labpack.parsing.flask import extract_request_details from server.utils import read_state, delete_state, create_token, construct_response # define landing kwargs from labpack.records.settings import load_settings landing_kwargs = { 'landing_page': True,