Fabric is a tool/python library which can be used to automate routine shell commands over SSH. The main attraction of Fabric is to enable automatic deployment of our project using script files and can used as a remote build tool.
Fabric scripts are basic Python files. They run using the fab tool that is shipped with Fabric. All this does is include (i.e. import) your script (i.e. instructions to perform) and execute the provided procedure.
1. Python 2.5+ with the development headers
2.Python-setuptools and pip (optional, but preferred) gcc
An easy way to install Fabric is by using the default operating system package manageraptitude. In order to install Fabric using aptitude, run the following command:
Python 2.5 and 2.7
For python 3.x this is in development stage
Fabric uses fabfile.py to execute tasks. Create a fabfile.py file in your current working directory.
If we put a function like below in the fab file and run the command, we will get the result in our console
We can also pass arguments to the tasks in the following way. Consider that our task is as follows
We can run the task using following command
Create a fabfile.py in the project
The run function will run the commands in our remote server. Writing the appropriate commands will do the auto deployment of our project. Although there are many functions, the most commonly used are:
Fabric, on being used for multiple purposes, including deployment, restarting of servers, stopping and restarting processes. Task to be done can be written in Python and fabric will take care to execute in all machine languages we specify. We can reuse the “fab files” that is accumulated after being used a bunch of times. Basically, Fabric can be used whenever we need to log in to multiple servers and is simple, powerful and comes with a really good documentation.