Chapter 11. Automating Deployment with Fabric
Automate, automate, automate.
Cay Horstman
Automating deployment is critical for our staging tests to mean anything. By making sure the deployment procedure is repeatable, we give ourselves assurances that everything will go well when we deploy to production. (These days people sometimes use the words “infrastructure as code” to describe automation of deployments, and provisioning.)
Fabric is a tool which lets you automate commands that you want to run on servers. “fabric3” is the Python 3 fork:
$ pip install fabric3
Tip
It’s safe to ignore any errors that say “failed building wheel” during the Fabric3 installation, as long as it says “Successfully installed…” at the end.
The usual setup is to have a file called fabfile.py, which will
contain one or more functions that can later be invoked from a command-line
tool called fab
, like this:
fab function_name:host=SERVER_ADDRESS
That will call function_name
, passing in a connection to the server at
SERVER_ADDRESS
. There are lots of other options for specifying usernames and
passwords, which you can find out about using fab --help
.
Breakdown of a Fabric Script for Our Deployment
The
best way to see how it works is with an example.
Here’s one
I made earlier, automating all the deployment steps we’ve been going through.
The main function is called deploy
; that’s the one we’ll invoke from the command line. It then calls out to several helper functions, which we’ll build together one by ...
Get Test-Driven Development with Python, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.