fabric应用举例

关于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的支持已经在进行,但fabirc2.x 什么时候发布,还没有时间点。

Fabric 竟然还不支持 Python 3
https://www.v2ex.com/t/184125
简介:
v2ex 上的评论:
Fabric 确实太过分了。

python3下使用fabric
http://www.jianshu.com/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 
    from fabric.main import main
  File "/root/.virtualenvs/py3.6.1env/lib/python3.6/site-packages/fabric/main.py", line 13, in 
    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 应用举例

$ 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.

操作远端服务器:

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

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

# 登录用户和主机名:
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')

执行:

$ 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 *

# 登录用户和主机名:
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中使用自己独立的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")

执行:
fab [方法], 命令行中选择不同的方法,来执行不同的逻辑。

$ fab deploy_uop_frontend

扩展阅读

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

你可能感兴趣的:(fabric应用举例)