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
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.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")
- 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.
Related posts
- Notes on Fabric 2 and Python 3 — posted 2021-02-07
- How to expose a Flask local development server to the public using SSH remote port forwarding — posted 2013-02-12
- How to run a Django local development server on a remote machine and access it in your browser on your local machine using SSH port forwarding — posted 2012-10-23
- Notes on debugging ssh connection problems — posted 2011-08-31
- Fabric post-run processing Python decorator — posted 2010-11-06
- Class-based Fabric scripts via a Python metaprogramming hack — posted 2010-09-23
Comments
Hmmm... Fabric.. curious.
Nice note ;-)