Skip to content

Commit

Permalink
Create users table and admin user in cesi app, instead of creating ma…
Browse files Browse the repository at this point in the history
…nuelly database.
  • Loading branch information
f9n committed Nov 9, 2018
1 parent e01e494 commit 266c7fd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions cesi/core/cesi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
24 changes: 7 additions & 17 deletions cesi/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
)
Expand All @@ -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
)
6 changes: 4 additions & 2 deletions defaults/cesi.conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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://<user>:<password>@localhost:5432/<database_name>"
#database = "mysql+pymysql://<user>:<password>@localhost:3306/<database_name>"
activity_log = "activity.log" # File path for CeSI logs
Expand Down

0 comments on commit 266c7fd

Please sign in to comment.