| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2018-2022 Matthias Klumpp <[email protected]> |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: LGPL-3.0+ |
| 6 |
|
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import logging as log |
| 10 |
from dataclasses import dataclass |
| 11 |
from logging.handlers import RotatingFileHandler |
| 12 |
|
| 13 |
from flask import Flask |
| 14 |
|
| 15 |
import laniakea.typing as T |
| 16 |
from laniakea import LocalConfig |
| 17 |
|
| 18 |
from .config import INSTANCE_FOLDER_PATH, DebugConfig, DefaultConfig |
| 19 |
|
| 20 |
# For import * |
| 21 |
__all__ = ['create_app'] |
| 22 |
|
| 23 |
|
| 24 |
thisfile = __file__ |
| 25 |
if not os.path.isabs(thisfile): |
| 26 |
thisfile = os.path.normpath(os.path.join(os.getcwd(), thisfile)) |
| 27 |
app_root_dir = os.path.normpath(os.path.join(os.path.dirname(thisfile), '..')) |
| 28 |
|
| 29 |
|
| 30 |
@dataclass |
| 31 |
class BaseDataCache: |
| 32 |
"""Some data that we cache on startup, for very low overhead per single upload.""" |
| 33 |
|
| 34 |
upload_chunk_size: int # chunk size for uploads to be written to disk |
| 35 |
master_user: str # Laniakea system user name |
| 36 |
incoming_dir: T.PathUnion # path to incoming root directory |
| 37 |
repo_names: T.Set[str] # set of repository+suite names that we accept uploads for |
| 38 |
|
| 39 |
def __init__(self, chunk_size: int): |
| 40 |
self.upload_chunk_size = chunk_size |
| 41 |
self.repo_names = set() |
| 42 |
|
| 43 |
|
| 44 |
gdata: T.Optional[BaseDataCache] = None |
| 45 |
|
| 46 |
|
| 47 |
def url_not_found(e): |
| 48 |
return 'upload target not found', 404 |
| 49 |
|
| 50 |
|
| 51 |
def create_app(config=None, app_name=None): |
| 52 |
if app_name is None: |
| 53 |
app_name = DefaultConfig.PROJECT |
| 54 |
|
| 55 |
app = Flask( |
| 56 |
app_name, template_folder='templates', instance_path=INSTANCE_FOLDER_PATH, instance_relative_config=True |
| 57 |
) |
| 58 |
configure_app(app, config) |
| 59 |
|
| 60 |
configure_blueprints(app) |
| 61 |
configure_logging(app) |
| 62 |
|
| 63 |
app.register_error_handler(404, url_not_found) |
| 64 |
|
| 65 |
return app |
| 66 |
|
| 67 |
|
| 68 |
def configure_app(app, config=None): |
| 69 |
""" |
| 70 |
Load app configuration - local production config takes |
| 71 |
precedence over others. |
| 72 |
""" |
| 73 |
import tomlkit |
| 74 |
|
| 75 |
from laniakea import get_config_file |
| 76 |
from laniakea.db import ArchiveRepoSuiteSettings, session_scope |
| 77 |
|
| 78 |
global gdata |
| 79 |
|
| 80 |
if app.debug: |
| 81 |
app.config.from_object(DebugConfig) |
| 82 |
else: |
| 83 |
app.config.from_object(DefaultConfig) |
| 84 |
app.config.from_pyfile('config.cfg', silent=True) |
| 85 |
|
| 86 |
if config: |
| 87 |
app.config.from_object(config) |
| 88 |
|
| 89 |
# cache some data we will need |
| 90 |
lconf = LocalConfig() |
| 91 |
rc_fname = get_config_file('rubicon.toml') |
| 92 |
rcdata = {} |
| 93 |
if rc_fname and os.path.isfile(rc_fname): |
| 94 |
with open(rc_fname) as f: |
| 95 |
rcdata = tomlkit.load(f) |
| 96 |
|
| 97 |
gdata = BaseDataCache(app.config['UPLOAD_CHUNK_SIZE']) |
| 98 |
gdata.master_user = lconf.master_user_name |
| 99 |
gdata.incoming_dir = rcdata.get('IncomingDir', lconf.upload_incoming_dir) |
| 100 |
|
| 101 |
with session_scope() as session: |
| 102 |
for rss in session.query(ArchiveRepoSuiteSettings).filter(ArchiveRepoSuiteSettings.accept_uploads.is_(True)): |
| 103 |
gdata.repo_names.add(str(rss.repo.name)) |
| 104 |
for repo_name in gdata.repo_names: |
| 105 |
repo_incoming_dir = os.path.join(gdata.incoming_dir, repo_name) |
| 106 |
if not os.path.isdir(repo_incoming_dir): |
| 107 |
os.makedirs(repo_incoming_dir, exist_ok=True) |
| 108 |
shutil.chown(repo_incoming_dir, group=gdata.master_user) |
| 109 |
os.chmod(repo_incoming_dir, 0o775) |
| 110 |
|
| 111 |
|
| 112 |
def configure_blueprints(app): |
| 113 |
""" |
| 114 |
Configure blueprints |
| 115 |
""" |
| 116 |
|
| 117 |
from .upload import upload |
| 118 |
|
| 119 |
blueprints = [upload] |
| 120 |
|
| 121 |
for bp in blueprints: |
| 122 |
app.register_blueprint(bp) |
| 123 |
|
| 124 |
|
| 125 |
def configure_logging(app): |
| 126 |
""" |
| 127 |
Configure file(info) and email(error) logging. |
| 128 |
""" |
| 129 |
|
| 130 |
if app.debug or app.testing: |
| 131 |
# Skip debug and test mode. Just check standard output. |
| 132 |
return |
| 133 |
|
| 134 |
# Set warning level on logger, which might be overwritten by handers. |
| 135 |
# Suppress DEBUG messages. |
| 136 |
app.logger.setLevel(log.WARNING) |
| 137 |
|
| 138 |
os.makedirs(app.config['LOG_FOLDER'], exist_ok=True) |
| 139 |
info_log = os.path.join(app.config['LOG_FOLDER'], 'info.log') |
| 140 |
info_file_handler = RotatingFileHandler(info_log, maxBytes=100000, backupCount=10) |
| 141 |
info_file_handler.setLevel(log.INFO) |
| 142 |
info_file_handler.setFormatter(log.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) |
| 143 |
app.logger.addHandler(info_file_handler) |
| 144 |
|
| 145 |
# supress GET etc. messages from Werkzeug |
| 146 |
wlog = log.getLogger('werkzeug') |
| 147 |
wlog.setLevel(log.ERROR) |