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;

 
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。
  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./py3.+ compatible fork).
  8. Home-page: https://github.com/mathiasertl/fabric/
  9. Author: Mathias Ertl
  10. Author-email: mati@er.tl
  11. License: UNKNOWN
  12. Location: c:\python27\lib\site-packages
  13. Requires: paramiko, six
  14. Required-by:

4 - 问题处理

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

处理方法:

  确认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. def hello():
  5. print("hello fabric!")
  6.  
  7. $ fab hello
  8. No idea what 'hello' is!
  9.  
  10. $ fab --list
  11. No tasks found in collection 'fabfile'!
处理方法:
  确认fabric版本信息,fabric2.x版本不兼容Fabric 1.x的fabfile。遵照fabric 2.x要求,更改fabfile文件内容格式,重新运行即可。
  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: jeff@bitprophet.org
  8. License: BSD
  9. Location: c:\python27\lib\site-packages
  10. Requires: paramiko, invoke, cryptography
  11. Required-by:
  12.  
  13. $ cat fabfile.py
  14. from invoke import task
  15.  
  16. @task
  17. def hello(c):
  18. c.run("echo 'hello fabric'")
  19. print("hello fabric!")
  20.  
  21. $
  22.  
  23. $ fab --list
  24. Available tasks:
  25.  
  26. hello
  27.  
  28. $ fab hello
  29. 'hello fabric'
  30. hello fabric!
  31.  
  32. $

5 - 参考信息

以下内容主要适用于Fabric 1.x 和 Fabric3。

Python - Fabric简介的更多相关文章

  1. Python Fabric远程自动部署简介

    Fabric是一个Python(2.5-2.7)库,用于简化使用SSH的应用程序部署或系统管理任务. 它提供的操作包括:执行本地或远程shell命令,上传/下载文件,以及其他辅助功能,如提示用户输入. ...

  2. Python Fabric ssh 配置解读

    Python Fabric ssh 配置解读 Fabric 2.4简介: Fabric is a high level Python (2.7, 3.4+) library designed to e ...

  3. 使用python fabric搭建RHEL 7.2大数据基础环境以及部分优化

    1.使用python fabric进行Linux基础配置 使用python,可以让任何事情高效起来,包括运维工作,fabric正式这样一套基于python2的类库,它执行本地或远程shell命令提供了 ...

  4. Python的简介以及安装和第一个程序以及用法

    Python的简介: 1.Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程.Pytho ...

  5. [Python] heapq简介

    [Python] heapq简介 « Lonely Coder [Python] heapq简介 judezhan 发布于 2012 年 8 月 8 日 暂无评论 发表评论 假设你需要维护一个列表,这 ...

  6. Python Fabric模块详解

    Python Fabric模块详解 什么是Fabric? 简单介绍一下: ​ Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...

  7. python fabric安装

    1 安装epel wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 2 安装pip yum i ...

  8. 后端程序员之路 6、Python fabric

    直接写shell固然也很好,但是用python来写脚本,也是美滋滋.fabric是一个封装部署.多机操作等功能的python库. Welcome to Fabric! - Fabric documen ...

  9. Python单元测试简介及Django中的单元测试

    Python单元测试简介及Django中的单元测试 单元测试负责对最小的软件设计单元(模块)进行验证,unittest是Python自带的单元测试框架. 单元测试与功能测试都是日常开发中必不可少的部分 ...

随机推荐

  1. css3(display)

    实现下拉 <!DOCTYPE html><html><head> <meta charset="utf-8"> <style ...

  2. this()基础用法

    this()表示调用构造方法,此种调用只能用在构造方法中,即构造方法中调用构造方法this(实参). 1.this().this(实参)必须方法构造方法的第一行 2.在有参数构造方法中调用无参数构造方 ...

  3. python3 第三十一章 - 模块

    1.什么是模块 如果从Python解释器退出并再次输入,您所做的定义(函数和变量)将丢失.因此,如果要编写一个稍长的程序,最好使用文本编辑器为解释器准备输入,并以该文件作为输入运行它.这称为创建脚本. ...

  4. 文件操作命令(replace)

    Replace 命令: // 描述: 替换文件. // 语法: REPLACE [drive1:][path1]filename [drive2:][path2] [/A] [/P] [/R] [/W ...

  5. Java匿名内部类访问外部

    匿名内部类访问外部局部变量必须是final修饰的,Java 1.8 会默认为其加上final 例子如下: public void send(String topicName, T obj) { Str ...

  6. layer.tips属性

    layer.tips(新加的内容,'选择节点',{time: 0, area: ['20%', '20%'], skin: 'layui-layer-rim', tips: [3, '#ffffff' ...

  7. 【机器学习】主成分分析法 PCA (II)

    主成分分析法(PAC)的优化——选择主成分的数量 根据上一讲,我们知道协方差为① 而训练集的方差为②. 我们希望在方差尽可能小的情况下选择尽可能小的K值. 也就是说我们需要找到k值使得①/②的值尽可能 ...

  8. asp.net core web 项目附加进程调试

    之前asp.net web项目在部署IIS站点的时候可以直接选择项目目录,不用发布,然后附加进程的时候,找到w3wp.exe开头的进程,再根据用户名找到要附加的进程,就可以附加进程调试了.但asp.n ...

  9. Jmeter分布式部署测试-----远程连接多台电脑做压力性能测试

    在使用Jmeter进行接口的性能测试时,由于Jmeter 是JAVA应用,对于CPU和内存的消耗比较大,所以,当需要模拟数以万计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至会引起J ...

  10. svn的基本使用方法

    一,svn的介绍 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库(repository) 中. ...