Jun
26
2017
0

How to handle Python 3 Fabric exceptions with a Decorator

Python’s Fabric module is an easy and efficient way for task based automation.  I use it to automate the installation of VoIP phone systems on CentOS 6/7.  In a nut shell, Fabric executes shell commands over SSH.  It also can be used to automate local tasks.  The purpose of this article is to highlight the basic process of capturing Fabric exceptions(try/except) using a wrapper function called a decorator.  A few well placed decorators can save lot of time coding the same thing over and over(like capturing a specific error). 

Most of you in the know about Python/Fabric probably  will find my examples trivial, but I had to dig to find the Fabric exception environment variable.  I’m sure it’s somewhere on the Fabric site… but I gave up after 30 minutes.  So hopefully you will find this post useful either way!

If you need help installing Python3, pip3 and Fabric please refer to my previous post.

Goals of this Post:

  • create a simple Fabric function that creates a directory
  • create a Fabric exception
  • create Decorator for adding exception handling

Lets start with a basic fabfile.py: 

(more…)