Published on

Published in: Technology
  • Be the first to comment

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

No notes for slide


  1. 1. Fabric Lo (Eric Lo) http://lxneng.com lxneng@gmail.com twitter: @lxneng Sunday, August 29, 2010
  2. 2. About Fabric is a Python library and command- line tool for streamlining the use of SSH for application deployment or systems administration tasks capistrano in python 单 学 经 统 Sunday, August 29, 2010
  3. 3. Fabric is two things Api for streamlining the use of ssh for app development or system management tasks Command-line interface for calling python methods Sunday, August 29, 2010
  4. 4. Install sudo easy_install fabric Sunday, August 29, 2010
  5. 5. Example vim fabfile.py #!/usr/bin/env python from fabric.api import env, run env.hosts = ['xxx.com'] env.user = 'eric' def du(): run("du -sh /home/eric") Erics-MacBook:~ eric$ fab du [xxx.com] run: du -sh /home/eric [xxx.com] out: 942M /home/eric Done. Disconnecting from xxx.com... done. Sunday, August 29, 2010
  6. 6. Deploy my rails app Erics-MacBook:~ eric$ cd NetBeans_Workspace/lxneng Erics-MacBook:lxneng eric$ touch fabfile.py Erics-MacBook:lxneng eric$ vim fabfile.py #!/usr/bin/env python # encoding: utf-8 from fabric.api import env, run, cd, local, put env.hosts = [''] env.user = 'eric' DEPLOY_PATH = '/var/www/' APP_NAME = 'lxneng' APP_PATH = '%s%s'%(DEPLOY_PATH, APP_NAME) DATABASE_CONF_PATH = '/srv/conf/database.yml' Sunday, August 29, 2010
  7. 7. pack & upload def pack_code():     local('tar czf /tmp/%s.tgz .'%APP_NAME) def upload_code():     put('/tmp/%s.tgz'%APP_NAME, '/tmp/')     with cd(APP_PATH):         run('tar xzf /tmp/%s.tgz'%APP_NAME) Sunday, August 29, 2010
  8. 8. config & restart def conf():     run("cp %s %s/config/"%(DATABASE_CONF_PATH, APP_PATH))     run("ln -nfs /srv/photos %s/public/photos"%APP_PATH)     with cd(APP_PATH):         run("rake db:migrate RAILS_ENV=production") def restart():     run("touch %s/tmp/restart.txt"%APP_PATH) Sunday, August 29, 2010
  9. 9. Deploy def deploy():     pack_code()     upload_code()     conf()     restart() Sunday, August 29, 2010
  10. 10. define roles env.roledefs = { ‘testing’: [‘’], ‘staging’: [‘.........’], ‘production’: [‘......’, ‘........’], ‘database’: [‘........’] } def testing(): env.hosts = env.roledefs[‘testing’] env.user = ‘testuser’ # ... def deploy(): # deploy code here fab testing deploy Sunday, August 29, 2010
  11. 11. 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. Sunday, August 29, 2010
  12. 12. 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. Sunday, August 29, 2010
  13. 13. 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 Sunday, August 29, 2010
  14. 14. app fabfile fabfile http://gist.github.com/472566 Sunday, August 29, 2010
  15. 15. Reference Fabric documentation http://fabfile.org/ Pythonic Deployment with Fabric 0.9 Fabric: A Capistrano Alternative Sunday, August 29, 2010
  16. 16. Q&A Thank You Sunday, August 29, 2010