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
Provenance
- Commit
34fcd19- Tree
d1578ff- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b7c0e56
5 files changed+219 added−2 removed
modified:docs/deploying/mod_wsgi.rst+15 added
1+.. _mod_wsgi-deployment:2+3 mod_wsgi (Apache)4 =================5 6 If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.128 unmodified lines135 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.138 139+Support for Automatic Reloading140+-------------------------------141+142+To help deployment tools you can activate support for automatic reloading.143+Whenever something changes the `.wsgi` file, `mod_wsgi` will reload all144+the daemon processes for us.145+146+For that, just add the following directive to your `Directory` section:147+148+.. sourcecode:: apache149+150+ WSGIScriptReloading On151+152 Working with Virtual Environments153 ---------------------------------154 155 Virtual environments have the advantage that they never install the11 unmodified linesmodified:docs/patterns/distribute.rst+4 added
31 unmodified lines32 not supported by `distribute`_ so we will not bother with it. If you have33 not yet converted your application into a package, head over to the34 :ref:`larger-applications` pattern to see how this can be done.35 36+A working deployment with distribute is the first step into more complex37+and more automated deployment scenarios. If you want to fully automate38+the process, also read the :ref:`fabric-deployment` chapter.39+40 Basic Setup Script41 ------------------42 43 Because you have Flask running, you either have setuptools or distribute123 unmodified linesadded:docs/patterns/fabric.rst+196 added
1+.. _fabric-deployment:2+3+Deploying with Fabric4+=====================5+6+`Fabric`_ is a tool for Python similar to Makefiles but with the ability7+to execute commands on a remote server. In combination with a properly8+set up Python package (:ref:`larger-applications`) and a good concept for9+configurations (:ref:`config`) it is very easy to deploy Flask10+applications to external servers.11+12+Before we get started, here a quick checklist of things we have to ensure13+upfront:14+15+- 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.24+25+Creating the first Fabfile26+--------------------------27+28+A fabfile is what controls what Fabric executes. It is named `fabfile.py`29+and executed by the `fab` command. All the functions defined in that file30+will show up as `fab` subcommands. They are executed on one or more31+hosts. These hosts can be defined either in the fabfile or on the command32+line. In this case we will add them to the fabfile.33+34+This is a basic first example that has the ability to upload the current35+sourcecode to the server and install it into a already existing36+virtual environment::37+38+ from fabric.api import *39+40+ # 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']44+45+ def pack():46+ # create a new source distribution as tarball47+ local('python setup.py sdist --formats=gztar', capture=False)48+49+ 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')67+68+The example above is well documented and should be straightforward. Here69+a recap of the most common commands fabric provides:70+71+- `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.76+77+Running Fabfiles78+----------------79+80+Now how do you execute that fabfile? You use the `fab` command. To81+deploy the current version of the code on the remote server you would use82+this command::83+84+ $ fab pack deploy85+86+However this requires that our server already has the87+``/var/www/yourapplication`` folder created and88+``/var/www/yourapplication/env`` to be a virtual environment. Furthermore89+are we not creating the configuration or `.wsgi` file on the server. So90+how do we bootstrap a new server into our infrastructure?91+92+This now depends on the number of servers we want to set up. If we just93+have one application server (which the majority of applications will94+have), creating a command in the fabfile for this is overkill. But95+obviously you can do that. In that case you would probably call it96+`setup` or `bootstrap` and then pass the servername explicitly on the97+command line::98+99+ $ fab -H newserver.example.com bootstrap100+101+To setup a new server you would roughly do these steps:102+103+1. Create the directory structure in ``/var/www``::104+105+ $ mkdir /var/www/yourapplication106+ $ cd /var/www/yourapplication107+ $ virtualenv --distribute env108+109+2. Upload a new `application.wsgi` file to the server and the110+ configuration file for the application (eg: `application.cfg`)111+112+3. 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)116+117+So now the question is, where do the `application.wsgi` and118+`application.cfg` files come from?119+120+The WSGI File121+-------------122+123+The WSGI file has to import the application and also to set an environment124+variable so that the application knows where to look for the config. This125+is a short example that does exactly that::126+127+ import os128+ os.environ['YOURAPPLICATION_CONFIG'] = '/var/www/yourapplication/application.cfg'129+ from yourapplication import app130+131+The application itself then has to initialize itself like this to look for132+the config at that environment variable::133+134+ app = Flask(__name__)135+ app.config.from_object('yourapplication.default_config')136+ app.config.from_envvar('YOURAPPLICATION_CONFIG')137+138+This approach is explained in detail in the :ref:`config` section of the139+documentation.140+141+The Configuration File142+----------------------143+144+Now as mentioned above, the application will find the correct145+configuration file by looking up the `YOURAPPLICATION_CONFIG` environment146+variable. So we have to put the configuration in a place where the147+application will able to find it. Configuration files have the unfriendly148+quality of being different on all computers, so you do not version them149+usually.150+151+A popular approach is to store configuration files for different servers152+in a separate version control repository and check them out on all153+servers. Then symlink the file that is active for the server into the154+location where it's expected (eg: ``/var/www/yourapplication``).155+156+Either way, in our case here we only expect one or two servers and we can157+upload them ahead of time by hand.158+159+First Deployment160+----------------161+162+Now we can do our first deployment. We have set up the servers so that163+they have their virtual environments and activated apache configs. Now we164+can pack up the application and deploy it::165+166+ $ fab pack deploy167+168+Fabric will now connect to all servers and run the commands as written169+down in the fabfile. First it will execute pack so that we have our170+tarball ready and then it will execute deploy and upload the source code171+to all servers and install it there. Thanks to the `setup.py` file we172+will automatically pull in the required libraries into our virtual173+environment.174+175+Next Steps176+----------177+178+From that point onwards there is so much that can be done to make179+deployment actually fun:180+181+- 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. 190+191+Working with Fabric is fun and you will notice that it's quite magical to192+type ``fab deploy`` and see your application being deployed automatically193+to one or more remote servers.194+195+196+.. _Fabric: http://fabfile.org/modified:docs/patterns/index.rst+1 added
17 unmodified lines18 19 packages20 appfactories21 distribute22+ fabric23 sqlite324 sqlalchemy25 fileuploads26 caching8 unmodified linesmodified:docs/config.rst+3 added-2 removed
181 unmodified lines182 183 app.config.from_object('configmodule.ProductionConfig')184 185 There are many different ways and it's up to you how you want to manage186-your configuration files. However here a list of good recommendations::186+your configuration files. However here a list of good recommendations:187 188 - 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 lines195 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.201 202 .. _fabric: http://fabfile.org/