Skip to content
Myr

Added chapter about fabric based deployments

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent b7c0e56 · Browse files · 5 files+219 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+219 added−2 removed

  • other:+219 added−2 removed

5 files+219 added−2 removed

modified:docs/deploying/mod_wsgi.rst+15 added
1.. _mod_wsgi-deployment:23mod_wsgi (Apache)4=================56If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.
128 unmodified lines
135 The reason for this is that for non-installed packages, the module136 filename is used to locate the resources and for symlinks the wrong137 filename is picked up.138139Support for Automatic Reloading140-------------------------------141142To help deployment tools you can activate support for automatic reloading.143Whenever something changes the `.wsgi` file, `mod_wsgi` will reload all144the daemon processes for us.145146For that, just add the following directive to your `Directory` section:147148.. sourcecode:: apache149150 WSGIScriptReloading On151152Working with Virtual Environments153---------------------------------154155Virtual environments have the advantage that they never install the
11 unmodified lines
modified:docs/patterns/distribute.rst+4 added
31 unmodified lines
32not supported by `distribute`_ so we will not bother with it. If you have33not yet converted your application into a package, head over to the34:ref:`larger-applications` pattern to see how this can be done.3536A working deployment with distribute is the first step into more complex37and more automated deployment scenarios. If you want to fully automate38the process, also read the :ref:`fabric-deployment` chapter.3940Basic Setup Script41------------------4243Because you have Flask running, you either have setuptools or distribute
123 unmodified lines
added:docs/patterns/fabric.rst+196 added
1.. _fabric-deployment:23Deploying with Fabric4=====================56`Fabric`_ is a tool for Python similar to Makefiles but with the ability7to execute commands on a remote server. In combination with a properly8set up Python package (:ref:`larger-applications`) and a good concept for9configurations (:ref:`config`) it is very easy to deploy Flask10applications to external servers.1112Before we get started, here a quick checklist of things we have to ensure13upfront:1415- Fabric 1.0 has to be installed locally. This tutorial assumes the16 latest version of Fabric.17- The application already has to be a package and requires a working18 `setup.py` file (:ref:`distribute-deployment`).19- In the following example we are using `mod_wsgi` for the remote20 servers. You can of course use your own favourite server there, but21 for this example we chose Apache + `mod_wsgi` because it's very easy22 to setup and has a simple way to reload applications without root23 access.2425Creating the first Fabfile26--------------------------2728A fabfile is what controls what Fabric executes. It is named `fabfile.py`29and executed by the `fab` command. All the functions defined in that file30will show up as `fab` subcommands. They are executed on one or more31hosts. These hosts can be defined either in the fabfile or on the command32line. In this case we will add them to the fabfile.3334This is a basic first example that has the ability to upload the current35sourcecode to the server and install it into a already existing36virtual environment::3738 from fabric.api import *3940 # the user to use for the remote commands41 env.user = 'appuser'42 # the servers where the commands are executed43 env.hosts = ['server1.example.com', 'server2.example.com']4445 def pack():46 # create a new source distribution as tarball47 local('python setup.py sdist --formats=gztar', capture=False)4849 def deploy():50 # figure out the release name and version51 dist = local('python setup.py --fullname').strip()52 # upload the source tarball to the temporary folder on the server53 put('sdist/%s.tar.gz' % dist, '/tmp/')54 # create a place where we can unzip the tarball, then enter55 # that directory and unzip it56 run('mkdir yourapplication')57 with cd('/tmp/yourapplication'):58 run('tar xzf /tmp/yourapplication.tar.gz')59 # now setup the package with our virtual environment's60 # python interpreter61 run('/var/www/yourapplication/env/bin/python setup.py install')62 # now that all is set up, delete the folder again63 run('rm -rf /tmp/yourapplication /tmp/yourapplication.tar.gz')64 # and finally touch the .wsgi file so that mod_wsgi triggers65 # a reload of the application66 run('touch /var/www/yourapplication.wsgi')6768The example above is well documented and should be straightforward. Here69a recap of the most common commands fabric provides:7071- `run` - executes a command on a remote server72- `local` - executes a command on the local machine73- `put` - uploads a file to the remote server74- `cd` - changes the directory on the serverside. This has to be used75 in combination with the `with` statement.7677Running Fabfiles78----------------7980Now how do you execute that fabfile? You use the `fab` command. To81deploy the current version of the code on the remote server you would use82this command::8384 $ fab pack deploy8586However this requires that our server already has the87``/var/www/yourapplication`` folder created and88``/var/www/yourapplication/env`` to be a virtual environment. Furthermore89are we not creating the configuration or `.wsgi` file on the server. So90how do we bootstrap a new server into our infrastructure?9192This now depends on the number of servers we want to set up. If we just93have one application server (which the majority of applications will94have), creating a command in the fabfile for this is overkill. But95obviously you can do that. In that case you would probably call it96`setup` or `bootstrap` and then pass the servername explicitly on the97command line::9899 $ fab -H newserver.example.com bootstrap100101To setup a new server you would roughly do these steps:1021031. Create the directory structure in ``/var/www``::104105 $ mkdir /var/www/yourapplication106 $ cd /var/www/yourapplication107 $ virtualenv --distribute env1081092. Upload a new `application.wsgi` file to the server and the110 configuration file for the application (eg: `application.cfg`)1111123. Create a new Apache config for `yourapplication` and activate it.113 Make sure to activate watching for changes of the `.wsgi` file so114 that we can automatically reload the application by touching it.115 (See :ref:`mod_wsgi-deployment` for more information)116117So now the question is, where do the `application.wsgi` and118`application.cfg` files come from?119120The WSGI File121-------------122123The WSGI file has to import the application and also to set an environment124variable so that the application knows where to look for the config. This125is a short example that does exactly that::126127 import os128 os.environ['YOURAPPLICATION_CONFIG'] = '/var/www/yourapplication/application.cfg'129 from yourapplication import app130131The application itself then has to initialize itself like this to look for132the config at that environment variable::133134 app = Flask(__name__)135 app.config.from_object('yourapplication.default_config')136 app.config.from_envvar('YOURAPPLICATION_CONFIG')137138This approach is explained in detail in the :ref:`config` section of the139documentation.140141The Configuration File142----------------------143144Now as mentioned above, the application will find the correct145configuration file by looking up the `YOURAPPLICATION_CONFIG` environment146variable. So we have to put the configuration in a place where the147application will able to find it. Configuration files have the unfriendly148quality of being different on all computers, so you do not version them149usually.150151A popular approach is to store configuration files for different servers152in a separate version control repository and check them out on all153servers. Then symlink the file that is active for the server into the154location where it's expected (eg: ``/var/www/yourapplication``).155156Either way, in our case here we only expect one or two servers and we can157upload them ahead of time by hand.158159First Deployment160----------------161162Now we can do our first deployment. We have set up the servers so that163they have their virtual environments and activated apache configs. Now we164can pack up the application and deploy it::165166 $ fab pack deploy167168Fabric will now connect to all servers and run the commands as written169down in the fabfile. First it will execute pack so that we have our170tarball ready and then it will execute deploy and upload the source code171to all servers and install it there. Thanks to the `setup.py` file we172will automatically pull in the required libraries into our virtual173environment.174175Next Steps176----------177178From that point onwards there is so much that can be done to make179deployment actually fun:180181- Create a `bootstrap` command that initializes new servers. It could182 initialize a new virtual environment, setup apache appropriately etc.183- Put configuration files into a separate version control repository184 and symlink the active configs into place.185- You could also put your application code into a repository and check186 out the latest version on the server and then install. That way you187 can also easily go back to older versions.188- hook in testing functionality so that you can deploy to an external189 server and run the testsuite. 190191Working with Fabric is fun and you will notice that it's quite magical to192type ``fab deploy`` and see your application being deployed automatically193to one or more remote servers.194195196.. _Fabric: http://fabfile.org/
modified:docs/patterns/index.rst+1 added
17 unmodified lines
1819 packages20 appfactories21 distribute22 fabric23 sqlite324 sqlalchemy25 fileuploads26 caching
8 unmodified lines
modified:docs/config.rst+3 added-2 removed
181 unmodified lines
182183 app.config.from_object('configmodule.ProductionConfig')184185There are many different ways and it's up to you how you want to manage186your configuration files. However here a list of good recommendations::186your configuration files. However here a list of good recommendations:187188- keep a default configuration in version control. Either populate the189 config with this default configuration or import it in your own190 configuration files before overriding values.
4 unmodified lines
195 code at all. If you are working often on different projects you can196 even create your own script for sourcing that activates a virtualenv197 and exports the development configuration for you.198- Use a tool like `fabric`_ in production to push code and199 configurations sepearately to the production server(s).199 configurations sepearately to the production server(s). For some200 details about how to do that, head over to the :ref:`deploy` pattern.201202.. _fabric: http://fabfile.org/