The Fabric library – the deployment and development task manager

Fabric is a Python library and a command-line tool that allows execution in application deployment and administration tasks. 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 Fabric library – the deployment and development task manager

The Python environment contextual menu

Clicking on it brings up the Install Python Package modal window as shown in the following screenshot:

The Fabric library – the deployment and development task manager

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 Fabric library – the deployment and development task manager

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:

The Fabric library – the deployment and development task manager

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 Fabric library – the deployment and development task manager

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.

Note

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_type task in the command line as follows:

The Fabric library – the deployment and development task manager

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 Fabric library – the deployment and development task manager

The following are two examples of how the task can be executed:

The Fabric library – the deployment and development task manager

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 Fabric library – the deployment and development task manager

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:

The Fabric library – the deployment and development task manager

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.66