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/

 


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

1 COMMENT
  • random guy
    Reply

    this one is cool keep posting things like it

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.