Following are the most beneficial procedures (commands or tools) that comes with Fabric to make the operations even more easier. We will use all these procedures inside the fabfile after installing Fabric.
While dealing with the remote hosts, run procedure is used to execute the shell commands. Using some variables, the output can also be stored for either future use or as log. Any shell command that we want to execute on one or more remote host needs to be placed inside the bracket.
Example: run(“mkdir /home/fabric”)
In most cases, we need to run the commands as a superuser. Using sudo is the most commonly used procedure other than run. The same way we used run, we will use sudo by placing the desired shell command inside the brackets.
Example: sudo(“mkdir /home/fabric”)
As stated above, fabric allows us to do the automation locally just like the way it allows us to automate on remote machines. For that purpose, it provides the local utility to execute commands locally. Only limitation in local procedure is, we cannot store the output. However, run and sudo procedure lets you store the output.
Example: local(“python /home/python/setup.py”)
get() and put()
For downloading files from the remote hosts, get() is used. We need to specify the path of source on remote and destination on local. For uploading files to the remote hosts, put() is used. Just like in get(), here also we need to specify source on local and destination on remote.
For downloading a database backup get("/backup/prod1db.gz", "./db.gz")
For uploading a tar to remote put("/local/path/to/app.tar.gz", "/tmp/trunk/app.tar.gz")
One of my favorite procedure, prompt() lets you take user input just by making users to give the input on the prompt.
Example: prompt('Enter the preferred username for this script:', 'pyuser')
For the demo purpose, I will be working on EC2 using RHEL 7 AMI
Fabric can be installed with very easy and quick installation process with minimal system requirements. Below are the system requirements
Python 2.5+ and Development Headers
EPEL Repository Enabled
If you are using the RHEL 7 AMI, then it will have first two requirements already installed on it. You will just need to download and install EPEL Repository.