python fabric使用筆記
fabric
title是開發,但是同時要幹開發測試還有維運的活…為毛task*3 不是salary * 3 (o(╯□╰)o)
近期接手越來越多的東西,發布和運維的工作相當機械,加上頻率還蠻高,導致時間浪費還是優點多。
修復bug什麼的,測試,提交版本庫(2分鐘),ssh到測試環境pull部署(2分鐘),rsync到線上機器A,B,C,D,E(1分鐘),分別ssh到ABCDE五台機器,逐一重啟(8-10分鐘) = 13-15分鐘
其中鬱悶的是,每次操作都是相同的,命令一樣,要命的是在多個機器上,很難在本機一個腳本搞定,主要時間都浪費在ssh,敲命令上了,寫成腳本,完全可以一鍵執行,花兩分鐘看下執行結果
直到,發現了fabric這貨
作用
很強大的工具
可以將自動化部署或多機操作的指令固化到一個腳本裡
和某些運維工具很像,用它主要是因為,python…..
簡單好用易上手
當然,shell各種指令組合起來也可以,上古神器和現代兵器的區別
環境配置
在本機和目標機器安裝對應包(注意,都要有)
sudo easy_install fabric
目前是1.6版本(或者用pip install,一樣的)
安裝完後,可以查看是否安裝成功
[ken@~$] which fab /usr/local/bin/fab
裝完之後,可以瀏覽下官方文件 http://docs.fabfile.org/en/1.6/
然後,可以動手了
hello world
先進行本機簡單操作,有一個初步認識,例子來源與官網
新建一個py腳本: fabfile.py
def hello(): print("Hello world!")
命令列執行:
[ken@~/tmp/fab$] fab hello Hello world! Done.
注意,這裡可以不用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! Done.
帶參數:
修改fabfile.py腳本:
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! Done.
執行本機操作
簡單的本地操作實樣
假設,你每天要提交一份配置文件settings.py到版本庫(這裡沒有考慮衝突的情況)
如果是手工操作:
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 Done.
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") #后面你懂的,懒得敲了…..
多混搭
伺服器混搭操作多個伺服器,需要配置多個host
#!/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: Done.
1.顏色
可以列印顏色,在查看操作結果資訊的時候更為醒目和方便
from fabric.colors import *
def show():
print red('fail')
print yellow('yellow')#fabhostf colorcolor.pyuser.設定列表,所有的都寫在一個文件
或直接搞到腳本裡,當然這個更........
#!/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: Done.
2.根據key解析成map嵌套,放到deploy中
另外指令其實也可以固化成一個cmds列表的…..
更多python fabric使用筆記相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...
