Fabric 应用案例

作者:mdxy-dxy 时间:2021-10-11 13:13:01 

示例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()
标签:Fabric
0
投稿

猜你喜欢

  • Python+OpenCV实战之利用 K-Means 聚类进行色彩量化

    2021-01-02 09:20:27
  • 分析SQL Server中数据库的快照工作原理

    2009-01-19 14:03:00
  • python得到电脑的开机时间方法

    2021-01-05 08:00:05
  • Python里字典的基本用法(包括嵌套字典)

    2023-04-26 14:51:00
  • Python pandas中read_csv参数示例详解

    2021-05-14 06:17:12
  • 如何给 legend 标签设定宽度

    2008-07-26 12:18:00
  • ASP程序中输出Excel文件实例一则

    2008-11-07 15:29:00
  • 深入浅析Python 中的sklearn模型选择

    2023-05-15 19:12:00
  • Django中间件实现拦截器的方法

    2022-10-21 04:47:53
  • Matplotlib绘制条形图的方法你知道吗

    2022-12-05 15:23:59
  • 常用的数据库访问方式是什么?

    2009-11-01 15:08:00
  • 简述Python中的进程、线程、协程

    2021-04-07 11:19:02
  • PHP设计模式之观察者模式浅析

    2023-05-27 12:20:11
  • 对pandas的dataframe绘图并保存的实现方法

    2021-12-21 14:54:50
  • python利用标准库如何获取本地IP示例详解

    2021-10-17 07:46:07
  • python分布式编程实现过程解析

    2023-11-10 21:13:48
  • tensorflow可视化Keras框架中Tensorboard使用示例

    2023-08-09 01:39:27
  • 修改Linux下MySQL 5.0的默认连接数

    2009-09-01 10:16:00
  • 分面搜索(Faceted Search)

    2009-07-31 12:44:00
  • python模仿网页版微信发送消息功能

    2022-01-15 00:12:54
  • asp之家 网络编程 m.aspxhome.com