Pythonic Deployment with Fabric 0.9


Published on

The slides from my July Django-District presentation. It shows some of the basics of using the new fabric. I have uploaded the example to slideshare as well.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Fabric allows for easy deployment for different environments. Not just for testing vs. production also for database vs. web.
  • Especially helpful if you need to perform the same tasks on multiple computers, as in a web server farm.
  • There has been a rather substantial change from the current release (0.1.1) and the pending release (0.9). I’ll be showing the pending release.
  • Fabric is just the fab command. It looks for a file named in the current directory, but you can specify a specific file. The contains one or more tasks, written in python, that are executed on one or more hosts.
  • The fabfile typically starts with some environment setup. The env variable is used for standard configs like roledefs and hosts, and anything else you need.
  • One way of handling multiple environments is to set up tasks that set the correct environment settings. Because fabric allows you to chain tasks, you can specify the appropriate environment task before the deployment task
  • A task is standard python, with some fabric-specific commands thrown in. In this example there is prompt, sudo, and use of the env variable. The task is run on every host specified.
  • These are the basic commands that fabric adds.
  • There are some contributed commands, in the contrib module that provide more functionality.
  • You can also decorate your tasks to specify a role or specific set of hosts.
  • In this example, you can see we are chaining the testing environment setup task in front of the clean task. We are passing the site parameter to the clean task. If necessary, it will prompt you for the password for sudo. It will attempt to use the same password on each host.
  • Pythonic Deployment with Fabric 0.9

    1. 1. Pythonic Deployment with Fabric 0.9 Corey Oordt The Washington Times @coordt
    2. 2. Easy Deployment for Different Environments Testing Staging Production
    3. 3. Easy Deployment to Multiple Computers Production 1 Production 2 Production 3
    4. 4. Installation of Fabric $ easy_install paramiko $ easy_install pycrypto $ easy_install snapshot/fabric-0.9b1.tar.gz
    5. 5. Fabric is just a command and a python script fab
    6. 6. The from fabric.api import * env.roledefs = { 'web': ['', '',], 'media': ['',], 'staging': ['',], 'testing': ['',], 'database': ['', '',], } env.root_path = '/var/websites'
    7. 7. Define Tasks to Setup Environments def testing(): env.hosts = env.roledefs['testing'] env.user = 'testuser' def staging(): env.hosts = env.roledefs['staging'] env.user = 'staginguser' def production(): env.hosts = env.roledefs['web'] env.user = 'produser'
    8. 8. Task Example def clean(site=None): """Remove .pyc files from a site.""" from os import path if not site: site = prompt('Please specify which site(s) to clean (a comma delimited list is accepted): ', validate=r'^[w-.,]+$') site_list = site.split(',') for item in site_list: path = path.join(env.root_path, item) sudo("find %s -name '*.pyc' -depth -exec rm {} ;" % path)
    9. 9. Fabric Commands • require: Make sure that certain environment variables are available. • prompt: Display a prompt to the user and store the input in the given variable. Validation is optional. • put: Upload files to the remote host(s). • get: Download a file from a remote host. • run: Run a shell command on the remote host(s). • sudo: Run a root privileged shell command command on the remote host(s). • local: Run a command locally.
    10. 10. Fabric Contrib Commands • rsync_project: Synchronize a remote directory with the current project directory via rsync. • upload_project: Upload the current project to a remote system, tar-gzipping during the move. • exists: Return True if given path exists. • first: Given one or more file paths, returns first one found, or None. • upload_template: Render and upload a template text file to a remote host. • sed: Run a search-and-replace on filename with given regex patterns. • comment/uncomment: Attempt to (un)comment out all lines in filename matching regex. • contains: Return True if filename contains text. • append: Append string (or list of strings) text to filename. • confirm: Ask user a yes/no question and return their response as True /False.
    11. 11. Fabric Decorators • hosts: Defines on which host or hosts to execute the wrapped function. • roles: Defines a list of role names, used to look up host lists. • runs_once: Prevents wrapped function from running more than once. @hosts('user1@host1', 'host2', 'user2@host3') @runs_once def my_func(): pass @roles('web') def my_other_func(): pass
    12. 12. Usage Example $ fab testing clean:site=basenews [] sudo: find /var/websites/basenews -name '*.pyc' -depth -exec rm {} ; Password for testuser@ Done. Disconnecting from done. $
    13. 13. Example Uses • Reload web server settings • Clean .pyc files • Check out new revision from a SCM repository • Install packages on servers • Apply patches • Restart caching server • Dump database and restore it locally
    14. 14. Let’s look at an example…
    15. 15. Thank You Corey Oordt The Washington Times @coordt