Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Actually, Fabric, in essence, is a deployment management framework written in Python which makes remotely managing multiple servers incredibly easy as shown in the example below:
for s in $(cat servers.txt); do ssh $s service httpd graceful; done
where we issue a change to a group servers.
Fabric is best installed via pip:
$ pip install fabric
In order for Fabric's installation to succeed, we'll need following:
- the setuptools packaging/installation library
- the Python Paramiko SSH library
- and Paramiko's dependency, the PyCrypto cryptography library
- and, if using the parallel execution mode, the multiprocessing library
- if we're using Paramiko 1.12 or above, we will also need an additional dependency for Paramiko, the ecdsa library
The installation process added a Python script called fab to a directory in our path. This is the script which will be used to make magic happen with Fabric, however, just running fab from the command-line won't do much at all. In order to do anything interesting, we'll need to create our first fabfile.
Typical use involves creating a Python module containing one or more functions, then executing them via the fab command-line tool. Below is a small but complete fabfile containing a single task (http://www.fabfile.org/):
from fabric.api import run def host_type(): run('uname -s')
$ fab -H localhost host_type [localhost] Executing task 'host_type' [localhost] run: uname -s [localhost] out: Linux [localhost] out: Done. Disconnecting from localhost... done.
The fabfile is where all of your functions, roles, configurations, etc. will be defined. It's just a little bit of Python which tells Fabric exactly what it needs to do. By convention, this file should be named fabfile.py, but we can name it anything we'd like. If it's something other than fabfile.py, we'll need to specify the path with fab -f /path/to/notfabfile.py. Here's a simple example which runs uptime locally (from github):
#!/usr/bin/env python from fabric.api import local def uptime(): local('uptime')
$ fab uptime [localhost] local: uptime 07:09:41 up 2 days, 12:39, 2 users, load average: 1.88, 1.78, 1.31 Done.
Fabric provides a set of functions which can be used to interact with these remote hosts. The most commonly used ones are:
- run - Run a shell command on a remote host.
- sudo - Run a shell command on a remote host, with superuser privileges.
- get - Download one or more files from a remote host.
- put - Upload one or more files to a remote host.
#!/usr/bin/env python from fabric.api import env, run #env.hosts = [ '18.104.22.168', 'localhost' ] #env.hosts = [ 'localhost' ] env.hosts = [ '127.0.0.1' ] def uptime(): run('uptime')
Here, I used localhost, however, it should work on remote as well.
Here is the list of differences and similarities (DevOps Technologies: Fabric or Ansible):
- We get results fairly quick in Fabric while Ansible needs more efforts to understand.
- Ansible is more powerful since it provides much deeper and more complex semantics for modeling multi-tier infrastructure, such as those with arrays of web and database hosts.
- From the user's perspective, Fabric has a more basic API and uses Python for authoring.
- Ansible usess YAML and provides a richness in its behavior.
- Both Ansible and Fabric do their taks via secure shell (SSH).
- Fabric executes simple command-line statements to target machines over SSH.
- Ansible pushes modules to remote machines and then executes these modules remotely.
- The biggest difference between them is in the features and complexity.
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Regular Expressions with Python
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
map, filter, and reduce
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
Classes and Instances (__init__, __call__, etc.)
if__name__ == '__main__'
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Interview Questions I
Python Interview Questions II
Python Interview Questions III
Python Interview Questions IV
Python Interview Questions V
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization