fabric-file.py——利用fabric實現文件打包、上傳和校驗

#!/usr/bin/evn pythonpython

#coding:utf-8api

from fabric.api import *ide

from fabric.context_managers import *函數

from fabric.contrib.console import confirmmd5

env.user = 'root'utf-8

env.hosts = ['', '']it

env.password = ''console

@taskclass

@runs_onceimport

def tar_task():    # 本地打包任務函數,只限執行一次

    with lcd("/data/logs"):

        local("tar -zcf access.tar.gz access.log")

@task

def put_task():    # 上傳文件任務函數

    run("mkdir -p /logs")

    with cd("/logs"):

        with settings(warn_only = True):

            result = put("/data/logs/access.tar.gz", "/logs/access.tar.gz")

        if result.failed and not confirm("put file faild, Continue[Y/N]?"):

            abort("Aborting file put task!")

@task

def check_task():    # 檢驗文件任務函數

    with settings(warn_only = True):

        lmd5 = local("md5sum /data/logs/access.tar.gz", capture = True).split(' ')[0]

        rmd5 = run("md5sum /logs/access.tar.gz").split(' ')[0]

    if lmd5 == rmd5:

        print "OK"

    else:

        print "ERROR"

@task

def go():

    tar_task()

    put_task()

    check_task()

相關文章
相關標籤/搜索