Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pyramid deployment


Published on

Published in: Technology
  • Be the first to comment

Pyramid deployment

  1. 1. Pyramid Deployment and Maintenance Carlos de la Guardia Plone Conference 2011
  2. 2. So, your application is ready and looking good
  3. 3. Time to deploy it and see how it holds up
  4. 4. Deployment with NginX More information at: upstream myapp-site { server; server; } server { server_name; access_log /home/example/env/access.log; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 60s; proxy_send_timeout 90s; proxy_read_timeout 90s; proxy_buffering off; proxy_pass http://myapp-site; proxy_redirect off; } }
  5. 5. Deployment with Apache and Mod_WSGI We need to create an app that will call our application with configuration. Then we set up apache to call this app. More information at: #pyramid.wsgi from pyramid.paster import get_app application = get_app('/mydir/modwsgi/env/myapp/production.ini', 'main') #apache.conf WSGIApplicationGroup %{GLOBAL} WSGIPassAuthorization On WSGIDaemonProcess pyramid user=cguardia group=staff threads=4 python-path=/mydir/modwsgi/env/lib/python2.6/site-packages WSGIScriptAlias /myapp /mydir/modwsgi/env/pyramid.wsgi <Directory /mydir/modwsgi/env> WSGIProcessGroup pyramid Order allow,deny Allow from all </Directory>
  6. 6. To paste or not to paste <ul><li>Paster is not considered a super fast wsgi server, but it's good enough for many use cases.
  7. 7. We already discussed mod_wsgi. If you are sure that you need something else, people on our mailing lists have reported success with uWSGI and gUnicorn.
  8. 8. If you are using ZODB with paster, consider using Jove to manage multiple sites and settings consistently:
  9. 9. Other approaches include using gevent, twisted or eventlets. </li></ul>
  10. 10. Putting all your eggs in the same basket
  11. 11. Your very own package index <ul><li>Create an egg directory inside an Apache web server or similar and use a simple script to generate the index in PyPI format.
  12. 12. Create an egg directory using a GitHub site and generate the index with the same script.
  13. 13. Install a PyPI clone like ClueReleaseManager, and configure your .pypirc file to point at the clone. Use setuptools to upload.
  14. 14. Jarn.mkrelease can be used to commit, tag and upload in one step: $ mkrelease -d mypypi src/my.package </li></ul>
  15. 15. Using Buildout with Pyramid <ul><li>Buildout is a Python system for assembling applications from multiple parts in a repeatable manner.
  16. 16. Recipes are used to define what each part of the buildout will install and/or setup. There are many available recipes on PyPI.
  17. 17. A buildout can have different configurations. For example, deployment and production.
  18. 18. Buildout can be used to setup a Pyramid application and it's dependencies easily. </li></ul>
  19. 19. Sample buildout [buildout] parts = myapp mkrelease Develop = src/mypackage index = [myapp] recipe = zc.recipe.egg eggs = some_dependency mypackage interpreter = py [mkrelease] recipe = zc.recipe.egg eggs = jarn.mkrelease scripts = mkrelease
  20. 20. Supervisor <ul><li>Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
  21. 21. We can use supervisor to control our pyramid applications, either alone or together with other applications or services.
  22. 22. A very easy way to configure it is to simply add the supervisor egg to our buildout and include a configuration file. </li></ul>
  23. 23. Sample supervisor configuration [inet_http_server] port= [supervisord] logfile= %(here)s/var/supervisord.log logfile_maxbytes= 50MB logfile_backups= 10 loglevel= info pidfile= %(here)s/var/ [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl= [program:core] command = %(here)s/bin/paster serve %(here)s/src/inav2_core/development.ini redirect_stderr = true
  24. 24. Useful maintenance tools
  25. 25. Scripting Pyramid <ul><li>Web applications usually expect to interact with a request object of some kind and return a response.
  26. 26. Importing web application code from a command line script will not work when it depends on a request.
  27. 27. Pyramid offers a facility for generating an environment similar to what the application would get from a real request.
  28. 28. This makes it possible to easily write scripts for tasks like updating databases, create sample content or initialize application services. </li></ul>
  29. 29. Writing the script <ul><ul><li>To generate the proper environment we use pyramid.paster.bootstrap, like this: from pyramid.paster import bootstrap env = bootstrap(' /path/to/my/development.ini ') print env[' request '].route_url(' home ')
  30. 30. We get back a dictionary with: </li><ul><li>sequest
  31. 31. app
  32. 32. root
  33. 33. registry
  34. 34. closer </li></ul></ul></ul>
  35. 35. Exception logging with pyramid_exclog [loggers] keys = exc_logger [handlers] keys = exc_handler [formatters] keys = exc_formatter [logger_exc_logger] level = ERROR handlers = exc_handler qualname = exc_logger [handler_exc_handler] class = handlers.SMTPHandler args = (('localhost', 25), '', [''], 'myapp Exception') level = ERROR formatter = exc_formatter [formatter_exc_formatter] format = %(asctime)s %(message)s
  36. 36. Backups For ZODB based applications: <ul><li>Collective.recipe.backup </li></ul>For Postgres based applications: <ul><li>SQL dump
  37. 37. File system level backup
  38. 38. Continuous archiving </li></ul>
  39. 39. Do not forget about staging
  40. 40. Thinking about staging <ul><li>A good deployment plan requires a good staging strategy.
  41. 41. It's important to be able to quickly deploy changes to staging before trying them in production.
  42. 42. Ideally, multiple branches could be deployed separately.
  43. 43. Take a look at octomotron: </li></ul>
  44. 44. So much to do, so little time Other deployment related tasks and decisions <ul><ul><li>Caching
  45. 45. Monitoring
  46. 46. Failover
  47. 47. Replication
  48. 48. Automated deployment
  49. 49. More... </li></ul></ul>
  50. 50. Case Study: KARL <ul><li>Open source web system for collaboration, organizational intranets, and knowledge management.
  51. 51. Mission-critical application used by organizations such as Open Society Foundations and Oxfam.
  52. 52. 5000+ users (no anonymous access).
  53. 53. More than 75,000 Pages of content.
  54. 54. Nearly 100,000 lines of code. </li></ul>
  55. 55. How is KARL deployed? <ul><li>Toolchain: Nginx, HAProxy, Paste, running under Supervisor.
  56. 56. Buildout for environment creation.
  57. 57. Custom package index stored on GitHub.
  58. 58. 1 App Server, 1 DB Server.
  59. 59. Uses ZODB with RelStorage and PGTextIndex.
  60. 60. Deployment is done with a Python script that creates a parallel environment, backs up and evolves data, then restarts the instance. </li></ul>
  61. 61. Thank You! Email: [email_address] IRC: #pyramid, #pylons
  62. 62. Goodbye!