I always loved to use Fabric as a tool to automate my dev workflows and deployments. It was so easy to get started, straightforward and uncomplicated.
Imagine my surprise, when I got literally stuck trying to use Fabric 2. It all looked different. Commands I got used to simply didn’t exist anymore, and I could not find a clear minimal example to get me started. Even basic things seemed hard to do, as I was so used to the old ways.
In this article, I’d like to provide you with the explanation I wish I had found. I hope that it will help you get started with Fabric 2, and to skip the initial learning curve.
Why Fabric (2)?
Fabric is a Python tool that you can use to automate your workflows and deployments. You might have seen fabfile.py
in some Python projects, or used it yourself with $ fab taskname
.
If you have a series of commands which you always run by hand - Fabric can help you to script them out. If you have to connect to your server via SSH to do a Git pull, and restart the Gunicorn service - Fabric can do it for you, without having to think about SSH details or typing out the same 3 commands again and again.
While Fabric 1 is still around, it only supports Python versions 2.5 - 2.7. If you’re using Python 3, you’ll have to use Fabric 2 instead. Fabric 2 is a complete rewrite, based on the learnings from Fabric 1. It’s been made to be better, faster and easier to use from code - addressing many technical shortcomings of Fabric 1.
I’m sure it’ll be a great tool for you. It can help you make your workflows a lot less tedious. The only challenge to overcome is an initially steep learning curve, and a bit of confusion when getting into it.
A Basic Example Fabfile for Fabric 2
It took me way too long, to finally stumble over an example fabfile.py
, which
is hidden in the docs of Fabric 2. At the very end of the section about upgrading from
Fabric 1 to Fabric 2, you can find a complete example fabfile.py.
Allow me to share my own small example fabfile with you.
It’s focused around an initial small set of tasks for a Django project.
This is the example content of a fabfile.py
file, which can be
located right at the root of your Django project:
from fabric import task
# the directory of your project on your VPS
code_dir = '/home/your_user/project'
# here, you can provide a default hostname
# (from your .ssh/config)
default_hosts = ["your_host_name"]
# to perform the task on your default hosts, you
# have to pass them in each task decorator
@task(hosts=default_hosts)
def update(c):
# I miss the "with cd(...)" syntax :( it's not yet ported
c.run("cd {} && git pull".format(code_dir))
# this command will only make sense if you're using docker-compose
# to deploy your project
c.run("cd {} && docker-compose up -d".format(code_dir))
@task(hosts=default_hosts)
def download_db(c):
c.get("{}/db.sqlite3".format(code_dir), "db.sqlite3")
@task(hosts=default_hosts)
def upload_db(c):
c.put("db.sqlite3", "{}/db.sqlite3".format(code_dir))
@task(hosts=default_hosts)
def yolo(c):
# local will do something on your current host, instead
# of the SSH host - very handy!
c.local("git push")
# you can call other tasks - cool, huh?
update(c)
With this fabfile.py
in place, and fabric 2.*
installed, we can run the following from the terminal:
$ fab update
If everything is setup correctly, this should execute the update task from your fabfile. No more SSH-ing into the machine, nor typing in the same 3 commands over and over! You can check out the result in your browser, and get back to working on your feature instead of breaking your flow.
In Conclusion
After an initially rocky experience with getting used to Fabric 2, I’m really content with the tool. It does exactly what I need of it - helping me automate small workflows using Python conveniently, without having to use heavy-weight tools which break my flow.
I hope that this article will help you to get started with using Fabric 2 for your (Django) project!
I’m looking forward to see where Fabric 2 will develop, and will be very happy to check back on it to learn about new quality-of-life features which will surely be implemented over time, to make the experience equally as smooth as with Fabric 1.
If you want to read more about Fabric 2, check out the docs for more details. If you found this article useful, and would be keen to learn more about Docker, deploying Django and automating workflows - sign up to the mailing list below!