Fabric: A Capistrano Alternative


Published on

Fabric: A lightweight deployment tool for Rubyists and Pythonists alike

Published in: Technology

Fabric: A Capistrano Alternative

  1. 1. Fabric - A Lightweight Deployment Tool for Rubyists and Pythonists Alike <ul><li>Mike Leone - Panoptic Development, Inc </li></ul>
  2. 2. Why Fabric? <ul><li>Capistrano is elegant, powerful, extensible
  3. 3. but...
  4. 4. Too many features for small, simple projects
  5. 5. If you're using a DSL, the implementation language isn't so important
  6. 6. If you know Ruby, you already know enough Python for a simple fabric script! </li></ul>
  7. 7. <ul><li>Can you teach a Rubyist how Capistrano works in 3 minutes? </li></ul><ul><li>Probably not!
  8. 8. Makes assumptions about your version control system
  9. 9. Release directory structure
  10. 10. The way your server restarts
  11. 11. In typical Ruby fashion, there's lots of ”magic” </li></ul>
  12. 12. Fabric is two things <ul><li>API for streamlining the use of SSH for app deployment or sys admin tasks
  13. 13. Command-line interface for calling Python methods </li></ul>
  14. 14. Example $ fab free_space
  15. 15. Write a deploy script in 3 minutes <ul><li>We want a script that will:
  16. 16. test our code
  17. 17. deploy it to our server in the /mnt directory
  18. 18. restart the web server </li></ul>
  19. 19. Install Fabric <ul>$ sudo apt-get install python-setuptools $ sudo easy_install fabric </ul>
  20. 20. Add Fabric to your Ruby App <ul>$ cd my/awsome/rails_app $ touch fabfile.py </ul>
  21. 21. Three Lines of Boilerplate <ul>from fabric.api import * env.hosts = ['myserver.com'] env.user = ”mike” </ul>
  22. 22. Run the tests <ul>def test(): local('rake spec') <li>If tests fail, deployment will stop </li></ul>
  23. 23. Send your App Code Over <ul>def pack_code(): local('tar czf /tmp/latest.tgz') def upload_code(): put('/tmp/latest.tgz', '/tmp/') with cd('/mnt/rails_app'): run('tar xzf /tmp/latest.tgz') </ul>
  24. 24. Restart the Web Server <ul>def restart(): sudo('/etc/init.d/apache2 restart') </ul>
  25. 25. Put it all Together <ul>def deploy(): test() pack_code() upload_code() restart() </ul>
  26. 26. Your new Deploy Script
  27. 27. Deploy! <ul>$ fab deploy </ul>
  28. 28. Deploy your Release Branch <ul>$ git checkout my-release-branch $ fab deploy </ul>
  29. 29. Advantages <ul><li>Don't have to include branch, repository info
  30. 30. Use any release directory structure you want
  31. 31. Someone knowing nothing about deployment tools can look at your script and understand exactly what's going on </li></ul>
  32. 32. Further Reading <ul><li>Fabric documentation:
  33. 33. http://docs.fabfile.org/0.9.1/
  34. 34. My Company Website and Blog: </li><ul><li>http://panopticdev.com </li></ul></ul>