標籤:systems amp 簡介 paramiko nta bubuko file traceback sum
1 - Fabric
Fabric是一個Python的庫,提供了豐富的同SSH互動的介面,可以用來在本地或遠程機器上自動化、流水化地執行Shell命令。
非常適合用來做應用的遠程部署及系統維護。簡單易用,只需懂得基本的Shell命令。
- HomePage:http://www.fabfile.org/
- Docs:http://docs.fabfile.org/
- GitHub:https://github.com/fabric/fabric/
- ChangeLog:http://www.fabfile.org/changelog.html
2 - 版本區分
目前,從PyPI可以搜尋到主要的fabric庫為“ Fabric 2.1.3 ”、“ fabric2 2.1.3 ”和“ Fabric3 1.14.post1 ”。
- Fabric:官方Fabric,相容 Python 2 & Python 3,但不相容Fabric 1.x的fabfile;
- fabric2: 與Fabric相同,僅作為平滑遷移(使用Fabric包安裝1.x 版本,使用Fabric2包安裝2.x版本,來實現1.x和2.x的共存);
- Fabric3:是一個基於Fabric 1.x 的fork,相容Python2 & Python3,相容 Fabric1.x 的 fabfile;
Fabric 1.x 與2.x版本的主要區別:
- Fabric 1.x只支援Python2.5-2.7,而Fabric2支援Python (2.7, 3.4+);
- Fabric 2.x是重寫Fabric 1.x的版本,不再相容1.x 版本的fabfile,而且有些模組和用法也發生了很大改變;
- 具體資訊請見:Rewrite for 2.0! See Upgrading from Fabric 1.x
3 - 關於Fabric3Fabric3 is a Python (2.7 or 3.4+) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.This is a fork of the original Fabric (git) with the intention of providing support for Python3, while maintaining support for all non-archaic versions of Python2.
GitHub:https://pypi.org/project/Fabric3/
HomePage:https://github.com/mathiasertl/fabric/
注意:
- 目前的版本“Fabric3 1.14.post1”的功能和使用方法仍然與“Fabric 1.x.”基本一致。
- 安裝fabric3之前,需要先卸載fabric。
1 $ pip2 uninstall fabric 2 $ pip2 install fabric3 --proxy="10.144.1.10:8080" 3 4 $ pip2 show fabric3 5 Name: Fabric3 6 Version: 1.14.post1 7 Summary: Fabric is a simple, Pythonic tool for remote execution and deployment (py2.7/py3.4+ compatible fork). 8 Home-page: https://github.com/mathiasertl/fabric/ 9 Author: Mathias Ertl10 Author-email: [email protected]11 License: UNKNOWN12 Location: c:\python27\lib\site-packages13 Requires: paramiko, six14 Required-by:
4 - 問題處理
1 - 匯入fabric.api提示報錯“No module named api”
1 >>> from fabric.api import run2 Traceback (most recent call last):3 File "<stdin>", line 1, in <module>4 ImportError: No module named api5 >>>
處理方法:
確認fabric版本資訊,“from fabric.api import run”的方式只適用fabric1.x版本。
2 - 運行fabric樣本提示報錯“No idea what ‘hello‘ is!”
1 $ cat fabfile.py 2 # coding:utf-8 3 4 5 def hello(): 6 print("hello fabric!") 7 8 $ fab hello 9 No idea what ‘hello‘ is!10 11 $ fab --list12 No tasks found in collection ‘fabfile‘!
處理方法: 確認fabric版本資訊,fabric2.x版本不相容Fabric 1.x的fabfile。遵照fabric 2.x要求,更改fabfile檔案內容格式,重新運行即可。 具體資訊可查看:http://docs.fabfile.org/en/2.1/getting-started.html#addendum-the-fab-command-line-tool
1 $ pip show fabric 2 Name: fabric 3 Version: 2.1.3 4 Summary: High level SSH command execution 5 Home-page: http://fabfile.org 6 Author: Jeff Forcier 7 Author-email: [email protected] 8 License: BSD 9 Location: c:\python27\lib\site-packages10 Requires: paramiko, invoke, cryptography11 Required-by:12 13 $ cat fabfile.py14 from invoke import task15 16 @task17 def hello(c):18 c.run("echo ‘hello fabric‘")19 print("hello fabric!")20 21 $22 23 $ fab --list24 Available tasks:25 26 hello27 28 29 $ fab hello30 ‘hello fabric‘31 hello fabric!32 33 $
5 - 參考資訊
以下內容主要適用於Fabric 1.x 和 Fabric3。
- [Python遠程部署利器Fabric詳解](http://python.jobbole.com/87241/)
- [fabric實現遠程操作和部署](http://python.jobbole.com/83716/)
- 中文文檔:http://fabric-chs.readthedocs.io/zh_CN/chs/
- [Python模組學習 - fabric](https://www.cnblogs.com/xiao-apple36/p/9124292.html)
Python - Fabric簡介