Fabric 应用案例

yipeiwu_com6年前Python基础

示例1:文件打包,上传与校验
我们时常做一些文件包分发的工作,实施步骤一般是先压缩打包,在批量上传至目标服务器,最后做一致性校验,本案例通过put()方法实现文件的上传,通过对比本地与远程主机文件的md5,最终实现文件一致性校验。

#!/usr/bin/env python
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
env.user = 'root'
env.hosts = ['192.168.1.23','192.168.1.24']
env.password = '123456'
 
@runs_once
def tar_task(): #本地打包任务函数,只限执行一次
  with lcd('/'):
    local("tar zcvf auto.tar.gz auto")
 
def put_task():
  run('mkdir /data') #上传任务函数
  with cd("/data"):
    with settings(warn_only=True):
      result = put("/auto.tar.gz","/data") #put上传出现异常时继续执行,非中止
    if result.failed and not confirm("put file failed, Continue[Y/N]?"):
      abort('Aboring file put task!') #出现异常时,确认用户是否继续
 
def check_task():
  with settings(warn_only=True):
    lmd5 = local("md5sum /auto.tar.gz",capture=True).split(' ')[0]
    rmd5 = run("md5sum /data/auto.tar.gz").split(' ')[0]
    if lmd5 == rmd5: #对比本地及远程文件MD5信息
      print "ok"
    else:
      print ERROR
def go():
  tar_task()
  put_task()
  check_task()      

相关文章

Python OpenCV之图片缩放的实现(cv2.resize)

Python OpenCV之图片缩放的实现(cv2.resize)

OpenCV函数原型: cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation) 参数解释:...

python读取二进制mnist实例详解

python读取二进制mnist实例详解 training data 数据结构: <br>[offset] [type] [value] [descrip...

python 字典操作提取key,value的方法

python 字典操作提取key,value的方法

python 字典操作提取key,value dictionaryName[key] = value 1.为字典增加一项 2.访问字典中的值 3、删除字典中的一项...

python隐藏终端执行cmd命令的方法

在用pyinstaller打包后不想要后面的终端命令框,但是打包时加了-w或者--noconsole命令后会导致cmd程序不能运行从而出错。这个时候用subprocess可以解决该类问题...

django框架F&Q 聚合与分组操作示例

本文实例讲述了django框架F&Q 聚合与分组操作。分享给大家供大家参考,具体如下: F 使用查询条件的值,专门取对象中某列值的操作,可以对同一个表中的两个列进行比较 from d...