歡迎使用 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 .