Package: vps-deploy
This is a base set of Fabric 2 deployment functions for projects based on
Django, Nginx, uWSGI Emperor, Memcached and PostgreSQL.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
neatly coordinates both local and remote SSH tasks. While Fabric may not be your
best choice for provisioning, it works well as a simple and flexible tool for
automatic custom workflows.Installation (Fabric 2.*)If Fabric 2 is available as an operating system package:$ sudo apt install fabric invoke
$ pip install --user vps-deployOtherwise:$ pip install --user fabric invoke vps-deployGetting startedTo get started, create afabfile.pyin your top-level project directory. It
might look something like this:fromfabricimporttaskfrominvoke.collectionimportCollectionfromvps_deployimportdjango_fabric2asdf2hosts=['user@examplehost.com']@task(hosts=hosts)defdeploy(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',},})DeployingTo make a deployment:fab –prompt-for-sudo-password deployThis 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!).Release History1.1.0 (2023-07-06)drop dependency on patchwork due to version conflict with Python
3.11-compatible version of invokeremove docs about Fabric 11.0.0 (2023-04-12)remove Python 2 supportalso look forbreakpoint(Python 3.7) when checking for debugger callsmakesudo_upload_templatefunction part of public APIusemktempinstead of deprecatedtempfileaddpip_install_optionsargument toprepare_virtualenvfunctioncheckoutenv.branchbefore runninggit reset0.4.10 (2021-09-20)Improvementsswitch tosetup.cfg0.4.9 (2021-09-20)Improvementsallow reading secrets from a GPG encrypted file withread_gpg_password_file0.4.8 (2021-09-18)Improvementsswitch to f-strings (pyupgrade –py36-plus)add homepage0.4.7 (2021-06-29)Improvementsaddtransfer_files_git_pullcommand0.4.6 (2021-06-22)ImprovementsAllow grep_for_pdb to accept a comma-separated string of exclusions.0.4.5 (2021-04-29)Improvementsprevent environment variables containing “%” due to uWSGI applying special
meaning to this characteraddflake8_testandmypy_testcommandsswitch togitls-files'*.py'to avoid false-positives on non-versioned files0.4.4 (2020-11-16)Bug fixdon’t attempt to restore database owner and privileges when runningmirror_postgres_db0.4.3 (2020-09-02)Bug fixfix issueAttributeError: Rawerror caused by dependencies0.4.2 (2020-08-13)Improvementsusepython3 -mto run pylint to avoid issues with Debian binarypylint3and others using justpylintinstall fabric>=2.5.0 as a dependency0.4.1 (2020-08-10)Bug fixremove workaround previously needed for Python 2 Fabric on Python 3 projects0.4.0 (2020-08-10)Improvementsadd Fabric 2 equivalents of existing features0.3.2 (2019-12-04)Improvementsadd optional “push_to_origin” argument to “transfer_files_git”0.3.1 (2019-07-17)Bug fixuse Python 3 compatible syntax for octal numbers0.3.0 (2019-07-17)Improvementscreate a separate uWSGI config file, rather than a symbolic link, reducing
downtime0.2.5 (2019-01-17)ImprovementsTest Nginx config to make errors fail loudlyAdded a TODO fileFail deployment if Django issues a warningAllow Django “check” fail level to be specifiedAdded download_postgres_db, mirror_postgres_db and mirror_media commandsPush the git branch configured inenv.branchRemove install dependencies, since you probably already have Fabric 1.x installed0.2.2 (2016-10-11)Add READMEAdd license informationRemove redundant Supervisor, Bazaar and Gunicorn rules