This project provides a simple workflow for registering services in Django. It is highly opinionated and makes a lot of assumptions.
Goal
When developing a Django application, you often need external services such as a database, a key-value store, a message broker, etc. etc. I prefer to use Docker and Docker-Compose to have those services running when I develop my application.
django-discovery makes it easy to connect to those services, as long as you follow a specific workflow:
Development is done on your own machine, with the services running under Docker;
Testing is done by running both the services and the application using Docker Compose;
The production environment provides an SRV-lookup capable DNS. Examples are:
Running everything under Kubernetes
Running everything on Docker, configured to use an SRV-capable DNS such as Consul
Requirements
django-discovery requires Python 3 and Django 1.8 because we live in modern times.
Quickstart
The following example assumes a Django application that requires a MySQL database. Add the following to your settings.py:
from discovery import services
db = services.register('my_db', 'mysql', secrets=['mysql_user', 'mysql_database', 'mysql_password'])
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': db.secrets['mysql_database'],
'USER': db.secrets['mysql_user'],
'PASSWORD': db.secrets['mysql_password'],
'HOST': db.host,
'PORT': db.port,
}
}
DEBUG = services.debug_mode
You can now run your application on localhost with your database under docker using the following docker-compose.yml:
my_db:
image: mysql:5.6
ports:
- ":3306"
environment:
MYSQL_ROOT_PASSWORD: supersecret
MYSQL_DATABASE: database_name
MYSQL_USER: database_user
MYSQL_PASSWORD: alsosecret
You can also run your application in its entirety in docker, using for example the following docker-compose file:
database:
image: mysql:5.6
environment:
MYSQL_ROOT_PASSWORD: supersecret
MYSQL_DATABASE: database_name
MYSQL_USER: database_user
MYSQL_PASSWORD: alsosecret
application:
build: .
links:
- database:my_db
ports:
- "80:8080"
env:
DISCOVERY_MODE: env
And still using the same settings.py, you can even run your application under Kubernetes using the following definitions:
apiVersion: v1
kind: Service
metadata:
name: my_db
spec:
ports:
- protocol: tcp
port: 3306
apiVersion: v1
kind: Endpoints
metadata:
name: my_db
subsets:
- addresses:
- ip: 1.2.3.4
- ports:
- port: 3306
apiVersion: v1
kind: Pod
metadata:
name: my_application
namespace: my_namespace
spec:
containers:
- name: my_application
image: my/docker-image
volumeMounts:
- name: db_secrets
mountPath: /etc/secrets/my_db
readOnly: true
volumes:
- name: db_secrets
secret:
mysql_database: some_name
mysql_user: some_user
mysql_password: secret_password
Full Documentation
Working on it…
License
This project is licensed under the MIT license.
Release files for django_discovery 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_discovery-0.0.6.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_discovery-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / django_discovery-0.0.6.tar.gz
| Download URL | django_discovery-0.0.6.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
330027880bad106d51ed102908f5b62c64ba9b6972217536ba46158e093428b9
|
|
BLAKE2b-256 checksum How to use checksums |
d107b9ac1aa7819c52b27b070a85504c7e8ad81a2945dee84dd5b7c90b6a7734
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_discovery-0.0.6-py3-none-any.whl
| Download URL | django_discovery-0.0.6-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5664f47dc7796677b4fa80eb35e9ed20ce7f078e7b12683ede6b8e0f73243b07
|
|
BLAKE2b-256 checksum How to use checksums |
996ed7d6630374fe43796344549198a7412e60feba4f0b8d1855da98cc189751
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |