Deploying with Fabric
Before we get started, here a quick checklist of things we have to ensureupfront:
Fabric 1.0 has to be installed locally. This tutorial assumes thelatest version of Fabric.
In the following example we are using mod_wsgi for the remoteservers. You can of course use your own favourite server there, butfor this example we chose Apache + mod_wsgi because it’s very easyto setup and has a simple way to reload applications without rootaccess.
A fabfile is what controls what Fabric executes. It is named fabfile.py
and executed by the fab command. All the functions defined in that filewill show up as fab subcommands. They are executed on one or morehosts. These hosts can be defined either in the fabfile or on the commandline. In this case we will add them to the fabfile.
This is a basic first example that has the ability to upload the currentsource code to the server and install it into a pre-existingvirtual environment:
Running Fabfiles
Now how do you execute that fabfile? You use the fab command. Todeploy the current version of the code on the remote server you would usethis command:
- $ fab pack deploy
However this requires that our server already has the/var/www/yourapplication
folder created and/var/www/yourapplication/env
to be a virtual environment. Furthermoreare we not creating the configuration or .wsgi
file on the server. Sohow do we bootstrap a new server into our infrastructure?
To setup a new server you would roughly do these steps:
- Create the directory structure in :
- $ mkdir /var/www/yourapplication
- $ cd /var/www/yourapplication
- $ virtualenv --distribute env
Upload a new
application.wsgi
file to the server and theconfiguration file for the application (eg:application.cfg
)
So now the question is, where do the andapplication.cfg
files come from?
The WSGI file has to import the application and also to set an environmentvariable so that the application knows where to look for the config. Thisis a short example that does exactly that:
The application itself then has to initialize itself like this to look forthe config at that environment variable:
- app = Flask(__name__)
- app.config.from_object('yourapplication.default_config')
- app.config.from_envvar('YOURAPPLICATION_CONFIG')
This approach is explained in detail in the section of thedocumentation.
The Configuration File
Now as mentioned above, the application will find the correctconfiguration file by looking up the YOURAPPLICATION_CONFIG
environmentvariable. So we have to put the configuration in a place where theapplication will able to find it. Configuration files have the unfriendlyquality of being different on all computers, so you do not version themusually.
Either way, in our case here we only expect one or two servers and we canupload them ahead of time by hand.
Now we can do our first deployment. We have set up the servers so thatthey have their virtual environments and activated apache configs. Now wecan pack up the application and deploy it:
Fabric will now connect to all servers and run the commands as writtendown in the fabfile. First it will execute pack so that we have ourtarball ready and then it will execute deploy and upload the source codeto all servers and install it there. Thanks to the setup.py
file wewill automatically pull in the required libraries into our virtualenvironment.
Next Steps
From that point onwards there is so much that can be done to makedeployment actually fun:
Create a bootstrap command that initializes new servers. It couldinitialize a new virtual environment, setup apache appropriately etc.
Put configuration files into a separate version control repositoryand symlink the active configs into place.
You could also put your application code into a repository and checkout the latest version on the server and then install. That way youcan also easily go back to older versions.
hook in testing functionality so that you can deploy to an externalserver and run the test suite.