fabric應(yīng)用舉例

關(guān)于fabrich和python版本

fabirc 目前(2017)不支持python3.

http://www.fabfile.org/
簡介:
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

http://www.fabfile.org/roadmap.html
簡介:
python3的支持已經(jīng)在進(jìn)行,但fabirc2.x 什么時候發(fā)布,還沒有時間點(diǎn)。

Fabric 竟然還不支持 Python 3
https://www.v2ex.com/t/184125
簡介:
v2ex 上的評論:
Fabric 確實(shí)太過分了。

python3下使用fabric
http://www.itdecent.cn/p/5bc1cc0ca477
簡介:
在python3下使用fabic的方法。

python3.6.1 下安裝fabric

$ workon py3.6.1env
(py3.6.1env)$ pip install Fabric

(py3.6.1env)$ fab -V
Traceback (most recent call last):
  File "/root/.virtualenvs/py3.6.1env/bin/fab", line 7, in <module>
    from fabric.main import main
  File "/root/.virtualenvs/py3.6.1env/lib/python3.6/site-packages/fabric/main.py", line 13, in <module>
    from operator import isMappingType
ImportError: cannot import name 'isMappingType'

python2.7.5 下安裝fabric

$ pip install Fabric

$ fab -V
Fabric 1.13.2
Paramiko 1.16.1

fabric 應(yīng)用舉例

$ cat  fabfile.py

from fabric.api import run

def host_type():
    run('uname -s')
    run('uptime')
$ fab  host_type

No hosts found. Please specify (single) host string for connection:
No hosts found. Please specify (single) host string for connection: 127.0.0.1
[127.0.0.1] run: uname -s
[127.0.0.1] Login password for 'root':
[127.0.0.1] out: Linux
[127.0.0.1] out:

[127.0.0.1] run: uptime
[127.0.0.1] out:  09:40:27 up 17 days, 23:09, 16 users,  load average: 0.35, 0.15, 0.12
[127.0.0.1] out:


Done.
Disconnecting from 127.0.0.1... done.
$ fab -H localhost,172.28.32.49 host_type
[localhost] Executing task 'host_type'
[localhost] run: uname -s
[localhost] Login password for 'root':
[localhost] out: Linux
[localhost] out:

[localhost] run: uptime
[localhost] out:  09:46:06 up 17 days, 23:14, 16 users,  load average: 0.05, 0.12, 0.13
[localhost] out:

[172.28.32.49] Executing task 'host_type'
[172.28.32.49] run: uname -s
[172.28.32.49] out: Linux
[172.28.32.49] out:

[172.28.32.49] run: uptime
[172.28.32.49] out:  09:46:09 up 17 days, 23:14, 16 users,  load average: 0.05, 0.11, 0.12
[172.28.32.49] out:


Done.
Disconnecting from 172.28.32.49... done.
Disconnecting from localhost... done.

操作遠(yuǎn)端服務(wù)器:

# -*- coding:utf-8 -*-

from fabric.api import run
from datetime import datetime
from fabric.api import *

# 登錄用戶和主機(jī)名:
env.user = 'root'

env.password='123.com'

env.hosts = ['172.28.32.49','172.28.32.50']

def host_type():
    run('uname -s')
    run('uptime')

執(zhí)行:

$ fab host_type
[172.28.32.49] Executing task 'host_type'
[172.28.32.49] run: uname -s
[172.28.32.49] out: Linux
[172.28.32.49] out:

[172.28.32.49] run: uptime
[172.28.32.49] out:  12:07:58 up 18 days,  1:36, 16 users,  load average: 0.03, 0.12, 0.13
[172.28.32.49] out:
[172.28.32.50] Executing task 'host_type'
[172.28.32.50] run: uname -s
[172.28.32.50] out: Linux
[172.28.32.50] out:
[172.28.32.50] run: uptime
[172.28.32.50] out:  12:07:59 up 18 days,  1:37,  2 users,  load average: 0.00, 0.01, 0.05
[172.28.32.50] out:
Done.
Disconnecting from 172.28.32.50... done.
Disconnecting from 172.28.32.49... done.

部署代碼:

一個部署代碼的fabric腳本:

# filename: fabricfile.py 
# -*- coding:utf-8 -*-

from fabric.api import run
from datetime import datetime
from fabric.api import *

# 登錄用戶和主機(jī)名:
env.user = 'root'
env.password='123.com'

#env.hosts = ['172.28.32.49','172.28.32.50',
#             '172.28.32.51','172.28.32.53',
#             ]
#
#env.hosts = ['172.28.32.49','172.28.32.50']
env.hosts = ['172.28.32.51','172.28.32.53']

#def host_type():
#    run('uname -s')
#    run('uptime')

def deploy_mpc_frontend():
    #cd('')
    MPC_BACKEND = '/opt/mpc-frontend'
    with cd(MPC_BACKEND):
        run("git pull ")
        run("npm run build")
        #run("git pull --rebase")


def deploy_uop_frontend():
    #cd('')
    MPC_BACKEND = '/opt/uop-frontend'
    with cd(MPC_BACKEND):
        run("git pull ")
        run("npm run build:test")

def deploy_mpc_backend():
    #cd('')
    MPC_BACKEND = '/opt/mpc-backend'
    with cd(MPC_BACKEND):
        run("git pull ")
        #run("git pull --rebase")
def deploy_crp_backend():
    #cd('')
    CRP_BACKEND = '/opt/mpc-backend'
    with cd(CRP_BACKEND):
        run("git pull ")
        #run("git pull --rebase")

def deploy_mpc_crp_backend():
    #cd('')
    CRP_BACKEND = '/opt/mpc_crp-backend'
    with cd(CRP_BACKEND):
        run("git pull ")
        #run("git pull --rebase")


def deploy_mpc_cmdb_backend():
    #cd('')
    CRP_BACKEND = '/opt/mpc_cmdb-backend'
    with cd(CRP_BACKEND):
        run("git pull ")
        #run("git pull --rebase")

def deploy_uop_backend():
    #cd('')
    UOP_BACKEND = '/opt/uop-backend'
    with cd(UOP_BACKEND):
        run("git pull ")
        #run("git pull --rebase")

def deploy_cmdb_backend():
    #NOTE: 在deploy_cmdb_backend中使用自己獨(dú)立的hosts的沒有效果。
    env.hosts = ['172.28.32.49','172.28.32.50']
    #cd('')
    CMDB_BACKEND = '/opt/cmdb-backend'
    with cd(CMDB_BACKEND):
        run("git pull ")
        #run("git pull --rebase")

執(zhí)行:
fab [方法], 命令行中選擇不同的方法,來執(zhí)行不同的邏輯。

$ fab deploy_uop_frontend

擴(kuò)展閱讀

python三大神器之一fabric使用
http://www.cnblogs.com/rufus-hua/p/5144210.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個 Awesome - XXX 系列...
    aimaile閱讀 26,836評論 6 427
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,062評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,376評論 4 61
  • #我的markdown初體驗(yàn) 不知道簡書是做什么的,只想用它來試一試markdown。
    藏鋒者閱讀 228評論 0 1

友情鏈接更多精彩內(nèi)容