(For more resources related to this topic, see here.)
Essentially, Fabric is a tool that allows the developer to execute arbitrary Python functions via the command line and also a set of functions in order to execute shell commands on remote servers via SSH. Combining these two things together offers developers a powerful way to administrate the application workflow without having to remember the series of commands that need to be executed on the command line.
The library documentation can be found at http://fabric.readthedocs.org/.
Installing the library in PTVS is straightforward. Like all other libraries, to insert this library into a Django project, right-click on the Python 2.7 node in Python Environments of the Solution Explorer window. Then, select the Install Python Package entry.
The Python environment contextual menu
Clicking on it brings up the Install Python Package modal window as shown in the following screenshot:
It's important to use easy_install to download from the Python package index. This will bring the precompiled versions of the library into the system instead of the plain Python C libraries that have to be compiled on the system.
Once the package is installed in the system, you can start creating tasks that can be executed outside your application from the command line. First, create a configuration file, fabfile.py, for Fabric. This file contains the tasks that Fabric will execute.
The previous screenshot shows a really simple task: it prints out the string hello world once it's executed. You can execute it from the command prompt by using the Fabric command fab, as shown in the following screenshot:
Now that you know that the system is working fine, you can move on to the juicy part where you can make some tasks that interact with a remote server through ssh. Create a task that connects to a remote machine and find out the type of OS that runs on it.
The env object provides a way to add credentials to Fabric in a programmatic way
We have defined a Python function, host_type, that runs a POSIX command, uname–s, on the remote. We also set up a couple of variables to tell Fabric which is the remote machine we are connecting to, i.e. env.hosts, and the password that has to be used to access that machine, i.e. env.password.
It's never a good idea to put plain passwords into the source code, as is shown in the preceding screenshot example.
Now, we can execute the host_typetask in the command line as follows:
The Fabric library connects to the remote machine with the information provided and executes the command on the server. Then, it brings back the result of the command itself in the output part of the response.
We can also create tasks that accept parameters from the command line. Create a task that echoes a message on the remote machine, starting with a parameter as shown in the following screenshot:
The following are two examples of how the task can be executed:
We can also create a helper function that executes an arbitrary command on the remote machine as follows:
def execute(cmd): run(cmd)
We are also able to upload a file into the remote server by using put:
The first argument of put is the local file you want to upload and the second one is the destination folder's filename. Let's see what happens:
Deploying process with Fabric
The possibilities of using Fabric are really endless, since the tasks can be written in plain Python language. This provides the opportunity to automate many operations and focus more on the development instead of focusing on how to deploy your code to servers to maintain them.
This article provided you with an in-depth look at remote task management and schema migrations using the third-party Python library Fabric.
Resources for Article:
- Through the Web Theming using Python [Article]
- Web Scraping with Python [Article]
- Python Data Persistence using MySQL [Article]