From 2913a8aa2450b146f96f62a0a153bedb2285cc46 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Mon, 1 Mar 2021 10:00:50 +0100 Subject: [PATCH] add: redirect and vendor-data - add redirect and vendor-data (empty only, to speed up boot) - some CQ too - default file names standardized --- main.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 73f2154..81174f4 100644 --- a/main.py +++ b/main.py @@ -13,8 +13,9 @@ PATH = os.path.dirname(os.path.abspath(__file__)) config = configparser.ConfigParser() config.read(os.path.join(PATH, "config.ini")) -user_data_filename = config["app"].get("user_data", "sample_file.txt") -meta_data_filename = config["app"].get("meta_data", "meta_data_extra.txt") +user_data_filename = config["app"].get("user_data", "user-data") +meta_data_filename = config["app"].get("meta_data", "meta-data") +redirect_filename = config["app"].get("redirect", "redirect") class MainApp: @@ -27,7 +28,7 @@ class MainApp: 'X-Real-Ip', cherrypy.request.remote.ip ) - except: + except BaseException: self.remoteip = cherrypy.request.remote.ip try: @@ -35,12 +36,25 @@ class MainApp: except socket.herror: self.hostinfo = ('localhost', ) + def _redirect_if_needed(self): + filepath = os.path.join(PATH, "data", self.hostinfo[0], + "redirect") + if os.path.exists(filepath): + try: + with open(filepath) as f: + content = f.read().splitlines() + raise cherrypy.HTTPRedirect(content[0], 301) + except BaseException: + return False + return False + @cherrypy.expose def user_data(self): """ Serves a static file """ self._init_ip() + self._redirect_if_needed() filepath = os.path.join(PATH, "data", self.hostinfo[0], user_data_filename) if not os.path.exists(filepath): @@ -53,8 +67,12 @@ class MainApp: Return meta-data in YAML """ self._init_ip() - hostname =self.hostinfo[0] - data = {"instance-id": hostname.split(".")[0], "local-hostname": hostname} + self._redirect_if_needed() + hostname = self.hostinfo[0] + data = { + "instance-id": hostname.split(".")[0], + "local-hostname": hostname + } filepath = os.path.join(PATH, "data", hostname, meta_data_filename) if os.path.exists(filepath): @@ -65,6 +83,13 @@ class MainApp: return yaml.dump(data) + @cherrypy.expose + def vendor_data(self): + """ + Return empty vendor-data + """ + return "" + @cherrypy.expose def finished(self, data): """