Deploying with Fabric — Flask 0.9-dev documentation
Deploying with Fabric¶
Fabric is a tool for Python similar to Makefiles but with the ability
to execute commands on a remote server. In combination with a properly
set up Python package (Larger Applications) and a good concept for
configurations (Configuration Handling) it is very easy to deploy Flask
applications to external servers.Before we get started, here a quick checklist of things we have to ensure
upfront:
- Fabric 1.0 has to be installed locally. This tutorial assumes the
latest version of Fabric.- The application already has to be a package and requires a working
setup.py file (Deploying with Distribute).- In the following example we are using mod_wsgi for the remote
servers. You can of course use your own favourite server there, but
for this example we chose Apache + mod_wsgi because it’s very easy
to setup and has a simple way to reload applications without root
access.Creating the first Fabfile¶
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 file
will show up as fab subcommands. They are executed on one or more
hosts. These hosts can be defined either in the fabfile or on the command
line. In this case we will add them to the fabfile.This is a basic first example that has the ability to upload the current
sourcecode to the server and install it into a pre-existing
virtual environment:from fabric.api import * # the user to use for the remote commands env.user = 'appuser' # the servers where the commands are executed env.hosts = ['server1.example.com', 'server2.example.com'] def pack(): # create a new source distribution as tarball local('python setup.py sdist --formats=gztar', capture=False)