Django Docker helpers
Project description
pip install -e git+https://github.com/night-crawler/django-docker-helpers.git#egg=django-docker-helpers
# OR
pip install django-docker-helpers
Utils
env_bool_flag(flag_name, strict) - check if ENV option specified, is it set to true, 1, 0, etc.
run_env_once ensure django management don’t call twice <https://stackoverflow.com/questions/16546652/why-does-django-run-everything-twice>_
is_dockerized - reads DOCKERIZED flag from env
is_production - reads PRODUCTION flag from env
Management Helper functions
ensure_databases_alive(max_retries=100) - tries to execute SELECT 1 for every specified database alias in DATABASES until success or max_retries reached
ensure_caches_alive(max_retries=100) - tries to execute SELECT 1 for every specified cache alias in CACHES until success or max_retries reached
migrate - executes ./manage.py migrate
modeltranslation_sync_translation_fields - run sync_translation_fields if modeltranslation is present
collect_static - alias for ./manage.py collectstatic -c --noinput -v0
create_admin - create superuser from settings.CONFIG['superuser'] if user does not exists and user has no usable password
run_gunicorn(application: WSGIHandler, gunicorn_module_name: str='gunicorn_prod') - runs gunicorn
Sample config
debug: true
db:
engine: django.db.backends.postgresql
host: postgres
port: 5432
database: mydb
user: mydb_user
password: mydb_password
conn_max_age: 60
Read config
import os
from django_docker_helpers.config import ConfigLoader, EnvironmentParser, RedisParser, YamlParser
yml_conf = os.('/tmp/my/config/without-docker.yml')
redis_conf = os.environ.get('DJANGO_CONFIG_REDIS_KEY', 'marfa_msa_cas/conf.yml')
parsers = [
EnvironmentParser(),
RedisParser(endpoint=redis_conf),
YamlParser(config=yml_conf),
]
configure = ConfigLoader(parsers=parsers, silent=True)
DATABASES = {
'default': {
'ENGINE': configure('db.name', 'django.db.backends.postgresql'),
'HOST': configure('db.host', 'localhost'),
'PORT': configure('db.port', 5432),
'NAME': configure('db.database', 'project_default'),
'USER': configure('db.user', 'project_default'),
'PASSWORD': configure('db.password', 'project_default'),
'CONN_MAX_AGE': configure('db.conn_max_age', 60, coerce_type=int),
}
}
Usage
#!/usr/bin/env python
import os
import sys
from django.core.management import execute_from_command_line
from django_docker_helpers.db import ensure_databases_alive, ensure_caches_alive, migrate, \
modeltranslation_sync_translation_fields
from django_docker_helpers.files import collect_static
from django_docker_helpers.management import create_admin, run_gunicorn
from msa_mailer.wsgi import application
PRODUCTION = bool(int(os.environ.get('MSA_MAILER_PRODUCTION', 0) or 0))
SERVER = bool(int(os.environ.get('MSA_MAILER_SERVER', 0) or 0))
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'msa_mailer.settings')
if PRODUCTION or os.environ.get('MSA_MAILER_FORCE_PRODUCTION'):
ensure_databases_alive(100)
ensure_caches_alive(100)
# skip collectstatic & migrations for worker
if SERVER:
collect_static()
migrate()
modeltranslation_sync_translation_fields()
create_admin()
if len(sys.argv) == 2 and sys.argv[1] == 'gunicorn':
gunicorn_module_name = 'gunicorn_dev'
if PRODUCTION:
gunicorn_module_name = 'gunicorn_prod'
run_gunicorn(application, gunicorn_module_name=gunicorn_module_name)
else:
execute_from_command_line(sys.argv)
Testing
$ pip install -r requirements/dev.txt
Download Consul and unzip it into the project’s directory.
$ ./consul agent -server -ui -dev
$ pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-docker-helpers-0.1.14.tar.gz.
File metadata
- Download URL: django-docker-helpers-0.1.14.tar.gz
- Upload date:
- Size: 27.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86014e25e2b02b436dc6f614626f028e2a49fee4bf6ef0e29ecf5435873f2725
|
|
| MD5 |
3bb00aa78c0b22c90cee50f2d6d1c542
|
|
| BLAKE2b-256 |
742a67615ded90e2649028f5be19022cd9393af4adb881370388d8cb9c14814c
|
File details
Details for the file django_docker_helpers-0.1.14-py3-none-any.whl.
File metadata
- Download URL: django_docker_helpers-0.1.14-py3-none-any.whl
- Upload date:
- Size: 42.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f97e07bc99db3d4a209c46c70a519327a0938d77f9406fbb41b1f7f5cdde4dc
|
|
| MD5 |
6d9eeed157d51fa5350712c656c33d6e
|
|
| BLAKE2b-256 |
f8cf40498b11967dcd7e1d76cdc0a5c54148d24393d6cdd2b68e61c14d204ae3
|