Using fabric to automate command line tasks

Python library fabric is a great way to automate command line tasks. If you use command line, know Python, and don't know bash-scripting (or prefer using Python to bash-scripting) then you are likely to find fabric extremely useful in automating repetitive tasks. In this "live" post I will record different ways in which I use fabric.

Using fabric to run a local command (example: rsync)

The following is a simple code snippet that shows how you can use fabric to fetch files from a remote folder (say, /home/fabricuser/projects/myproject) to a local folder (say, ~/localProjects/myproject) using rsync.

Of course, replace the pathnames as necessary. But for the code snippet to work:

(1) you must save the code snippet locally with the name fabfile.py. A fabric script must be named fabfile.py;

(2) you must save the code snippet in the folder that contains the local folder that you want to sync. In the example here we are syncing the folder myproject that is contained within ~/localProjects folder; so the code snippet must be saved as fabfile.py in ~/localProjects.

Also, replace fabricuser by your username and 123.456.78.90 by the ip address of your server.

from fabric.api import local

# absolute remote path
remotePath = '/home/fabricuser/projects/myproject' 

# relative path from ~/localProjects
localPath = './' 


def getFiles():                
    local( 'rsync -avz fabricuser@123.456.78.90:{0} {1}'\
                        .format(remotePath, localPath) )

Once you have saved the above code snippet in ~/localProjects as fabfile.py, you can get files from the remote server by navigating to ~/localProjects using the terminal, and running fab getFiles. This is so much easier that typing out the entire rsync -avz <source> <destination> command repeatedly.

The only fabric specific command in the above snippet is the local() command. local() says that "run the shell command in the parantheses on the local machine". In the above snippet we are running the standard rsync command on the local machine.

A fabric script can contain multiple functions, and you can then use fab <function name> to execute the tasks specified by that function. The code snippet below expands the above script with two additional functions:

from fabric.api import local


def getFiles():        
    # absolute remote path
    remotePath = '/home/fabricuser/projects/myproject' 

    # relative path from ~/localProjects
    localPath = './' 

    local( 'rsync -avz fabricuser@123.456.78.90:{0} {1}'\
                        .format(remotePath, localPath) )


def sendFiles():
    # absolute remote path
    remotePath = '/home/fabricuser/projects' 

    # relative path from ~/localProjects
    localPath = './myproject' 

    local( 'rsync -avz {0} fabricuser@123.456.78.90:{1}'\
                        .format(localPath, remotePath) )


def syncBothWays():
    getFiles()
    sendFiles()

Function sendFiles sends files from the local folder to the remote folder. To use this we need to run fab sendFiles at the command prompt. To get files and send files using a single command, you can run fab syncBothWays. In each case, you must be in the folder where fabfile.py is saved.


comments powered by Disqus