Deploying Python Web Applications

Every once in a while I’m really impressed by a library I stumble upon. A while back that was virtualenv, now i stumbled upon fabric. I was using capistrano for a project I was working on which was kinda okay but somehow I wasn’t sold to it.

Yesterday however apollo13 stumbled upon fabric which is capistrano just in Python, with a working put command and less annoying in general.

In combination with a custom virtualenv bootstrapping script Python web application deployment is a charm. One “fab bootstrap” later the servers are creating a virtual python environment, compiling all dependencies, checking out all eggs and initializing the application environment. Updates are just one “fab production deploy” away.

And the best part is that fabric is not limited to Python. You can use it to deploy anything you can control over ssh.

Here an example fabfile (the file that controls the deployment)

set(
    fab_hosts = ['srv1.example.com', 'srv2.example.com']
)

def deploy():
    """Deploy the latest version."""
    # pull all changes from mercurial and touch the wsgi file to
    # tell the apache to reload the application.
    run("hg pull -u; touch application.wsgi")

def bootstrap():
    """Asks for a list of servers and bootstrapps the application there."""
    set(fab_hosts=[x.strip() for x in raw_input('Servers: ').split()])
    run("hg clone http://repository.example.com/application")
    local("./generate-wsgi-file.py > /tmp/application.wsgi")
    put("/tmp/application.wsgi", "application.wsgi")

Saved as fabfile.py “fab bootstrap” then asks for some servers and bootstraps the application there, after changes in the repository you can “fab deploy” the latest version. Of course that’s just a very basic made up example, but it shows how you can use fabric.

I’m using makefiles currently to execute common tasks for various Python projects (like releasing code, resting unittests and much more), I suppose fabric could also do that for me. And that would have the advantage that it works for windows users too.

10 Responses to “Deploying Python Web Applications”

  1. Very nice example, I discovered fabric when it was first released to the pypi and I’m keeping an eye on it since then, it’s nice to know that it’s working and growing well.

    Comment by michele — Thursday, July 17th, 2008 @ 4:44 pm
  2. Great find, thanks for the info

    Comment by sharms — Thursday, July 17th, 2008 @ 5:14 pm
  3. Looks awesome, I need to check this out

    Comment by Siddharta — Friday, July 18th, 2008 @ 4:43 am
  4. I’m also an virtualenv fanboy… works really great!
    Something like this was missing in my python toolbox, great find!

    Comment by yvess — Friday, July 18th, 2008 @ 7:42 am
  5. I wish this worked as a normal library like how setup.py does. The fab files are too magic for my tastes.

    Comment by Gattster — Friday, July 18th, 2008 @ 7:59 am
  6. @5: You can “import fabric” and use it in a custom script. If you want go get rid of the fab variables you would have to craft a patch I guess.

    Comment by Armin Ronacher — Friday, July 18th, 2008 @ 8:53 am
  7. Armin — thanks for the reply. Your solution sounds like what I’m looking for. I have a lot of experience with RoR. One of the things I love about python is how clear the python import statements make things. I spend way too much time reading docs in RoR, where in python I can figure things out myself or with docstrings (most of the time).

    Comment by Gattster — Friday, July 18th, 2008 @ 7:36 pm
  8. Ok, so this saves me from the hassle of executing dynamic commands over a SSH connection (which normally, because of shell-quoting issues, is a real nightmare to get right). But that’s only half of the picture. You still need an installation script for your app and the infrastructure on the server to support it. For example a supervisor set up to restart your application after installation. And maybe a script to save the the previous deployment and make a backup of the database. And migrate the database schema. and so on…

    Still it seems like a nice, handy tool. But it’s only building stone in the chain necessary tool-chain for web app deployment.

    Comment by Christopher Arndt — Saturday, July 19th, 2008 @ 12:16 pm
  9. @8: Sounds like you want to combine it with a custom virtualenv bootstrapping script.

    Comment by Armin Ronacher — Sunday, July 20th, 2008 @ 11:00 am
  10. Glad to see anything I could replace paver (http://www.blueskyonmars.com/projects/paver/) with. Starting with this tool seemed pretty easy (hey, it’s all python!), but actually the amount of code required to deploy any non-python app along with python apps is enormous. Finally I ended with a mish-mash of paver and ant for my deployments.

    Hope this one will do better.

    Comment by zgoda — Monday, July 21st, 2008 @ 6:56 am

Leave a Reply

cogitations driven by wordpress