We’ve been using Capistrano to deploy Afrigator to our various servers for a while now. I can seriously not complain or say anything bad about Capistrano. Thing is, I’m more familiar with Python than I am with Ruby, thus for me it just makes more sense for me to use Fabric.
Here is a small example of how to write a typical deploy script with Python Fabric:
First, define your various servers which you need to deploy to. Ideally, you’ll have a test, staging and live server. Thus you’ll set them up as follows.
def test():
config.fab_user = 'test_user_name'
config.fab_hosts = ['test.yourserver.com']
def staging():
config.fab_user = 'staging_user_name'
config.fab_hosts = ['staging.yourserver.com']
def live():
config.fab_user = 'live_user_name'
config.fab_hosts = ['www1.yourserver.com', \
'www2.yourserver.com', 'www3.yourserver.com']
This allows you to deploy your code to the various servers. Please note, you need to setup automatic login for your different servers. See here how to do automatic logins.
Next, write the steps you would take to deploy your site manually:
def deploy():
"Deploy code to servers"
msg = "deploying"
require('fab_hosts', provided_by = [test,staging,live])
local('svn ci -m "$(msg)"')
run('svn export repos /path/to/repository/export/')
run('cp -R /path/to/repository/export/* /path/to/your/site/')
To deploy to the staging server you can run the following command:
$ fab staging let:msg="Reason for check in" deploy
To deploy to the 3 live servers, all you do is:
$ fab live let:msg="Reason for check in" deploy
The command works as follows: fab is the command. live/stating/test are the environments you would like to load. If you said staging, it will do the commands in deploy for the staging server. The last part is the command you want to run. In this instance, deploy.
The let:msg=”Reason for check in” is the coolest bit! It basically allow you to override Fabric variables. If you look closely, in my deploy script I’ve set a variable msg to just say “deploy”. Now that is a stupid SVN message for a commit. In order to commit with meaningful messages, I override the msg variable with my own message. Simple, yet very effective!
You may want to write a number of different functions in a single fabfile. If you have 100 commands and you’re not one hundred percent sure, just do a:
$ fab list
and all the commands available to you will be printed with their description. Fabric rocks! Seriously.
Welcome back! You should subscribe to my RSS feed here.
You should follow me on Twitter here
You should follow me on Gatorpeeps here.







billz 12:02 pm on March 4, 2009 Permalink |
Good stuff, Stii! I’m going to have to give Fabric a closer look. Capistrano is nice but this appears to be an elegant alternative.
Stii 12:16 pm on March 4, 2009 Permalink |
Check it out! It is very cool. I’ve just scratched the surface and already it does anything i need. I’ll write up as i find new cool features.
matimba 1:31 pm on March 5, 2009 Permalink |
Great post, thanks
Also possible to set username, password, port, etc. in fabfile
def test():
config.fab_user = ‘username’
config.fab_password = ‘mypassword’
config.fab_port = 30000
config.fab_hosts = ['test.server.com']
Delicious Bookmarks (2009-03-01 - 2009-03-05) | Josh Babetski : Quixotic Bravado 3:19 am on March 6, 2009 Permalink |
[...] Switched to Python Fabric | Es Tea Double Eye [...]
AkitaOnRails 5:32 pm on March 6, 2009 Permalink |
For simpler things I think Fabric would be enough. Capistrano has several Strategy classes for every major SCM, many deployment strategies (copy, scm, etc), gateway support, etc.
For simpler things in Ruby, there is also Vlad, the Deployer http://rubyhitsquad.com/Vlad_the_Deployer.html from the RubySquad.
Alex G 6:41 pm on March 6, 2009 Permalink |
so here’s what I’m curious about… since fab is GPL and fabfiles are mostly a bunch of “dynamically linked calls” to fab’s methods, does that mean you must release your fabfiles? :)
http://www.gnu.org/licenses/gpl-faq.html#IfInterpreterIsGPL
A consequence is that if you choose to use GPL’d Perl modules or Java classes in your program, you must release the program in a GPL-compatible way, regardless of the license used in the Perl or Java interpreter that the combined Perl or Java program will run on.
perl and java is just an example.
Christian Vest Hansen 2:39 am on March 9, 2009 Permalink |
@Alex G
That’s an interesting question. I don’t think people normally release fabfiles in a way that would otherwise be covered by the GPL, but the answer is no; fabfiles are not covered by the GPL that covers Fabric itself. I consider it an implementation detail that Fabric loads the fabfile into the same interpretor instance.
That is what I intended when I licensed Fabric under the GPL.
If someone more knowledgeable on the legal details insists that Fabric is incompatible with its intended use, then I will handle it by fixing the license, rather than the program.
Stii 12:12 pm on March 12, 2009 Permalink |
Christian, thats the way to do it! Thanx
David Novakovic 7:13 am on March 18, 2009 Permalink |
@Alex – you have misinterpreted the interpretation of the GPL – the link you posted refers directly to the JNI or java native interface, a way of binding with machine code libraries through java. That is linking, not running bytecode on an interepter.
For the record – as far as I know Fabric uses paramiko, which is pure python. :)
mark hellewell 4:26 am on May 27, 2009 Permalink |
Hi, I think I’m right in saying this is for the 0.1 version of Fabric not the 0.9 whose release is fairly imminent? Looking for more examples of fabfiles written with the new API! :)
Lorcon 11:37 pm on November 29, 2009 Permalink |
Thanks for the article. I have to tell you that “hab list” doesn’t work for me, it should be “fab –list”.
Christian Vest Hansen 1:41 am on November 30, 2009 Permalink |
The article was written for the 0.1 series of Fabric. What you have installed is a 0.9 series version, which has an overhauled command line interface.
Another Fabric Tutorial 10:56 am on April 8, 2010 Permalink |
[...] References: SSH Without Password Fabic 0.9.0 Documentation Switched-To-Python-Fabric/ [...]