天天看點

python訂閱gerrit服務,同步事件流資料

目的:

監聽gerrit事件流,解析資料入庫,用于後續資料分析

暫時隻做了監聽部分,解析部分(40-42行)根據需要調整

from project.settings import GERRIT_HOSTNAME, GERRIT_PORT, G_USERNAME
import 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()