SaltyCrane Blog — Notes on JavaScript and web development

Notes on Python deployment using Fabric

I found out about Fabric via Armin Ronacher's article Deploying Python Web Applications. Fabric is a Capistrano inspired deployment tool for the Python community. It is very simple to use. There are 4 main commands: local is almost like os.system because it runs a command on the local machine, run and sudo run a command on a remote machine as either a normal user or as root, and put transfers a file to a remote machine.

Here is a sample setup which displays information about the Apache processes on my remote EC2 instance.

  • Install Easy Install
  • Install Fabric
    $ sudo easy_install Fabric
  • Create a file called fabfile.py located at ~/myproject
    def ec2():
        set(fab_hosts = ['ec2-65-234-55-183.compute-1.amazonaws.com'],
            fab_user = 'sofeng',
            fab_password = 'mypassword',)
    
    def ps_apache():
        run("ps -e -O rss,pcpu | grep apache")
    
    Note: for security reasons, you can remove the password from the fabfile and Fabric will prompt for it interactively. Per the documentation, Fabric also supports key-based authentication.

  • Run it
    $ cd ~/myproject
    $ fab ec2 ps_apache
    Results:
       Fabric v. 0.0.9, Copyright (C) 2008 Christian Vest Hansen.
       Fabric comes with ABSOLUTELY NO WARRANTY; for details type `fab warranty'.
       This is free software, and you are welcome to redistribute it
       under certain conditions; type `fab license' for details.
    
    Running ec2...
    Running ps_apache...
    Logging into the following hosts as sofeng:
        ec2-65-234-55-183.compute-1.amazonaws.com
    [ec2-65-234-55-183.compute-1.amazonaws.com] run: ps -e -O rss,pcpu | grep apache
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2163  5504  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2520 15812  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2521  3664  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2522  3664  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2523  3664  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2524  3664  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2619  3664  0.0 S ?        00:00:00 /usr/sbin/apache2 -k start
    [ec2-65-234-55-183.compute-1.amazonaws.com] out:  2629  1204  0.0 R ?        00:00:00 /bin/bash -l -c ps -e -O rss,pcpu | grep apache
    Done.

Comments


#1 Basil Shubin commented on :

Hmmm... Fabric.. curious.

Nice note ;-)