From 10446740740bae9845d71481a2c91f314d342ed2 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 5 Sep 2019 16:39:50 +0200 Subject: [PATCH] Dockerize the App! --- .flaskenv | 2 -- .gitignore | 6 ++---- Dockerfile | 9 +++++++-- config.py | 29 +++++++++++++++-------------- docker-entrypoint.sh | 6 ++++-- opaque.py | 5 +++++ opaque_database_env_example | 3 +++ .env_example => opaque_env_example | 3 --- requirements.txt | 1 + 9 files changed, 37 insertions(+), 27 deletions(-) delete mode 100644 .flaskenv create mode 100644 opaque_database_env_example rename .env_example => opaque_env_example (84%) diff --git a/.flaskenv b/.flaskenv deleted file mode 100644 index 47093cda..00000000 --- a/.flaskenv +++ /dev/null @@ -1,2 +0,0 @@ -### Flask ### -FLASK_APP=opaque.py diff --git a/.gitignore b/.gitignore index fc9b731c..682148e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ -data_dev.sqlite migrations -venv -.env -.env_database +opaque_database_env +opaque_env __pycache__ diff --git a/Dockerfile b/Dockerfile index e588a372..b474e068 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,9 @@ FROM python:3.6-alpine -RUN apk add build-base +RUN apk add \ + build-base \ + postgresql-dev RUN adduser -D opaque @@ -13,10 +15,13 @@ WORKDIR /home/opaque COPY app app COPY migrations migrations -COPY opaque.py config.py ./ +COPY config.py opaque.py ./ COPY requirements.txt requirements.txt +ENV FLASK_APP=opaque.py + + RUN python -m venv venv && \ venv/bin/pip install -r requirements.txt diff --git a/config.py b/config.py index fb7ff521..ed0dadbf 100644 --- a/config.py +++ b/config.py @@ -28,32 +28,33 @@ class Config: class DevelopmentConfig(Config): ''' ### Flask ### ''' DEBUG = True - # SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, - # 'data_dev.sqlite') - SQLALCHEMY_DATABASE_URI = \ - 'postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}'.format( - user=os.environ.get('POSTGRES_USER'), - passwd=os.environ.get('POSTGRES_PASSWORD'), - host=os.environ.get('POSTGRES_HOST'), - port=os.environ.get('POSTGRES_PORT'), - db=os.environ.get('POSTGRES_DB_NAME')) - print(SQLALCHEMY_DATABASE_URI) ''' ### Flask-SQLAlchemy ### ''' - SQLALCHEMY_DATABASE_URI = 'sqlite:///{}'.format( - os.path.join(os.path.dirname(os.path.abspath(__file__)), - 'data_dev.sqlite') + SQLALCHEMY_DATABASE_URI = 'postgresql://{}:{}@opaque_database/{}'.format( + os.environ.get('POSTGRES_USER'), + os.environ.get('POSTGRES_PASSWORD'), + os.environ.get('POSTGRES_DB_NAME') ) class TestingConfig(Config): + ''' ### Flask ### ''' + TESTING = True + ''' ### Flask-SQLAlchemy ### ''' SQLALCHEMY_DATABASE_URI = 'sqlite://' + ''' ### Flask-WTF ### ''' + WTF_CSRF_ENABLED = False + class ProductionConfig(Config): ''' ### Flask-SQLAlchemy ### ''' - SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI') + SQLALCHEMY_DATABASE_URI = 'postgresql://{}:{}@opaque_database/{}'.format( + os.environ.get('POSTGRES_USER'), + os.environ.get('POSTGRES_PASSWORD'), + os.environ.get('POSTGRES_DB_NAME') + ) config = { diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 535d86a0..b317cda4 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,13 +1,15 @@ -#!/bin/bash +#!/bin/sh -# If no argument is given, start Opaque if [ $# -eq 0 ] then + # If no argument is given, start Opaque venv/bin/python opaque.py else if [[ $1 == "--create-db" ]] then + source venv/bin/activate flask db init flask db upgrade + flask foo fi fi diff --git a/opaque.py b/opaque.py index 8d4b0101..f0f2fc8d 100644 --- a/opaque.py +++ b/opaque.py @@ -28,5 +28,10 @@ def test(): unittest.TextTestRunner(verbosity=2).run(tests) +@app.cli.command() +def foo(): + Role.insert_roles() + + if __name__ == '__main__': socketio.run(app, host='0.0.0.0') diff --git a/opaque_database_env_example b/opaque_database_env_example new file mode 100644 index 00000000..5ad5b2b6 --- /dev/null +++ b/opaque_database_env_example @@ -0,0 +1,3 @@ +POSTGRES_DB_NAME=opaque +POSTGRES_USER=opaque +POSTGRES_PASSWORD=opaque diff --git a/.env_example b/opaque_env_example similarity index 84% rename from .env_example rename to opaque_env_example index f328a3ed..274698b7 100644 --- a/.env_example +++ b/opaque_env_example @@ -12,6 +12,3 @@ MAIL_DEFAULT_SENDER=username@example.com ### Opaque ### OPAQUE_ADMIN=admin.opaque@example.com - -### Flask-SQLAlchemy ### -# SQLALCHEMY_DATABASE_URI= diff --git a/requirements.txt b/requirements.txt index 19d3e6ed..f68e78f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ Flask-SQLAlchemy Flask-Table Flask-WTF jsonpatch +psycopg2 redis