Deploying Django Projects Using Fabric

    In one of the projects, you need to regularly upload the code from the stage branch to the staging server. We started doing this manually - you enter through ssh, do git push origin stage, if necessary, update the database and then restart apache. By the end of this week they decided that it would be good to carry out all these actions with one team. I went through blogs - now they are very actively writing about using the Fabric libraries for this purpose (this is an analogue of Capistrano from Ruby on Rails).

    How to install

    Install the fabric library via pip or easy_install, then create a symbolic link for the fab application, or add the folder with bin files in your Python distribution in PATH. Below is a brief tutorial for Mac OS X + ports.

    sudo pip-2.6 install fabric
    sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/fab /usr/bin/fab

    What Fabric does

    Fabric allows you to perform a variety of ssh actions on a group of servers. The actions are described in the file and are just regular Python functions. Usually is put in the root folder of the project (next to,,, ...).

    from fabric.api import *

    env.hosts = ['']
    def deploy():
        local('hg push')
        with cd('hgreps/vorushin_ru'):
            run('hg update')
            run('/etc/init.d/apache2 reload')

    Now if I run fab deploy from the folder with the vorushin_ru project (the code for this blog written in Django), then push will first happen from my local machine, then an update will be made via ssh on the server and then restarting Apache.

    All sorts of subtleties

    First, ssh should go through certificates. See ssh-keygen , ssh authorized keys .

    Secondly, if you need to do pull from another server, you need to add the -A option when calling ssh. Details -

    Thirdly, if the development team does it one at a time, then the project files must have write permissions for the whole development team. Details -

    Fourth, if several projects are running under the Apache, it is better to restart it through touch your.wsgi (if mod_wsgi is configured to work in daemon_mode).

    The original article on my blog

    Also popular now: