欢迎使用 Fabric !

什么是 Fabric ?

Fabric是一个高级python(2.7,3.4+)库,旨在通过ssh远程执行shell命令,从而产生有用的python对象:

>>> from fabric import Connection
>>> result = Connection('web1.example.com').run('uname -s', hide=True)
>>> msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
>>> print(msg.format(result))
Ran 'uname -s' on web1.example.com, got stdout:
Linux

它建立在 Invoke (子流程命令执行和命令行功能)和 Paramiko (ssh协议实现),扩展它们的API以互相补充并提供附加功能。

它是如何使用的?

Fabric 的核心用例包括(但不限于):

  • 单个主机上的单个命令:

    >>> result = Connection('web1').run('hostname')
    web1
    >>> result
    <Result cmd='hostname' exited=0>
    
  • 跨多个主机的单个命令(通过不同的方法:串行、并行等):

    >>> from fabric import SerialGroup
    >>> result = SerialGroup('web1', 'web2').run('hostname')
    web1
    web2
    >>> # Sorting for consistency...it's a dict!
    >>> sorted(result.items())
    [(<Connection host=web1>, <Result cmd='hostname' exited=0>), ...]
    
  • 针对单个连接的python代码块(函数/方法):

    >>> def disk_free(c):
    ...     uname = c.run('uname -s', hide=True)
    ...     if 'Linux' in uname.stdout:
    ...         command = "df -h / | tail -n1 | awk '{print $5}'"
    ...         return c.run(command, hide=True).stdout.strip()
    ...     err = "No idea how to get disk space on {}!".format(uname)
    ...     raise Exit(err)
    ...
    >>> print(disk_free(Connection('web1')))
    33%
    
  • 多主机上的 Python代码块:

    >>> # NOTE: Same code as above!
    >>> def disk_free(c):
    ...     uname = c.run('uname -s', hide=True)
    ...     if 'Linux' in uname.stdout:
    ...         command = "df -h / | tail -n1 | awk '{print $5}'"
    ...         return c.run(command, hide=True).stdout.strip()
    ...     err = "No idea how to get disk space on {}!".format(uname)
    ...     raise Exit(err)
    ...
    >>> for cxn in SerialGroup('web1', 'web2', 'db1'):
    ...    print("{}: {}".format(cxn, disk_free(cxn)))
    <Connection host=web1>: 33%
    <Connection host=web2>: 17%
    <Connection host=db1>: 2%
    

除了这些面向库的用例之外,Fabric还可以方便地与invoke的命令行任务功能集成,通过 fab 二进制存根:

  • python函数、方法或整个对象可以用作cli可寻址的任务,例如 fab deploy

  • 任务可以指示在执行之前或之后要运行的其他任务(前置或后置任务);

  • 任务通过常规的GNU样式参数进行参数化,例如 fab deploy --env=prod -d

  • 可以在单个CLI会话中提供多个任务,例如 fab build deploy

  • 更多-支持所有其他调用功能-请参见 its documentation 有关详细信息。

我是Fabric1的用户,如何升级?

我们以允许与Fabric1一起安装的方式包装了现代结构,因此您可以按照您的用例需要的任何速度升级。有多种可能的方法——见 detailed upgrade documentation 有关详细信息。

这是什么网站?

www.fabfile.org 为结构提供项目信息,如变更日志、贡献指南、开发路线图、新闻/博客等。

详细的概念和API文档可以在我们的代码文档站点找到。 docs.fabfile.org .