链条传动

砥砺前行,不忘初心!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

简介

Func自带的模块已经非常丰富,但在日常系统运维当中,尤其是面对大规模的服务器集群、不同类别的业务平台,次是Func自带的模块或许已经不能满足我们的需求,所以有必要通过自定义模块来填补这块的不足。

 

自定义模块的步骤

生成模块----->编写逻辑----->分发模块----->执行模块

生成模块:通过fun-create-module命令创建模块初始模板

编写逻辑:即填充我们的业务功能逻辑

分发模块:将编写完成的模块分发到所有被控主机

执行模块:执行已分发完成的模块,调用方法与Func自带模块无差异

 

生成模块

切换到Func安装包minion模块存储目录:/usr/lib/python2.6/site-packages/func/minion/modules/,执行命令fun-create-module创建模块

cd /usr/lib/python2.6/site-packages/func/minion/modules/
fun-create-module #后面就按照提示进行操作

结果:

[root@wx modules]# func-create-module 
Module Name: MyModule
Description: My Module for func.
Author: Macolee

Email: lihui__love@163.com

Leave blank to finish.
Method: echo
Method: 
Your module is ready to be hacked on. Wrote out to mymodule.py.

最终生成了一个初始化的模块代码文件mymodule.py

 

【/usr/lib/python2.6/site-packages/func/minion/modules/mymodule.py】

#
# Copyright 2016
# Macolee <lihui__love@163.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import func_module


class Mymodule(func_module.FuncModule):

# Update these if need be.
version = "0.0.1"
api_version = "0.0.1"
description = "My Module for func."

def echo(self):
"""
TODO: Document me ...
"""
pass

 

编写逻辑

这一步只需在上述模块基础上修改即可,如模块实现一个根据指定的条数返回最新系统日志(/var/log/messages)信息,修改后代码如下:

【/usr/lib/python2.6/site-packages/func/minion/modules/mymodule.py】

#
# Copyright 2016
# Macolee <lihui__love@163.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import func_module


class Mymodule(func_module.FuncModule):

# Update these if need be.
version = "0.0.1"
api_version = "0.0.1"
description = "My Module for func."

def echo(self,vcount):
"""
TODO: response system messages info
"""
command = "/usr/bin/tail -n"+str(vcount)+"/var/log/messages"
cmdref = sub_process.Popen(command,stdout=sub_process.PIPE,stderr=sub_process.PIPE,shell=True,close_fds=True)

data = cmdref.communicate()
return (cmdref.returncode,data[0],data[1])

 

分发模块

首先编写分发模块的功能,使用Func的copyfile模块来实现,原理是:读写主控端func minion包下的模块文件(参数传入),通过Func的copyfile模块同步到目标主机的同路径下。一次编写可持续使用。

【/home/test/func/RsyncModule.py】

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
import func.overlord.client as fc
import xmlrpclib

module = sys.argv[1]
pythonmodulepath = '/usr/lib/python2.6/site-packages/func/minion/modules/'
client = fc.Client('*')
fb = file(pythonmodulepath+module,'r').read()
data = xmlrpclib.Binary(fb)

#分发模块
print client.copyfile.copyfile(pythonmodulepath+module,data)

#重启Func服务
print client.command.run('/etc/init.d/funcd restart')

 

分发命令:

cd /home/test/func/
cp /usr/lib/python2.6/site-packages/func/minion/modules/mymodule.py /home/test/func/

python RsyncModule.py mymodule.py

检查被控主机的/usr/lib/python2.6/site-packages/func/minion/modules/目录是否多了一个mymodule.py文件,是则说明分发成功。

 

执行模块

func '*' call mymodule echo 5

 

 


参考资料:

根据刘天斯《Python自动化运维技术与最佳实践》整理

posted on 2016-08-18 10:34  链条君  阅读(564)  评论(0编辑  收藏  举报