然后比如关于Searcher的配置文件,在上面也是一个模版文件阿,我们可以把这个文件设计成:
- se_conf_file=${searchRoot}/scripts/conf/se.conf
- simon_conf_path=${searchRoot}/scripts/conf/simon_searcher.xml
- sort_config=${searchRoot}/scripts/conf/searcher_sort.xml
- cache_size=0
- cache_min_doc=0
- conn_queue_limit=500
- [services]
- tcp ${port} # 主要就是为了替换监听的端口,其实要做得通用一点的话,很多配置都可以搞成变量,但就是可能你自己的配置文件变得很复杂。因此我们能不改的就尽量不改。
-
- [clustermap]
- local_config_path=${searchRoot}/scripts/conf/clustermap.xml
|
上述就是关于searcher和merger多行多列的配置,下面我们完善一下我们刚才的Python脚本
- # 得的一个ssh登录后的client对象,用于调用远程机器上的命令
- def getClient(host, port, username, password):
- client = paramiko.SSHClient()
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.WarningPolicy()
- client.connect(hostname, port, username, password)
- return client
-
- # 得到一个sftp对象,因为需要scp渲染好的配置文件什么的,因此需要sftp对象,它的put方法其实就类似scp
- def getSftp(host, port, username, password):
- transport = paramiko.Transport((hostname, port))
- transport.connect(username=username, password=password)
- sftp = paramiko.SFTPClient.from_transport(transport)
- return sftp
-
- # 更新和部署Searchers
- def cleanSearchers(config, searchers):
- for searcher in searchers:
- # 得到渲染好的配置文件的内容
- templateLine = Template(file(config["searcher"]["templateConfigFile"]).read()).render(
- port=searcher["port"],
- searchRoot=config["searchRoot"]
- )
- # 将渲染好的配置文件写入一个临时文件
- tmpConfigFile = tempfile.NamedTemporaryFile(delete=False)
- tmpConfigFile.file.write(templateLine)
- tmpConfigFile.file.close()
- # 将这个临时文件scp拷远程机器上的哪儿
- targetConfigFile = Template(searcher["configFile"]).render(searchRoot=config["searchRoot"])
- sftp = getSftp(searcher["host"], 22, searcher["username"], searcher["password"])
- sftp.put(tmpConfigFile.name, targetConfigFile)
- sftp.close()
- # 删除掉之前的临时文件
- os.remove(tmpConfigFile.name)
- # 运行启动searcher的命令
- client = getClient(searcher["host"], 22, searcher["username"], searcher["password"])
- for command in config["searcher"]["commands"]:
- command = Template(command).render(
- searchRoot=config["searchRoot"],
- configFile=targetConfigFile,
- logConfigFile=targetLogConfigFile
- )
- client.exec_command(cmd)
- client.close()
|