近期接手越來越多的東西,發布和運維的工作相當機械,加上頻率還蠻高,導致時間浪費還是優點多。修復bug什麼的,測試,提交版本庫(2分鐘),ssh到測試環境pull部署(2分鐘),rsync到線上機器A,B,C,D,E(1分鐘),分別ssh到ABCDE五台機器,逐一重啟(8-10分鐘) = 13-15分鐘其中鬱悶的是,每次操作都是相同的,命令一樣,要命的是在多個機器上,很難在本機一個腳本搞定,主要時間都浪費在ssh,敲命令上了,寫成腳本,完全可以一鍵執行,花兩分鐘看下執行結果
直到,發現了fabric可以將自動化部署或者多機操作的命令固化到一個腳本裡和某些維運工具很像,用它主要是因為,簡單好用易上手,當然,shell各種命令組合起來也可以,上古神器和現代兵器的區別
環境配置
在本機和目標機器安裝對應套件(注意,都要有)
sudo easy_install fabric
目前是1.6版本(或者用pip install,一樣的)
安裝完後,可以查看是否安裝成功
[ken@~$] which fab /usr/local/bin/fab
裝完之後,可以安裝成功
裝完瀏覽下官方文件然後,可以動手了
hello world
def hello(): print("Hello world!")
命令行執行:
[ken@~/tmp/fab$] fab hello Hello world!
注意,這裡可以不用fabfile當檔名,但執行時需指定檔
[ken@~/tmp/fab$] mv fabfile.py test.py fabfile.py -> test.py [ken@~/tmp/fab$] fab hello Fatal error: Couldn't find any fabfiles! Remember that -f can be used to specify fabfile path, and use -h for help. Aborting. [ken@~/tmp/fab$] fab -f test.py hello Hello world!
帶參數:
修改fabfile.pycle
def hello(name, value): print("%s = %s!" % (name, value))
執行本機操作
簡單的本地操作
[ken@~/tmp/fab$] fab hello:name=age,value=20 age = 20! Done. [ken@~/tmp/fab$] fab hello:age,20 age = 20!
from fabric.api import local def lsfab(): local('cd ~/tmp/fab') local('ls')
實戰開始:
[ken@~/tmp/fab$] pwd;ls /Users/ken/tmp/fab fabfile.py fabfile.pyc test.py test.pyc [ken@~/tmp/fab$] fab -f test.py lsfab [localhost] local: cd ~/tmp/fab [localhost] local: ls fabfile.py fabfile.pyc test.py test.pyc
cd /home/project/test/conf/ git add settings.py git commit -m 'daily update settings.py' git pull origin git push origin
from fabric.api import local def setting_ci(): local("cd /home/project/test/conf/") local("git add settings.py") #后面你懂的,懒得敲了…..
#!/usr/bin/env python # encoding: utf-8 from fabric.api import local,cd,run env.hosts=['user@ip:port',] #ssh要用到的参数 env.password = 'pwd' def setting_ci(): local('echo "add and commit settings in local"') #刚才的操作换到这里,你懂的 def update_setting_remote(): print "remote update" with cd('~/temp'): #cd用于进入某个目录 run('ls -l | wc -l') #远程操作用run def update(): setting_ci() update_setting_remote()
多伺服器混搭
[ken@~/tmp/fab$] fab -f deploy.py update [user@ip:port] Executing task 'update' [localhost] local: echo "add and commit settings in local" add and commit settings in local remote update [user@ip:port] run: ls -l | wc -l [user@ip:port] out: 12 [user@ip:port] out:
#!/usr/bin/env python # encoding: utf-8 from fabric.api import * #操作一致的服务器可以放在一组,同一组的执行同一套操作 env.roledefs = { 'testserver': ['user1@host1:port1',], 'realserver': ['user2@host2:port2', ] } #env.password = '这里不要用这种配置了,不可能要求密码都一致的,明文编写也不合适。打通所有ssh就行了' @roles('testserver') def task1(): run('ls -l | wc -l') @roles('realserver') def task2(): run('ls ~/temp/ | wc -l') def dotask(): execute(task1) execute(task2)
[ken@~/tmp/fab$] fab -f mult.py dotask [user1@host1:port1] Executing task 'task1' [user1@host1:port1] run: ls -l | wc -l [user1@host1:port1] out: 9 [user1@host1:port1] out: [user2@host2:port2] Executing task 'task2' [user2@host2:port2] run: ls ~/temp/ | wc -l [user2@host2:port2] out: 11 [user2@host2:port2] out:
from fabric.colors import * def show(): print green('success') print red('fail') print yellow('yellow') #fab -f color.py show
env.hosts = [ 'host1', 'host2' ] env.passwords = { 'host1': "pwdofhost1", 'host2': "pwdofhost2", }
另外命令其實也可以固化成一個cmds列表的…..
🎜更多python fabric實現遠端操作和部署範例相關文章請關注PHP中文網! 🎜