Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to init some user from environment variable to init some users without using the UI #135

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from utils import *
from library import *
import titledb
import os

def init():
global watcher
Expand All @@ -36,6 +37,47 @@ def init():
titledb.update_titledb(app_settings)
load_titledb(app_settings)

def init_user_from_environment(environment_name, admin=False):
"""
allow to init some user from environment variable to init some users without using the UI
"""
with app.app_context():

username = os.getenv(environment_name + '_NAME')
password = os.getenv(environment_name + '_PASSWORD')
if admin:
logger.info('Initializing an admin user from environment variable...')
admin_access = True
shop_access = True
backup_access = True
else:
logger.info('Initializing a regular user from environment variable...')
admin_access = False
shop_access = True
backup_access = False

if username and password:
if not admin:
existing_admin = admin_account_created()
if not existing_admin and not admin_access:
logger.error(f'Error creating user {username}, first account created must be admin')
return

logger.info(f'Looking for existing user {username}')
user = User.query.filter_by(user=username).first()
if user:
logger.info(f'Updating an existing user {username}')
user.admin_access = admin_access
user.shop_access = shop_access
user.backup_access = backup_access
user.password = generate_password_hash(password, method='scrypt')
else:
logger.info(f'Creating an user {username}')
new_user = User(user=username, password=generate_password_hash(password, method='scrypt'), admin_access=admin_access, shop_access=shop_access, backup_access=backup_access)
db.session.add(new_user)
db.session.commit()


os.makedirs(CONFIG_DIR, exist_ok=True)
os.makedirs(DATA_DIR, exist_ok=True)

Expand Down Expand Up @@ -430,5 +472,7 @@ def before_request():
if __name__ == '__main__':
logger.info('Starting initialization of Ownfoil...')
init()
init_user_from_environment(environment_name="USER_ADMIN", admin=True)
init_user_from_environment(environment_name="USER_GUEST", admin=False)
logger.info('Initialization steps done, starting server...')
app.run(debug=False, host="0.0.0.0", port=8465)
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ services:
# For write permission in config directory
- PUID=1000
- PGID=1000
# to create/update an admin user at startup
# - USER_ADMIN_NAME=admin
# - USER_ADMIN_PASSWORD=asdvnf!546
# to create/update a regular user at startup
# - USER_GUEST_NAME=guest
# - USER_GUEST_PASSWORD=oerze!@8981
volumes:
- /your/game/directory:/games
- ./config:/app/config
ports:
- "8465:8465"
- "8465:8465"