fabric-让部署变得简单

16
Fabric ——部署简单 Lo小能(Eric Lo) http://lxneng.com [email protected] twitter: @lxneng Sunday, August 29, 2010

Upload: eric-lo

Post on 13-May-2015

2.992 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Fabric-让部署变得简单

Fabric——让部署变得简单

Lo小能(Eric Lo)http://[email protected]: @lxneng

Sunday, August 29, 2010

Page 2: Fabric-让部署变得简单

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-让部署变得简单

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-让部署变得简单

Install

sudo easy_install fabric

Sunday, August 29, 2010

Page 5: Fabric-让部署变得简单

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-让部署变得简单

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-让部署变得简单

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-让部署变得简单

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-让部署变得简单

Deploydef deploy():    pack_code()    upload_code()    conf()    restart()

Sunday, August 29, 2010

Page 10: Fabric-让部署变得简单

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-让部署变得简单

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-让部署变得简单

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-让部署变得简单

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-让部署变得简单

如果你有很多的app要部署那么你可以写一个通用一点的fabfile我的fabfile: http://gist.github.com/472566

Sunday, August 29, 2010

Page 16: Fabric-让部署变得简单

Q & A

Thank You !

Sunday, August 29, 2010