Learn Steps

Fabric : Tool you need to automate installations and deployments.

[sgmb id=3]

 

Starting with the Fabric, Fabric is a library in python that provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.

With fabric you can easily automate the installation and deployment tasks. Lets see what it does and how.

So fabric make use of paramiko library which is a Python (2.6+, 3.3+) implementation of the SSHv2 protocol, providing both client and server functionality.

With Fabric you can directly run your commands that you run in terminal to your remote machine. Lets see how we can accomplish it.

 

Installation:

It is simply installed like any other python package by using pip.

pip install fabric

Keep in mind it requires paramiko which you can install by same method.

pip install paramiko

Usage:

Lets start with fabfile. With fabric you have a command at your disposal named fab. By default fab reads from the fabfile. Example file is below.

# Fabfile to:
# - update the remote system(s)
# - download and install an application

# Import Fabric's API module
from fabric.api import *

env.hosts = [
 "HOST",
 # 'ip.add.rr.ess
 # 'server2.domain.tld',
]
# Set the username
env.user = "USERNAME"
env.password = "PASSWORD" #avoid this in case of your ssh key is already added to the machine.

def list_file():
 """
 Update the default OS installation's
 basic default tools.
 """
 run("ls -l")

def install_pip():
 run("wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9")
 run("tar -xvzf pip-9.0.1.tar.gz")
 run("cd pip-9.0.1")
 run("python setup.py install")

def make_installs():
 run("aptitude update")
 run("aptitude -y upgrade")
 install_pip()
 list_file()

Avoid using password and prefer adding you ssh key to the remote machine instead.

Now that you have the file you can simply call its function like.

fab make_installs

This command will call the make_install function mentioned in the above file and will update and upgrade the function and after that install the pip and list the files.

So this was a simple introduction of fabric and how to use it. Using this you can easily automate a whole lot of tasks.

Python books for automation:

Subscribe for more such articles.

 

Read more articles

 

https://www.learnsteps.com/how-i-created-a-100-twitter-bots-and-made-them-interact-using-python/

 

https://www.learnsteps.com/javascript-understanding-repaint-and-reflow-of-dom-for-increasing-performance/