From 7623b3c615501a9853cdaf3bfc26f16512993a8c Mon Sep 17 00:00:00 2001 From: David Cruciani Date: Wed, 4 Sep 2024 09:38:01 +0200 Subject: [PATCH] chg: [website] launch and migrate python script --- website/app.py | 52 ----------------------------------------- website/app_creation.py | 33 ++++++++++++++++++++++++++ website/launch.py | 43 ++++++++++++++++++++++++++++++++++ website/launch.sh | 49 -------------------------------------- website/migrate.py | 19 +++++++++++++++ website/migrate.sh | 30 ------------------------ 6 files changed, 95 insertions(+), 131 deletions(-) delete mode 100644 website/app.py create mode 100644 website/app_creation.py create mode 100755 website/launch.py delete mode 100755 website/launch.sh create mode 100644 website/migrate.py delete mode 100755 website/migrate.sh diff --git a/website/app.py b/website/app.py deleted file mode 100644 index a2fcf7ef..00000000 --- a/website/app.py +++ /dev/null @@ -1,52 +0,0 @@ -from app import create_app, db -import argparse -from flask import render_template -import os -from app.utils.init_modules import create_modules_db - -import signal -import sys -import subprocess -from app.utils.utils import gen_admin_password - -def signal_handler(sig, frame): - path = os.path.join(os.getcwd(), "launch.sh") - req = [path, "-ks"] - subprocess.call(req) - sys.exit(0) - -signal.signal(signal.SIGINT, signal_handler) - - -parser = argparse.ArgumentParser() -parser.add_argument("-i", "--init_db", help="Initialise the db if it not exist", action="store_true") -parser.add_argument("-r", "--recreate_db", help="Delete and initialise the db", action="store_true") -parser.add_argument("-d", "--delete_db", help="Delete the db", action="store_true") -parser.add_argument("-m", "--create_module", help="Create modules in db", action="store_true") -args = parser.parse_args() - -os.environ.setdefault('FLASKENV', 'development') - -app = create_app() - -@app.errorhandler(404) -def error_page_not_found(e): - return render_template('404.html'), 404 - - -if args.init_db: - with app.app_context(): - db.create_all() -elif args.recreate_db: - with app.app_context(): - db.drop_all() - db.create_all() -elif args.delete_db: - with app.app_context(): - db.drop_all() -elif args.create_module: - with app.app_context(): - create_modules_db() -else: - gen_admin_password() - app.run(host=app.config.get("FLASK_URL"), port=app.config.get("FLASK_PORT")) diff --git a/website/app_creation.py b/website/app_creation.py new file mode 100644 index 00000000..4f538d05 --- /dev/null +++ b/website/app_creation.py @@ -0,0 +1,33 @@ +from app import create_app, db +from flask import render_template +import os +from app.utils.init_modules import create_modules_db + +from app.utils.utils import gen_admin_password + +os.environ.setdefault('FLASKENV', 'development') + +app = create_app() + +@app.errorhandler(404) +def error_page_not_found(e): + return render_template('404.html'), 404 + + +def main(init_db=False, recreate_db=False, delete_db=False, create_module=False): + if init_db: + with app.app_context(): + db.create_all() + elif recreate_db: + with app.app_context(): + db.drop_all() + db.create_all() + elif delete_db: + with app.app_context(): + db.drop_all() + elif create_module: + with app.app_context(): + create_modules_db() + else: + gen_admin_password() + app.run(host=app.config.get("FLASK_URL"), port=app.config.get("FLASK_PORT") , use_reloader=False) diff --git a/website/launch.py b/website/launch.py new file mode 100755 index 00000000..a7605bf9 --- /dev/null +++ b/website/launch.py @@ -0,0 +1,43 @@ +import os +import argparse +import subprocess +import time +from app_creation import main + +import signal +import sys +def signal_handler(sig, frame): + kill_script() + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + +parser = argparse.ArgumentParser() +parser.add_argument("-i", "--init_db", help="Initialise the db if it not exist", action="store_true") +parser.add_argument("-r", "--reload_db", help="Delete and initialise the db", action="store_true") +parser.add_argument("-l", "--launch", help="Launch the app", action="store_true") +parser.add_argument("-ks", "--killscript", help="Kill screen running background", action="store_true") +args = parser.parse_args() + +def kill_script(): + r = ["screen", "-ls", "|", "egrep", "[0-9]+.misp_mod", "|", "cut", "-d.", "-f1"] + process = subprocess.Popen(r, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + if out: + subprocess.call(["screen", "-X", "-S", "misp_mod", "quit"]) + +if args.init_db: + main(init_db=True) +elif args.reload_db: + main(recreate_db=True) +elif args.launch: + os.environ.setdefault('FLASKENV', 'development') + kill_script() + subprocess.call(["screen", "-dmS", "misp_mod"]) + r = ["screen", "-S", "misp_mod", "-X", "screen", "-t", "misp_modules_server", "bash", "-c", "../env/bin/misp-modules", "-l", "127.0.0.1;", "read x"] + subprocess.call(r) + time.sleep(2) + main(create_module=True) + main() +elif args.killscript: + kill_script() \ No newline at end of file diff --git a/website/launch.sh b/website/launch.sh deleted file mode 100755 index c99de04d..00000000 --- a/website/launch.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash -isscripted=`screen -ls | egrep '[0-9]+.misp_mod' | cut -d. -f1` - -function killscript { - if [ $isscripted ]; then - screen -X -S misp_mod quit - fi -} - -function launch { - export FLASKENV="development" - killscript - screen -dmS "misp_mod" - screen -S "misp_mod" -X screen -t "misp_modules_server" bash -c "misp-modules -l 127.0.0.1; read x" - sleep 2 - python3 app.py -m - python3 app.py -} - -function test { - export FLASKENV="testing" - pytest -} - -function init_db { - python3 app.py -i -} - -function reload_db { - python3 app.py -r -} - - -if [ "$1" ]; then - case $1 in - -l | --launch ) launch; - ;; - -i | --init_db ) init_db; - ;; - -r | --reload_db ) reload_db; - ;; - -t | --test ) test; - ;; - -ks | --killscript ) killscript; - esac - shift -else - launch -fi diff --git a/website/migrate.py b/website/migrate.py new file mode 100644 index 00000000..ee0dc7c6 --- /dev/null +++ b/website/migrate.py @@ -0,0 +1,19 @@ +import os +import argparse +import subprocess + +os.environ.setdefault('FLASKENV', 'development') + +parser = argparse.ArgumentParser() +parser.add_argument("-m", "--migrate", help="Initialise the db if it not exist", action="store_true") +parser.add_argument("-u", "--upgrade", help="Delete and initialise the db", action="store_true") +parser.add_argument("-d", "--downgrade", help="Launch the app", action="store_true") +args = parser.parse_args() + + +if args.migrate: + subprocess.call(["flask", "db", "migrate"]) +elif args.upgrade: + subprocess.call(["flask", "db", "upgrade"]) +elif args.downgrade: + subprocess.call(["flask", "db", "downgrade"]) diff --git a/website/migrate.sh b/website/migrate.sh deleted file mode 100755 index 7cd38c8d..00000000 --- a/website/migrate.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -source env/bin/activate -export FLASKENV=development - -function migrate { - flask db migrate -} - -function upgrade { - flask db upgrade -} - -function downgrade { - flask db downgrade -} - - -if [ "$1" ]; then - case $1 in - -m | --migrate ) migrate; - ;; - -u | --upgrade ) upgrade; - ;; - -d | --downgrade ) downgrade; - esac - shift -else - echo "need -m or -u or -d" -fi