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.
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.
— michele on Thursday, July 17, 2008 16:44 #
Great find, thanks for the info
— sharms on Thursday, July 17, 2008 17:14 #
Looks awesome, I need to check this out
— Siddharta on Friday, July 18, 2008 4:43 #
I'm also an virtualenv fanboy... works really great! Something like this was missing in my python toolbox, great find!
— yvess on Friday, July 18, 2008 7:42 #
I wish this worked as a normal library like how setup.py does. The fab files are too magic for my tastes.
— Gattster on Friday, July 18, 2008 7:59 #
@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.
— Armin Ronacher on Friday, July 18, 2008 8:53 #
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).
— Gattster on Friday, July 18, 2008 19:36 #
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.
— Christopher Arndt on Saturday, July 19, 2008 12:16 #
@8: Sounds like you want to combine it with a custom virtualenv bootstrapping script.
— Armin Ronacher on Sunday, July 20, 2008 11:00 #
Glad to see anything I could replace paver (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.
— zgoda on Monday, July 21, 2008 6:56 #