fabric极简简介

fabric主要用于自动化运维,可以使用类似于make命令一样执行Python的函数。

fabric主要依赖于pyinvokeparamiko两个库,前者也是类似于make命令一样的工具,后者是处理ssh远程的库。

pyinvokefabric的主要区别是,fabric是建立在pyinvokeparamiko之上,提供远程操作的功能。

所以安装fabric后,其依赖库pyinvokeparamiko也会一起安装。网上搜索到的中文教程是旧版本的,已经被淘汰了。

pyinvoke最简介

项目目录建立task.py,内容如下:

1
2
3
4
5
from invoke import task

@task
def build(ctx):
    print('Building...')

运行方式:

1
2
^_^@~]$ inv build
Building...

参数ctx里面可运行命令,命令分组,读配置(ctx.config获得)等,配置文件可位于项目当前目录,用户目录,系统目录等,文件名invoke.yml.invoke.yml格式不限,不过推荐yml格式。task函数里面可有参数,依赖关系等。

fabric最简介

fabric和invoke类似,是invoke的远程版本。tasks.py变成fabfile.py, 配置文件名, invoke.yml改为fabric.yml。运行的命令由invoke改为fab,既然如此,那么用invoke替代fabric是否可以,答案是可以。但是,fab客户端提供更多的和网络相关的功能,省去很多手写的样板代码。

1
2
3
4
5
6
7
from fabric import task
import os

@task
def compile(ctx):
    ctx.run("mvn compile", env=os.environ)

输出:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
^_^@~]$ fab compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.138 s
[INFO] Finished at: 2020-06-29T17:07:46+08:00
[INFO] Final Memory: 6M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/Users/luoguochun/tmp/fabric). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more infORMation about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

总结

没细到fabric的细节,非常粗暴的介绍,细节用时,需要参考官网。不过好像,不用fabric也可以完成那么一回事,那么用fabric/invoke做系统管理运维的好处是啥?最大的好处估计是,用统一的方式去处理一件事,提供类似于make的方式。