From baaf9577da7dc56b989cc08de8f85f67a60f51a5 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 31 Oct 2018 08:57:32 -0700 Subject: [PATCH] Environment Variables and Docker standalone support (#21) * Ignore .dist folder used by visual studio code * Add example env file * Added environment variables * Ignore env file * Added default name to be ytmanager instead * Default to postgres * Update example env for postgres instead of sqlite * Added default as postgres as well in settings * Add volume for sqlite3 database storage (incase desired) * Added example env file for sqlite3 configuration * whitespace for sanity of my own * Add database_url as an optional env variable * hopefully working towards chibicitiberiu/ytsm#6 * Added default command * Expose 8000 * Added environment variable prefixes * added environment variable to parse for dj_database_url * Take out command in docker-compose since it's already in the Dockerfile as a cmd * SQLite as default * Create Docker README with information about how to run with Docker * Took out postgres. If people desire it, they can add it themselves. * Took out db depenednecy for docker-compose * Change cmd * Fixed up commands to run better in readme * Added underscore to Docker_README * Change default .db file location --- .gitignore | 7 +++++++ Docker_README.md | 39 +++++++++++++++++++++++++++++++++++++++ Dockerfile | 17 ++++++++++++++++- app/YtManager/settings.py | 18 +++++++++++------- app/requirements.txt | 3 ++- docker-compose.yml | 16 +++------------- sqlite3.env.env | 3 +++ 7 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 Docker_README.md create mode 100644 sqlite3.env.env diff --git a/.gitignore b/.gitignore index 62681d9..6fb4095 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .vs .vscode temp/ +env.env # Byte-compiled / optimized / DLL files __pycache__/ @@ -115,3 +116,9 @@ venv.bak/ dmypy.json media/ + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json diff --git a/Docker_README.md b/Docker_README.md new file mode 100644 index 0000000..daa479c --- /dev/null +++ b/Docker_README.md @@ -0,0 +1,39 @@ +Running with Docker +=== + +Sample Run command +----- +```bash +docker run -d --name ytsm -p 80:8000 --env-file sqlite3.env.env --volume ./downloads:/usr/src/app/data/downloads chibicitiberiu/ytsm:latest +``` +### Quick Rundown: +- `--expose 80:8000` maps the Host OS port 80 to the container port 80 +- `--env-file sqlite3.env.env` points to the env file with the desired variable settings (saves from typing them in the command line) +- `--volume ./downloads:/usr/src/app/data/downloads` maps the downloads folder of the current directory to the container folder `downloads` (where you could set the program to download to) +- `chibicitiberiu/ytsm:latest` tells Docker which image to run the container with (in this case, the latest version) + +**Note:** Replace `./downloads` in the command to where you want the downloads folder to be mapped to on the Host OS. Ex: `/path/to/host/download/folder:/path/to/container/download/folder` + + +Environment variables +----- +- YTSM_DATABASE_ENGINE +- YTSM_DATABASE_NAME +- YTSM_YOUTUBE_API_KEY + + +Volumes +----- +- /usr/src/app/data/media +- /usr/src/app/data/db + + +Notes +---- +If you experience any issues with the app running, make sure to run the following command to apply Django migrations to the database + +### When using just the Dockerfile/Image +- `docker exec ytsm python manage.py migrate` + +### When using the docker-compose file +- `docker exec ytsm_web_1 python manage.py migrate` diff --git a/Dockerfile b/Dockerfile index c00fc2c..4f10986 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,20 @@ RUN apt-get install ffmpeg -y COPY ./app/requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt +ENV YTSM_DATABASE_ENGINE='django.db.backends.sqlite3' +ENV YTSM_DATABASE_NAME='/usr/src/app/data/db/ytmanager.db' +ENV YTSM_DATABASE_HOST='' +ENV YTSM_DATABASE_USERNAME='' +ENV YTSM_DATABASE_PASSWORD='' +ENV YTSM_DATABASE_PORT='' +ENV YTSM_YOUTUBE_API_KEY='AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8' + +VOLUME /usr/src/app/data/media +VOLUME /usr/src/app/data/db + COPY ./app/ . -COPY ./config/ ./config/ \ No newline at end of file +COPY ./config/ ./config/ + +EXPOSE 8000 + +CMD ["/bin/bash", "init.sh"] \ No newline at end of file diff --git a/app/YtManager/settings.py b/app/YtManager/settings.py index 35bbb2f..5e5f8e2 100644 --- a/app/YtManager/settings.py +++ b/app/YtManager/settings.py @@ -21,7 +21,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '^zv8@i2h!ko2lo=%ivq(9e#x=%q*i^^)6#4@(juzdx%&0c+9a0' -YOUTUBE_API_KEY = "AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8" +YOUTUBE_API_KEY = os.getenv('YTSM_YOUTUBE_API_KEY', 'AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -81,15 +81,19 @@ WSGI_APPLICATION = 'YtManager.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'postgres', - 'USER': 'postgres', - 'HOST': 'db', - 'PORT': 5432, + 'ENGINE': os.getenv('YTSM_DATABASE_ENGINE', 'django.db.backends.postgresql'), + 'NAME': os.getenv('YTSM_DATABASE_NAME', os.path.join(BASE_DIR, 'ytmanager.db')), + 'HOST': os.getenv('YTSM_DATABASE_HOST', None), + 'USER': os.getenv('YTSM_DATABASE_USERNAME', None), + 'PASSWORD': os.getenv('YTSM_DATABASE_PASSWORD', None), + 'PORT': os.getenv('YTSM_DATABASE_PORT', None) } - } +if os.getenv('YTSM_DATABASE_URL', None): + import dj_database_url + DATABASES['default'] = dj_database_url.parse(os.environ['YTSM_DATABASE_URL'], conn_max_age=600) + # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators diff --git a/app/requirements.txt b/app/requirements.txt index df7ef7c..9ee6d74 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -2,9 +2,10 @@ apscheduler gunicorn django django-crispy-forms +dj_database_url youtube-dl google-api-python-client google_auth_oauthlib oauth2client psycopg2-binary -python-dateutil +python-dateutil \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 751e3ad..c666333 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,23 +11,13 @@ services: depends_on: - web - db: - image: postgres - restart: always - volumes: - - /var/lib/postgresql/data - environment: - POSTGRES_PASSWORD: postgres - POSTGRES_DB: ytmanager - web: build: . + env_file: + - sqlite3.env.env tty: true - command: ./init.sh ports: - "8000:8000" volumes: - ./media:/usr/src/app/data/media - depends_on: - - db - + - ./db:/usr/src/app/data/db diff --git a/sqlite3.env.env b/sqlite3.env.env new file mode 100644 index 0000000..adfe8b4 --- /dev/null +++ b/sqlite3.env.env @@ -0,0 +1,3 @@ +YTSM_DATABASE_ENGINE=django.db.backends.sqlite3 +YTSM_DATABASE_NAME=/usr/src/app/data/db/ytmanager.db +YTSM_YOUTUBE_API_KEY=AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8