From 266c7fd7a4b4cc79eef336dcb3e8c2540ba31f51 Mon Sep 17 00:00:00 2001 From: Fatih Sarhan Date: Fri, 9 Nov 2018 17:01:07 +0300 Subject: [PATCH] Create users table and admin user in cesi app, instead of creating manuelly database. --- README.md | 6 ------ cesi/core/cesi.py | 16 ++++++++-------- cesi/run.py | 24 +++++++----------------- defaults/cesi.conf.toml | 6 ++++-- 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 7c5ad88..483406b 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,6 @@ $ python3 -m venv venv $ source venv/bin/activate (venv) $ pip3 install -r requirements.txt -$ # Create database -(venv) $ python3 ${CESI_SETUP_PATH}/cesi/run.py --config-file ${CESI_SETUP_PATH}/defaults/cesi.conf.toml --initialize-database - $ # Run with command line (venv) $ python3 ${CESI_SETUP_PATH}/cesi/run.py --config-file ${CESI_SETUP_PATH}/defaults/cesi.conf.toml ``` @@ -68,9 +65,6 @@ $ # Create virtual environment and install requirement packages $ python3 -m venv venv $ source venv/bin/activate (venv) $ pip3 install -r requirements.txt - -$ # Create database -(venv) $ python3 ${CESI_SETUP_PATH}/cesi/run.py --config-file ${CESI_SETUP_PATH}/defaults/cesi.conf.toml --initialize-database (venv) $ deactivate # Deactivate virtual environment $ # Build ui (First you must install dependencies for ui -> yarn) - Optional diff --git a/cesi/core/cesi.py b/cesi/core/cesi.py index f0df2d9..04efb3f 100644 --- a/cesi/core/cesi.py +++ b/cesi/core/cesi.py @@ -110,14 +110,14 @@ def reload(self): self.load_config() print("Reloaded.") - def create_default_database(self): - ### Drop All tables - db.reflect() - db.drop_all() - ### Create Tables - db.create_all() - ### Add Admin User - admin_user = User.register(username="admin", password="admin", usertype=0) + def check_database(self): + try: + ### Create Tables + db.create_all() + ### Add Admin User + admin_user = User.register(username="admin", password="admin", usertype=0) + except Exception as e: + print(e) def get_all_processes(self): processes = [] diff --git a/cesi/run.py b/cesi/run.py index f7f3b85..703c6ce 100755 --- a/cesi/run.py +++ b/cesi/run.py @@ -48,6 +48,10 @@ def configure(config_file_path): app = create_app(cesi) + # Check database + with app.app_context(): + cesi.check_database() + signal.signal(signal.SIGHUP, lambda signum, frame: cesi.reload()) return app, cesi @@ -59,11 +63,6 @@ def configure(config_file_path): parser.add_argument("-c", "--config-file", help="config file", required=True) parser.add_argument("--host", help="Host of the cesi", default="0.0.0.0") parser.add_argument("-p", "--port", help="Port of the cesi", default="5000") - parser.add_argument( - "--initialize-database", - help="Initialize database of the cesi", - action="store_true", - ) parser.add_argument( "--debug", help="Actived debug mode of the cesi", action="store_true" ) @@ -78,15 +77,6 @@ def configure(config_file_path): app, cesi = configure(args.config_file) - if args.initialize_database: - print("Initializing database...") - with app.app_context(): - cesi.create_default_database() - else: - app.run( - host=args.host, - port=args.port, - use_reloader=args.auto_reload, - debug=args.debug, - ) - + app.run( + host=args.host, port=args.port, use_reloader=args.auto_reload, debug=args.debug + ) diff --git a/defaults/cesi.conf.toml b/defaults/cesi.conf.toml index 72926b8..659e623 100644 --- a/defaults/cesi.conf.toml +++ b/defaults/cesi.conf.toml @@ -3,8 +3,10 @@ # This is the CeSI's own configuration. [cesi] -database = "sqlite:///users.db" # Database uri -# Etc. +# Database Uri +database = "sqlite:///users.db" # Relative path +# Etc +#database = "sqlite:////opt/cesi/< version >/users.db" # Absolute path #database = "postgres://:@localhost:5432/" #database = "mysql+pymysql://:@localhost:3306/" activity_log = "activity.log" # File path for CeSI logs