Fabric 自动部署

本文介绍如何使用Fabric进行自动化部署,包括环境配置、本机及远程操作,并实现文件校验与上传等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fabric 自动部署

部署大多都是一些重复的工作,故在这里记录一下学习fabric的过程,借鉴了网上的大神和文档,留作纪念。

环境配置

sudo pip install fabric # sudo 取决于OS

主要参考:官方文档

本机操作

from fabric.api import local, lcd

def test_local():
    with lcd("./filename"):
        local("ls")
# fab test_local
def test_local_params(name, value):
    print(value, name)
# fab test_local_params:name=name, value=value

local() 可以执行linux命令 在这里不多描述了。

远端操作

from fabric.api import local, cd, run, env

env.hosts = ['hostname@ip:port', ]
env.password = 'pwd' # 密码自己处理,不建议保存明文.

def test_online():
    with cd("/var/www/"):
        run("ls -l")
# fab test_online

文件校验上传

from fabric.api import *

env.hosts = ['hostname@ip:port', ]
env.password = 'pwd' 

@runs_once
@task
def tarfile():
    with lcd('/tmp/file'):
        local('tar czf messages.tar.gz messages')

@task
def putfile():
    run('mkdir -p /tmp/file')
    with cd('/tmp/file'):
        with settings(warn_only=True):
            result=put('/tmp/file/messages.tar.gz','/tmp/file')
        if result.failed and not confirm('put file filed,Continue[Y/N]?'):
            abort('Aborting file put task!')

@task
def checkfile():
    with settings(warn_only=True):
        lmd5=local('md5sum /tmp/file/messages.tar.gz',capture=True).split(' ')[0]
        rmd5=run('md5sum /tmp/file/messages.tar.gz').split(' ')[0]
    if lmd5==rmd5:
        print 'ok'
    else:
        print 'error'

# 合并操作
@task   
def main():
    tarfile()
    putfile()
    checkfile()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值