2020-08-27 18:00:14 +00:00
|
|
|
import json
|
2019-12-18 19:02:17 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2021-10-19 14:13:43 +00:00
|
|
|
|
|
|
|
import click
|
2020-06-02 01:59:55 +00:00
|
|
|
import sentry_sdk
|
2019-12-18 19:02:17 +00:00
|
|
|
|
|
|
|
import connexion
|
2021-10-19 14:13:43 +00:00
|
|
|
from SpiffWorkflow import WorkflowException
|
2020-08-27 18:00:14 +00:00
|
|
|
from connexion import ProblemException
|
|
|
|
from flask import Response
|
2019-12-19 16:58:51 +00:00
|
|
|
from flask_cors import CORS
|
2019-12-18 19:02:17 +00:00
|
|
|
from flask_marshmallow import Marshmallow
|
2020-06-05 02:37:28 +00:00
|
|
|
from flask_mail import Mail
|
2019-12-18 19:02:17 +00:00
|
|
|
from flask_migrate import Migrate
|
2020-05-13 02:42:02 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2020-06-02 01:59:55 +00:00
|
|
|
from sentry_sdk.integrations.flask import FlaskIntegration
|
2021-06-09 13:24:37 +00:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2021-10-01 19:35:22 +00:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2021-06-09 14:42:34 +00:00
|
|
|
|
2021-10-19 14:13:43 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
connexion_app = connexion.FlaskApp(__name__)
|
|
|
|
|
|
|
|
app = connexion_app.app
|
2021-10-01 19:35:22 +00:00
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) # respect the X-Forwarded-Proto if behind a proxy.
|
2019-12-18 19:02:17 +00:00
|
|
|
app.config.from_object('config.default')
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
if "TESTING" in os.environ and os.environ["TESTING"] == "true":
|
|
|
|
app.config.from_object('config.testing')
|
2020-02-28 16:33:08 +00:00
|
|
|
app.config.from_pyfile('../config/testing.py')
|
2020-02-05 18:43:59 +00:00
|
|
|
else:
|
2020-02-06 19:36:02 +00:00
|
|
|
app.config.root_path = app.instance_path
|
2020-02-05 18:43:59 +00:00
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-11-19 16:25:58 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
db = SQLAlchemy(app)
|
2020-01-21 20:21:33 +00:00
|
|
|
""":type: sqlalchemy.orm.SQLAlchemy"""
|
2020-01-14 16:45:12 +00:00
|
|
|
|
|
|
|
session = db.session
|
2020-01-21 20:21:33 +00:00
|
|
|
""":type: sqlalchemy.orm.Session"""
|
2021-06-09 13:24:37 +00:00
|
|
|
scheduler = BackgroundScheduler()
|
2020-01-14 16:45:12 +00:00
|
|
|
|
2020-06-25 22:18:42 +00:00
|
|
|
# Mail settings
|
|
|
|
mail = Mail(app)
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
migrate = Migrate(app, db)
|
|
|
|
ma = Marshmallow(app)
|
|
|
|
|
|
|
|
from crc import models
|
2020-01-14 20:26:39 +00:00
|
|
|
from crc import api
|
2020-06-16 16:26:25 +00:00
|
|
|
from crc.api import admin
|
2021-09-30 18:11:33 +00:00
|
|
|
from crc.services.file_service import FileService
|
2021-06-09 14:42:34 +00:00
|
|
|
from crc.services.workflow_service import WorkflowService
|
2020-05-24 04:05:13 +00:00
|
|
|
connexion_app.add_api('api.yml', base_path='/v1.0')
|
2020-05-12 16:23:47 +00:00
|
|
|
|
2021-06-09 14:42:34 +00:00
|
|
|
# needed function to avoid circular import
|
|
|
|
|
|
|
|
def process_waiting_tasks():
|
|
|
|
with app.app_context():
|
|
|
|
WorkflowService.do_waiting()
|
|
|
|
|
2021-07-22 20:02:08 +00:00
|
|
|
scheduler.add_job(process_waiting_tasks,'interval',minutes=1)
|
2021-09-30 18:11:33 +00:00
|
|
|
scheduler.add_job(FileService.cleanup_file_data, 'interval', minutes=1440) # once a day
|
2021-06-09 14:42:34 +00:00
|
|
|
scheduler.start()
|
|
|
|
|
2020-06-08 13:14:31 +00:00
|
|
|
|
2020-05-12 16:23:47 +00:00
|
|
|
# Convert list of allowed origins to list of regexes
|
2020-05-12 18:27:17 +00:00
|
|
|
origins_re = [r"^https?:\/\/%s(.*)" % o.replace('.', '\.') for o in app.config['CORS_ALLOW_ORIGINS']]
|
2020-05-16 15:36:48 +00:00
|
|
|
cors = CORS(connexion_app.app, origins=origins_re)
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-06-08 13:14:31 +00:00
|
|
|
# Sentry error handling
|
2020-07-19 22:40:33 +00:00
|
|
|
if app.config['SENTRY_ENVIRONMENT']:
|
2020-06-02 01:59:55 +00:00
|
|
|
sentry_sdk.init(
|
2020-07-19 22:40:33 +00:00
|
|
|
environment=app.config['SENTRY_ENVIRONMENT'],
|
2020-06-02 01:59:55 +00:00
|
|
|
dsn="https://25342ca4e2d443c6a5c49707d68e9f40@o401361.ingest.sentry.io/5260915",
|
|
|
|
integrations=[FlaskIntegration()]
|
|
|
|
)
|
|
|
|
|
2020-08-27 18:00:14 +00:00
|
|
|
|
|
|
|
# Connexion Error handling
|
|
|
|
def render_errors(exception):
|
|
|
|
from crc.api.common import ApiError, ApiErrorSchema
|
2021-03-08 19:00:03 +00:00
|
|
|
error = ApiError(code=exception.title, message=exception.detail, status_code=exception.status)
|
2020-08-27 18:00:14 +00:00
|
|
|
return Response(ApiErrorSchema().dump(error), status=401, mimetype="application/json")
|
|
|
|
|
|
|
|
|
|
|
|
connexion_app.add_error_handler(ProblemException, render_errors)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-27 02:42:49 +00:00
|
|
|
print('=== USING THESE CONFIG SETTINGS: ===')
|
2020-05-31 20:49:39 +00:00
|
|
|
print('APPLICATION_ROOT = ', app.config['APPLICATION_ROOT'])
|
2020-05-27 02:42:49 +00:00
|
|
|
print('CORS_ALLOW_ORIGINS = ', app.config['CORS_ALLOW_ORIGINS'])
|
2020-05-31 20:49:39 +00:00
|
|
|
print('DB_HOST = ', app.config['DB_HOST'])
|
2020-05-27 02:42:49 +00:00
|
|
|
print('LDAP_URL = ', app.config['LDAP_URL'])
|
2020-05-31 20:49:39 +00:00
|
|
|
print('PB_BASE_URL = ', app.config['PB_BASE_URL'])
|
2020-05-27 02:42:49 +00:00
|
|
|
print('PB_ENABLED = ', app.config['PB_ENABLED'])
|
2020-05-31 20:49:39 +00:00
|
|
|
print('PRODUCTION = ', app.config['PRODUCTION'])
|
|
|
|
print('TESTING = ', app.config['TESTING'])
|
|
|
|
print('TEST_UID = ', app.config['TEST_UID'])
|
|
|
|
print('ADMIN_UIDS = ', app.config['ADMIN_UIDS'])
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
@app.cli.command()
|
|
|
|
def load_example_data():
|
|
|
|
"""Load example data into the database."""
|
2019-12-18 19:16:26 +00:00
|
|
|
from example_data import ExampleDataLoader
|
2019-12-30 18:03:57 +00:00
|
|
|
ExampleDataLoader.clean_db()
|
2019-12-18 19:16:26 +00:00
|
|
|
ExampleDataLoader().load_all()
|
2020-12-01 20:47:27 +00:00
|
|
|
ExampleDataLoader().load_default_user()
|
2020-05-25 16:29:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.cli.command()
|
|
|
|
def load_example_rrt_data():
|
|
|
|
"""Load example data into the database."""
|
|
|
|
from example_data import ExampleDataLoader
|
|
|
|
ExampleDataLoader.clean_db()
|
|
|
|
ExampleDataLoader().load_rrt()
|
2020-06-05 21:49:55 +00:00
|
|
|
|
2021-01-08 18:23:01 +00:00
|
|
|
|
|
|
|
@app.cli.command()
|
|
|
|
def load_reference_files():
|
|
|
|
"""Load example data into the database."""
|
|
|
|
from example_data import ExampleDataLoader
|
|
|
|
ExampleDataLoader().load_reference_documents()
|
|
|
|
|
2020-06-05 21:49:55 +00:00
|
|
|
@app.cli.command()
|
|
|
|
def clear_db():
|
|
|
|
"""Load example data into the database."""
|
|
|
|
from example_data import ExampleDataLoader
|
|
|
|
ExampleDataLoader.clean_db()
|
2020-06-17 21:11:15 +00:00
|
|
|
|
2021-04-09 12:40:58 +00:00
|
|
|
@app.cli.command()
|
|
|
|
def sync_with_testing():
|
|
|
|
"""Load all the workflows currently on testing into this system."""
|
|
|
|
from crc.api import workflow_sync
|
|
|
|
workflow_sync.sync_all_changed_workflows("https://testing.crconnect.uvadcos.io/api")
|
2021-10-19 14:13:43 +00:00
|
|
|
|
|
|
|
@app.cli.command()
|
|
|
|
@click.argument("study_id")
|
|
|
|
@click.argument("category")
|
|
|
|
@click.argument("spec_id")
|
|
|
|
def validate_all(study_id, category=None, spec_id=None):
|
|
|
|
"""Step through all the local workflows and validate them, returning any errors. This make take forever.
|
|
|
|
Please provide a real study id to use for validation, an optional category can be specified to only validate
|
|
|
|
that category, and you can further specify a specific spec, if needed."""
|
|
|
|
from crc.models.workflow import WorkflowSpecModel
|
|
|
|
from crc.services.workflow_service import WorkflowService
|
|
|
|
from crc.api.common import ApiError
|
|
|
|
|
|
|
|
specs = session.query(WorkflowSpecModel).all()
|
|
|
|
for spec in specs:
|
|
|
|
if spec_id and spec_id != spec.id:
|
|
|
|
continue
|
|
|
|
if category and (not spec.category or spec.category.display_name != category):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
WorkflowService.test_spec(spec.id, validate_study_id=study_id)
|
|
|
|
except ApiError as e:
|
|
|
|
print("Failed to validate workflow " + spec.id)
|
|
|
|
print(e)
|
|
|
|
return
|
|
|
|
except WorkflowException as e:
|
|
|
|
print("Failed to validate workflow " + spec.id)
|
|
|
|
print(e)
|
|
|
|
return
|