fabric-让部署变得简单
TRANSCRIPT
![Page 2: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/2.jpg)
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比capistrano更简单易学习,不需要有py经验不依赖版本控制系统
Sunday, August 29, 2010
![Page 3: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/3.jpg)
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
![Page 4: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/4.jpg)
Install
sudo easy_install fabric
Sunday, August 29, 2010
![Page 5: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/5.jpg)
Examplevim fabfile.py
#!/usr/bin/env pythonfrom 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
![Page 6: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/6.jpg)
Deploy my rails appErics-MacBook:~ eric$ cd NetBeans_Workspace/lxnengErics-MacBook:lxneng eric$ touch fabfile.pyErics-MacBook:lxneng eric$ vim fabfile.py#!/usr/bin/env python# encoding: utf-8
from fabric.api import env, run, cd, local, put
env.hosts = ['192.168.1.5']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
![Page 7: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/7.jpg)
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
![Page 8: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/8.jpg)
config & restartdef 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
![Page 9: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/9.jpg)
Deploydef deploy(): pack_code() upload_code() conf() restart()
Sunday, August 29, 2010
![Page 10: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/10.jpg)
define rolesenv.roledefs = {
‘testing’: [‘192.168.1.5’],‘staging’: [‘.........’],‘production’: [‘......’, ‘........’],‘database’: [‘........’]
}
def testing():env.hosts = env.roledefs[‘testing’]env.user = ‘testuser’# ...
def deploy():# deploy code here
fab testing deploySunday, August 29, 2010
![Page 11: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/11.jpg)
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
![Page 12: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/12.jpg)
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
![Page 13: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/13.jpg)
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
![Page 14: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/14.jpg)
如果你有很多的app要部署那么你可以写一个通用一点的fabfile我的fabfile: http://gist.github.com/472566
Sunday, August 29, 2010
![Page 15: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/15.jpg)
Reference
Fabric documentation http://fabfile.org/
Pythonic Deployment with Fabric 0.9
Fabric: A Capistrano Alternative
Sunday, August 29, 2010
![Page 16: Fabric-让部署变得简单](https://reader036.vdocuments.pub/reader036/viewer/2022081721/5552f4e0b4c90587048b4d0a/html5/thumbnails/16.jpg)
Q & A
Thank You !
Sunday, August 29, 2010