Skip to main content

Common Fabric tasks for use in Django development/deployments

Reason this release was yanked:

No longer supporting.

Project description

Django Fabric Tasks

Author: Tim Santor tsantor@xstudios.agency

Overview

These generic tasks are for use on internal X Studios Django projects. These tasks pair perfectly with our Cookiecutter Django starter project.

Project Structure

In order to make these tasks reusable without a ton of parameters, etc. a portion of these commands assume a local/remote project structure as follows:

my-project/              # Top-level project dir (any name)
├── fabfile.py           # Our fabfile
├── logs/                # Contains our application/server logs
├── manage.py
├── media/               # Contains our media files (user uploads)
├── requirements.txt     # Our `pip freeze > requirements.txt` file
├── app/                 # Contains our app and its modules
└── static_collection/   # Contains our collected static files

Note: We setup all our Django projects in the same manner for
consistency and start from this
`cookiecutter <https://bitbucket.org/tsantor/cookiecutter-django18-project>`__
tempate.

Note: ``media`` and ``static_collection`` (or whatever you wish to
name them) are assumed to live on the same physical server as the
Django project. This is because we typically do not serve
media/static files from Amazon S3 or another solution.

Installation

To install Django Fabric Tasks, simply run the following within your virtualenv:

pip install django-fabtasks

Create fabfile.py

In your project root, create fabfile.py with the following contents:

import os

# 3rd Party
from fabric.api import *
from contextlib import contextmanager

import fabtasks.development as dev
import fabtasks.production as prod
from fabtasks.context import virtualenv

# -----------------------------------------------------------------------------
# Config
# -----------------------------------------------------------------------------

# Base env config
env.colorize_errors = True
env.package_name = 'PACKAGE_NAME'
env.repository = 'REPO_URL'
env.db_user = 'DB_USER'
env.db_name = 'DB_NAME'
env.virtualenv_dir = 'VIRTUALENV_DIR'
env.activate = '/root/.virtualenvs/VIRTUALENV_DIR/bin/activate'
env.apache_restart_command = 'apache_restart'

# Local env
env.local_group = 'staff'
env.local_project_root = os.getcwd()
env.local_static_root = os.path.join(os.getcwd(), 'static_collection', '')
env.local_media_root = os.path.join(os.getcwd(), 'media', '')


@task
def production():
    """Production env overrides"""
    env.hosts = ['xstudiosdev.com']
    env.user = 'root'
    env.group = 'apache'
    env.domain = 'DOMAIN'
    env.project_root = '/var/www/vhosts/{domain}'.format(**env)
    env.static_root = '/var/www/vhosts/{domain}/static_collection/'.format(**env)
    env.media_root = '/var/www/vhosts/{domain}/media/'.format(**env)


# Set the default environment
production()

# -----------------------------------------------------------------------------
# Project specific
# -----------------------------------------------------------------------------

# Your code goes here

Features

Do a quick fab -l and behold the commands at your fingertips:

dev.install                          Install from scratch (eg - after pull for first time)
dev.reset                            Reset project after breaking change
dev.sync                             Sync project with latest master
dev.db.copy_mysql                    Perform a local MySQL dump and import it on the remote machine
dev.db.createsuperuser               Create superuser
dev.db.dump_mysql                    Perform a MySQL dump of the database
dev.db.import_mysql                  Perform a MySQL import of the database
dev.db.makemigrations                Make migrations
dev.db.migrate                       Apply migrations
dev.db.reset                         Reset database
dev.files.collect_static             Collect static files
dev.files.copy_media                 Copy local media files to remote server
dev.files.copy_static                Copy local static files to remote server
dev.files.delete_media               Delete all media files but preserve directories
dev.files.delete_pyc                 Delete pyc files
dev.files.delete_unused              Delete unused media files that are not referenced in the database
dev.files.fix_permissions            Ensure proper permissions on project folders
dev.git.pull_master                  Git pull origin master
dev.setup.clear_logs                 Clear debug logs
dev.setup.create_local_settings      Create default local settings file
dev.setup.create_logs                Create logs dir and log files
dev.setup.create_media_dir           Create media dir
dev.setup.create_virtualenv          Create and active a virtual environment
dev.setup.pip_install_requirements   Install pip requirements from requirements.txt
dev.setup.pip_update                 Upgrade pip
dev.setup.pip_update_packages        Update outdated pip packages (Be careful!)
prod.deploy                          Deploy latest commit
prod.install                         Install from scratch (eg - after pull for first time)
prod.db.copy_mysql                   Perform a remote MySQL dump and import it on the local machine
prod.db.createsuperuser              Create superuser
prod.db.dump_mysql                   Perform a MySQL dump of the database
prod.db.import_mysql                 Perform a MySQL import of the database
prod.db.migrate                      Apply migrations
prod.db.reset                        Reset database
prod.files.collect_static            Collect static files
prod.files.copy_media                Copy remote media files to local
prod.files.copy_static               Copy remote static files to local
prod.files.delete_pyc                Delete pyc files
prod.files.delete_unused             Delete unused media files
prod.files.fix_permissions           Ensure proper permissions on project folders
prod.git.pull_master                 Git pull origin master
prod.services.apache_restart         Restart apache
prod.setup.clear_logs                Clear debug logs
prod.setup.create_logs               Create logs dir and log files
prod.setup.create_media_dir          Create media dir
prod.setup.pip_install_requirements  Install pip requirements from requirements.txt
prod.setup.pip_update                Upgrade pip

Documentation

Documentation is available here.

Issues

If you experience any issues, please create an issue on Bitbucket.

Not Exactly What You Want?

This is what I want. It might not be what you want. If you have differences in your preferred setup, I encourage you to fork this to create your own version. Or create your own; it doesn’t strictly have to be a fork.

History

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

0.1.0 (2015-08-17)

  • First release on PyPI.

0.1.1 (2015-09-01)

  • Updated production deploy with collect static.

0.1.2 (2015-10-05)

  • Removed problematic local settings auto setup on dev.install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-fabtasks-0.1.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distributions

django_fabtasks-0.1.2-py2.py3-none-any.whl (15.8 kB view details)

Uploaded Python 2 Python 3

django_fabtasks-0.1.2-py2.7.egg (11.8 kB view details)

Uploaded Source

File details

Details for the file django-fabtasks-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django-fabtasks-0.1.2.tar.gz
Algorithm Hash digest
SHA256 80003dffdd6ca393f172b0ddac63069c15876ed77867c411e2690360c651cb98
MD5 5652b341bd16d3b0caff65472e8e26d6
BLAKE2b-256 790a1fe0bae1b2542dd6be45ab1131cf0abebc5133723934b83d1ce152f03418

See more details on using hashes here.

File details

Details for the file django_fabtasks-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_fabtasks-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7596a79416634a639aa4919df4e6cca682bc1b427bef427bd4e0f3cd9c29ada0
MD5 8d1e05fc7f837c274f8afb44ffa92656
BLAKE2b-256 189d564f9c7fd9f86467f9df67758d68e55e3fec7aff6c404c3f83b7547620fc

See more details on using hashes here.

File details

Details for the file django_fabtasks-0.1.2-py2.7.egg.

File metadata

File hashes

Hashes for django_fabtasks-0.1.2-py2.7.egg
Algorithm Hash digest
SHA256 e33e6208777ad034164283a79d4620ce916f96a056fb768978f5f7ebd653fd62
MD5 d9da2d953aa9c6069b9789bc42d60480
BLAKE2b-256 65bc03c5e3fbfe5142223e9d68bf3b49440df8e0e6f645ea3a21c821f03a569f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page