fabric 自動化部署

2022-06-13 18:18:13 字數 3233 閱讀 3885

專案發布和運維的工作相當機械,頻率還蠻高,導致時間浪費在敲大量重複的命令上。

修復bug什麼的,測試,提交版本庫(2分鐘),ssh到測試環境pull部署(2分鐘),rsync到線上機器a,b,c,d,e(1分鐘),分別ssh到abcde五颱機器,逐一重啟(8-10分鐘) = 13-15分鐘

其中鬱悶的是,每次操作都是相同的,命令一樣,要命的是在多個機器上,很難在本機乙個指令碼搞定,主要時間都浪費在ssh,敲命令上了,寫成指令碼,完全可以一鍵執行,花兩分鐘看下執行結果。

安裝pip install fabric

入門示例

#fabfile.py

from fabric.api import run

def host_type():

run('uname -s')

啟動

itcast@ubuntu:~/tmp/fab$ fab -h 127.0.0.1 host_type

[127.0.0.1] executing task 'host_type'

[127.0.0.1] run: uname -s

[127.0.0.1] login password for 'itcast':

[127.0.0.1] out: linux

[127.0.0.1] out:

done.

disconnecting from 127.0.0.1... done.

itcast@ubuntu:~/tmp/fab$ fab -h 127.0.0.1 host_type

[127.0.0.1] executing task 'host_type'

[127.0.0.1] run: uname -s

[127.0.0.1] login password for 'itcast':

[127.0.0.1] out: linux

[127.0.0.1] out:

fabric常用引數

fabric常用api

fabric全域性屬性設定

示例1:動態獲取遠端目錄列表

from fabric.api import *

env.hosts=['192.168.17.192', '192.168.17.193']

#env.password='python'

env.passwords =

@runs_once

def input_raw():

return prompt("please input directory name:", default="/home")

def workask(dirname):

run('ls -l ' + dirname)

@task

def go():

print('start ...')

getdirname = input_raw()

workask(getdirname)

print('end ...')

示例2:上傳檔案並執行

from fabric.api import *

env.user = 'itcast'

env.hosts = ['192.168.17.192', '192.168.17.193']

env.password = 'python'

@task

@runs_once

def tar_task():

with lcd('/home/itcast/testdemo'):

local('tar zcvf demo.tar.gz demo.py')

@task

def put_task():

run('mkdir -p /home/itcast/testdemo')

with cd('/home/itcast/testdemo'):

put('/home/itcast/testdemo/demo.tar.gz', '/home/itcast/testdemo/demo.tar.gz')

@task

def check_task():

lmd5 = local('md5sum /home/itcast/testdemo/demo.tar.gz', capture=true).split(' ')[0]

rmd5 = run('md5sum /home/itcast/testdemo/demo.tar.gz').split(' ')[0]

if lmd5 == rmd5:

print('ok ...')

else:

print('error ...')

@task

def run_task():

with cd('/home/itcast/testdemo'):

run('tar zxvf demo.tar.gz')

run('python demo.py')

@task

def go():

tar_task()

put_task()

check_task()

run_task()

**自動化部署

from fabric.api import *

env.user = 'itcast'

env.hosts = ['192.168.17.192', '192.168.17.193']

env.password = 'python'

@runs_once

@task

def local_update():

with lcd("/home/itcast/tmp/itcasthello"):

local("git add -a")

local("git commit -m 'update'")

local("git pull origin master")

local("git push origin master")

@task

def remote_update():

with cd("/home/itcast/tmp/itcasthello"):

run("git checkout master")

run("git pull origin master")

@task

def deploy():

local_update()

remote_update()

有關自動化部署Fabric

要部署多台生產伺服器的時候,一台一台去配置不方便,所以我們需要自動化部署的方式來部署。本文採用的是fabric,在ubuntu 64 上實現。fabric python內建的模組,用來提高基於 ssh 的應用部署和系統管理效率。可以實現與遠端伺服器的自動化互動。一般使用情況為需要運維幾台至幾百台機器...

使用 Fabric 自動化部署

fabric 目前僅支援 python2,如果你的系統中只有 python3 版本,可以使用 fabric3,但是只能安裝低版本1.14.post1,高版本不支援api方法。接下就可以簡單地通過 pip 命令安裝 fabric 了。如果是 python 2 pip install fabric 如果...

自動化部署工具Fabric簡介

在持續整合 灰度發布越來越流行的今天,模組在預覽或生產環境的部署流程自動化顯得越來越重要。本文要介紹的fabric就是乙個幫助我們在上線時減少重複 繁瑣操作的自動化部署利器,對於缺乏成熟運維平台的眾多小公司的運維或開發人員來說,掌握這個工具是有必要的。在系統運維和部署自動化領域,與fabric類似的...