Insights from Our Experts
Welcome to Fabric!
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.
Installation of Fabric
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
Executing commands on the local Linux machine
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
Auto Deployment using fabric
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:
- run – which runs a shell command on a remote machine.
- local – which runs command on the local machine.
- sudo – which runs a shell command on a remote machine, with root privileges.
- Get – which downloads one or more files from a remote machine.
- Put – which uploads one or more files to a remote machine.
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.