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安装Imaging报错:The _imaging C module is not installed问题解决方法

今天写Python程序上传图片需要用到PIL库,于是到http://www.pythonware.com/products/pil/#pil117下载了一个1.1.7版本的,我用的是Ce...

很酷的python表白工具 你喜欢我吗

很酷的python表白工具 你喜欢我吗

本文实例为大家分享了python表白工具的具体代码,供大家参考,具体内容如下 实现代码: # 打包操作 # 安装pyinstaller # cmd输入 pip install pyi...

Python学习笔记_数据排序方法

1. 原地排序:采用sort()方法,按照指定的顺序排列数据后用排序后的数据替换原来的数据(原来的顺序丢失),如: 复制代码 代码如下:>>> data1=[4,2,6...

python3.5 + PyQt5 +Eric6 实现的一个计算器代码

python3.5 + PyQt5 +Eric6 实现的一个计算器代码

目前可以实现简单的计算。计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少。 python3.5 + PyQt5 +Eric6 在windows7 32位系统...

详解Python 模拟实现生产者消费者模式的实例

详解Python 模拟实现生产者消费者模式的实例 散仙使用python3.4模拟实现的一个生产者与消费者的例子,用到的知识有线程,队列,循环等,源码如下: Python代码 impo...