Modular Django deployment system based on pyinfra
Project description
djaploy
A modular Django deployment system based on pyinfra, designed to standardize and simplify infrastructure management across Django projects.
Features
- Modular Architecture: Extensible plugin system for deployment components
- Django Integration: Seamless integration via Django management commands
- Python Compilation Support: Compile Python from source for specific versions
- Multiple Deployment Modes: Support for
--local,--latest, and--releasedeployments - Infrastructure as Code: Define infrastructure using Python with pyinfra
- Git-based Artifacts: Automated artifact creation from git repository
Installation
From PyPI (once published)
pip install djaploy
Or with Poetry:
poetry add djaploy
Development Installation
For testing djaploy locally without publishing to PyPI:
# Clone the repository
git clone https://github.com/techco-fi/djaploy.git
cd djaploy
# Install in editable mode
pip install -e .
# Or with Poetry
poetry install
Then in your Django project:
# Using pip
pip install -e /path/to/djaploy
# Or add as a local dependency in pyproject.toml
[tool.poetry.dependencies]
djaploy = {path = "../djaploy", develop = true}
Quick Start
1. Project Structure
your-django-project/
├── manage.py
├── your_app/
│ └── settings.py
└── djaploy/ # Deployment configuration
├── config.py # Main configuration
├── inventory/ # Host definitions
│ ├── production.py
│ └── staging.py
└── deploy_files/ # Environment-specific files
├── production/
│ └── etc/systemd/system/app.service
└── staging/
2. Django Settings
Add to your Django settings.py:
# Required: Set project paths
BASE_DIR = '/path/to/django'
PROJECT_DIR = BASE_DIR # folder containing manage.py
DJAPLOY_CONFIG_DIR = BASE_DIR + '/djaploy'
GIT_DIR = BASE_DIR.parent # Git repository root
3. Create Configuration
Create djaploy/config.py:
from djaploy.config import DjaployConfig
from pathlib import Path
config = DjaployConfig(
# Required fields
project_name="myapp",
djaploy_dir=Path(__file__).parent, # REQUIRED: This djaploy directory
manage_py_path=Path("manage.py"), # REQUIRED: Path to manage.py (relative to project root)
# Python settings
python_version="3.11",
python_compile=False, # Set True to compile from source
# Server settings
app_user="app",
ssh_user="deploy",
# Modules to use
modules=[
"djaploy.modules.core", # Core setup (required)
"djaploy.modules.nginx", # Web server
"djaploy.modules.systemd", # Service management
],
# Services to manage
services=["myapp", "myapp-worker"],
)
4. Define Inventory
Create djaploy/inventory/production.py:
from djaploy.config import HostConfig
hosts = [
HostConfig(
name="web-1",
ssh_host="192.168.1.100",
ssh_user="deploy",
app_user="app",
env="production",
services=["myapp", "myapp-worker"],
),
]
5. Deploy Files
Place service files in djaploy/deploy_files/production/:
# etc/systemd/system/myapp.service
[Unit]
Description=My Django App
After=network.target
[Service]
Type=simple
User=app
WorkingDirectory=/home/app/apps/myapp
ExecStart=/home/app/.local/bin/poetry run gunicorn config.wsgi
Restart=on-failure
[Install]
WantedBy=multi-user.target
Usage
Configure Server
python manage.py configureserver --env production
This will:
- Create application user
- Install/compile Python
- Install Poetry
- Set up directory structure
Deploy Application
# Deploy local changes (for development)
python manage.py deploy --env production --local
# Deploy latest git commit
python manage.py deploy --env production --latest
# Deploy specific release
python manage.py deploy --env production --release v1.0.0
Deployment process:
- Creates tar.gz artifact from git
- Uploads to servers
- Extracts application code
- Installs dependencies
- Runs migrations
- Collects static files
- Restarts services
Module System
Djaploy uses a modular architecture where each component (nginx, systemd, backups, etc.) is a separate module that can be enabled or disabled per project.
Available Modules
nginx: NGINX web server configurationsystemd: Systemd service managementlitestream: Litestream database backupsrclone: Rclone-based backup systemtailscale: Tailscale networkingssl: SSL certificate managementpython_build: Python compilation from source
Creating Custom Modules
Projects can create their own modules by extending the base module class:
from djaploy.modules.base import BaseModule
class MyCustomModule(BaseModule):
def configure_server(self, host):
# Server configuration logic
pass
def deploy(self, host, artifact_path):
# Deployment logic
pass
Project Customization
prepare.py
Projects can include a prepare.py file for local build steps before deployment:
# prepare.py
from djaploy.prepare import run_command
def prepare():
run_command("npm run build")
run_command("python manage.py collectstatic --noinput")
Custom Deploy Files
Projects can include environment-specific configuration files in a deploy_files/ directory that will be copied to the server during deployment.
License
MIT
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 djaploy-0.1.2.tar.gz.
File metadata
- Download URL: djaploy-0.1.2.tar.gz
- Upload date:
- Size: 36.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b07a160b34e37ab1d0f3bca7dc47b2e3ee74c26c1ffb962e4372baa6d3d5e6bd
|
|
| MD5 |
9f3e32c8869fe8af7f62088679c7cf54
|
|
| BLAKE2b-256 |
27b705d172fa56aef4c72a8c5943467b4c9168f4566a57bbbd65221f80008ee9
|
File details
Details for the file djaploy-0.1.2-py3-none-any.whl.
File metadata
- Download URL: djaploy-0.1.2-py3-none-any.whl
- Upload date:
- Size: 43.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e07b8f4986a624fb9bfd0733c0e6ef6a585926b9bfb4268d3ac5dafb6f94ae2
|
|
| MD5 |
37e5270fb8b571245812f87e1a589936
|
|
| BLAKE2b-256 |
2d82836ed86ecd264976937caa515e38153d852733d2fabcba55a946651dcb5a
|