fabric-让部署变得简单

Post on 13-May-2015

2.992 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Fabric——让部署变得简单

Lo小能(Eric Lo)http://lxneng.comlxneng@gmail.comtwitter: @lxneng

Sunday, August 29, 2010

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

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

Install

sudo easy_install fabric

Sunday, August 29, 2010

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

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

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

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

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

Sunday, August 29, 2010

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

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

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

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

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

Sunday, August 29, 2010

Q & A

Thank You !

Sunday, August 29, 2010

top related