Skip to content

Commit

Permalink
Merge branch 'chore/misc'
Browse files Browse the repository at this point in the history
  • Loading branch information
night-crawler committed Dec 10, 2018
2 parents 2298cda + 062c33b commit 908b431
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ tests/test_app/migrations

# Files
consul
tests/test.sqlite
tests/test.sqlite
/.coverage
consul.zip
4 changes: 4 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python:
version: 3.6
pip_install: true
requirements_file: null
54 changes: 54 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
dist: xenial
language: python
cache:
directories:
- $CONSUL_DIR
pip: true
sudo: false

services:
- redis-server

env:
global:
- CONSUL_VERSION=1.4.0
- CONSUL_DIR=$HOME/consul_$CONSUL_VERSION

install:
- pip install -r requirements/dev.txt

before_install:
- mkdir -p ${CONSUL_DIR}
- curl -sLo ${CONSUL_DIR}/consul.zip https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip
- unzip -o /${CONSUL_DIR}/consul.zip -d /${CONSUL_DIR}/
- export PATH="${CONSUL_DIR}:$PATH"
- consul agent -server -ui -dev &
- sleep 5
# - pip install -U pip setuptools virtualenvwrapper
# - source $(which virtualenvwrapper.sh)
# - wipeenv

script:
- pip install django==$DJANGO
- pytest --cov="django_docker_helpers"

after_success:
- pip install codecov
- codecov

after_failure:
- dmesg | tail -n 100

matrix:
fast_finish: true
include:
- { python: "3.6", env: [DJANGO=1.11] }
- { python: "3.6", env: [DJANGO=2.0] }
- { python: "3.6", env: [DJANGO=2.1] }

- { python: "3.7", env: [DJANGO=2.0] }
- { python: "3.7", env: [DJANGO=2.1] }

notifications:
email: false

