From 3a8497325f6104533b393e26d1c590507a6b773c Mon Sep 17 00:00:00 2001 From: s3lph Date: Wed, 11 Jul 2018 16:04:08 +0200 Subject: [PATCH] Moved database and user generated content to /var/matemat --- .gitlab-ci.yml | 2 +- Dockerfile | 2 ++ matemat/__main__.py | 2 +- matemat/webserver/pagelets/admin.py | 10 +++++----- matemat/webserver/pagelets/buy.py | 2 +- matemat/webserver/pagelets/deposit.py | 2 +- matemat/webserver/pagelets/login.py | 2 +- matemat/webserver/pagelets/main.py | 2 +- matemat/webserver/pagelets/modproduct.py | 8 ++++---- matemat/webserver/pagelets/moduser.py | 8 ++++---- matemat/webserver/pagelets/touchkey.py | 2 +- testing/Dockerfile | 2 ++ 12 files changed, 24 insertions(+), 20 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f89c018..2da70d5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,5 @@ --- -image: s3lph/matemat-ci:20180710-03 +image: s3lph/matemat-ci:20180711-01 stages: - test diff --git a/Dockerfile b/Dockerfile index faa0d1a..d70119d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,8 @@ FROM debian:buster RUN useradd -d /home/matemat -m matemat +RUN mkdir -p /var/matemat/db && chown matemat:matemat -R /var/matemat/db +RUN mkdir -p /var/matemat/upload && chown matemat:matemat -R /var/matemat/upload RUN apt-get update -qy RUN apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools build-essential RUN pip3 install wheel pycodestyle mypy diff --git a/matemat/__main__.py b/matemat/__main__.py index 9654b64..eef1fd5 100644 --- a/matemat/__main__.py +++ b/matemat/__main__.py @@ -13,4 +13,4 @@ if __name__ == '__main__': port = int(sys.argv[1]) # Start the web server - MatematWebserver(port=port).start() + MatematWebserver(port=port, staticroot='/var/matemat/upload').start() diff --git a/matemat/webserver/pagelets/admin.py b/matemat/webserver/pagelets/admin.py index 3d45fb4..f7e5ce8 100644 --- a/matemat/webserver/pagelets/admin.py +++ b/matemat/webserver/pagelets/admin.py @@ -23,7 +23,7 @@ def admin(method: str, if authlevel < 2: raise HttpException(403) - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: user = db.get_user(uid) if method == 'POST' and 'change' in args: handle_change(args, user, db) @@ -78,8 +78,8 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase) -> No if 'avatar' not in args: return avatar = bytes(args.avatar) - os.makedirs('./static/img/thumbnails/users/', exist_ok=True) - with open(f'./static/img/thumbnails/users/{user.id}.png', 'wb') as f: + os.makedirs('/var/matemat/upload/thumbnails/users/', exist_ok=True) + with open(f'/var/matemat/upload/thumbnails/users/{user.id}.png', 'wb') as f: f.write(avatar) except UnicodeDecodeError: @@ -111,8 +111,8 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase): newproduct = db.create_product(name, price_member, price_non_member) if 'image' in args: image = bytes(args.image) - os.makedirs('./static/img/thumbnails/products/', exist_ok=True) - with open(f'./static/img/thumbnails/products/{newproduct.id}.png', 'wb') as f: + os.makedirs('/var/matemat/upload/thumbnails/products/', exist_ok=True) + with open(f'/var/matemat/upload/thumbnails/products/{newproduct.id}.png', 'wb') as f: f.write(image) elif change == 'restock': diff --git a/matemat/webserver/pagelets/buy.py b/matemat/webserver/pagelets/buy.py index f8b4834..5cc685c 100644 --- a/matemat/webserver/pagelets/buy.py +++ b/matemat/webserver/pagelets/buy.py @@ -13,7 +13,7 @@ def buy(method: str, -> Union[str, bytes, PageletResponse]: if 'authenticated_user' not in session_vars: return RedirectResponse('/') - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: uid: int = session_vars['authenticated_user'] user = db.get_user(uid) if 'n' in args: diff --git a/matemat/webserver/pagelets/deposit.py b/matemat/webserver/pagelets/deposit.py index d081fa4..85e87c4 100644 --- a/matemat/webserver/pagelets/deposit.py +++ b/matemat/webserver/pagelets/deposit.py @@ -13,7 +13,7 @@ def deposit(method: str, -> Union[str, bytes, PageletResponse]: if 'authenticated_user' not in session_vars: return RedirectResponse('/') - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: uid: int = session_vars['authenticated_user'] user = db.get_user(uid) if 'n' in args: diff --git a/matemat/webserver/pagelets/login.py b/matemat/webserver/pagelets/login.py index 1747496..0d41e78 100644 --- a/matemat/webserver/pagelets/login.py +++ b/matemat/webserver/pagelets/login.py @@ -19,7 +19,7 @@ def login_page(method: str, if method == 'GET': return TemplateResponse('login.html') elif method == 'POST': - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: try: user: User = db.login(str(args.username), str(args.password)) except AuthenticationError: diff --git a/matemat/webserver/pagelets/main.py b/matemat/webserver/pagelets/main.py index 6b1fb8d..524e4e3 100644 --- a/matemat/webserver/pagelets/main.py +++ b/matemat/webserver/pagelets/main.py @@ -12,7 +12,7 @@ def main_page(method: str, session_vars: Dict[str, Any], headers: Dict[str, str])\ -> Union[bytes, str, PageletResponse]: - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: if 'authenticated_user' in session_vars: uid: int = session_vars['authenticated_user'] authlevel: int = session_vars['authentication_level'] diff --git a/matemat/webserver/pagelets/modproduct.py b/matemat/webserver/pagelets/modproduct.py index 9acf911..c5938a4 100644 --- a/matemat/webserver/pagelets/modproduct.py +++ b/matemat/webserver/pagelets/modproduct.py @@ -23,7 +23,7 @@ def modproduct(method: str, if authlevel < 2: raise HttpException(403) - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: authuser = db.get_user(auth_uid) if not authuser.is_admin: raise HttpException(403) @@ -47,7 +47,7 @@ def handle_change(args: RequestArguments, product: Product, db: MatematDatabase) if change == 'del': db.delete_product(product) try: - os.remove(f'./static/img/thumbnails/products/{product.id}.png') + os.remove(f'/var/matemat/upload/thumbnails/products/{product.id}.png') except FileNotFoundError: pass @@ -76,6 +76,6 @@ def handle_change(args: RequestArguments, product: Product, db: MatematDatabase) if 'image' in args: image = bytes(args.image) if len(image) > 0: - os.makedirs('./static/img/thumbnails/products/', exist_ok=True) - with open(f'./static/img/thumbnails/products/{product.id}.png', 'wb') as f: + os.makedirs('/var/matemat/upload/thumbnails/products/', exist_ok=True) + with open(f'/var/matemat/upload/thumbnails/products/{product.id}.png', 'wb') as f: f.write(image) diff --git a/matemat/webserver/pagelets/moduser.py b/matemat/webserver/pagelets/moduser.py index edacc99..f385d56 100644 --- a/matemat/webserver/pagelets/moduser.py +++ b/matemat/webserver/pagelets/moduser.py @@ -23,7 +23,7 @@ def moduser(method: str, if authlevel < 2: raise HttpException(403) - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: authuser = db.get_user(auth_uid) if not authuser.is_admin: raise HttpException(403) @@ -47,7 +47,7 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase) -> No if change == 'del': db.delete_user(user) try: - os.remove(f'./static/img/thumbnails/users/{user.id}.png') + os.remove(f'/var/matemat/upload/thumbnails/users/{user.id}.png') except FileNotFoundError: pass @@ -83,6 +83,6 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase) -> No if 'avatar' in args: avatar = bytes(args.avatar) if len(avatar) > 0: - os.makedirs('./static/img/thumbnails/users/', exist_ok=True) - with open(f'./static/img/thumbnails/users/{user.id}.png', 'wb') as f: + os.makedirs('/var/matemat/upload/thumbnails/users/', exist_ok=True) + with open(f'/var/matemat/upload/thumbnails/users/{user.id}.png', 'wb') as f: f.write(avatar) diff --git a/matemat/webserver/pagelets/touchkey.py b/matemat/webserver/pagelets/touchkey.py index 45abd90..da4f530 100644 --- a/matemat/webserver/pagelets/touchkey.py +++ b/matemat/webserver/pagelets/touchkey.py @@ -19,7 +19,7 @@ def touchkey_page(method: str, if method == 'GET': return TemplateResponse('touchkey.html', username=str(args.username), uid=int(str(args.uid))) elif method == 'POST': - with MatematDatabase('test.db') as db: + with MatematDatabase('/var/matemat/db/test.db') as db: try: user: User = db.login(str(args.username), touchkey=str(args.touchkey)) except AuthenticationError: diff --git a/testing/Dockerfile b/testing/Dockerfile index 5c0e1b3..a06b6ed 100644 --- a/testing/Dockerfile +++ b/testing/Dockerfile @@ -2,6 +2,8 @@ FROM debian:buster RUN useradd -d /home/matemat -m matemat +RUN mkdir -p /var/matemat/db && chown matemat:matemat -R /var/matemat/db +RUN mkdir -p /var/matemat/upload && chown matemat:matemat -R /var/matemat/upload RUN apt-get update -qy RUN apt-get install -y --no-install-recommends sudo git docker.io python3-dev python3-pip python3-coverage python3-setuptools build-essential RUN pip3 install wheel pycodestyle mypy