I’m a big fan of the Python programming language and to be quite honest, the majority of my masters work is written in Python. Along with Python, I also spend a lot of time working with multiple workstations on the network. This is because my masters is focused on securing media in a peer-to-peer media distribution platform. Now anyone with experience in working with multiple workstations knows you spend plenty of time SSHing into those workstations. Today I’m going to make your life easier with the help of Fabric.
What is Fabric?
“Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks”. You could have read that on the website so let me elaborate.
“It provides …” Ok, seriously now. Fabric helps you execute commands remotely without you having to individually SSH into each machine. What’s more, Fabrics allows you to define roles and associate hosts to these roles. With the roles in place, functions can then be created to execute on a role.
How I use it
In the scenario of a peer-to-peer media distribution platform, I have multiple peers sharing information with each other. The tracker maintains the availability of media in the platform and provides this information to the peers.
Now in a production environment Fabric is of no use to me, as the end user will be operating the peer but as my masters is still a work in progress, I am not in a production environment. Instead I have to test the system without asking colleagues to install and run the peer software.
What Fabric allows me to do, is too install the peer software on any devices on the network that I have SSH access to and then execute commands. In order to do this I create two roles, namely peer and tracker. For the tracker role I only specify the tracker as a host, but for the peer roles, I specify all hosts that will function as peers. I then define the folder structure for synchronization through rsync.
With all the configurations set up, I am then able to modify code on my local machine and deploy the code straight to the tracker and peers. I can then with another command, start the tracker and run all the peers on the network in uploading mode. All of this without having to manually SSH into each and every machine.
Something to take note of with Fabric is that is does not release from a host until the command on the host has finished executing. This is a problem if you need to start multiple peers in uploading mode and leave them running until your tests are complete. In this case Fabric will SSH into the first host, run the command and wait until execution has finished before moving on to the next host.
This is where a very clever application called Screen comes into the equation. Screen is included in most *nix systems. According to the Wikipedia page for Screen, “it is a software application that can be used to multiplex several virtual consoles”. This is especially useful if you only have one session available on a host.
The issue of a stuck session can be solved with Screen, if you include the following steps in your Fabric file:
- SSHing into a remote host
- Creating a detached Screen session and attaching
- Executing a bash script and then detaching
Give it a try
I use Fabric everyday now and would be completely stuck without it. Give it a try and even if you don’t need to make use of Fabric, then you should definitely have a look at GNU Screen.
PS: A big thank you to an old labber Jacques for introducing Fabric to me.