使用Faric+Git進行分散式程式碼管理
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq1010885678/article/details/49129921
Fabric是一個Python庫,可以通過SSH在多個host上批量執行任務。
可以通過編寫任務指令碼,然後通過Fabric在本地就可以使用SSH在大量遠端伺服器上自動執行。
這些功能非常適合應用的自動化部署,或者執行系統管理任務。
Fabric官方文件:
1、安裝
(1)easy_install
如果沒有easy_install,使用
yum install python-setuptools
進行安裝。
若安裝失敗,其餘安裝方法:
wget -q http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
(2)安裝Fabric
在所有機器上使用
easy_install fabric
進行安裝。
若出現錯誤:缺少Python.h檔案等
解決方法:yum install python-devel -y
2、測試過程
簡單的使用教程請看:
任一機器上專案建立git倉庫,push到遠端庫。
Fabric執行機制為通過ssh遠端操作伺服器,使用python編寫指令碼檔案。
使用詳情請看程式碼註釋。
新建fabfile.py檔案,檔名為固定格式:
from fabric.api import *
from fabric.colors import *
#伺服器主機列表
#env.hosts=[`username1@host1:port`,`username2@host2:port`]
#如果多個伺服器密碼相同直接使用
#env.password=`password`
#可以使用ssh金鑰來代替密碼訪問遠端主機(更安全),前提是需要將公鑰儲存在遠端主機的authorized_keys檔案中
#env.key_filename=‘~/.ssh/id_rsa`
#若伺服器之間密碼不同,使用passwords變數儲存鍵值對
#env.passwords = {`username1@host1:port`:`password1`,`username2@host2:port`:`password2`}
#使用roledefs進行伺服器角色分組
#env.roledefs = {
# `role1`: [`username1@host1:port`],[`username2@host2:port`]
# `role2`: [`[`username3@host3:port`]`]
#}
#稱為任務函式,可在命令列使用fab local_update:msg=msg 直接呼叫
def local_update(msg):
#列印紅色字型
print red(`local host:ali_ibignose`)
#列印綠色字型
print green(`local git pushing...`)
#lcd為本地的cd操作
with lcd(`/home/omniREST/`):
#如果當前沒有新的更改直接使用commit會終止任務進行,所以將此中斷設定僅為輸出警告資訊
with settings(warn_only=True):
#local為在本地執行命令
local(`git add *`)
local(`git commit -m "%s"` % msg)
local(`git push -u origin master`)
print green(`local git push complete!`)
#parallel註解的作用是可以使函式並行執行,可用在多個伺服器同時進行同一個任務函式
#@parallel
def local_docker_restart():
print red(`local host:ali_ibignose`)
print green(`local docker restarting...`)
local(`docker restart omniREST ./root/run.sh`)
print green(`local docker restart complete!`)
#設定角色使用roles註解,那麼該任務函式將會在所有角色為role1的機器上執行
#@roles(`role1`)
def remote1_update():
#也可直接在任務函式中指明連線主機的資訊
env.user=`username`
env.host_string=`username@host:port`
env.password=`password`
print red(`remote host:nc_test`)
print green(`remote git pulling...`)
#cd為遠端cd操作
with cd(`/home/omniREST/tomcat/webapps/ROOT`):
#run為遠端執行命令操作
run(`git pull origin master`)
print green(`remote git pull complete!`)
print ``
print green(`remote tomcat restarting...`)
with cd(`/home/omniREST/tomcat/bin`):
run(`./shutdown.sh`)
run(`nohup ./startup.sh`)
print green(`remote tomcat restart complete!`)
def remote2_update():
print red(`remote host:ibignose`)
print green(`remote git pulling...`)
with cd(`/data2/usr/ibignose/tomcat/webapps/ROOT`):
run(`git pull origin master`)
print green(`remote git pull complete!`)
print ``
print green(`remote tomcat restarting...`)
with cd(`/data2/usr/ibignose/tomcat/bin`):
run(`./shutdown.sh`)
run(`nohup ./startup.sh`)
print green(`remote tomcat restart complete!`)
#可以在命令列直接使用fab update:msg=msg 來呼叫所有任務函式
#執行流程為從上到下,一旦遇到終止性的錯誤將不會繼續執行
def update(msg):
local_update(msg)
print ``
local_docker_restart
print ``
remote1_update()
print ``
remote2_update()
#遍歷字典方式操作多伺服器
dict_host={`ibignose`:`root@42.62.50.218:22`,`nc_test`:`root@54.223.196.153:22`}
dict_user={`ibignose`:`root`,`nc_test`:`root`}
dict_pwd={`ibignose`:`richardg@b83a6fa8`,`nc_test`:`T6yuj&hg`}
dict_path={`ibignose`:`/data2/usr/ibignose/tomcat`,`nc_test`:`/home/omniREST/tomcat`}
def remote_update():
for hostname,host in dict_host.iteritems():
env.user=`%s` % dict_user[hostname]
env.host_string=`%s` % host
env.password=`%s` % dict_pwd[hostname]
print red(`remote hostname:%s` % hostname)
print green(`remote git pulling...`)
with cd(`%s/webapps/ROOT` % dict_path[hostname]):
run(`git pull origin master`)
print green(`remote git pull complete!`)
print ``
print green(`remote tomcat restarting...`)
with cd(`%s/bin` % dict_path[hostname]):
run(`./shutdown.sh`)
run(`nohup ./startup.sh`)
print green(`remote tomcat restart complete!`)
3、遇到的問題
(1)多賬號環境下使用遠端git倉庫。
具體流程請參考:
注意:
(a)~/.ssh/config中的Host不要相同,並且如果有Host值為*的,將其移動至最後。
(b)要使用ssh-add id_rsa_work將私鑰新增入ssh-agent快取中。
(c)git remote add新增遠端庫的時候記得使用”git@config中Host的值”。
(2)不同的任務如何在不同的伺服器上並行執行。
未解決,待繼續研究。
相關文章
- 分散式程式碼管理系統GIT分散式Git
- 使用Git進行小專案程式碼管理Git
- 大型分散式團隊的程式碼版本管理分散式
- Mercurial 分散式程式碼管理系統 HG分散式
- [翻譯] 使用 TensorFlow 進行分散式訓練分散式
- 配置 Gitblit 進行 Git 程式碼管理Git
- Hadoop MapReduce進階 使用分散式快取進行replicated joinHadoop分散式快取
- AWS上使用MXNet進行分散式深度學習分散式深度學習
- 使用Git Bash進行程式碼管理Git行程
- [原始碼解析] PyTorch 分散式(18) --- 使用 RPC 的分散式管道並行原始碼PyTorch分散式RPC並行
- 分散式技術“上位”進行時分散式
- linux環境下使用jmeter進行分散式測試LinuxJMeter分散式
- 使用acorn對JavaScript程式碼進行解析。JavaScript
- 為物料管理進行公司程式碼初始化
- [分散式][zookeeper]--一起走進動物園管理員分散式
- 使用TLA +進行分散式系統的建模與除錯設計分散式除錯
- Spring Cloud Sleuth 和 Zipkin 進行分散式跟蹤使用指南SpringCloud分散式
- windows下使用pytorch進行單機多卡分散式訓練WindowsPyTorch分散式
- 分散式監控系統Zabbix--使用Grafana進行圖形展示分散式Grafana
- API服務平臺,可進行分散式執行API分散式
- 藉助Python 函式進行模組化程式碼Python函式
- 分散式 | 如何與 DBLE 進行“祕密通話”分散式
- 使用Project進行專案管理Project專案管理
- 使用Java 9 Flow進行響應式程式設計Java程式設計
- 你還在生產環境改程式碼麼?函式計算版本管理(三)使用別名進行灰度釋出函式
- Swarm管理分散式dockerSwarm分散式Docker
- Docker中使用Xhprof 對程式碼進行效能分析Docker
- 分散式爬蟲的部署之Gerapy分散式管理分散式爬蟲
- springcloud(十二):使用Spring Cloud Sleuth和Zipkin進行分散式鏈路跟蹤SpringGCCloud分散式
- [原始碼解析] 並行分散式任務佇列 Celery 之 多程式模型原始碼並行分散式佇列模型
- 使用 Visual Studio 進行 SQL Server 原始碼管理和部署SQLServer原始碼
- 圖解|搞定分散式?程式設計師進階之路圖解分散式程式設計師
- 應用伺服器能進行“分散式處理”嗎?伺服器分散式
- 如何管理基於微服務的分散式應用程式微服務分散式
- 如何使用 apt 進行 Linux 包管理APTLinux
- 如何使用lerna進行多包(package)管理Package
- OPA 進階-分散式利器 Bundle分散式
- 向分散式應用進軍分散式