Python - Fabric简介

1 - Fabric

Fabric是一个Python的库,提供了丰富的同SSH交互的接口,可以用来在本地或远程机器上自动化、流水化地执行Shell命令。

非常适合用来做应用的远程部署及系统维护。简单易用,只需懂得基本的Shell命令。

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;

Python - Fabric简介

 
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 - 关于Fabric3

Fabric3 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.
 
 
注意
  • 当前版本“Fabric3 1.14.post1”的功能和使用方法仍然与“Fabric 1.x.”基本一致。
  • 安装fabric3之前,需要先卸载fabric。
 $ pip2 uninstall fabric
$ pip2 install fabric3 --proxy="10.144.1.10:8080" $ pip2 show fabric3
Name: Fabric3
Version: 1.14.post1
Summary: Fabric is a simple, Pythonic tool for remote execution and deployment (py2./py3.+ compatible fork).
Home-page: https://github.com/mathiasertl/fabric/
Author: Mathias Ertl
Author-email: mati@er.tl
License: UNKNOWN
Location: c:\python27\lib\site-packages
Requires: paramiko, six
Required-by:

4 - 问题处理

1 - 导入fabric.api提示报错“No module named api”
 >>> from fabric.api import run
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named api
>>>

处理方法:

  确认fabric版本信息,“from fabric.api import run”的方式只适用fabric1.x版本。

2 - 运行fabric示例提示报错“No idea what 'hello' is!”

 $ cat fabfile.py
# coding:utf-8 def hello():
print("hello fabric!") $ fab hello
No idea what 'hello' is! $ fab --list
No tasks found in collection 'fabfile'!
处理方法:
  确认fabric版本信息,fabric2.x版本不兼容Fabric 1.x的fabfile。遵照fabric 2.x要求,更改fabfile文件内容格式,重新运行即可。
 $ pip show fabric
Name: fabric
Version: 2.1.3
Summary: High level SSH command execution
Home-page: http://fabfile.org
Author: Jeff Forcier
Author-email: jeff@bitprophet.org
License: BSD
Location: c:\python27\lib\site-packages
Requires: paramiko, invoke, cryptography
Required-by: $ cat fabfile.py
from invoke import task @task
def hello(c):
c.run("echo 'hello fabric'")
print("hello fabric!") $ $ fab --list
Available tasks: hello $ fab hello
'hello fabric'
hello fabric! $

5 - 参考信息

以下内容主要适用于Fabric 1.x 和 Fabric3。
上一篇:pandas 数据检索


下一篇:移动支付马云还是输给了马化腾,虽然马云爱撒钱,但还是没赢