in Uncategorized

Deployment with Fabric

Fabric is an awesome tool written in Python that we can utilize to execute shell commands on a remote computer via SSH, so any commands we would run locally on our computer we can run on remote servers without much effort. The way this works is that we will create a local utility program where we define local commands that will execute the remote ones.

We can execute anything with Fabric, so everything from deployment to complex server management is a fair game. In this blog I want to focus on deployment and how we can create a simple and automated deployment process with Fabric.

Let’s first think of how the deployment of our example application can work:

  • The source code is stored in a remote git repository, our server can access it and pull the new sources, which means our code hosting platform like Github or Github is configured with that server’s SSH key.
  • The release branches are named releases/<version> where version is the version we want to deploy, e.g. 3.2.0.
  • We have access to the server from our computer or computer where we want to run Fabric via SSH, so that means our SSH key is allowed to be used as authentication mechanism on that server.
  • Application can be run using a systemd service called serverapp, which is configured to start our application from a specific folder /home/serverapp. This will make our solution more general. However, instead of running a systemd service, we can start or restart our application in any other way we need.

The best way would be to create a project with its own Python virtual environment using Pipenv or Poetry, but we can also use our system Python for simplicity. Let’s start by installing Fabric:

Now we need to prepare our Fabric file, where we will define our deploy command. Let’s save it in fabfile.py:

This Fabric script defines one command called deploy, taking one argument called version. It will:

  • Connect to our server HOST (where we have server login and address of our server)
  • Check out our release branch from Git in the GIT_DIR folder (we have to pull this folder first before running our Fabric deploy command as here we are already working in existing git directory)
  • Copy all files from our release branch to APP_DIR from where we run our application
  • Restart our serverapp using a systemd service (here we can put any other command to stop & start the application)

All there is left to do is to start our deployment! Run:

This will call our deploy command with 0.1.18 as our version argument. We can define any number of commands we want and run them individually like this. There is also a way to show all commands we have defined via Fabric:

Another thing we will get for free with Fabric is that it will show us output of the commands automatically, so when we run our deploy, we will see the output of git, rsync and systemd status commands on our own command line, which is really neat.

I really like Fabric and I think it is a great tool for many situations. Check out the documentation and make great stuff. I hope it will work well for you as well! Let me know at @stribny.

Loading Likes...

Check out my upcoming book Efficient developer on software development.