103 changes: 77 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# django-docker-helpers
[![Docs](https://readthedocs.org/projects/django-docker-helpers/badge/?style=flat)](https://readthedocs.org/projects/django-docker-helpers/)
[![Coverage](https://codecov.io/gh/night-crawler/django-docker-helpers/branch/master/graph/badge.svg)](https://codecov.io/gh/night-crawler/django-docker-helpers)
[![Build status](https://travis-ci.org/night-crawler/django-docker-helpers.svg?branch=master)](https://travis-ci.org/night-crawler/django-docker-helpers)
[![PyPI version](https://img.shields.io/pypi/v/django-docker-helpers.svg)](https://pypi.python.org/pypi/django-docker-helpers)
[![PyPI Wheel](https://img.shields.io/pypi/wheel/django-docker-helpers.svg)](https://pypi.python.org/pypi/django-docker-helpers)
[![Requirements Status](https://requires.io/github/night-crawler/django-docker-helpers/requirements.svg?branch=master)](https://requires.io/github/night-crawler/django-docker-helpers/requirements/?branch=master)
[![Supported versions](https://img.shields.io/pypi/pyversions/django-docker-helpers.svg)](https://pypi.python.org/pypi/django-docker-helpers)
[![Supported implementations](https://img.shields.io/pypi/implementation/django-docker-helpers.svg)](https://pypi.python.org/pypi/django-docker-helpers)

This package provides some useful tools you can use with your `manage.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 functions

## Installation
```bash
pip install -e git+https://github.com/night-crawler/django-docker-helpers.git#egg=django-docker-helpers
Expand Down Expand Up @@ -40,8 +56,8 @@ debug: true
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')
yml_conf = '/tmp/my/config/without-docker.yml'
redis_conf = os.environ.get('DJANGO_CONFIG_REDIS_KEY', 'msa_cas/conf.yml')

parsers = [
EnvironmentParser(),
Expand All @@ -66,50 +82,85 @@ DATABASES = {
```

### Usage

In the most cases your manage.py may look like:

```python
#!/usr/bin/env python
#!/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.db import (
ensure_caches_alive, ensure_databases_alive, migrate
)
from django_docker_helpers.files import collect_static
from django_docker_helpers.management import create_admin, run_gunicorn
from msa_mailer.wsgi import application
from django_docker_helpers.utils import env_bool_flag, run_env_once, wf

from setproctitle import setproctitle


@run_env_once
def invalidate_static_rev():
from django.core.management import call_command
call_command('invalidate_static_rev')


PRODUCTION = bool(int(os.environ.get('MSA_MAILER_PRODUCTION', 0) or 0))
@run_env_once
def load_lang_fixtures():
from django.core.management import call_command
call_command('populate_languages')

SERVER = bool(int(os.environ.get('MSA_MAILER_SERVER', 0) or 0))

@run_env_once
def load_dev_fixtures():
from django.core.management import call_command

wf('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', 'msa_mailer.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')

setproctitle('MyProject')

if PRODUCTION or os.environ.get('MSA_MAILER_FORCE_PRODUCTION'):
if env_bool_flag('CHECK_CONNECTIONS'):
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)

if env_bool_flag('RUN_PREPARE'):
collect_static()
migrate()
invalidate_static_rev()
load_lang_fixtures()
create_admin('SUPERUSER')

if env_bool_flag('LOAD_DEV_FIXTURES'):
load_dev_fixtures()

if len(sys.argv) == 2:
if sys.argv[1] == 'rungunicorn':
from my_project.wsgi import application

gunicorn_module_name = os.getenv('GUNICORN_MODULE_NAME', 'gunicorn_dev')
run_gunicorn(application, gunicorn_module_name=gunicorn_module_name)
exit()

from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)


```

### Testing
1. `$ pip install -r requirements/dev.txt`
2. [Download Consul](https://www.consul.io/downloads.html) and unzip it into the project's directory.
- `CONSUL_VERSION=1.4.0 bash -c 'curl -sLo consul.zip https://releases.hashicorp.com/consul/"$CONSUL_VERSION"/consul_"$CONSUL_VERSION"_linux_amd64.zip' && unzip consul.zip`
3. `$ ./consul agent -server -ui -dev`
4. `$ pytest`
2 changes: 1 addition & 1 deletion django_docker_helpers/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def ensure_caches_alive(max_retries: int = 100,
"""
for cache_alias in settings.CACHES.keys():
cache = caches[cache_alias]
wf('Checking redis connection alive for cache `%s`... ' % cache_alias, False)
wf('Checking if the cache backed is accessible for the alias `%s`... ' % cache_alias, False)
for i in range(max_retries):
try:
cache.set('django-docker-helpers:available-check', '1')
Expand Down
1 change: 1 addition & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Installation
At the command line::

pip install django-docker-helpers

1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pypandoc
pytest
pytest-django
python-memcached
pytest-cov
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'License :: OSI Approved :: MIT License',
],
install_requires=['dpath', 'pyaml', 'gunicorn', 'django', 'terminaltables']
Expand Down
2 changes: 1 addition & 1 deletion tests/django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': '{0}:{1}'.format(MEMCACHED_HOST, MEMCACHED_PORT),
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/test_backend_mpt_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def store_redis_config():
sample = {
'bool_flag': '', # flag
'unicode': 'вася',
'none_value': None,
# TODO: they've changed something under the hood
# None now is not supported. Have to do with it something later.
# Take a look at utils.mp_serialize_dict
# 'none_value': None,
'debug': True,
'mixed': ['ascii', 'юникод', 1, {'d': 1}, {'b': 2}],
'nested': {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_management_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test__ensure_databases_alive(self):

def test__ensure_caches_alive(self):
with patch('django_docker_helpers.db.wf') as wf:
assert ensure_caches_alive(max_retries=1)
assert ensure_caches_alive(max_retries=1, exit_on_failure=False)
assert any('[+]' in arg[0][0] for arg in wf.call_args_list)

def test__collect_static(self):
Expand Down

0 comments on commit 908b431

Please sign in to comment.