目的: 监听gerrit事件流,解析数据入库,用于后续数据分析 暂时只做了监听部分,解析部分(40-42行)根据需要调整 from project . settings import GERRIT_HOSTNAME , GERRIT_PORT , G_USERNAME import sys
目的:
监听gerrit事件流,解析数据入库,用于后续数据分析
暂时只做了监听部分,解析部分(40-42行)根据需要调整
from project.settings import GERRIT_HOSTNAME, GERRIT_PORT, G_USERNAMEimport sys
import paramiko
import os
import logging
class SubscribeGerrit():
"""
subscribe gerrit stream-events, save to local db real time for show
"""
def __init__(self):
self.hostname = GERRIT_HOSTNAME
self.port = GERRIT_PORT
self.username = G_USERNAME
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.logger = logging.getLogger("subscribe")
self.arg = sys.argv[1]
def connectGerrit(self):
try:
files = os.listdir("./")
for file in files:
if file.endswith("rsa"):
self.client.connect(hostname=self.hostname, port=self.port, username=self.username, pkey=paramiko.RSAKey(filename=file))
self.channel = self.client.get_transport().open_session(timeout=5)
self.logger.info("connect to gerrit ok")
break
except Exception as e:
self.logger.error("connect to gerrit failed, %s" % e)
sys.exit(1)
def listenStreamEvents(self):
self.channel.exec_command("gerrit stream-events -s %s" % self.arg)
while True:
if self.channel.exit_status_ready():
break
print(self.channel.recv(1024000))
# parse and compare
# cache and db
self.channel.close()
if __name__ == '__main__':
sgobj = SubscribeGerrit()
sgobj.connectGerrit()
sgobj.listenStreamEvents()
等完成后再补充。。。