2013年2月7日 星期四

Fabric: 以python 寫的大量deployment tool

原始網站:http://docs.fabfile.org/en/1.5/

屬於python 一個module,安裝時可以透過easy_install 安裝

 

安裝

#easy_install fabric

 

####Example 1

#使用時,只要編寫一個檔案名稱叫做fabfile.py

#######################

from fabric.api import run

def host_type():

   run('uname -a')

######################

 

使用時,在console上使用命令列,fabric即會把你把指令送到要執行的server 位置

#fab -H xxx.xxx.xx   host_type

 

 

####Example 2

夠過env 執行指令在不同的環境上

##指令執行如下

#! fab sit rpm_query

#!fab dev rpm_query

 

### Example 3

#可以將要送出的command 直接使用 -- 'cat /etc/*-release'送出即可

fab -H 192.168.1.1  -- 'cat /etc/*-release'

##fabfile.py

from fabric.api import run,env

## disable ssh known hosts env.disable_known_hosts=True

env.user = 'root'

## For differrent environment

def sit(): wf=WFServer(DIR='/root/dbinst.pdg') print wf.get_all_ip_list() env.hosts=wf.get_all_ip_list()

def dev(): env.hosts=['xxx.xxx.xxx.xxx] def staging(): env.hosts=['xxx.xxx.xxx.xxx']

def host_type(): run('uname -a')

def rpm_query(): run('rpm -qa --queryformat "%{NAME} %{EPOCH}:%{VERSION} %{RELEASE} %{ARCH}\n" 2>/dev/null | sed -e \'s/(none)/0/g\'')

 

### Example 4:

如果要給於變數,使用方法為

意思是把variable 當parameter 傳入build 這個method

#!fab build:[variable]
 或指定變數名稱
#!fab build:name=value

進階篇

如果想在所有目錄執行fab command ,請修改下列的範例

把檔案存到 vim ~/.fabricrc

<code>

# Copy this file to ~/ and rename it .fabricrc
# See http://docs.fabfile.org/en/1.3.3/usage/fab.html
# and http://docs.fabfile.org/en/1.3.3/usage/env.html for more documentation.
 
# The location of the fabfile so fab can find it anywhere (fabfiles are usually
# stored in the relation to a "project").
fabfile=~/bin/goodold-bin/fabric/fabfile.py
 
# The directory where projects are stored. Defaults to $PROJECTS_DIR if set in
# your bash profile or "~/Projects" if nothing is set.
# projects_dir=~/Sites
 
# Set TLD used in project directory name. Set to empty if you don't use a TLD
# in the directory name. Defaults to your username.
# local_tld=
 
# Local MYSQL info. Used in tasks where this can't be parsed from the project.
# local_db_user=root
# local_db_password=pass

 

</code>

 

 

Reference:

http://awaseroot.wordpress.com/category/fabric/

http://www.pythonforbeginners.com/systems-programming/how-to-use-fabric-in-python/

 

 

 

沒有留言:

如何下載Facebook 相簿跟影片

影片:透過chrome plug-in : Video Downloader for Facebook<sup>TM</sup> 相簿: 透過chrome plug-in : Tampermonkey 搭配script : Facebo...