From 591b86892065490024a8180f9f4d1e9cb92cbc40 Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Wed, 22 Jun 2022 11:20:30 +0100 Subject: [PATCH] Separated install script to avoid launch errors --- ref-test/Dockerfile | 1 + ref-test/app/__init__.py | 3 --- ref-test/app/install.py | 34 ---------------------------------- ref-test/install.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 37 deletions(-) delete mode 100644 ref-test/app/install.py create mode 100755 ref-test/install.py diff --git a/ref-test/Dockerfile b/ref-test/Dockerfile index 24850e9..60cf232 100644 --- a/ref-test/Dockerfile +++ b/ref-test/Dockerfile @@ -2,4 +2,5 @@ FROM python:3.10-slim WORKDIR /ref-test COPY . . RUN pip install --upgrade pip && pip install -r requirements.txt +RUN chmod +x install.py && ./install.py CMD [ "gunicorn", "-b", "0.0.0.0:5000", "-w", "5", "wsgi:app" ] \ No newline at end of file diff --git a/ref-test/app/__init__.py b/ref-test/app/__init__.py index 7f92438..4122f2c 100644 --- a/ref-test/app/__init__.py +++ b/ref-test/app/__init__.py @@ -1,5 +1,4 @@ from .config import Production as Config -from .install import install_app from .models import User from .extensions import bootstrap, csrf, db, login_manager, mail @@ -53,7 +52,5 @@ def create_app(): app.register_blueprint(views) app.register_blueprint(quiz) app.register_blueprint(editor, url_prefix='/admin/editor') - - install_app(app) return app \ No newline at end of file diff --git a/ref-test/app/install.py b/ref-test/app/install.py deleted file mode 100644 index 2ef212b..0000000 --- a/ref-test/app/install.py +++ /dev/null @@ -1,34 +0,0 @@ -from .extensions import db -from .tools.data import save -from .tools.logs import write - -from sqlalchemy_utils import create_database, database_exists - -from cryptography.fernet import Fernet -from os import mkdir, path -from pathlib import Path - -def install_app(app): - with app.app_context(): - data = Path(app.config.get('DATA')) - database_uri = app.config.get('SQLALCHEMY_DATABASE_URI') - if not path.isdir(f'./{data}'): mkdir(f'./{data}') - if not path.isdir(f'./{data}/questions'): mkdir(f'./{data}/questions') - if not path.isfile(f'./{data}/.gitignore'): - with open(f'./{data}/.gitignore', 'w') as file: file.write(f'*') - if not path.isfile(f'./{data}/config.json'): save({}, 'config.json') - if not path.isdir(f'./{data}/logs'): mkdir(f'./{data}/logs') - if not path.isfile(f'./{data}/logs/users.log'): write('users.log', 'Log file created.') - if not path.isfile(f'./{data}/logs/system.log'): write('system.log', 'Log file created.') - if not path.isfile(f'./{data}/logs/tests.log'): write('tests.log', 'Log file created.') - if not database_exists(database_uri): - create_database(database_uri) - write('system.log', 'No database found. Creating a new database.') - from .models import Entry, Dataset, Test, User - db.create_all() - write('system.log', 'Creating database schema.') - if not path.isfile(f'./{data}/.encryption.key'): - write('system.log', 'No encryption key found. Generating new encryption key.') - with open(f'./{data}/.encryption.key', 'wb') as key_file: - key = Fernet.generate_key() - key_file.write(key) \ No newline at end of file diff --git a/ref-test/install.py b/ref-test/install.py new file mode 100755 index 0000000..4570d59 --- /dev/null +++ b/ref-test/install.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +from main import app +from app.extensions import db +from app.tools.data import save +from app.tools.logs import write +from sqlalchemy_utils import create_database, database_exists +from cryptography.fernet import Fernet +from os import mkdir, path +from pathlib import Path + +database_uri = app.config.get('SQLALCHEMY_DATABASE_URI') + +with app.app_context(): + data = Path(app.config.get('DATA')) + if not path.isdir(f'./{data}'): mkdir(f'./{data}') + if not path.isdir(f'./{data}/questions'): mkdir(f'./{data}/questions') + if not path.isfile(f'./{data}/.gitignore'): + with open(f'./{data}/.gitignore', 'w') as file: file.write(f'*') + if not path.isfile(f'./{data}/config.json'): save({}, 'config.json') + if not path.isdir(f'./{data}/logs'): mkdir(f'./{data}/logs') + if not path.isfile(f'./{data}/logs/users.log'): write('users.log', 'Log file created.') + if not path.isfile(f'./{data}/logs/system.log'): write('system.log', 'Log file created.') + if not path.isfile(f'./{data}/logs/tests.log'): write('tests.log', 'Log file created.') + if not database_exists(database_uri): + create_database(database_uri) + write('system.log', 'No database found. Creating a new database.') + from app.models import * + db.create_all() + write('system.log', 'Creating database schema.') + if not path.isfile(f'./{data}/.encryption.key'): + write('system.log', 'No encryption key found. Generating new encryption key.') + with open(f'./{data}/.encryption.key', 'wb') as key_file: + key = Fernet.generate_key() + key_file.write(key) \ No newline at end of file