From 0e2f3d0a9d935c9b6a2bfeb1480d903ed4aab513 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Tue, 5 Oct 2021 12:50:41 +1100 Subject: [PATCH] ptgbot-web: add simple service to serve static pages This serves the static pages from the container directly Change-Id: I86f3f5002f88005760e00c5714b0891c1892a6f2 --- Dockerfile | 3 +- init.sh | 7 ++ {html => ptgbot/html}/bootstrap-3.3.7.min.css | 0 {html => ptgbot/html}/bootstrap-3.3.7.min.js | 0 {html => ptgbot/html}/etherpads.html | 0 .../fonts/glyphicons-halflings-regular.eot | Bin .../fonts/glyphicons-halflings-regular.svg | 0 .../fonts/glyphicons-halflings-regular.ttf | Bin .../fonts/glyphicons-halflings-regular.woff | Bin .../fonts/glyphicons-halflings-regular.woff2 | Bin {html => ptgbot/html}/handlebars-4.0.6.js | 0 {html => ptgbot/html}/index.html | 0 {html => ptgbot/html}/index.js | 0 {html => ptgbot/html}/jquery-1.9.1.min.js | 0 {html => ptgbot/html}/logo.png | Bin {html => ptgbot/html}/motd.js | 0 {html => ptgbot/html}/ptg.html | 0 {html => ptgbot/html}/ptg.js | 0 {html => ptgbot/html}/signage.html | 0 ptgbot/web.py | 73 ++++++++++++++++++ requirements.txt | 2 +- setup.cfg | 5 ++ test-requirements.txt | 2 +- 23 files changed, 89 insertions(+), 3 deletions(-) create mode 100755 init.sh rename {html => ptgbot/html}/bootstrap-3.3.7.min.css (100%) rename {html => ptgbot/html}/bootstrap-3.3.7.min.js (100%) rename {html => ptgbot/html}/etherpads.html (100%) rename {html => ptgbot/html}/fonts/glyphicons-halflings-regular.eot (100%) rename {html => ptgbot/html}/fonts/glyphicons-halflings-regular.svg (100%) rename {html => ptgbot/html}/fonts/glyphicons-halflings-regular.ttf (100%) rename {html => ptgbot/html}/fonts/glyphicons-halflings-regular.woff (100%) rename {html => ptgbot/html}/fonts/glyphicons-halflings-regular.woff2 (100%) rename {html => ptgbot/html}/handlebars-4.0.6.js (100%) rename {html => ptgbot/html}/index.html (100%) rename {html => ptgbot/html}/index.js (100%) rename {html => ptgbot/html}/jquery-1.9.1.min.js (100%) rename {html => ptgbot/html}/logo.png (100%) rename {html => ptgbot/html}/motd.js (100%) rename {html => ptgbot/html}/ptg.html (100%) rename {html => ptgbot/html}/ptg.js (100%) rename {html => ptgbot/html}/signage.html (100%) create mode 100644 ptgbot/web.py diff --git a/Dockerfile b/Dockerfile index a372018..d9b361f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,5 @@ FROM opendevorg/python-base:3.9 as ptgbot COPY --from=builder /output/ /output RUN /output/install-from-bindep -CMD ["/usr/local/bin/ptgbot", "-d", "/etc/ptgbot/ptgbot.config"] \ No newline at end of file +COPY init.sh init.sh +CMD ./init.sh diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..8767030 --- /dev/null +++ b/init.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Serves website as a daemon +/usr/local/bin/ptgbot-web --debug /etc/ptgbot/ptgbot.config + +# Run in foreground as the main process +/usr/local/bin/ptgbot -d /etc/ptgbot/ptgbot.config diff --git a/html/bootstrap-3.3.7.min.css b/ptgbot/html/bootstrap-3.3.7.min.css similarity index 100% rename from html/bootstrap-3.3.7.min.css rename to ptgbot/html/bootstrap-3.3.7.min.css diff --git a/html/bootstrap-3.3.7.min.js b/ptgbot/html/bootstrap-3.3.7.min.js similarity index 100% rename from html/bootstrap-3.3.7.min.js rename to ptgbot/html/bootstrap-3.3.7.min.js diff --git a/html/etherpads.html b/ptgbot/html/etherpads.html similarity index 100% rename from html/etherpads.html rename to ptgbot/html/etherpads.html diff --git a/html/fonts/glyphicons-halflings-regular.eot b/ptgbot/html/fonts/glyphicons-halflings-regular.eot similarity index 100% rename from html/fonts/glyphicons-halflings-regular.eot rename to ptgbot/html/fonts/glyphicons-halflings-regular.eot diff --git a/html/fonts/glyphicons-halflings-regular.svg b/ptgbot/html/fonts/glyphicons-halflings-regular.svg similarity index 100% rename from html/fonts/glyphicons-halflings-regular.svg rename to ptgbot/html/fonts/glyphicons-halflings-regular.svg diff --git a/html/fonts/glyphicons-halflings-regular.ttf b/ptgbot/html/fonts/glyphicons-halflings-regular.ttf similarity index 100% rename from html/fonts/glyphicons-halflings-regular.ttf rename to ptgbot/html/fonts/glyphicons-halflings-regular.ttf diff --git a/html/fonts/glyphicons-halflings-regular.woff b/ptgbot/html/fonts/glyphicons-halflings-regular.woff similarity index 100% rename from html/fonts/glyphicons-halflings-regular.woff rename to ptgbot/html/fonts/glyphicons-halflings-regular.woff diff --git a/html/fonts/glyphicons-halflings-regular.woff2 b/ptgbot/html/fonts/glyphicons-halflings-regular.woff2 similarity index 100% rename from html/fonts/glyphicons-halflings-regular.woff2 rename to ptgbot/html/fonts/glyphicons-halflings-regular.woff2 diff --git a/html/handlebars-4.0.6.js b/ptgbot/html/handlebars-4.0.6.js similarity index 100% rename from html/handlebars-4.0.6.js rename to ptgbot/html/handlebars-4.0.6.js diff --git a/html/index.html b/ptgbot/html/index.html similarity index 100% rename from html/index.html rename to ptgbot/html/index.html diff --git a/html/index.js b/ptgbot/html/index.js similarity index 100% rename from html/index.js rename to ptgbot/html/index.js diff --git a/html/jquery-1.9.1.min.js b/ptgbot/html/jquery-1.9.1.min.js similarity index 100% rename from html/jquery-1.9.1.min.js rename to ptgbot/html/jquery-1.9.1.min.js diff --git a/html/logo.png b/ptgbot/html/logo.png similarity index 100% rename from html/logo.png rename to ptgbot/html/logo.png diff --git a/html/motd.js b/ptgbot/html/motd.js similarity index 100% rename from html/motd.js rename to ptgbot/html/motd.js diff --git a/html/ptg.html b/ptgbot/html/ptg.html similarity index 100% rename from html/ptg.html rename to ptgbot/html/ptg.html diff --git a/html/ptg.js b/ptgbot/html/ptg.js similarity index 100% rename from html/ptg.js rename to ptgbot/html/ptg.js diff --git a/html/signage.html b/ptgbot/html/signage.html similarity index 100% rename from html/signage.html rename to ptgbot/html/signage.html diff --git a/ptgbot/web.py b/ptgbot/web.py new file mode 100644 index 0000000..938f3ba --- /dev/null +++ b/ptgbot/web.py @@ -0,0 +1,73 @@ +import argparse +import daemon +import daemon.pidfile +import http.server +import json +import logging +import os +import pkg_resources +import socketserver + +CONFIG = {} + + +class RequestHandler(http.server.SimpleHTTPRequestHandler): + def do_GET(self): + if self.path.endswith('ptg.json'): + with open(CONFIG['db_filename'], 'rb') as fp: + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.end_headers() + self.wfile.write(fp.read()) + else: + http.server.SimpleHTTPRequestHandler.do_GET(self) + + def log_message(self, format, *args): + logging.debug("%s - - [%s] %s" % (self.address_string(), + self.log_date_time_string(), + format % args)) + + +def start(): + os.chdir(CONFIG['source_dir']) + with socketserver.TCPServer(("", CONFIG['port']), RequestHandler) as httpd: + httpd.serve_forever() + + +def main(): + global CONFIG + parser = argparse.ArgumentParser(description='PTG web') + parser.add_argument('configfile', help='config file') + parser.add_argument('-d', dest='nodaemon', action='store_true', + help='do not run as daemon') + parser.add_argument('-p', '--port', dest='port', help='Port to listen on', + default=8000) + parser.add_argument('--debug', dest='debug', action='store_true') + args = parser.parse_args() + + CONFIG['debug'] = True if args.debug else False + CONFIG['port'] = int(args.port) + + with open(args.configfile, 'r') as fp: + file_config = json.load(fp) + CONFIG['db_filename'] = file_config['db_filename'] + + CONFIG['source_dir'] = pkg_resources.resource_filename(__name__, "html") + + logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) + logging.info('Starting daemon on port: %s' % args.port) + logging.info('Serving files from: %s' % CONFIG['source_dir']) + logging.info('JSON from: %s' % CONFIG['db_filename']) + logging.debug('Debugging on') + + if not args.nodaemon: + os.makedirs('/var/run/ptgbot', exist_ok=True) + pid = daemon.pidfile.TimeoutPIDLockFile( + '/var/run/ptgbot/ptgbot-web.pid', 10) + with daemon.DaemonContext(pidfile=pid): + start() + start() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index 0525778..9147466 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # Pin irc module to 15.1.1 until ib3 releases https://github.com/bd808/python-ib3/commit/92da70155e5378478802cb38baaa2a00187201d6 irc==15.1.1 -python-daemon +python-daemon >= 1.6 ib3 requests diff --git a/setup.cfg b/setup.cfg index 1a0d2ec..cc94838 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,6 +19,11 @@ classifier = Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 +[files] +packages = + ptgbot + [entry_points] console_scripts = ptgbot = ptgbot.bot:main + ptgbot-web = ptgbot.web:main diff --git a/test-requirements.txt b/test-requirements.txt index 7b06820..c957936 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,2 @@ -hacking>=3.0,<3.1.0 # Apache-2.0 +hacking>=3.0,<4.1.0 # Apache-2.0 stestr>=2.0.0 # Apache-2.0