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
This commit is contained in:
Brandon 2018-10-31 08:57:32 -07:00 committed by chibicitiberiu
parent 092949ff9c
commit baaf9577da
7 changed files with 81 additions and 22 deletions

7
.gitignore vendored
View File

@ -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

39
Docker_README.md Normal file
View File

@ -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`

View File

@ -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/
COPY ./config/ ./config/
EXPOSE 8000
CMD ["/bin/bash", "init.sh"]

View File

@ -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

View File

@ -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

View File

@ -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

3
sqlite3.env.env Normal file
View File

@ -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