fabric自动部署

Fabric commands

run - run a command on a remote host

sudo - run a sudoed command on a remote host

local - run a command on the local host

get/put - copy a file from/to a remote host

prompt - ask the user for information*

 

Execute commands

get output and return code

output = run("command")

 

扫描二维码关注公众号,回复: 1265769 查看本文章

chain commands

run("workon project && git pull origin master")

 

start pseudo-daemons

run("screen -d -m not-a-daemon")

 

any return code except() is treated as an error and an exception is

thrown(unless warn_only is set)

 

Move files

Put a file(upload)

put("local-file", "remote-file", mode=0755)

 

Get a file(download):

get("remote-file", "local-file")

 

if a problem occurs while uploading or downloading files, fabric throws an exception

 

Ask questions

Validation

relase_name = prompt("What do you want to name the new release?: ", validate="^[a-zA-Z0-9] + $")

proxy_port_number = prompt("Proxy port: ", validate=int)

 

Default

username = prompt("Username: ", default="jack")

 

Keeping state(sort of)

Directories

with cd("xmpp/"):

 run("git clone git://git/something.git")

 run("mkvirtualenv --no-site-packages somthing")

 

For those running from trunk 

with prefix("workon something"):

 run("pip install xx>=1.1")

 

Contrib

Append text to files

from fabric.contrib.files import append

append(" DATABASE_NAME = 'hello.db' ", "local_settings.py")

 

Search and replace

Use fabric's sed function to modify content of files

from fabric.contrib.files import sed

sed("local_settings.py", "^DEBUG = True$", "DEBUG = False")

 

Fabric decorators

hosts 定义那些host或hosts可以执行

roles 定义一个角色名称的list,用于查看host lists

runs_once 确保方法只运行一次

Example

from fabric.api import run

env.hosts = [localhost]

env.user = 'djangodev'

 

def install_django():

 run('workon myproject && pip install diango')

 

$fab install_django

 

ps:

rails的自动部署框架Capistrano

http://gist.github.com/472566

1. 修正bugs和实现新的特征

2. git存档

3. 把文件从开发环境分期移到生产环境

4. 安装更新,重复使用apps,pypi,packages

5. 运行备份

6. 保持跟踪本地设置

7. 清空和预热缓存

8. 迁移数据库

 

推荐

http://www.slideshare.net/


猜你喜欢

转载自zhou-xingbo.iteye.com/blog/948479