Skip to main content

Common commands for deployment on a VPS.

Project description

This is a base set of Fabric deployment functions for projects based on Django, Nginx, uWSGI Emperor, Memcached and PostgreSQL. Both Fabric 2 and legacy Fabric 1 systems are supported.

Fabric is a handy tool for automating deployment processes. Unlike Salt or Ansible, you’re writing code from the beginning rather than diving into a Turing Tarpit of templated YAML. Also unlike Salt, Ansible or shell scripts, Fabric also neatly coordinates both local and remote SSH tasks. While Fabric might not necessarily be your best choice for provisioning, as a simple and flexible tool for automatic custom workflows, it is excellent.

Installation (Fabric 2.*)

If Fabric 2 is available as an operating system package:

$ sudo apt install fabric invoke
$ pip install --user vps-deploy

Otherwise:

$ pip install --user fabric invoke vps-deploy

Legacy installation for Fabric 1.*

Fabric 1.* is recommended only for supporting existing projects. For new projects, use Fabric 2.*.

Since Fabric 1.* is Python 2-only and you’re probably using a Python 3 virtual env, you’ll need to install Fabric and VPS Deploy as a –user package:

# System package for Fabric, user package for vps-deploy:
$ sudo apt install fabric
$ pip install --user vps-deploy

# All in user packages:
$ pip install --user fabric~=1.14 vps-deploy

Alternately, while Fabric 1.* remains available for some operating systems, you could also install it that way.

Getting started

To get started, create a fabfile.py in your top-level project directory. It might look something like this:

from fabric import task
from invoke.collection import Collection

from vps_deploy import django_fabric2 as df2

hosts = ['user@examplehost.com']

@task(hosts=hosts)
def deploy(c):
    df2.transfer_files_git(c)
    df2.init(c)
    df2.grep_for_pdb(c)
    df2.lint(c)
    df2.prepare_virtualenv(c)
    df2.prepare_django(c)
    df2.fix_permissions(c,
        # Sentry needs access to Git repo.
        read=[
            '.git', 'deploy', 'env', 'project'],
        read_write=['project', 'project/collected_static/CACHE'],
    )
    df2.reload_uwsgi(c)
    df2.flush_memcached(c)
    df2.update_nginx(c)

ns = Collection(
    deploy,
    task(df2.download_postgres_db, hosts=hosts),
    task(df2.mirror_postgres_db, hosts=hosts),
    task(df2.mirror_media, hosts=hosts),
)
ns.configure({
    # Built-in Fabric config.
    'run': {
        'echo': True,
        # Needed so local commands work. Can also use FABRIC_RUN_REPLACE_ENV.
        'replace_env': False,
        # Needed for Guix. Can also use FABRIC_RUN_SHELL.
        # 'shell': '/run/current-system/profile/bin/bash',
    },

    # Our custom project config.
    'env': {
        'branch': 'master',
        'app_user': 'www-data',
        'db_name': 'exampledb',
        'project_dir': '/srv/exampleproject',
        'media_dir': 'project/media',
        'virtualenv': '/srv/venvs/exampleproject-django-py38',
        'site_name': 'examplesite',
        'requirements': 'requirements/production.txt',
        'settings': 'project.settings.live',
        'uwsgi_conf': 'deploy/uwsgi.ini',
        'nginx_conf': 'deploy/nginx.conf',
        'python': '/usr/bin/python3.8',
    },
})

Deploying

To make a deployment:

fab –prompt-for-sudo-password deploy

This depends on a few things being already set up, such as SSH access to the server and having the server-site software and accounts set up. Those tend to to be better handled with configuration management tools like Salt or Ansible (and potentially triggered by Fabric!).

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

vps-deploy-0.4.0a10.tar.gz (11.1 kB view hashes)

Uploaded Source

Built Distribution

vps_deploy-0.4.0a10-py3-none-any.whl (23.0 kB view hashes)

Uploaded Python 3

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