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 ...
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.