Fabric 代码发布
基礎(chǔ)使用
fabric的典型使用方式就是,創(chuàng)建一個(gè)Python文件,該文件包含一到多個(gè)函數(shù),然后使用fab命令調(diào)用這些函數(shù)。這些函數(shù)在fabric中成為task,下面是一個(gè)例子。
from fabric.api import *env.hosts = ['10.0.1.15', '10.0.1.16', '10.0.1.17'] env.port = 22 env.user = 'root' env.password = '123456'def get_host_name():run('hostname')def get_local_file(path='.'):run('ls %s' % (path))def get_tail(path='/etc/passwd', linne=10):run('tail -n %s %s' % (linne, path))def hello():with settings(hide('everything'), warn_only=True):result = run('netstat -lntup|grep -w 25')print(result)print(result.return_code)print(result.failed)命令行執(zhí)行結(jié)果
獲取列表
(play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f1.py --list Available commands:get_host_nameget_local_fileget_tailhello 獲取列表執(zhí)行函數(shù)
--
(play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f1.py --list Available commands:get_host_nameget_local_fileget_tailhello(play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f1.py get_host_name [10.0.1.15] Executing task 'get_host_name' [10.0.1.15] run: hostname [10.0.1.15] out: cs7-1 [10.0.1.15] out:[10.0.1.16] Executing task 'get_host_name' [10.0.1.16] run: hostname [10.0.1.16] out: cs7-2 [10.0.1.16] out:[10.0.1.17] Executing task 'get_host_name' [10.0.1.17] run: hostname [10.0.1.17] out: cs7-3 [10.0.1.17] out:Done. Disconnecting from 10.0.1.15... done. Disconnecting from 10.0.1.16... done. Disconnecting from 10.0.1.17... done. fab -f f1.py get_host_name-
(play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f1.py hello [10.0.1.15] Executing task 'hello' /bin/bash: netstat: command not found 1 True [10.0.1.16] Executing task 'hello' /bin/bash: netstat: command not found 1 True [10.0.1.17] Executing task 'hello' /bin/bash: netstat: command not found 1 TrueDone. Disconnecting from 10.0.1.15... done. Disconnecting from 10.0.1.16... done. Disconnecting from 10.0.1.17... done. fab -f f1.py hello (play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f1.py get_local_file:/ [10.0.1.15] Executing task 'get_local_file' [10.0.1.15] run: ls / [10.0.1.15] out: bin dev home lib64 mnt proc run srv sys usr var [10.0.1.15] out: boot etc lib media opt root sbin swapfile tmp vagrant [10.0.1.15] out:[10.0.1.16] Executing task 'get_local_file' [10.0.1.16] run: ls / [10.0.1.16] out: bin dev home lib64 mnt proc run srv sys usr var [10.0.1.16] out: boot etc lib media opt root sbin swapfile tmp vagrant [10.0.1.16] out:[10.0.1.17] Executing task 'get_local_file' [10.0.1.17] run: ls / [10.0.1.17] out: bin dev home lib64 mnt proc run srv sys usr var [10.0.1.17] out: boot etc lib media opt root sbin swapfile tmp vagrant [10.0.1.17] out:Done. Disconnecting from 10.0.1.15... done. Disconnecting from 10.0.1.16... done. Disconnecting from 10.0.1.17... done. 多個(gè)參數(shù)情況需要注意的是:
- 一次可以多個(gè)task,按照順序執(zhí)行: fab -f f1.py get_host_name get_local_file
- 給task傳遞參數(shù)使用task:參數(shù),多個(gè)參數(shù)按照位置進(jìn)行傳遞(和Python相同,對(duì)于關(guān)鍵字的參數(shù)可以,在命令行中指定:fab get_local_file:path=/home)
fabric的命令行參數(shù)
fab命令作為fabric程序的入口提供了,豐富的參數(shù)調(diào)用.
-l:查看task列表
-f:指定fab的入口文件,默認(rèn)是fabfile.py
-g:指定網(wǎng)管設(shè)備,比如堡壘機(jī)環(huán)境下,填寫(xiě)堡壘機(jī)的IP
-H:在命令行指定目標(biāo)服務(wù)器,用逗號(hào)分隔多個(gè)服務(wù)器
-P:以并行方式運(yùn)行任務(wù),默認(rèn)為串行
-R:以角色區(qū)分不同的服務(wù)
-t:連接超時(shí)的時(shí)間,以秒為單位
-w:命令執(zhí)行失敗時(shí)的警告,默認(rèn)是終止任務(wù)
-- Fabric參數(shù),其他包含fabric腳本的中的參數(shù)的快捷操作,比如--user,--port,或者直接跟要執(zhí)行的Linux命令
如下例子,不寫(xiě)一行代碼獲取所有主機(jī)的ip地址
(play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test>fab -H 10.0.1.16 --port 22 --user=root --password=123456 -- ip a [10.0.1.16] Executing task '<remainder>' [10.0.1.16] run: ip a [10.0.1.16] out: 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 [10.0.1.16] out: link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 [10.0.1.16] out: inet 127.0.0.1/8 scope host lo [10.0.1.16] out: valid_lft forever preferred_lft forever [10.0.1.16] out: inet6 ::1/128 scope host [10.0.1.16] out: valid_lft forever preferred_lft forever [10.0.1.16] out: 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 [10.0.1.16] out: link/ether 52:54:00:75:dc:3d brd ff:ff:ff:ff:ff:ff [10.0.1.16] out: inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0 [10.0.1.16] out: valid_lft 76820sec preferred_lft 76820sec [10.0.1.16] out: inet6 fe80::5054:ff:fe75:dc3d/64 scope link [10.0.1.16] out: valid_lft forever preferred_lft forever [10.0.1.16] out: 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 [10.0.1.16] out: link/ether 08:00:27:3e:37:98 brd ff:ff:ff:ff:ff:ff [10.0.1.16] out: inet 10.0.1.16/24 brd 10.0.1.255 scope global noprefixroute eth1 [10.0.1.16] out: valid_lft forever preferred_lft forever [10.0.1.16] out: inet6 fe80::a00:27ff:fe3e:3798/64 scope link [10.0.1.16] out: valid_lft forever preferred_lft forever [10.0.1.16] out:常用的對(duì)象和方法介紹
介紹fabric中的env對(duì)象,以及其他的比如執(zhí)行命令模塊,上傳文件等。
fabric中的env
env是一個(gè)全局唯一的字典,保存了Fabric所有的配置,在Fabric的實(shí)現(xiàn)中,他是一個(gè)_AttributeDict()對(duì)象,之所以封裝成_AttributeDict()對(duì)象,是覆蓋了__getattr__和__setattr__,使我們可以使用“對(duì)象.屬性=值”的方式,操作字典。
我們可以通過(guò)源碼的方式,查看env的配置參數(shù),或者使用如下方式查看:
import json from fabric.api import envprint(type(env)) print(json.dumps(env,indent=4)) <class 'fabric.utils._AttributeDict'> {"abort_exception": null,"again_prompt": "Sorry, try again.","all_hosts": [],"combine_stderr": true,"colorize_errors": false,"command": null,"command_prefixes": [],"cwd": "","dedupe_hosts": true,"default_port": "22","eagerly_disconnect": false,"echo_stdin": true,"effective_roles": [],"exclude_hosts": [],"gateway": null,"gss_auth": null,"gss_deleg": null,"gss_kex": null,"host": null,"host_string": null,"lcwd": "","local_user": "CMSZ","output_prefix": true,"passwords": {},"path": "","path_behavior": "append","port": "22","real_fabfile": null,"remote_interrupt": null,"roles": [],"roledefs": {},"shell_env": {},"skip_bad_hosts": false,"skip_unknown_tasks": false,"ssh_config_path": "C:\\Users\\CMSZ\\.ssh\\config","sudo_passwords": {},"ok_ret_codes": [0],"sudo_prefix": "sudo -S -p '%(sudo_prompt)s' ","sudo_prompt": "sudo password:","sudo_user": null,"tasks": [],"prompts": {},"use_exceptions_for": {"network": false},"use_shell": true,"use_ssh_config": false,"user": "CMSZ","version": "1.14.post1","no_agent": false,"forward_agent": false,"abort_on_prompts": false,"rcfile": "C:\\Users\\CMSZ/.fabricrc","disable_known_hosts": false,"fabfile": "fabfile","hide": ["NO","DEFAULT"],"hosts": [],"key_filename": null,"no_keys": false,"keepalive": 0,"linewise": false,"connection_attempts": 1,"always_use_pty": true,"password": null,"parallel": false,"reject_unknown_hosts": false,"sudo_password": null,"system_known_hosts": null,"shell": "/bin/bash -l -c","show": ["NO","DEFAULT"],"timeout": 10,"command_timeout": null,"warn_only": false,"pool_size": 0 }Process finished with exit code 0 輸出結(jié)果常用的env配置如下:
- env.hosts:定義目標(biāo)服務(wù)器列表
- env.exclude_hosts:排除特定的服務(wù)器
- env.user SSH:到遠(yuǎn)程服務(wù)器的用戶名
- env.port:遠(yuǎn)程服務(wù)器的端口號(hào)
- env.key_filename:私鑰文件的位置
- env.password SSH:到遠(yuǎn)程服務(wù)器的密碼
針對(duì)不同主機(jī)不同密碼的情況,可以使用如下的方式:
env.hosts = ['root@192.168.10.201:22','root@192.168.10.202:22','root@192.168.10.203:22' ] env.passwords = {'root@192.168.10.201:22':'123456201','root@192.168.10.202:22':'123456202','root@192.168.10.203:22':'123456203' }fabric提供的命令
run():在遠(yuǎn)程服務(wù)器上執(zhí)行Linux命令,還有一個(gè)重要的參數(shù)pty,如果我們執(zhí)行命令以后需要有一個(gè)常駐的服務(wù)進(jìn)程,那么就需要設(shè)置pty=False,避免因?yàn)镕abric退出導(dǎo)致進(jìn)程的退出
run('service mysqld start',pty=False)PS:執(zhí)行完畢會(huì)返回輸出的信息,我們可以定義變量接受,同時(shí)這個(gè)返回信息有一個(gè)方法return_code,當(dāng)返回的是正確執(zhí)行的結(jié)果時(shí)code為0,否則不為0
def hello():with settings(hide('everything'),warn_only=True): # 關(guān)閉顯示result = run('anetstat -lntup|grep -w 25')print(result) # 命令執(zhí)行的結(jié)果print(result.return_code) # 返回碼,0表示正確執(zhí)行,1表示錯(cuò)誤輸出
(play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f1.py hello [10.0.1.15] Executing task 'hello' /bin/bash: netstat: command not found 1 Truesudo():與run類似,使用管理員權(quán)限在遠(yuǎn)程服務(wù)器上執(zhí)行shell命令,還有一個(gè)重要的參數(shù)pty,如果我們執(zhí)行命令以后需要有一個(gè)常駐的服務(wù)進(jìn)程,那么就需要設(shè)置pty=False,避免因?yàn)镕abric退出導(dǎo)致進(jìn)程的退出。
local():用以執(zhí)行本地命令,返回要執(zhí)行的命令,local是對(duì)Python的Subprocess模塊的封裝,更負(fù)載的功能可以直接使用Subprocess模塊,包含capture參數(shù),默認(rèn)為False,表示subprocess輸出的信息進(jìn)行顯示,如果不想顯示,那么指定capture=True即可
def test():result = local('make test',capture=True)print(result)print(result.failed)print(result.succeeded)# 返回執(zhí)行的命令 # 如果執(zhí)行失敗那么 result.failed 為T(mén)rue # 如果執(zhí)行成功那么 result.succeeded 為T(mén)rueget():從遠(yuǎn)程服務(wù)器上獲取文件,通過(guò)remote_path參數(shù)聲明從何處下載,通過(guò)local_path表示下載到何處。remote_path支持通配符。
get(remote_path='/etc/passwd',local_path='/tmp/passwd')put():將本地的文件上傳到遠(yuǎn)程服務(wù)器,參數(shù)與get相似,此外,還可以通過(guò)mode參數(shù)執(zhí)行遠(yuǎn)程文件的權(quán)限配置。
get(remote_path='/etc/passwd',local_path='/tmp/passwd')reboot():重啟遠(yuǎn)程服務(wù)器,可以通過(guò)wait參數(shù)設(shè)置等待幾秒鐘重啟
reboot(wait=30)propmt():用以在Fabric執(zhí)行任務(wù)的過(guò)程中與管理員進(jìn)行交互,類似于python的input
key = prompt('Please specify process nice level:',key='nice',validate=int) # 會(huì)返回采集到的keyfabric的上下文管理器
env中存儲(chǔ)的是全局配置,有時(shí)候我們并不希望修改全局配置參數(shù),只希望臨時(shí)修改部分配置,例如:修改當(dāng)前工作目錄,修改日志輸出級(jí)別等。
在fabric中我們可以通過(guò)上下文管理器臨時(shí)修改參數(shù)配置,而不會(huì)影響全局配置。當(dāng)程序進(jìn)入上下文管理器的作用域時(shí),臨時(shí)修改就會(huì)起作用;當(dāng)程序離開(kāi)上下文管理器時(shí),臨時(shí)修改就會(huì)消失。
cd():切換遠(yuǎn)程目錄
def change(dir='/tmp'):with cd(dir):run('pwd') # /tmprun('pwd') # /rootlcd():切換本地目錄
path():配置遠(yuǎn)程服務(wù)器PATH環(huán)境變量,只對(duì)當(dāng)前會(huì)話有效,不會(huì)影響遠(yuǎn)程服務(wù)器的其他操作,path的修改支持多種模式
- append:默認(rèn)行為,將給定的路徑添加到PATH后面。
- prepend:將給定的路徑添加到PATH的前面。
- replace:替換當(dāng)前環(huán)境的PATH變量。
prefix():前綴,它接受一個(gè)命令作為參數(shù),表示在其內(nèi)部執(zhí)行的代碼塊,都要先執(zhí)行prefix的命令參數(shù)。
def testprefix():with cd('/tmp'):with prefix('echo 123'):run('echo 456')run('echo 789')# 轉(zhuǎn)換為L(zhǎng)inux命令為: cd /tmp && echo '123' && echo '456' cd /tmp && echo '123' && echo '789'shell_env():設(shè)置shell腳本的環(huán)境變量
def setenv():with shell_env(HTTP_PROXY='1.1.1.1'):run('echo $HTTP_PROXY')run('echo $HTTP_PROXY')# 等同于shell中的export export HTTP_PROXY='1.1.1.1'settings():通用配置,用于臨時(shí)覆蓋env變量
def who():with settings(user='dev'): # 臨時(shí)修改用戶名為devrun('who')run('who')remote_tunnel():通過(guò)SSH的端口轉(zhuǎn)發(fā)建立的鏈接
with remote_tunnel(3306):run('mysql -uroot -p password')hide():用于隱藏指定類型的輸出信息,hide定義的可選類型有7種
- status:狀態(tài)信息,如服務(wù)器斷開(kāi)鏈接,用戶使用ctrl+C等,如果Fabric順利執(zhí)行,不會(huì)有狀態(tài)信息
- aborts:終止信息,一般將fabric當(dāng)作庫(kù)使用的時(shí)候需要關(guān)閉
- warnings:警告信息,如grep的字符串不在文件中
- running:fabric運(yùn)行過(guò)程中的數(shù)據(jù)
- stdout:執(zhí)行shell命令的標(biāo)準(zhǔn)輸出
- stderr:執(zhí)行shell命令的錯(cuò)誤輸出
- user:用戶輸出,類似于Python中的print函數(shù)
為了方便使用,fabric對(duì)以上其中類型做了進(jìn)一步的封裝
- output:包含stdout,stderr
- everything:包含stdout,stderr,warnings,running,user
- commands:包含stdout,running
show():與hide相反,表示顯示指定類型的輸出
def hello():with settings(show('everything'), warn_only=True):result = run('ss -lntup|grep')print('1=' + result)print('2=' + str(result.return_code))print('3=' + str(result.failed)) (play-test) D:\Users\CMSZ\Desktop\2020-Python\play-test\fabric>fab -f f2.py hello [10.0.1.15] Executing task 'hello' [10.0.1.15] run: ss -lntup|grep [10.0.1.15] out: Usage: grep [OPTION]... PATTERN [FILE]... [10.0.1.15] out: Try 'grep --help' for more information. [10.0.1.15] out:Warning: run() received nonzero return code 2 while executing 'ss -lntup|grep'!NoneType: None1=Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. 2=2 3=True 結(jié)果quiet():隱藏全部輸出,僅在執(zhí)行錯(cuò)誤的時(shí)候發(fā)出告警信息,功能等同于 with settings(hide('everything'),warn_only=True) .
# 比如創(chuàng)建目錄的時(shí)候,如果目錄存在,默認(rèn)情況下Fabric會(huì)報(bào)錯(cuò)退出,我們是允許這種錯(cuò)誤的,所以針對(duì)這種錯(cuò)誤,我們進(jìn)行如下設(shè)置,使fabric只打出告警信息而不會(huì)中斷執(zhí)行。 with settings(warn_only=True)裝飾器
Fabric提供的命令一般都是執(zhí)行某一個(gè)具體的操作,提供的上下文管理器一般都是用于臨時(shí)修改配置參數(shù),而fabric提供的裝飾器,既不是執(zhí)行具體的操作,也不是修改參數(shù),而是控制如何執(zhí)行這些操作,在那些服務(wù)器上執(zhí)行這些操作,fabric的裝飾器與人物執(zhí)行緊密相關(guān)。下面從幾個(gè)方面來(lái)進(jìn)行說(shuō)明
- hosts:定制執(zhí)行task的服務(wù)器列表
- roles:定義執(zhí)行task的role列表
- parallel:并行執(zhí)行task
- serial:串行執(zhí)行task
- task:定義一個(gè)task
- runs_once:該task只執(zhí)行一次
fabric的task
task就是fabric需要在遠(yuǎn)程服務(wù)器上執(zhí)行的函數(shù),在fabric中有3中方法定義一個(gè)task
PS:默認(rèn)情況下,fabfile中的所有函數(shù)對(duì)象都是一個(gè)task,但是如果我們使用了task裝飾器,顯示的定義了一個(gè)task,那么,其他沒(méi)有通過(guò)task裝飾器裝飾的函數(shù)將不會(huì)被認(rèn)為是一個(gè)task。
fabric的host
為了方便我們的使用,fabric提供了非常靈活的方式指定對(duì)哪些遠(yuǎn)程服務(wù)器執(zhí)行操作,根據(jù)我們前面的知識(shí),我們知道有兩種方式:通過(guò)env.hosts來(lái)執(zhí)行,或者在fab執(zhí)行命令的時(shí)候使用-H參數(shù),除此之外,還有以下需要注意的地方
fabric的role
role是對(duì)服務(wù)器進(jìn)行分類的手段,通過(guò)role可以定義服務(wù)器的角色,以便對(duì)不同的服務(wù)器執(zhí)行不同的操作,Role邏輯上將服務(wù)器進(jìn)行了分類,分類以后,我們可以對(duì)某一類服務(wù)器指定一個(gè)role名即可。進(jìn)行task任務(wù)時(shí),對(duì)role進(jìn)行控制。
# role在env.roledefs中進(jìn)行定義 env.roledefs = {'web':['root@192.168.10.201','192.168.10.202'] # role名稱為:web'db':['root@192.168.10.203',] # role名稱為:db }當(dāng)我們定義好role以后,我們就可以通過(guò)roles裝飾器來(lái)指定在哪些role上運(yùn)行task。 from fabric.api import *env.roledefs = {'web':['root@192.168.10.201:22','root@192.168.10.202:22',],'db':['root@192.168.10.203:22',] } env.passwords = {'root@192.168.10.201:22':'123456201','root@192.168.10.202:22':'123456202','root@192.168.10.203:22':'123456203' }@roles('db') # 只對(duì)role為db的主機(jī)進(jìn)行操作 @task def hello():run('ifconfig br0')注意:hosts裝飾器可以和roles裝飾器一起使用(全集),看起來(lái)容易造成混亂,不建議混搭。
fabric的執(zhí)行模型
fabric執(zhí)行任務(wù)的步驟如下:
PS:關(guān)于并行模式:
其他裝飾器
前面介紹了task,hosts,roles和parallel裝飾器,此外還有兩個(gè)裝飾器比較常用
- runs_once:只執(zhí)行一次,防止task被多次調(diào)用。例如,對(duì)目錄打包進(jìn)行上傳,上傳動(dòng)作對(duì)不同的服務(wù)器可能會(huì)執(zhí)行多次,但是打包的動(dòng)作只需要執(zhí)行一次即可。
- serial:強(qiáng)制當(dāng)前task穿行執(zhí)行。使用該參數(shù)時(shí)優(yōu)先級(jí)最高,即便是制定了并發(fā)執(zhí)行的參數(shù)
常用的功能函數(shù)
fabric中還有其他的一些好用的函數(shù)
封裝task
fabric提供了一個(gè)execute函數(shù),用來(lái)對(duì)task進(jìn)行封裝。它最大的好處就是可以將一個(gè)大的任務(wù)拆解為很多小任務(wù),每個(gè)小任務(wù)互相獨(dú)立,互不干擾
from fabric.api import *env.roledefs = {'web':['root@192.168.10.201:22','root@192.168.10.202:22',],'db':['root@192.168.10.203:22',] } env.passwords = {'root@192.168.10.201:22':'123456201','root@192.168.10.202:22':'123456202','root@192.168.10.203:22':'123456203' }@roles('db') def hello():run('echo hello')@roles('web') def world():run('echo world')@task def helloworld():execute(hello)execute(world)# 函數(shù)helloworld作為入口,分別調(diào)用兩個(gè)task,對(duì)不同的主機(jī)進(jìn)行操作
utils函數(shù)
包含一些輔助行的功能函數(shù),這些函數(shù)位于fabric.utils下,常用的函數(shù)如下:
帶顏色的輸出
fabric為了讓輸出日志更具有可讀性,對(duì)命令行中斷的顏色輸出進(jìn)行了封裝,使用print打印帶有不同顏色的文本,這些顏色包含在fabric.colors中。像warn,puts打印輸出的,也可以直接渲染顏色
- blue(text,blod=False)? 藍(lán)色
- cyan(text,blod=False)? 淡藍(lán)色
- green(text,blod=False)? 綠色
- magenta(text,blod=False)? 紫色
- red(text,blod=False)? 紅色
- white(text,blod=False)? 白色
- yellow(text,blod=False)?? 黃色
確認(rèn)信息
有時(shí)候我們?cè)谀骋徊綀?zhí)行錯(cuò)誤,會(huì)給用戶提示,是否繼續(xù)執(zhí)行時(shí),confirm就非常有用了,它包含在 fabric.contrib.console中
def testconfirm():result = confirm('Continue Anyway?')print(result)# 會(huì)提示輸入y/n # y 時(shí) result為T(mén)rue # n 時(shí) result為False使用Fabric源碼安裝redis
下載一個(gè)redis的包和fabfile.py放在同級(jí)目錄即可,不同目錄需要修改包的位置,這里使用的是redis-4.0.9版本。
#!/usr/bin/env python3 from fabric.api import * from fabric.contrib.console import confirm from fabric.utils import abort from fabric.colors import *env.hosts = ['192.168.10.202',] env.user = 'root' env.password = '123456202'@runs_once @task def test():with settings(warn_only=True):local('tar xf redis-4.0.9.tar.gz')with lcd('redis-4.0.9'):result = local('make test',capture=True)if result.failed and not confirm('Test is Faild Continue Anyway?'):abort('Aborting at user request.')with lcd('redis-4.0.9'):local("make clean")local('tar zcvf redis-4.0.10.tar.gz redis-4.0.9')@task def deploy():put('redis-4.0.10.tar.gz','/tmp/')with cd('/tmp'):run('tar xf redis-4.0.10.tar.gz')with cd('redis-4.0.9'):sudo('make install')@task def start_redis():with settings(warn_only=True):result = run('netstat -lntup | grep -w redis-server')if result.return_code == 0:print(green('redis is started!'))else:run('set -m ; /usr/local/bin/redis-server &') # 用pty=False, fabric進(jìn)程退不出來(lái),不知道為啥,所以這里用set -mprint(green('redis start Successful'))@task def clean_local_file():local('rm -rf redis-4.0.10.tar.gz')@task def clean_file():with cd('/tmp'):sudo('rm -rf redis-4.0.9')sudo('rm -rf redis-4.0.10.tar.gz')@task def install():execute(test)execute(deploy)execute(clean_file)execute(clean_local_file)<br><code class="python spaces hljs"> </code><code class="python plain hljs">execute(start_redis)</code>1 2 3 PS:關(guān)于set -m 的作用如下:"set -m" turns on job control, you can run processes in a separate process group.<br>理解:在一個(gè)獨(dú)立的進(jìn)程組里面運(yùn)行我們的進(jìn)程。
參考鏈接
https://www.cnblogs.com/xiao-apple36/p/9124292.html#_label6_5
總結(jié)
以上是生活随笔為你收集整理的Fabric 代码发布的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 博客园文章目录结构
- 下一篇: 4 Oracle 操作表中数据