diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9e692b1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +# Ignore everything +* + +# Include source, config and scripts +!app +!etc +!*.md +!*.sh +!*.txt +!LICENSE diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5c2add3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +# syntax=docker/dockerfile:1 +FROM python:3.9.2-slim AS development + +WORKDIR /src + +RUN pip install pymysql + +ADD https://git.zeus.gent/haldis/menus/-/archive/master/menus-master.tar /tmp +RUN mkdir menus && \ + tar --directory=menus --extract --strip-components=1 --file=/tmp/menus-master.tar + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY . . + +WORKDIR /src/app +CMD python app.py db upgrade && \ + python app.py runserver -h 0.0.0.0 -p 8000 + +FROM development AS production + +RUN pip install waitress + +CMD python app.py db upgrade && \ + python waitress_wsgi.py diff --git a/app/config.example.py b/app/config.example.py index 1b016a8..ae60a55 100644 --- a/app/config.example.py +++ b/app/config.example.py @@ -1,11 +1,16 @@ """An example for a Haldis config""" -# config +# import os class Configuration: "Haldis configuration object" # pylint: disable=too-few-public-methods SQLALCHEMY_DATABASE_URI = "sqlite:///haldis.db" + # MARIADB_HOST = os.environ.get("MARIADB_HOST") + # MARIADB_DB = os.environ.get("MARIADB_DATABASE") + # MARIADB_USER = os.environ.get("MARIADB_USER") + # MARIADB_PASS = os.environ.get("MARIADB_PASSWORD") + # SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://{MARIADB_USER}:{MARIADB_PASS}@{MARIADB_HOST}/{MARIADB_DB}" SQLALCHEMY_TRACK_MODIFICATIONS = False DEBUG = True HALDIS_ADMINS = [] diff --git a/app/hlds/definitions.py b/app/hlds/definitions.py index 7f6e0c1..1e540e7 100644 --- a/app/hlds/definitions.py +++ b/app/hlds/definitions.py @@ -17,5 +17,8 @@ location_definitions: List[Location] = parse_all_directory(str(DATA_DIR)) location_definitions.sort(key=lambda l: l.name) -proc = subprocess.run(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, cwd=str(ROOT_DIR), check=True) -location_definition_version = proc.stdout.decode().strip() +try: + proc = subprocess.run(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, cwd=str(ROOT_DIR), check=True) + location_definition_version = proc.stdout.decode().strip() +except FileNotFoundError: + location_definition_version = "" diff --git a/app/migrations/versions/150252c1cdb1_.py b/app/migrations/versions/150252c1cdb1_.py index 927c101..77c7961 100644 --- a/app/migrations/versions/150252c1cdb1_.py +++ b/app/migrations/versions/150252c1cdb1_.py @@ -43,7 +43,7 @@ def upgrade(): sa.Column("starttime", sa.DateTime(), nullable=True), sa.Column("stoptime", sa.DateTime(), nullable=True), sa.Column("public", sa.Boolean(), nullable=True), - sa.ForeignKeyConstraint(["location_id"], ["location.id"]), + sa.ForeignKeyConstraint(["location_id"], ["location.id"], name="order_ibfk_1"), sa.PrimaryKeyConstraint("id"), ) op.create_table( @@ -65,7 +65,7 @@ def upgrade(): sa.Column("extra", sa.String(length=254), nullable=True), sa.Column("name", sa.String(length=120), nullable=True), sa.ForeignKeyConstraint(["order_id"], ["order.id"]), - sa.ForeignKeyConstraint(["product_id"], ["product.id"]), + sa.ForeignKeyConstraint(["product_id"], ["product.id"], name="order_item_ibfk_3"), sa.ForeignKeyConstraint(["user_id"], ["user.id"]), sa.PrimaryKeyConstraint("id"), ) diff --git a/app/migrations/versions/9159a6fed021_initial_haldis_support.py b/app/migrations/versions/9159a6fed021_initial_haldis_support.py index 710feb2..70ce1d7 100644 --- a/app/migrations/versions/9159a6fed021_initial_haldis_support.py +++ b/app/migrations/versions/9159a6fed021_initial_haldis_support.py @@ -112,14 +112,12 @@ def upgrade(): ) ) # Historical product data migrated, drop obsolete column and table - op.execute(text("ALTER TABLE order_item DROP FOREIGN KEY order_item_ibfk_3")) + op.drop_constraint("order_item_ibfk_3", "order_item", type_="foreignkey") op.drop_column("order_item", "product_id") op.drop_table("product") # ---------------------------------------------------------------------------------------------- # Migrate historical location data to orders - - op.execute(text("ALTER TABLE `order` DROP FOREIGN KEY order_ibfk_2")) op.alter_column( "order", "location_id", @@ -157,6 +155,7 @@ def upgrade(): for query in chain(new_location_id, [location_name_from_location]): op.execute(query) # Historical location data migrated, drop obsolete column and table + op.drop_constraint("order_ibfk_1", "order", type_="foreignkey") op.drop_column("order", "legacy_location_id") op.drop_table("location") diff --git a/app/waitress_wsgi.py b/app/waitress_wsgi.py new file mode 100644 index 0000000..579aa92 --- /dev/null +++ b/app/waitress_wsgi.py @@ -0,0 +1,16 @@ +import sentry_sdk +from sentry_sdk.integrations.flask import FlaskIntegration +from waitress import serve + +from app import create_app +from config import Configuration + +if __name__ == "__main__": + if Configuration.SENTRY_DSN: + sentry_sdk.init( + dsn=Configuration.SENTRY_DSN, + integrations=[FlaskIntegration()] + ) + + app, app_mgr = create_app() + serve(app, host="0.0.0.0", port=8000) diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..536e11b --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,17 @@ +version: "3.4" + +services: + app: + build: + target: "development" + environment: + - MARIADB_DATABASE=haldis + - MARIADB_USER=haldis + - MARIADB_PASSWORD=haldis + volumes: ["$PWD:/src"] + database: + environment: + - MARIADB_DATABASE=haldis + - MARIADB_ROOT_PASSWORD=mariadb + - MARIADB_USER=haldis + - MARIADB_PASSWORD=haldis \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..297cffd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: "3.4" + +services: + app: + build: + context: . + target: production + restart: on-failure + depends_on: [database] + ports: ["8000:8000"] + environment: + - MARIADB_HOST=database + - MARIADB_DATABASE + - MARIADB_USER + - MARIADB_PASSWORD + networks: [haldis] + database: + image: mariadb:10.8 + hostname: database + restart: on-failure + environment: + - MARIADB_DATABASE + - MARIADB_ROOT_PASSWORD + - MARIADB_USER + - MARIADB_PASSWORD + networks: [haldis] + volumes: [haldis_data:/var/lib/mysql] +networks: + haldis: +volumes: + haldis_data: \ No newline at end of file