Automation is better than manual work. The less we intervene in a process, the better. Everything. Let's also figure out a way to do this using a single tool. One that is easy to program, easy to use. And why not do all this with nothing but SSH installed on the remote machine – all commands scripted at a single location for executing locally or on any number of various servers.
In this blog, we will talk about this amazing tool called Fabric. I will show you how to install it, create simple deployment and management functions that you can run across multiple servers. Yes, you can throw away your bash hacks and complex scripts.
I will assume you are using a Unix/Linux System. First of all, install pip
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
Then, install fabric
python get-pip.py && pip install Fabric
Fabric brings some amazing ssh automation and integration with python where you can write python functions and code and these will run across multiple servers. Fabric has a huge selection of functions to help with a lot of remote automation tasks. To get started, create a fab file called fabfile.py in any directory you want. We will take a look at few of Fabricfunctions and operations.
The first operation is run (fabric.operations.run). The run feature is used for executing a shell command on one or more remote hosts. You can capture the output of this feature in a variable and pass it in your script. Here is a simple example:
# Check Memory
# Check hostname
# Check Disk Space
This command allows you to execute code as a super-user with root privileges on the remote system
# Create a directory
# Install Apache WebServer
sudo('yum -y install httpd')
# Yum Upgrade
sudo("yum -y upgrade",pty=True)
The get is used to download (i.e. pull) file(s) from the remote system to the computer where the Fabric is being used The remote path is identified by the remote_path argument. The local download path is specified by the local_path argument
# Download Apache logs & store them locally
The get is used to put (i.e. upload) file(s) from the local system to the remote server(s) The remote path is identified by the remote_path argument. The local download path is specified by the local_path argumentThe mode argument set the file mode.
# Upload a tar archive of a local file to the servers
# Upload a file and set mode
upload = put("requirements.txt", "requirements.txt", mode=664)
# Verify the upload
The reboot command straight-forward: it is used to reboot the remote system. By default, it waits two minutes (i.e. 120 seconds -> wait=120) before doing its job.
# Reboot the remote system
# # Reboot after 20 seconds
Let us create an empty fabfile.py to show you how this works.
# import all the fabric functions that we from fabric.api import * # import the os module to get file basenames import os # Define Hosts you want to run commands on env.hosts = ['220.127.116.11','18.104.22.168'] env.user = "username" env.password = "passwordcomeshere" #Use ssh keys for authentication instead env.parallel = False def uptime(): """Check the uptime on Servers""" with hide('running','warnings'): run('hostname') run('uptime') def disk_space(): """Check the Diskspace on Servers""" with hide('running','warnings'): run('hostname') run('df -h') def memory(): """Check the Diskspace on Servers""" with hide('running','warnings'): run('hostname') run('free -m') def os-type(): """Check OS Type""" with hide('running','warnings'): run('hostname') run('cat /etc/issue')
Run fabric: In this case, check the memory on the servers
fab memory [22.214.171.124] Executing task 'memory' [126.96.36.199] Executing task 'memory' [188.8.131.52] out: 184.108.40.206 [220.127.116.11] out: [18.104.22.168] out: total used free shared buff/cache available [22.214.171.124] out: Mem: 1504 97 1368 5 37 1342 [126.96.36.199] out: Swap: 1504 364 1139 [188.8.131.52] out: [184.108.40.206] out: supremeoverlord.io [220.127.116.11] out: [18.104.22.168] out: total used free shared buffers cached [22.214.171.124] out: Mem: 996 264 731 0 37 163 [126.96.36.199] out: -/+ buffers/cache: 64 932 [188.8.131.52] out: Swap: 0 0 0 [184.108.40.206] out: Done.
That's fab for you. I will update this blog in the coming weeks on how I use fab for automated deployment to multiple tomcat servers. Enjoy!