Fabric
fabric允許通過命令行執(zhí)行任意Python函數(shù),是用來在SSH和python上執(zhí)行shell命令的子程序庫值戳。
工具fab導(dǎo)入fabfile.py并執(zhí)行指定的Python函數(shù)产弹。如
# filename:fabfile.py
def hello():
print("Hello world!")
命令行執(zhí)行語句:
$ fab hello
輸出:hello world!
執(zhí)行帶參數(shù)Python函數(shù):
# filename:fabfile.py
def hello(name="world"):
print("Hello %s!" % name)
$ fab hello:name=Jeff
輸出:Hello Jeff!
定義連接
# env.host確定要連接的遠(yuǎn)程服務(wù)器
# filename:fabfile.py
env.hosts = ['my_server_ip']
env.user = 'my_server_name'
env.sudo_user = 'root'
def test():
do_test_stuff()
fabric APIs
-
local(string of command)
在本地執(zhí)行shell命令,命令以字符串的形式作為參數(shù)荐捻,如:
# filename:fabfile.py
# 執(zhí)行命令行,添加浩螺、提交git倉庫的修改靴患,并push到遠(yuǎn)程倉庫
from fabric.api import local
def prepare_deploy():
local("git add -p && git commit")
local("git push")
-
run(string of command)
在遠(yuǎn)程服務(wù)器上'my_server'執(zhí)行shell命令,命令以字符串的形式作為參數(shù)要出,如:
# filename:fabfile.py
# 在遠(yuǎn)程服務(wù)器上執(zhí)行命令行鸳君,進(jìn)入目錄code_dir,git獲取倉庫
from fabric.api import local
def deploy():
code_dir = '/srv/django/myproject'
with cd(code_dir):
run("git pull")
-
lcd(string of directory)
在本地執(zhí)行目錄切換操作患蹂,路徑以字符串形式作為參數(shù)
-
cd(string of directory)
在遠(yuǎn)程服務(wù)器'my_server'上執(zhí)行目錄切換操作或颊,路徑以字符串形式作為參數(shù)
-
put(file, remote_directory)
把本地文件file上傳到遠(yuǎn)程服務(wù)器的remote_directory目錄下