Package: django-docker-helpers
This package provides some useful tools you can use with yourmanage.py, so you have no need to use bash entry points and
non-python scripting. As well it: - reads configs with typing support
from env, yaml, redis, consul; - provides some helper functionsInstallationpipinstall-egit+https://github.com/night-crawler/django-docker-helpers.git#egg=django-docker-helpers# ORpipinstalldjango-docker-helpersUtilsenv_bool_flag(flag_name, strict)- check if ENV option specified,
is it set to true, 1, 0, etc.run_env_onceensure django management don’t calltwice<https://stackoverflow.com/questions/16546652/why-does-django-run-everything-twice>_is_dockerized- readsDOCKERIZEDflag from envis_production- readsPRODUCTIONflag from envManagement Helper functionsensure_databases_alive(max_retries=100)- tries to executeSELECT 1for every specified database alias inDATABASESuntil success or max_retries reachedensure_caches_alive(max_retries=100)- tries to executeSELECT 1for every specified cache alias inCACHESuntil
success or max_retries reachedmigrate- executes./manage.py migratemodeltranslation_sync_translation_fields- runsync_translation_fieldsifmodeltranslationis presentcollect_static- alias for./manage.py collectstatic-c--noinput-v0create_admin- create superuser fromsettings.CONFIG['superuser']if user does not exists and user has
no usable passwordrun_gunicorn(application: WSGIHandler, gunicorn_module_name:str='gunicorn_prod')- runs gunicornSample configdebug:truedb:engine:django.db.backends.postgresqlhost:postgresport:5432database:mydbuser:mydb_userpassword:mydb_passwordconn_max_age:60Read configimportosfromdjango_docker_helpers.configimportConfigLoader,EnvironmentParser,RedisParser,YamlParseryml_conf='/tmp/my/config/without-docker.yml'redis_conf=os.environ.get('DJANGO_CONFIG_REDIS_KEY','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),}}UsageIn the most cases your manage.py may look like:#!/usr/bin/env python#!/usr/bin/env pythonimportosimportsysfromdjango_docker_helpers.dbimport(ensure_caches_alive,ensure_databases_alive,migrate)fromdjango_docker_helpers.filesimportcollect_staticfromdjango_docker_helpers.managementimportcreate_admin,run_gunicornfromdjango_docker_helpers.utilsimportenv_bool_flag,run_env_once,wffromsetproctitleimportsetproctitle@run_env_oncedefinvalidate_static_rev():fromdjango.core.managementimportcall_commandcall_command('invalidate_static_rev')@run_env_oncedefload_lang_fixtures():fromdjango.core.managementimportcall_commandcall_command('populate_languages')@run_env_oncedefload_dev_fixtures():fromdjango.core.managementimportcall_commandwf('Loading DEVELOPMENT fixtures... ',False)call_command('loaddata','fixtures/dev/service_api__api_key.json')wf('[DONE]\n')if__name__=='__main__':os.environ.setdefault('DJANGO_SETTINGS_MODULE','my_project.settings')setproctitle('MyProject')ifenv_bool_flag('CHECK_CONNECTIONS'):ensure_databases_alive(100)ensure_caches_alive(100)ifenv_bool_flag('RUN_PREPARE'):collect_static()migrate()invalidate_static_rev()load_lang_fixtures()create_admin('SUPERUSER')ifenv_bool_flag('LOAD_DEV_FIXTURES'):load_dev_fixtures()iflen(sys.argv)==2:ifsys.argv[1]=='rungunicorn':frommy_project.wsgiimportapplicationgunicorn_module_name=os.getenv('GUNICORN_MODULE_NAME','gunicorn_dev')run_gunicorn(application,gunicorn_module_name=gunicorn_module_name)exit()fromdjango.core.managementimportexecute_from_command_lineexecute_from_command_line(sys.argv)Testing$ pip install-rrequirements/dev.txtDownload Consuland unzip
it into the project’s directory.CONSUL_VERSION=1.4.0 bash-c'curl-sLoconsul.ziphttps://releases.hashicorp.com/consul/"$CONSUL_VERSION"/consul_"$CONSUL_VERSION"_linux_amd64.zip'&& unzip consul.zip$ ./consul agent-server-ui-dev$ pytest