From e22fac98fc532c34ad2262b71302f559015d6e54 Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 28 Oct 2019 18:53:02 +0100 Subject: [PATCH] Add main service which will launch both http and ssh services. --- ferrit/http.py | 24 +++++++++++++++--------- ferrit/service.py | 32 ++++++++++++++++++++++++++++++++ ferrit/ssh.py | 38 ++++++++++++++------------------------ 3 files changed, 61 insertions(+), 33 deletions(-) create mode 100644 ferrit/service.py diff --git a/ferrit/http.py b/ferrit/http.py index 67bf9f8..1445bc6 100755 --- a/ferrit/http.py +++ b/ferrit/http.py @@ -4,6 +4,8 @@ import os import bottle +# This global variable meant to be set in module, which imports this one +FIFO = None FILE_DIR = os.path.dirname(__file__) BASE_NAME = os.path.extsep.join(os.path.basename(__file__) .split(os.path.extsep)[:-1]) @@ -19,7 +21,6 @@ LOG.addHandler(handler) class App(bottle.Bottle): def __init__(self): super(App, self).__init__() - self.get('/', callback=self._hello) self.route('/Documentation/', callback=self._documentation) self.route('/plugins/events-log/', callback=self._events_log) self.route('/plugins/events-log/events/', callback=self._events) @@ -27,9 +28,6 @@ class App(bottle.Bottle): self.post('/a/changes/~~/revisions/' '/review', callback=self._changes) - def _hello(self, cos): - return {'data': {'foo': 'bar', 'baz': True, 'param': cos}} - def _documentation(self, whatever, params=None): return '' @@ -37,7 +35,6 @@ class App(bottle.Bottle): return '' def _events(self, t1=None): - __import__('pdb').set_trace() return {} def _projects(params=None): @@ -56,9 +53,15 @@ class App(bottle.Bottle): "description": "users", "state": "ACTIVE", "web_links": [{"name": "browse", - "url": "/plugins/gitiles/i" + "url": "/plugins/gitiles/" "All-Users", - "target": "_blank"}]}} + "target": "_blank"}]}, + "example": {"id": "example", + "description": "example ptoject", + "state": "ACTIVE", + "web_links": [{"name": "browse", + "url": "/plugins/gitiles/example", + "target": "_blank"}]}} def _changes(self, project, branch, id, commit_id): # We are looking for labels in the json @@ -74,8 +77,11 @@ class App(bottle.Bottle): def main(): app = App() - app.run(port=8181, host='localhost', debug=True) + app.run(port=8181, host='localhost', debug=False, quiet=True) if __name__ == "__main__": - main() + # development version, meant to be run as stand alone module, like + # python -m ferrit.http + app = App() + app.run(port=8181, host='localhost', debug=True, reloader=True) diff --git a/ferrit/service.py b/ferrit/service.py new file mode 100644 index 0000000..b0c1fe8 --- /dev/null +++ b/ferrit/service.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +import multiprocessing +import os +import tempfile + +from ferrit import ssh +from ferrit import http + + +def main(): + fd, fifo = tempfile.mkstemp(suffix='.fifo', prefix='ferrit.') + os.close(fd) + os.unlink(fifo) + os.mkfifo(fifo) + ssh.FIFO = fifo + http.FIFO = fifo + try: + p1 = multiprocessing.Process(target=ssh.main) + p1.daemon = True + p1.start() + p2 = multiprocessing.Process(target=http.main) + p2.daemon = False + p2.start() + + p1.join() + p2.join() + except Exception: + os.unlink(fifo) + + +if __name__ == "__main__": + main() diff --git a/ferrit/ssh.py b/ferrit/ssh.py index f2cf64e..b4f474b 100755 --- a/ferrit/ssh.py +++ b/ferrit/ssh.py @@ -1,10 +1,8 @@ -#!/usr/bin/env python import inspect import logging import os import socketserver import sys -import tempfile import threading import time import traceback @@ -12,24 +10,21 @@ import traceback import paramiko +# This global variable meant to be set in module, which imports this one +FIFO = None + # it could be even 29418, which is standard gerrit port PORT = 2200 FILE_DIR = os.path.dirname(__file__) BASE_NAME = os.path.extsep.join(os.path.basename(__file__) .split(os.path.extsep)[:-1]) -HOST_KEY = paramiko.RSAKey(filename=os.path.join(FILE_DIR, - 'gerrit-server-key'), - password='jenkins') -fd, FIFO = tempfile.mkstemp(suffix='.fifo', prefix='ferrit.') -os.close(fd) -os.unlink(FIFO) +# TODO(gryf): make the path to the key configurable +HOST_KEY = paramiko.RSAKey(filename='gerrit-server-key', password='jenkins') GERRIT_CMD_PROJECTS = """All-Projects All-Users -openstack -openstack/nova -openstack/neutron +example """ GERRIT_CMD_VERSION = "ferrit version 0.0.1\n" GERRIT_SHELL_MSG = """\r @@ -51,11 +46,6 @@ handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] ' '%(filename)s:%(lineno)s - ' '%(message)s')) LOG.addHandler(handler) -handler = logging.StreamHandler(sys.stdout) -handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] ' - '%(filename)s:%(lineno)s - ' - '%(message)s')) -LOG.addHandler(handler) class Server(paramiko.ServerInterface): @@ -186,15 +176,15 @@ class SSHHandler(socketserver.StreamRequestHandler): def main(): - try: - os.mkfifo(FIFO) - sys.stdout.write('Opening named FIFO queue: %s\n' % FIFO) - sshserver = socketserver.ThreadingTCPServer(('127.0.0.1', PORT), - SSHHandler) - sshserver.serve_forever() - finally: - os.remove(FIFO) + sshserver = socketserver.ThreadingTCPServer(('127.0.0.1', PORT), + SSHHandler) + sshserver.serve_forever() if __name__ == "__main__": + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] ' + '%(filename)s:%(lineno)s - ' + '%(message)s')) + LOG.addHandler(handler) main()