Fabric 一个与多台服务器远程交互的Python库和工具

去年,Facebook收购Instagram的事情也算是比较轰动的,当时Instagram也算名声大噪,偶然看到一篇博客(其实就是Instagram的开发者博客)专门介绍了Instagram的技术架构,其中提及了使用Fabric来管理他们上百台服务器的事情。BTW,介绍Instagram技术架构的文章见:What Powers Instagram: Hundreds of Instances, Dozens of Technologies这两天,偶尔再次看了遍这篇文章,并且对Fabric感兴趣,或许我管理一些服务器时也许还能用得上,所以花了点时间简单了解了一下Fabric。

首先,Fabric是什么呢? Fabric是一个Python库和命令行工具,它可以将使用SSH操作的应用部署或系统管理任务更加地高效(流水线化)。Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.Fabric提供了一些核心API和外围API,见:http://fabric.readthedocs.org/en/1.6/index.html#api-documentation(Fabric还提供了多进程让任务并行执行,是通过Python自带的multiprocessing module来实现的,不过目前并非线程安全的哦。)

然后,安装Fabric试用一下吧。参见:http://fabric.readthedocs.org/en/1.6/installation.html我是下载源代码,运行“python setup.py install”来安装的,在Ubuntu上也可以使用“sudo apt-get install fabric”来直接安装。

接下来,试一个Helloword这样的例子吧。编辑一个名为test-fabric.py的文件,内容如下:

from fabric.api import rundef host_type():    run('uname -s')

通过Fabric来执行它,命令行如下:

[root@jay-linux jay]# fab -H jay-linux host_typeFatal error: Couldn't find any fabfiles!Remember that -f can be used to specify fabfile path, and use -h for help.Aborting.[root@jay-linux jay]# fab -H jay-linux -f test-fabric.py host_type[jay-linux] Executing task 'host_type'[jay-linux] run: uname -s[jay-linux] Login password for 'root':[jay-linux] out: Linux[jay-linux] out:Done.Disconnecting from jay-linux... done.

执行第一个命令运行时,遇到了“Fatal error: Couldn’t find any fabfiles!”错误,是因为fab没有找到fabfile。如果没有用“-f test-fabric.py”参数来指定使用哪个fabfile,那么fab命令会默认使用fabfile.py这个名称在当前目前及其父附录中去寻找fabfile.py文件(在Linux上它不搜寻最高层的根目录)。所以,解决fabfile找到的问题的方法主要有如下3个:1. “-f your-fabfile.py”来指定自己的fabfile;2. 就在当前目录中将自己的任务编辑好并命名为 fabfile.py;3. 可以将默认的fabfile定义在fabric的配置文件,如:在~/.fabricrc文件中指定“fabfile = fab_tasks.py”.关于Fabric如何查找fabfile的方法,请参考:http://docs.fabfile.org/en/1.6/usage/fabfiles.html#fabfile-discovery或者可以直接查看源代码中fabric/main.py文件中的find_fabfile(names=None)。

最后,在fabric中可以将主机名、用户名、密码等信息写在fabfile中,但是真的不推荐将明文写在代码中,还是最好使用SSH的Key来认证吧。所以如下我就用Fabric写了一个例子(就在名为fabfile.py文件中),将我的总控机器(my-master)的Key分发到需要被控制的机器上取得SSH key的认证,今后操作那些机器时就不用输入登录密码来交互了。注意“@roles(‘master’)”这样的修饰,这也是Fabric提供的功能,让任务在指定的机器上执行。

#!/usr/bin/pythonfrom fabric.colors import red, greenfrom fabric.context_managers import cdfrom fabric.operations import *from fabric.api import *env.roledefs = {    'master':['my-master'],    'slave':['vt9', 'vt7', 'vt2']    }#env.hosts = ['my-master', 'vt9', 'vt7', 'vt1', 'vt2']#env.passwords = {'jay-linux':'123456', 'my-master':'123456'}env.password = '123456'def color():    local('ls -l | wc -l')    print(red("This sentence is red, except for ", bold=True) \            + green("these words, which are green."))def ctx_mgr():    with cd('/var/www'):        run('ls')@roles('master')def get_sshkey():    get('/root/.ssh/id_rsa.pub', 'id_rsa.pub.master')@roles('slave')def put_sshkey():    with cd('/tmp'):        put('id_rsa.pub.master', 'id_rsa.pub.master')        run('cat id_rsa.pub.master >> /root/.ssh/authorized_keys')def do_task():    execute(get_sshkey)    execute(put_sshkey)

该示例程序的,执行如下:

[root@jay-linux jay]# fab do_task[my-master] Executing task 'get_sshkey'[my-master] download: /root/jay/id_rsa.pub.master <- /root/.ssh/id_rsa.pub[vt9] Executing task 'put_sshkey'[vt9] put: id_rsa.pub.master -> /tmp/id_rsa.pub.master[vt9] run: cat id_rsa.pub.master >> /root/.ssh/authorized_keys[vt7] Executing task 'put_sshkey'[vt7] put: id_rsa.pub.master -> /tmp/id_rsa.pub.master[vt7] run: cat id_rsa.pub.master >> /root/.ssh/authorized_keys[vt2] Executing task 'put_sshkey'[vt2] put: id_rsa.pub.master -> /tmp/id_rsa.pub.master[vt2] run: cat id_rsa.pub.master >> /root/.ssh/authorized_keysDone.Disconnecting from my-master... done.Disconnecting from vt7... done.Disconnecting from vt9... done.Disconnecting from vt2... done.

总之,使用了一下Fabric,感觉用起来还是比较方便的,用Python定义自己的任务。要是管理的服务器数量较大,并且上面执行的操作比较重复时,Fabric应该是个不错的选择。

参考资料:Fabric官方文档:http://docs.fabfile.org/en/1.6/index.html这篇中文文档也不错:http://blog.csdn.net/wklken/article/details/8719541

Original article: Fabric 一个与多台服务器远程交互的Python库和工具

©2013 笑遍世界. All Rights Reserved.

Fabric 一个与多台服务器远程交互的Python库和工具

相关文章:

你感兴趣的文章:

标签云: