文件打包,上传与校验

[root@node110 fabric]# cat simple4.py
#!/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.21','192.168.1.22','192.168.1.23']
env.hosts=['192.168.2.110','192.168.2.120']
env.password='123456'




@task
@runs_once
def tar_task():
    with lcd("/usr/local/apache2.4.12/logs"):
        local("tar -czf access.tar.gz access_log")
@task
def put_task():
    run("mkdir -p /data/logs")
    with cd("/data/logs"):
        with settings(warn_only=True):
            result = put("/usr/local/apache2.4.12/logs/access.tar.gz", "/data/logs/access.tar.gz")
        if result.failed and not confirm("put file failed, Continue[Y/N]?"):
            abort("Aborting file put task!")


@task
def check_task():
    with settings(warn_only=True):
        lmd5=local("md5sum /usr/local/apache2.4.12/logs/access.tar.gz",capture=True).split(' ')[0]
        rmd5=run("md5sum /data/logs/access.tar.gz").split(' ')[0]
    if lmd5==rmd5:
        print "OK"
    else:
        print "ERROR"


@task
def go():
    tar_task()
    put_task()
    check_task()


结果:
[root@node110 fabric]# fab -f simple4.py go
[192.168.2.110] Executing task 'go'
[localhost] local: tar -czf access.tar.gz access_log
[192.168.2.110] run: mkdir -p /data/logs
[192.168.2.110] put: /usr/local/apache2.4.12/logs/access.tar.gz -> /data/logs/access.tar.gz
[localhost] local: md5sum /usr/local/apache2.4.12/logs/access.tar.gz
[192.168.2.110] run: md5sum /data/logs/access.tar.gz
[192.168.2.110] out: 0972344cdcfd664e52d7353e0acd21d0  /data/logs/access.tar.gz
[192.168.2.110] out: 


OK
[192.168.2.120] Executing task 'go'
[192.168.2.120] run: mkdir -p /data/logs
[192.168.2.120] put: /usr/local/apache2.4.12/logs/access.tar.gz -> /data/logs/access.tar.gz
[localhost] local: md5sum /usr/local/apache2.4.12/logs/access.tar.gz
[192.168.2.120] run: md5sum /data/logs/access.tar.gz
[192.168.2.120] out: 0972344cdcfd664e52d7353e0acd21d0  /data/logs/access.tar.gz
[192.168.2.120] out: 


OK


Done.
Disconnecting from 192.168.2.110... done.
Disconnecting from 192.168.2.120... done.
分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS