web:Fabric基础知识

Fabric

Fabric 就是一个自动化部署工具。需要在客户端安装(需要安装python2.7版本,因为Fabric使用此版本开发的。)
文档:http://docs.fabfile.org/en/1.13/tutorial.html#hello-fab

一、安装

pip install fabric

二、什么是Frabic?

一种工具,它允许您通过命令行执行任意Python函数。
一个子程序库,通过SSH轻松执行shell命令和Pythonic。

三、Hello, fab

在 fabfile.py 中写一个函数:

def hello():
    print("Hello world!")

然后再控制台运行:

$ fab hello
Hello world!

Done.

瞬间觉得吊炸天啊。
如果你想带参数:

def hello(name):
    print('fab hello! %s' % name)

输出:

$ fab hello:kd
fab hello! kd

Done.

四、Failure(失败)

Fabric通过操作调用的程序的返回值来进行检查,如果它们没有完全退出,则中止执行。

五、Failure handling(失败处理)

from __future__ import with_statement
from fabric.api import local, settings, abort
from fabric.contrib.console import confirm

def test():
    # warn_only=True将abort转换为警告
    with settings(warn_only=True):
        result = local('./manage.py test my_app', capture=True)
        # 如果执行失败并且confirm时选择的是n
    if result.failed and not confirm("Tests failed. Continue anyway?"):
        # abort()用于手动中止执行
        abort("Aborting at user request.")

六、Making connections(连接)

from __future__ import with_statement
from fabric.api import local, settings, abort, run, cd
from fabric.contrib.console import confirm

def deploy():
    code_dir = '/srv/django/myproject'
    with cd(code_dir):
        run("git pull")
        run("touch app.wsgi")
# cd() 是切换远程服务器的目录,lcd() 是切换本地目录(Fabric所在的主机)
# run() 是运行远程指令,local() 是在本地运行命令行(Fabric所在的主机)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

outer199

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值