From: t.moriyama Date: Sun, 13 Oct 2013 16:31:08 +0000 (+0900) Subject: add api_server X-Git-Url: http://git.sourceforge.jp/view?p=ti2%2Fti2.git;a=commitdiff_plain;h=c3c60a5bc9f94ec36876b0940452851f8e4d2166 add api_server --- diff --git a/api_server.py b/api_server.py new file mode 100755 index 0000000..c0ea372 --- /dev/null +++ b/api_server.py @@ -0,0 +1,27 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# -*- coding: utf-8 -*- +# + +from flask import Flask +from linkpair.exporter.JSON import JsonExporter + +app = Flask(__name__) +util = None +collect_func = None + +def parse_linkpair_records(): + result_strings = [] + linkpairs = collect_func() + exporter = JsonExporter(util, linkpairs) + return exporter.run() + "\n" + +def start_app(listen='localhost:23456', debug=True): + app.config.update( + DEBUG=debug, + SERVER_NAME=listen + ) + app.run() + +@app.route("/") +def index(): + return parse_linkpair_records() diff --git a/linkpair/timer/__init__.py b/linkpair/timer/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/linkpair/timer/collect_task.py b/linkpair/timer/collect_task.py new file mode 100755 index 0000000..5538104 --- /dev/null +++ b/linkpair/timer/collect_task.py @@ -0,0 +1,19 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# -*- coding: utf-8 -*- +# + +import threading +import time + +class CollectTask(threading.Thread): + + def __init__(self, collect_function, interval): + self.collect_function = collect_function + self.interval = interval + threading.Thread.__init__(self) + self.setDaemon(True) + + def run(self): + while True: + time.sleep(self.interval) + self.collect_function diff --git a/ti2.py b/ti2.py index f121db5..8c3929f 100755 --- a/ti2.py +++ b/ti2.py @@ -6,7 +6,7 @@ # # -__version__ = '1.1' +__version__ = '1.2' import sys from optparse import OptionParser, OptionGroup @@ -18,15 +18,30 @@ from linkpair.formatter.graphviz_tb import GraphvizTBFormatter from linkpair.utils.common import CommonUtils from linkpair.exporter.JSON import JsonExporter from linkpair.importer.JSON import JsonImporter +from linkpair.timer.collect_task import CollectTask +import api_server -def print_linkpair_records(lps): +def print_linkpair_records(linkpairs): util.debug_out("start print_linkpair_records()") - formatter.set_linkpairs(lps) - for lp in formatter.format_linkpairs(): - util.message_out(lp) + formatter.set_linkpairs(linkpairs) + for linkpair in formatter.format_linkpairs(): + util.message_out(linkpair) util.debug_out("end print_linkpair_records()") +def collect_linkpairs(opts, util, formatter): + def collect_func(): + col = Collector(opts, util, formatter) + if opts.remote_sshkey is not None: + col.set_remote_sshkey(opts.remote_sshkey) + col.connect_ssh() + if opts.remote_password is not None: + col.set_remote_password(opts.remote_password) + col.connect_ssh() + col.run() + return col.get_linkpairs() + return collect_func + if __name__ == "__main__": # init program options p = OptionParser(version="ver:%s" % __version__) @@ -53,6 +68,8 @@ if __name__ == "__main__": help="export linkpairs to stdout") p.add_option('-i', '--import_file', dest='import_file', help="import json file as linkpairs", metavar="FILE") + p.add_option('-a', '--api_server', dest='api_server', action="store_true", default=False, + help="enable Web API server") g2 = OptionGroup(p, "Collector Agent flags", "disable Collector Agents") g2.add_option( @@ -108,22 +125,18 @@ if __name__ == "__main__": importer.run() linkpairs = importer.get_linkpairs() else: - col = Collector(opts, util, formatter) - if opts.remote_sshkey is not None: - col.set_remote_sshkey(opts.remote_sshkey) - col.connect_ssh() - if opts.remote_password is not None: - col.set_remote_password(opts.remote_password) - col.connect_ssh() - col.run() - linkpairs = col.get_linkpairs() - + collect_func = collect_linkpairs(opts, util, formatter) + linkpairs = collect_func() + if opts.export_stdout: exporter = JsonExporter(util, linkpairs) - print(exporter.run()) + util.message_out(exporter.run()) sys.exit(0) - - print_linkpair_records(linkpairs) - -# if dbu.enable_db: -# dbu.close_db() + elif opts.api_server: + collect_task = CollectTask(collect_func, 30) + collect_task.start() + api_server.util = util + api_server.collect_func = collect_func + api_server.start_app() + else: + print_linkpair_records(linkpairs)