Python之Fabric

时间:2017-09-16 19:58:36   收藏:0   阅读:252

【Fabric】

Fabric是一个用Python开发的部署工具,最大特点是不用登录远程服务器,在本地运行远程命令,几行Python脚本就可以轻松部署。

 

安装

 wget https://bootstrap.pypa.io/get-pip.py
 python get-pip.py
 pip install fabric

 

fabric常用参数

fabric常用API

fabric全局属性设定

 

示例:上传文件并执行

from fabric.api import *

env.user = ‘mysql‘
env.hosts = [‘192.168.56.1‘, ‘192.168.56.2‘]
#env.password = ‘1qaz@WSX‘
env.passwords = {
    ‘root@192.168.56.1:22‘:‘1qaz@WSX‘,
    ‘root@192.168.56.2:22‘:‘1qaz@WSX‘,
}

@task
@runs_once
def tar_task():
    with lcd(‘/home/mysql/yanjun_wang‘):
        local(‘tar zcvf hello.tar.gz hello_world.py‘)

@task
def put_task():
    run(‘mkdir -p /home/mysql/yanjun_wang‘)
    with cd(‘/home/mysql/yanjun_wang‘):
        put(‘/home/mysql/yanjun_wang/hello.tar.gz‘, ‘/home/mysql/yanjun_wang/hello.tar.gz‘)

@task
def check_task():
    lmd5 = local(‘md5sum /home/mysql/yanjun_wang/hello.tar.gz‘, capture=True).split(‘ ‘)[0]
    rmd5 = run(‘md5sum /home/mysql/yanjun_wang/hello.tar.gz‘).split(‘ ‘)[0]
    if lmd5 == rmd5:
        print(‘OK ...‘)
    else:
        print(‘ERROR ...‘)

@task
def run_task():
    with cd(‘/home/mysql/yanjun_wang‘):
        run(‘tar zxvf hello.tar.gz‘)
        run(‘python hello_world.py‘)

@task
def execute():
    print(‘start ...‘)
    tar_task()
    put_task()
    check_task()
    run_task()
    print(‘end ...‘)

  

 

原文:http://www.cnblogs.com/vadim/p/7532411.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!